Procedure Revision and tests

This commit is contained in:
Kara Diaby 2023-09-28 13:56:32 +00:00
parent 74c240ef66
commit f422b314a0
2 changed files with 77 additions and 0 deletions

View file

@ -19,6 +19,7 @@ class ProcedureRevision < ApplicationRecord
validate :conditions_are_valid?
validate :header_sections_are_valid?
validate :expressions_regulieres_are_valid?
delegate :path, to: :procedure, prefix: true
@ -412,6 +413,13 @@ class ProcedureRevision < ApplicationRecord
repetition_tdcs_errors + root_tdcs_errors
end
def expressions_regulieres_are_valid?
types_de_champ_public.to_a
.flat_map { _1.repetition? ? children_of(_1) : _1 }
.filter { _1.expression_reguliere? && _1.invalid_regexp? }
.each { |tdc| errors.add(:expression_reguliere, type_de_champ: tdc) }
end
def errors_for_header_sections_order(tdcs)
tdcs
.map.with_index

View file

@ -908,6 +908,75 @@ describe ProcedureRevision do
end
end
describe "expressions_regulieres_are_valid" do
let(:procedure) do
create(:procedure).tap do |p|
p.draft_revision.add_type_de_champ(type_champ: :expression_reguliere, libelle: 'exemple', expression_reguliere:, expression_reguliere_exemple_text:)
end
end
let(:draft_revision) { procedure.draft_revision }
subject do
draft_revision.save
draft_revision.errors
end
context "When no regexp and no example" do
let(:expression_reguliere_exemple_text) { nil }
let(:expression_reguliere) { nil }
it { is_expected.to be_empty }
end
context "When expression_reguliere but no example" do
let(:expression_reguliere) { "[A-Z]+" }
let(:expression_reguliere_exemple_text) { nil }
it { is_expected.to be_empty }
end
context "When expression_reguliere and bad example" do
let(:expression_reguliere_exemple_text) { "01234567" }
let(:expression_reguliere) { "[A-Z]+" }
it { is_expected.not_to be_empty }
end
context "When expression_reguliere and good example" do
let(:expression_reguliere_exemple_text) { "A" }
let(:expression_reguliere) { "[A-Z]+" }
it { is_expected.to be_empty }
end
context "When bad expression_reguliere" do
let(:expression_reguliere_exemple_text) { "0123456789" }
let(:expression_reguliere) { "(" }
it { is_expected.not_to be_empty }
end
context "When repetition" do
let(:procedure) do
create(:procedure,
types_de_champ_public: [{ type: :repetition, children: [{ type: :expression_reguliere, expression_reguliere:, expression_reguliere_exemple_text: }] }])
end
context "When bad expression_reguliere" do
let(:expression_reguliere_exemple_text) { "0123456789" }
let(:expression_reguliere) { "(" }
it { is_expected.not_to be_empty }
end
context "When expression_reguliere and bad example" do
let(:expression_reguliere_exemple_text) { "01234567" }
let(:expression_reguliere) { "[A-Z]+" }
it { is_expected.not_to be_empty }
end
end
end
describe "#dependent_conditions" do
include Logic