2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-19 09:43:19 +01:00
|
|
|
describe Champs::EpciChamp, type: :model do
|
2023-02-06 14:24:51 +01:00
|
|
|
describe 'validations' do
|
2024-04-02 11:04:00 +02:00
|
|
|
subject { champ.validate(:champs_public_value) }
|
|
|
|
|
2023-04-13 13:10:23 +02:00
|
|
|
describe 'code_departement' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:champ) { Champs::EpciChamp.new(code_departement: code_departement, dossier: build(:dossier)) }
|
2024-08-30 16:43:30 +02:00
|
|
|
before { allow(champ).to receive(:visible?).and_return(true) }
|
2023-02-06 14:24:51 +01:00
|
|
|
context 'when nil' do
|
|
|
|
let(:code_departement) { nil }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when empty' do
|
|
|
|
let(:code_departement) { '' }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_falsey }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when included in the departement codes' do
|
|
|
|
let(:code_departement) { "01" }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not included in the departement codes' do
|
|
|
|
let(:code_departement) { "totoro" }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_falsey }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'external_id' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :epci }]) }
|
|
|
|
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
|
|
|
let(:champ) { dossier.champs.first }
|
2023-02-06 14:24:51 +01:00
|
|
|
|
|
|
|
before do
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.code_departement = code_departement
|
|
|
|
champ.external_id = nil
|
2024-04-02 11:04:00 +02:00
|
|
|
champ.save!(validate: false)
|
2023-02-06 14:24:51 +01:00
|
|
|
champ.update_columns(external_id: external_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when code_departement is nil' do
|
|
|
|
let(:code_departement) { nil }
|
|
|
|
let(:external_id) { nil }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when code_departement is not nil and valid' do
|
|
|
|
let(:code_departement) { "01" }
|
|
|
|
|
|
|
|
context 'when external_id is nil' do
|
|
|
|
let(:external_id) { nil }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when external_id is empty' do
|
|
|
|
let(:external_id) { '' }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_falsey }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when external_id is included in the epci codes of the departement' do
|
|
|
|
let(:external_id) { '200042935' }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when external_id is not included in the epci codes of the departement' do
|
|
|
|
let(:external_id) { 'totoro' }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_falsey }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'value' do
|
2024-07-01 15:31:32 +02:00
|
|
|
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :epci }]) }
|
|
|
|
let(:dossier) { create(:dossier, :with_populated_champs, procedure:) }
|
|
|
|
let(:champ) { dossier.champs.first }
|
2023-02-06 14:24:51 +01:00
|
|
|
|
|
|
|
before do
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.value = nil
|
|
|
|
champ.code_departement = code_departement
|
|
|
|
champ.external_id = nil
|
2024-04-02 11:04:00 +02:00
|
|
|
champ.save!(validate: false)
|
2024-07-01 15:31:32 +02:00
|
|
|
champ.update_columns(external_id:, value:)
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when code_departement is nil' do
|
|
|
|
let(:code_departement) { nil }
|
|
|
|
let(:external_id) { nil }
|
|
|
|
let(:value) { nil }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when external_id is nil' do
|
|
|
|
let(:code_departement) { '01' }
|
|
|
|
let(:external_id) { nil }
|
|
|
|
let(:value) { nil }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when code_departement and external_id are not nil and valid' do
|
|
|
|
let(:code_departement) { '01' }
|
|
|
|
let(:external_id) { '200042935' }
|
|
|
|
|
|
|
|
context 'when value is nil' do
|
|
|
|
let(:value) { nil }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
2023-12-26 16:36:53 +01:00
|
|
|
context 'when value is in departement epci names' do
|
|
|
|
let(:value) { 'CA Haut - Bugey Agglomération' }
|
2023-02-06 14:24:51 +01:00
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when value is in departement epci names' do
|
|
|
|
let(:value) { 'CA Haut - Bugey Agglomération' }
|
|
|
|
|
2024-04-02 11:04:00 +02:00
|
|
|
it { is_expected.to be_truthy }
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
|
2023-12-26 16:36:53 +01:00
|
|
|
context 'when epci name had been renamed' do
|
2023-02-06 14:24:51 +01:00
|
|
|
let(:value) { 'totoro' }
|
|
|
|
|
2023-12-26 16:36:53 +01:00
|
|
|
it 'is valid and updates its own value' do
|
2024-04-02 11:04:00 +02:00
|
|
|
expect(subject).to be_truthy
|
|
|
|
expect(champ.value).to eq('CA Haut - Bugey Agglomération')
|
2023-12-26 16:36:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when value is not in departement epci names nor in departement epci codes' do
|
|
|
|
let(:value) { 'totoro' }
|
|
|
|
|
|
|
|
it 'is invalid' do
|
|
|
|
allow(APIGeoService).to receive(:epcis).with(champ.code_departement).and_return([])
|
2024-04-02 11:04:00 +02:00
|
|
|
expect(subject).to be_falsey
|
2023-12-26 16:36:53 +01:00
|
|
|
end
|
2023-02-06 14:24:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-01-19 09:43:19 +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 }
|
2024-04-23 10:14:19 +02:00
|
|
|
let(:epci) { APIGeoService.epcis('01').first }
|
|
|
|
|
2023-01-19 09:43:19 +01:00
|
|
|
it 'with departement and code' do
|
2024-07-01 15:31:32 +02:00
|
|
|
allow(champ).to receive(:type_de_champ).and_return(build(:type_de_champ_epci))
|
2023-01-19 09:43:19 +01:00
|
|
|
champ.code_departement = '01'
|
2024-04-23 10:14:19 +02:00
|
|
|
champ.value = epci[:code]
|
|
|
|
expect(champ.blank?).to be_falsey
|
|
|
|
expect(champ.external_id).to eq(epci[:code])
|
|
|
|
expect(champ.value).to eq(epci[:name])
|
|
|
|
expect(champ.selected).to eq(epci[:code])
|
|
|
|
expect(champ.code).to eq(epci[:code])
|
2023-01-19 09:43:19 +01:00
|
|
|
expect(champ.departement?).to be_truthy
|
2024-04-23 10:14:19 +02:00
|
|
|
expect(champ.to_s).to eq(epci[:name])
|
2023-01-19 09:43:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|