demarches-normaliennes/spec/models/columns/champ_column_spec.rb

134 lines
5.3 KiB
Ruby
Raw Normal View History

2024-10-31 18:29:23 +01:00
# frozen_string_literal: true
describe Columns::ChampColumn do
describe '#value' do
2024-11-04 16:34:29 +01:00
let(:procedure) { create(:procedure, :with_all_champs_mandatory) }
context 'without any cast' do
2024-10-31 18:29:23 +01:00
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
let(:types_de_champ) { procedure.all_revisions_types_de_champ }
it 'extracts values for columns and type de champ' do
expect_type_de_champ_values('civilite', eq(["M."]))
expect_type_de_champ_values('email', eq(['yoda@beta.gouv.fr']))
expect_type_de_champ_values('phone', eq(['0666666666']))
expect_type_de_champ_values('address', eq(["2 rue des Démarches"]))
expect_type_de_champ_values('communes', eq(["Coye-la-Forêt"]))
expect_type_de_champ_values('departements', eq(['01']))
expect_type_de_champ_values('regions', eq(['01']))
expect_type_de_champ_values('pays', eq(['France']))
expect_type_de_champ_values('epci', eq([nil]))
expect_type_de_champ_values('iban', eq([nil]))
expect_type_de_champ_values('siret', eq(["44011762001530", "postal_code", "city_name", "departement_code", "region_name"]))
expect_type_de_champ_values('text', eq(['text']))
expect_type_de_champ_values('textarea', eq(['textarea']))
expect_type_de_champ_values('number', eq(['42']))
expect_type_de_champ_values('decimal_number', eq([42.1]))
expect_type_de_champ_values('integer_number', eq([42]))
expect_type_de_champ_values('date', eq([Time.zone.parse('2019-07-10').to_date]))
expect_type_de_champ_values('datetime', eq([Time.zone.parse("1962-09-15T15:35:00+01:00")]))
expect_type_de_champ_values('checkbox', eq([true]))
expect_type_de_champ_values('drop_down_list', eq(['val1']))
expect_type_de_champ_values('multiple_drop_down_list', eq([["val1", "val2"]]))
expect_type_de_champ_values('linked_drop_down_list', eq([nil, "categorie 1", "choix 1"]))
expect_type_de_champ_values('yes_no', eq([true]))
expect_type_de_champ_values('annuaire_education', eq([nil]))
expect_type_de_champ_values('piece_justificative', be_an_instance_of(Array))
expect_type_de_champ_values('titre_identite', be_an_instance_of(Array))
expect_type_de_champ_values('cnaf', eq([nil]))
expect_type_de_champ_values('dgfip', eq([nil]))
expect_type_de_champ_values('pole_emploi', eq([nil]))
expect_type_de_champ_values('mesri', eq([nil]))
expect_type_de_champ_values('cojo', eq([nil]))
expect_type_de_champ_values('expression_reguliere', eq([nil]))
2024-10-31 18:29:23 +01:00
end
end
2024-11-04 16:34:29 +01:00
context 'with cast' do
def column(label) = procedure.find_column(label:)
context 'from a integer_number' do
let(:champ) { double(last_write_type_champ: 'integer_number', value: '42') }
it do
expect(column('decimal_number').value(champ)).to eq(42.0)
expect(column('text').value(champ)).to eq('42')
end
end
context 'from a decimal_number' do
let(:champ) { double(last_write_type_champ: 'decimal_number', value: '42.1') }
it do
expect(column('integer_number').value(champ)).to eq(42)
expect(column('text').value(champ)).to eq('42.1')
end
end
context 'from a date' do
let(:champ) { double(last_write_type_champ: 'date', value:) }
describe 'when the value is valid' do
let(:value) { '2019-07-10' }
it { expect(column('datetime').value(champ)).to eq(Time.zone.parse('2019-07-10')) }
end
describe 'when the value is invalid' do
let(:value) { 'invalid' }
it { expect(column('datetime').value(champ)).to be_nil }
end
end
context 'from a datetime' do
let(:champ) { double(last_write_type_champ: 'datetime', value:) }
describe 'when the value is valid' do
let(:value) { '1962-09-15T15:35:00+01:00' }
it { expect(column('date').value(champ)).to eq('1962-09-15'.to_date) }
end
describe 'when the value is invalid' do
let(:value) { 'invalid' }
it { expect(column('date').value(champ)).to be_nil }
end
end
context 'from a drop_down_list' do
let(:champ) { double(last_write_type_champ: 'drop_down_list', value: 'val1') }
it do
expect(column('multiple_drop_down_list').value(champ)).to eq(['val1'])
expect(column('text').value(champ)).to eq('val1')
end
end
context 'from a multiple_drop_down_list' do
let(:champ) { double(last_write_type_champ: 'multiple_drop_down_list', value: '["val1","val2"]') }
it do
expect(column('simple_drop_down_list').value(champ)).to eq('val1')
expect(column('text').value(champ)).to eq('val1, val2')
end
end
end
2024-10-31 18:29:23 +01:00
end
private
def expect_type_de_champ_values(type, assertion)
2024-10-31 18:29:23 +01:00
type_de_champ = types_de_champ.find { _1.type_champ == type }
champ = dossier.send(:filled_champ, type_de_champ, nil)
columns = type_de_champ.columns(procedure:)
expect(columns.map { _1.value(champ) }).to assertion
2024-10-31 18:29:23 +01:00
end
def retrieve_champ(type)
type_de_champ = types_de_champ.find { _1.type_champ == type }
dossier.send(:filled_champ, type_de_champ, nil)
end
end