2018-04-24 20:27:19 +02:00
|
|
|
describe ChampSerializer do
|
|
|
|
describe '#attributes' do
|
2018-11-29 23:10:13 +01:00
|
|
|
subject { ChampSerializer.new(serializable_object).serializable_hash }
|
|
|
|
let(:serializable_object) { champ }
|
2018-04-24 20:27:19 +02:00
|
|
|
|
|
|
|
context 'when type champ is piece justificative' do
|
2018-11-13 18:48:04 +01:00
|
|
|
let(:champ) { create(:champ_piece_justificative) }
|
2018-04-24 20:27:19 +02:00
|
|
|
|
2019-12-19 18:29:01 +01:00
|
|
|
it { expect(subject[:value]).to match('/rails/active_storage/disk/') }
|
2018-04-24 20:27:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when type champ is not piece justificative' do
|
|
|
|
let(:champ) { create(:champ, value: "blah") }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: "blah") }
|
|
|
|
end
|
2018-11-13 18:48:04 +01:00
|
|
|
|
|
|
|
context 'when type champ is carte' do
|
2020-04-30 16:59:38 +02:00
|
|
|
let(:champ) { create(:champ_carte, geo_areas: [geo_area].compact) }
|
|
|
|
let(:geo_area) { create(:geo_area, :cadastre, :multi_polygon) }
|
|
|
|
let(:geo_json) { attributes_for(:geo_area, :multi_polygon)[:geometry].stringify_keys }
|
2018-11-29 23:10:13 +01:00
|
|
|
|
|
|
|
let(:serialized_champ) {
|
|
|
|
{
|
2018-11-30 13:19:19 +01:00
|
|
|
type_de_champ: serialized_type_de_champ,
|
|
|
|
value: serialized_value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let(:serialized_type_de_champ) {
|
|
|
|
{
|
|
|
|
description: serialized_description,
|
|
|
|
id: serialized_id,
|
|
|
|
libelle: serialized_libelle,
|
|
|
|
order_place: serialized_order_place,
|
|
|
|
type_champ: serialized_type_champ
|
|
|
|
}
|
2018-11-29 23:10:13 +01:00
|
|
|
}
|
|
|
|
let(:serialized_id) { -1 }
|
|
|
|
let(:serialized_description) { "" }
|
|
|
|
let(:serialized_order_place) { -1 }
|
2018-12-19 11:09:13 +01:00
|
|
|
let(:serialized_value) { geo_json }
|
2018-11-29 23:10:13 +01:00
|
|
|
|
|
|
|
context 'and geo_area is selection_utilisateur' do
|
2020-04-30 16:59:38 +02:00
|
|
|
let(:geo_area) { create(:geo_area, :selection_utilisateur, :polygon) }
|
2018-11-30 13:19:19 +01:00
|
|
|
|
2018-11-29 23:10:13 +01:00
|
|
|
context 'old_api' do
|
|
|
|
let(:serialized_libelle) { "user geometry" }
|
|
|
|
let(:serialized_type_champ) { "user_geometry" }
|
|
|
|
|
2020-04-09 17:32:20 +02:00
|
|
|
let(:serializable_object) { champ.selection_utilisateur_legacy_geo_area }
|
2018-11-29 23:10:13 +01:00
|
|
|
|
|
|
|
context 'when value is coordinates' do
|
|
|
|
it { expect(subject).to eq(serialized_champ) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'new_api' do
|
|
|
|
let(:geo_area) { nil }
|
2018-11-30 13:19:19 +01:00
|
|
|
let(:serialized_champ) {
|
|
|
|
{
|
|
|
|
type_de_champ: serialized_type_de_champ,
|
|
|
|
geo_areas: [],
|
|
|
|
value: serialized_value
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 23:10:13 +01:00
|
|
|
let(:serialized_id) { champ.type_de_champ.stable_id }
|
|
|
|
let(:serialized_description) { champ.description }
|
|
|
|
let(:serialized_libelle) { champ.libelle }
|
|
|
|
let(:serialized_type_champ) { champ.type_champ }
|
2018-12-19 11:09:13 +01:00
|
|
|
let(:serialized_value) { nil }
|
2018-11-29 23:10:13 +01:00
|
|
|
|
|
|
|
context 'when value is coordinates' do
|
|
|
|
let(:value) { coordinates.to_json }
|
|
|
|
|
|
|
|
it { expect(subject).to eq(serialized_champ) }
|
|
|
|
end
|
|
|
|
end
|
2018-11-29 11:40:07 +01:00
|
|
|
end
|
2018-11-13 18:48:04 +01:00
|
|
|
|
|
|
|
context 'and geo_area is cadastre' do
|
2018-11-29 23:10:13 +01:00
|
|
|
context 'new_api' do
|
|
|
|
it {
|
|
|
|
expect(subject[:geo_areas].first).to include(
|
|
|
|
source: GeoArea.sources.fetch(:cadastre),
|
2018-12-19 11:09:13 +01:00
|
|
|
geometry: geo_json,
|
2018-11-29 23:10:13 +01:00
|
|
|
numero: '42',
|
2021-05-06 18:50:06 +02:00
|
|
|
section: 'A11'
|
2018-11-29 23:10:13 +01:00
|
|
|
)
|
|
|
|
expect(subject[:geo_areas].first.key?(:nom)).to be_falsey
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'old_api' do
|
|
|
|
let(:serializable_object) { champ.geo_areas.first }
|
|
|
|
let(:serialized_libelle) { "cadastre" }
|
|
|
|
let(:serialized_type_champ) { "cadastre" }
|
|
|
|
|
|
|
|
it { expect(subject).to eq(serialized_champ) }
|
|
|
|
end
|
2018-11-13 18:48:04 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when type champ is siret' do
|
|
|
|
let(:etablissement) { create(:etablissement) }
|
|
|
|
let(:champ) { create(:type_de_champ_siret).champ.create(etablissement: etablissement, value: etablissement.siret) }
|
|
|
|
|
|
|
|
it {
|
|
|
|
is_expected.to include(value: etablissement.siret)
|
|
|
|
expect(subject[:etablissement]).to include(siret: etablissement.siret)
|
|
|
|
expect(subject[:entreprise]).to include(capital_social: etablissement.entreprise_capital_social)
|
|
|
|
}
|
|
|
|
end
|
2019-01-29 14:37:57 +01:00
|
|
|
|
|
|
|
context 'when type champ yes_no' do
|
|
|
|
context 'true' do
|
|
|
|
let(:champ) { create(:champ_yes_no, value: 'true') }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: 'true') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'false' do
|
|
|
|
let(:champ) { create(:champ_yes_no, value: 'false') }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: 'false') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'nil' do
|
|
|
|
let(:champ) { create(:champ_yes_no, value: nil) }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when type champ checkbox' do
|
|
|
|
context 'on' do
|
|
|
|
let(:champ) { create(:champ_checkbox, value: 'on') }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: 'on') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'off' do
|
|
|
|
let(:champ) { create(:champ_checkbox, value: 'off') }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: 'off') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'nil' do
|
|
|
|
let(:champ) { create(:champ_checkbox, value: nil) }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when type champ engagement' do
|
|
|
|
context 'on' do
|
|
|
|
let(:champ) { create(:champ_engagement, value: 'on') }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: 'on') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'off' do
|
|
|
|
let(:champ) { create(:champ_engagement, value: 'off') }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: 'off') }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'nil' do
|
|
|
|
let(:champ) { create(:champ_engagement, value: nil) }
|
|
|
|
|
|
|
|
it { is_expected.to include(value: nil) }
|
|
|
|
end
|
|
|
|
end
|
2018-04-24 20:27:19 +02:00
|
|
|
end
|
|
|
|
end
|