Skip to content

Commit

Permalink
feat(type_de_champ.referentiel): begin to add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfo committed Dec 23, 2024
1 parent d9ddf6a commit 3a5a444
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ def self.dump(options)
allow_blank: true
}

validates :referentiel_adapter, inclusion: { in: ['csv', 'url'] }, allow_blank: true, allow_nil: true
validates :referentiel_presenter, inclusion: { in: ['exact_match', 'autocomplete'] }, allow_blank: true, allow_nil: true

before_validation :check_mandatory
before_validation :normalize_libelle

Expand Down Expand Up @@ -372,6 +375,19 @@ def drop_down_options_with_other
end
end

def configured?
case referentiel_adapter
when 'url'
[referentiel_presenter, referentiel_url, referentiel_test_data].all?(&:present?)
when 'csv'
false
when nil
false
else
fail "unknown adapter: #{referentiel_adapter}"
end
end

def header_section_level_value
if header_section_level.presence
header_section_level.to_i
Expand Down
29 changes: 29 additions & 0 deletions spec/models/type_de_champ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,35 @@ def never_valid
end
end

describe "referentiel" do
it 'validates referentiel_adapter as csv/url or nil' do
expect(build(:type_de_champ_referentiel, referentiel_adapter: 'csv').tap(&:validate).errors.map(&:attribute)).not_to include(:referentiel_adapter)
expect(build(:type_de_champ_referentiel, referentiel_adapter: 'url').tap(&:validate).errors.map(&:attribute)).not_to include(:referentiel_adapter)
expect(build(:type_de_champ_referentiel, referentiel_adapter: nil).tap(&:validate).errors.map(&:attribute)).not_to include(:referentiel_adapter)
expect(build(:type_de_champ_referentiel, referentiel_adapter: 'wrong').tap(&:validate).errors.map(&:attribute)).to include(:referentiel_adapter)
end

it 'validates referentiel_presentater as exact_match/autocomplete or nil' do
expect(build(:type_de_champ_referentiel, referentiel_presenter: 'exact_match').tap(&:validate).errors.map(&:attribute)).not_to include(:referentiel_presenter)
expect(build(:type_de_champ_referentiel, referentiel_presenter: 'autocomplete').tap(&:validate).errors.map(&:attribute)).not_to include(:referentiel_presenter)
expect(build(:type_de_champ_referentiel, referentiel_presenter: nil).tap(&:validate).errors.map(&:attribute)).not_to include(:referentiel_presenter)
expect(build(:type_de_champ_referentiel, referentiel_presenter: 'wrong').tap(&:validate).errors.map(&:attribute)).to include(:referentiel_presenter)
end

describe 'configured?' do
context 'when referentiel_adapter is url' do
it 'tests url params' do
tdc = build(:type_de_champ_referentiel, referentiel_adapter: 'url')
expect(tdc).to receive(:referentiel_presenter).and_return(double(present?: true))
expect(tdc).to receive(:referentiel_url).and_return(double(present?: true))
expect(tdc).to receive(:referentiel_test_data).and_return(double(present?: true))

expect(tdc.configured?).to eq(true)
end
end
end
end

describe "validate_regexp" do
let(:tdc) { create(:type_de_champ_expression_reguliere, expression_reguliere:, expression_reguliere_exemple_text:) }
subject { tdc.invalid_regexp? }
Expand Down

0 comments on commit 3a5a444

Please sign in to comment.