2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-12-20 21:29:18 +01:00
|
|
|
|
describe Champs::DepartementChamp, type: :model do
|
2023-04-13 13:10:23 +02:00
|
|
|
|
describe 'validations' do
|
2023-01-20 11:23:52 +01:00
|
|
|
|
describe 'external link' do
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:champ) { described_class.new(external_id: external_id, dossier: build(:dossier)) }
|
2024-09-20 14:54:26 +02:00
|
|
|
|
before do
|
|
|
|
|
allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_departements))
|
|
|
|
|
allow(champ).to receive(:in_dossier_revision?).and_return(true)
|
|
|
|
|
end
|
2024-04-02 11:04:00 +02:00
|
|
|
|
subject { champ.validate(:champs_public_value) }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
|
|
|
|
|
context 'when nil' do
|
|
|
|
|
let(:external_id) { nil }
|
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
|
it { is_expected.to be_truthy }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when blank' do
|
|
|
|
|
let(:external_id) { '' }
|
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
|
it { is_expected.to be_falsey }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when included in the departement codes' do
|
|
|
|
|
let(:external_id) { "01" }
|
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
|
it { is_expected.to be_truthy }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when not included in the departement codes' do
|
|
|
|
|
let(:external_id) { "totoro" }
|
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
|
it { is_expected.to be_falsey }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe 'value' do
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :departements }]) }
|
|
|
|
|
let(:dossier) { create(:dossier, procedure:) }
|
|
|
|
|
let(:champ) { dossier.champs.first }
|
2024-04-02 11:04:00 +02:00
|
|
|
|
subject { champ.validate(:champs_public_value) }
|
|
|
|
|
before { champ.update_columns(value: value) }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
|
|
|
|
|
context 'when nil' do
|
|
|
|
|
let(:value) { nil }
|
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
|
it { is_expected.to be_truthy }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when blank' do
|
|
|
|
|
let(:value) { '' }
|
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
|
it { is_expected.to be_falsey }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when included in the departement names' do
|
|
|
|
|
let(:value) { "Ain" }
|
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
|
it { is_expected.to be_truthy }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when not included in the departement names' do
|
|
|
|
|
let(:value) { "totoro" }
|
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
|
it { is_expected.to be_falsey }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2022-12-20 21:29:18 +01:00
|
|
|
|
|
2023-04-13 13:10:23 +02:00
|
|
|
|
describe 'value' do
|
2024-07-01 15:31:32 +02:00
|
|
|
|
let(:champ) { described_class.new(value: nil, dossier: build(:dossier)) }
|
|
|
|
|
before { allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_departements)) }
|
2023-01-20 11:23:52 +01:00
|
|
|
|
|
2022-12-27 12:10:50 +01:00
|
|
|
|
it 'with code having 2 chars' do
|
2022-12-20 21:29:18 +01:00
|
|
|
|
champ.value = '01'
|
|
|
|
|
expect(champ.external_id).to eq('01')
|
|
|
|
|
expect(champ.code).to eq('01')
|
|
|
|
|
expect(champ.name).to eq('Ain')
|
|
|
|
|
expect(champ.value).to eq('Ain')
|
|
|
|
|
expect(champ.selected).to eq('01')
|
|
|
|
|
expect(champ.to_s).to eq('01 – Ain')
|
|
|
|
|
end
|
|
|
|
|
|
2022-12-27 12:10:50 +01:00
|
|
|
|
it 'with code having 3 chars' do
|
|
|
|
|
champ.value = '971'
|
|
|
|
|
expect(champ.external_id).to eq('971')
|
|
|
|
|
expect(champ.code).to eq('971')
|
|
|
|
|
expect(champ.name).to eq('Guadeloupe')
|
|
|
|
|
expect(champ.value).to eq('Guadeloupe')
|
|
|
|
|
expect(champ.selected).to eq('971')
|
|
|
|
|
expect(champ.to_s).to eq('971 – Guadeloupe')
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-05 16:39:36 +01:00
|
|
|
|
it 'with alphanumeric code' do
|
|
|
|
|
champ.value = '2B'
|
|
|
|
|
expect(champ.external_id).to eq('2B')
|
|
|
|
|
expect(champ.code).to eq('2B')
|
|
|
|
|
expect(champ.name).to eq('Haute-Corse')
|
|
|
|
|
expect(champ.value).to eq('Haute-Corse')
|
|
|
|
|
expect(champ.selected).to eq('2B')
|
|
|
|
|
expect(champ.to_s).to eq('2B – Haute-Corse')
|
|
|
|
|
end
|
|
|
|
|
|
2022-12-20 21:29:18 +01:00
|
|
|
|
it 'with nil' do
|
|
|
|
|
champ.write_attribute(:value, 'Ain')
|
|
|
|
|
champ.write_attribute(:external_id, '01')
|
|
|
|
|
champ.value = nil
|
|
|
|
|
expect(champ.external_id).to be_nil
|
|
|
|
|
expect(champ.code).to be_nil
|
|
|
|
|
expect(champ.name).to be_nil
|
|
|
|
|
expect(champ.value).to be_nil
|
|
|
|
|
expect(champ.selected).to be_nil
|
|
|
|
|
expect(champ.to_s).to eq('')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'with blank' do
|
|
|
|
|
champ.write_attribute(:value, 'Ain')
|
|
|
|
|
champ.write_attribute(:external_id, '01')
|
|
|
|
|
champ.value = ''
|
|
|
|
|
expect(champ.external_id).to be_nil
|
|
|
|
|
expect(champ.value).to be_nil
|
|
|
|
|
expect(champ.selected).to be_nil
|
|
|
|
|
expect(champ.to_s).to eq('')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'with initial nil' do
|
|
|
|
|
champ.write_attribute(:value, nil)
|
|
|
|
|
expect(champ.external_id).to be_nil
|
|
|
|
|
expect(champ.code).to be_nil
|
|
|
|
|
expect(champ.name).to be_nil
|
|
|
|
|
expect(champ.value).to be_nil
|
|
|
|
|
expect(champ.selected).to be_nil
|
|
|
|
|
expect(champ.to_s).to eq('')
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-05 16:39:36 +01:00
|
|
|
|
it 'with initial code and name' do
|
2022-12-20 21:29:18 +01:00
|
|
|
|
champ.write_attribute(:value, '01 - Ain')
|
|
|
|
|
expect(champ.external_id).to be_nil
|
|
|
|
|
expect(champ.code).to eq('01')
|
|
|
|
|
expect(champ.name).to eq('Ain')
|
|
|
|
|
expect(champ.value).to eq('01 - Ain')
|
|
|
|
|
expect(champ.selected).to eq('01')
|
|
|
|
|
expect(champ.to_s).to eq('01 – Ain')
|
|
|
|
|
end
|
2023-01-05 16:39:36 +01:00
|
|
|
|
|
|
|
|
|
it 'with initial code and alphanumeric name' do
|
|
|
|
|
champ.write_attribute(:value, '2B - Haute-Corse')
|
|
|
|
|
expect(champ.external_id).to be_nil
|
|
|
|
|
expect(champ.code).to eq('2B')
|
|
|
|
|
expect(champ.name).to eq('Haute-Corse')
|
|
|
|
|
expect(champ.value).to eq('2B - Haute-Corse')
|
|
|
|
|
expect(champ.selected).to eq('2B')
|
|
|
|
|
expect(champ.to_s).to eq('2B – Haute-Corse')
|
|
|
|
|
end
|
2022-12-20 21:29:18 +01:00
|
|
|
|
end
|
|
|
|
|
end
|