fix(champ value): fix compute_value_json on departement champ

This commit is contained in:
Eric Leroy-Terquem 2023-12-21 16:09:23 +01:00
parent 9bdf525ff1
commit 4c82f2c9d7
2 changed files with 137 additions and 121 deletions

View file

@ -2,6 +2,7 @@ class Champs::DepartementChamp < Champs::TextChamp
store_accessor :value_json, :code_region
validate :value_in_departement_names, unless: -> { value.nil? }
validate :external_id_in_departement_codes, unless: -> { external_id.nil? }
before_save :store_code_region
def for_export
[name, code]
@ -40,7 +41,7 @@ class Champs::DepartementChamp < Champs::TextChamp
end
end
def code_region(departement_name)
def code_region
APIGeoService.region_code_by_departement(name)
end
@ -74,4 +75,8 @@ class Champs::DepartementChamp < Champs::TextChamp
errors.add(:external_id, :not_in_departement_codes)
end
def store_code_region
self.code_region = code_region
end
end

View file

@ -1,6 +1,7 @@
describe Logic::ChampValue do
include Logic
describe '#compute' do
subject { champ_value(champ.stable_id).compute([champ]) }
context 'yes_no tdc' do
@ -88,25 +89,6 @@ describe Logic::ChampValue do
it { is_expected.to eq(true) }
end
context 'commune tdc' do
let(:champ) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
it { is_expected.to eq('92') }
end
context 'epci tdc' do
let(:champ) { build(:champ_epci, code_departement: '43') }
before do
champ.save!
champ.update_columns(external_id: '244301016', value: 'CC des Sucs')
end
it do
is_expected.to eq('43')
end
end
context 'departement tdc' do
let(:champ) { create(:champ_departements, value: '02') }
@ -157,4 +139,33 @@ describe Logic::ChampValue do
end
end
end
end
describe '#compute_value_json' do
subject { champ_value(champ.stable_id).compute_value_json([champ]) }
context 'commune tdc' do
let(:champ) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
it { is_expected.to eq({ 'code_departement' => '92', 'code_postal' => '92500', 'code_region' => '11' }) }
end
context 'epci tdc' do
let(:champ) { build(:champ_epci, code_departement: '43') }
before do
champ.save!
champ.update_columns(external_id: '244301016', value: 'CC des Sucs')
end
it { is_expected.to eq({ 'code_departement' => '43', 'code_region' => '84' }) }
end
context 'departement tdc' do
let(:champ) { create(:champ_departements, value: '02') }
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:departement_enum) }
it { is_expected.to eq({ 'code_region' => '32' }) }
end
end
end