add example
This commit is contained in:
parent
76c9d1ada5
commit
da6ce0f528
2 changed files with 49 additions and 36 deletions
|
@ -5,7 +5,13 @@ class TypesDeChamp::PrefillCommuneTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
|
|||
end
|
||||
end
|
||||
|
||||
def transform_value_to_assignable_attributes(value)
|
||||
def example_value
|
||||
departement_code = departements.pick(:code)
|
||||
commune_code = APIGeoService.communes(departement_code).pick(:code)
|
||||
[departement_code, commune_code]
|
||||
end
|
||||
|
||||
def to_assignable_attributes(champ, value)
|
||||
return if value.blank? || !value.is_a?(Array)
|
||||
return if (departement_code = value.first).blank?
|
||||
return if (departement_name = APIGeoService.departement_name(departement_code)).blank?
|
||||
|
@ -13,25 +19,26 @@ class TypesDeChamp::PrefillCommuneTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
|
|||
return if !value.one? && (commune_name = APIGeoService.commune_name(departement_code, commune_code)).blank?
|
||||
|
||||
if value.one?
|
||||
departement_attributes(departement_code, departement_name)
|
||||
departement_attributes(champ, departement_code, departement_name)
|
||||
else
|
||||
departement_and_commune_attributes(departement_code, departement_name, commune_code, commune_name)
|
||||
departement_and_commune_attributes(champ, departement_code, departement_name, commune_code, commune_name)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def departement_attributes(departement_code, departement_name)
|
||||
def departement_attributes(champ, departement_code, departement_name)
|
||||
{
|
||||
id: champ.id,
|
||||
code_departement: departement_code,
|
||||
departement: departement_name
|
||||
}
|
||||
end
|
||||
|
||||
def departement_and_commune_attributes(departement_code, departement_name, commune_code, commune_name)
|
||||
def departement_and_commune_attributes(champ, departement_code, departement_name, commune_code, commune_name)
|
||||
postal_code = APIGeoService.commune_postal_codes(departement_code, commune_code).first
|
||||
|
||||
departement_attributes(departement_code, departement_name).merge(
|
||||
departement_attributes(champ, departement_code, departement_name).merge(
|
||||
external_id: commune_code,
|
||||
value: "#{commune_name} (#{postal_code})"
|
||||
)
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
|
||||
let(:type_de_champ) { build(:type_de_champ_communes) }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let(:type_de_champ) { build(:type_de_champ_communes, procedure: procedure) }
|
||||
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
|
||||
|
||||
before do
|
||||
|
@ -9,26 +10,6 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
|
|||
Rails.cache.clear
|
||||
end
|
||||
|
||||
describe 'ancestors' do
|
||||
subject { described_class.new(type_de_champ) }
|
||||
|
||||
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
|
||||
end
|
||||
|
||||
describe '#possible_values', vcr: { cassette_name: 'api_geo_departements' } do
|
||||
let(:expected_values) do
|
||||
departements.map { |departement| "#{departement[:code]} (#{departement[:name]}) : https://geo.api.gouv.fr/communes?codeDepartement=#{departement[:code]}" }
|
||||
end
|
||||
subject(:possible_values) { described_class.new(type_de_champ).possible_values }
|
||||
|
||||
it { expect(possible_values).to match(expected_values) }
|
||||
end
|
||||
|
||||
describe '#transform_value_to_assignable_attributes' do
|
||||
subject(:transform_value_to_assignable_attributes) do
|
||||
described_class.build(type_de_champ).transform_value_to_assignable_attributes(value)
|
||||
end
|
||||
|
||||
before do
|
||||
VCR.insert_cassette('api_geo_departements')
|
||||
VCR.insert_cassette('api_geo_communes')
|
||||
|
@ -39,34 +20,59 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
|
|||
VCR.eject_cassette('api_geo_communes')
|
||||
end
|
||||
|
||||
shared_examples "a transformation to" do |expected|
|
||||
it { is_expected.to match(expected) }
|
||||
describe 'ancestors' do
|
||||
subject { described_class.new(type_de_champ, procedure.active_revision) }
|
||||
|
||||
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
|
||||
end
|
||||
|
||||
describe '#possible_values' do
|
||||
let(:expected_values) do
|
||||
departements.map { |departement| "#{departement[:code]} (#{departement[:name]}) : https://geo.api.gouv.fr/communes?codeDepartement=#{departement[:code]}" }
|
||||
end
|
||||
subject(:possible_values) { described_class.new(type_de_champ, procedure.active_revision).possible_values }
|
||||
|
||||
it { expect(possible_values).to match(expected_values) }
|
||||
end
|
||||
|
||||
describe '#example_value' do
|
||||
let(:departement_code) { departements.pick(:code) }
|
||||
let(:commune_code) { APIGeoService.communes(departement_code).pick(:code) }
|
||||
subject(:example_value) { described_class.new(type_de_champ, procedure.active_revision).example_value }
|
||||
|
||||
it { is_expected.to eq([departement_code, commune_code]) }
|
||||
end
|
||||
|
||||
describe '#to_assignable_attributes' do
|
||||
let(:champ) { create(:champ_communes, type_de_champ: type_de_champ) }
|
||||
subject(:to_assignable_attributes) do
|
||||
described_class.build(type_de_champ, procedure.active_revision).to_assignable_attributes(champ, value)
|
||||
end
|
||||
|
||||
context 'when the value is nil' do
|
||||
let(:value) { nil }
|
||||
it_behaves_like "a transformation to", nil
|
||||
it { is_expected.to match(nil) }
|
||||
end
|
||||
|
||||
context 'when the value is empty' do
|
||||
let(:value) { '' }
|
||||
it_behaves_like "a transformation to", nil
|
||||
it { is_expected.to match(nil) }
|
||||
end
|
||||
|
||||
context 'when the value is a string' do
|
||||
let(:value) { 'hello' }
|
||||
it_behaves_like "a transformation to", nil
|
||||
it { is_expected.to match(nil) }
|
||||
end
|
||||
|
||||
context 'when the value is an array of one element' do
|
||||
context 'when the first element is a valid departement code' do
|
||||
let(:value) { ['01'] }
|
||||
it_behaves_like "a transformation to", { code_departement: '01', departement: 'Ain' }
|
||||
it { is_expected.to match({ id: champ.id, code_departement: '01', departement: 'Ain' }) }
|
||||
end
|
||||
|
||||
context 'when the first element is not a valid departement code' do
|
||||
let(:value) { ['totoro'] }
|
||||
it_behaves_like "a transformation to", nil
|
||||
it { is_expected.to match(nil) }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -74,18 +80,18 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
|
|||
context 'when the first element is a valid departement code' do
|
||||
context 'when the second element is a valid insee code' do
|
||||
let(:value) { ['01', '01457'] }
|
||||
it_behaves_like "a transformation to", { code_departement: '01', departement: 'Ain', external_id: '01457', value: 'Vonnas (01540)' }
|
||||
it { is_expected.to match({ id: champ.id, code_departement: '01', departement: 'Ain', external_id: '01457', value: 'Vonnas (01540)' }) }
|
||||
end
|
||||
|
||||
context 'when the second element is not a valid insee code' do
|
||||
let(:value) { ['01', 'totoro'] }
|
||||
it_behaves_like "a transformation to", nil
|
||||
it { is_expected.to match(nil) }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the first element is not a valid departement code' do
|
||||
let(:value) { ['totoro', '01457'] }
|
||||
it_behaves_like "a transformation to", nil
|
||||
it { is_expected.to match(nil) }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -93,18 +99,18 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do
|
|||
context 'when the first element is a valid departement code' do
|
||||
context 'when the second element is a valid insee code' do
|
||||
let(:value) { ['01', '01457', 'hello'] }
|
||||
it_behaves_like "a transformation to", { code_departement: '01', departement: 'Ain', external_id: '01457', value: 'Vonnas (01540)' }
|
||||
it { is_expected.to match({ id: champ.id, code_departement: '01', departement: 'Ain', external_id: '01457', value: 'Vonnas (01540)' }) }
|
||||
end
|
||||
|
||||
context 'when the second element is not a valid insee code' do
|
||||
let(:value) { ['01', 'totoro', 'hello'] }
|
||||
it_behaves_like "a transformation to", nil
|
||||
it { is_expected.to match(nil) }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the first element is not a valid departement code' do
|
||||
let(:value) { ['totoro', '01457', 'hello'] }
|
||||
it_behaves_like "a transformation to", nil
|
||||
it { is_expected.to match(nil) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue