Test and fix all possible cases of champ carte serialization

This commit is contained in:
Paul Chavard 2018-11-29 23:10:13 +01:00
parent c2161fbf85
commit 097a15d624
3 changed files with 108 additions and 42 deletions

View file

@ -51,7 +51,7 @@ class Champs::CarteChamp < Champ
# We used to store in the value column a json array with coordinates. # We used to store in the value column a json array with coordinates.
if geo_json.is_a?(Array) if geo_json.is_a?(Array)
# If it is a coordinates array, format it as a GEO-JSON # If it is a coordinates array, format it as a GEO-JSON
GeojsonService.to_json_polygon_for_selection_utilisateur(geo_json) JSON.parse(GeojsonService.to_json_polygon_for_selection_utilisateur(geo_json))
else else
# It is already a GEO-JSON # It is already a GEO-JSON
geo_json geo_json

View file

@ -12,11 +12,9 @@ class ChampSerializer < ActiveModel::Serializer
def value def value
case object case object
when GeoArea when GeoArea
object.geometry object.geometry.to_json
when Champs::CarteChamp when Champs::CarteChamp
if object.value.present? object.user_geometry.to_json
JSON.parse(object.value)
end
when Champs::DecimalNumberChamp when Champs::DecimalNumberChamp
if object.value.present? if object.value.present?
object.value.to_f object.value.to_f
@ -71,7 +69,7 @@ class ChampSerializer < ActiveModel::Serializer
libelle: legacy_carto_libelle, libelle: legacy_carto_libelle,
type_champ: legacy_carto_type_champ, type_champ: legacy_carto_type_champ,
order_place: -1, order_place: -1,
descripton: '' description: ''
} }
end end

View file

@ -1,6 +1,7 @@
describe ChampSerializer do describe ChampSerializer do
describe '#attributes' do describe '#attributes' do
subject { ChampSerializer.new(champ).serializable_hash } subject { ChampSerializer.new(serializable_object).serializable_hash }
let(:serializable_object) { champ }
context 'when type champ is piece justificative' do context 'when type champ is piece justificative' do
include Rails.application.routes.url_helpers include Rails.application.routes.url_helpers
@ -20,51 +21,118 @@ describe ChampSerializer do
end end
context 'when type champ is carte' do context 'when type champ is carte' do
let(:geo_area) { create(:geo_area) } let(:champ) { create(:champ_carte, value: value, geo_areas: [geo_area].compact) }
let(:coordinates) { [[{ "lat": 48.87442541960633, "lng": 2.3859214782714844 }, { "lat": 48.87273183590832, "lng": 2.3850631713867183 }, { "lat": 48.87081237174292, "lng": 2.3809432983398438 }, { "lat": 48.8712640169951, "lng": 2.377510070800781 }, { "lat": 48.87510283703279, "lng": 2.3778533935546875 }, { "lat": 48.87544154230615, "lng": 2.382831573486328 }, { "lat": 48.87442541960633, "lng": 2.3859214782714844 }]] } let(:value) { nil }
let(:geo_area) { create(:geo_area, geometry: parsed_geo_json) }
let(:parsed_geo_json) { JSON.parse(geo_json) }
let(:geo_json) { GeojsonService.to_json_polygon_for_selection_utilisateur(coordinates) }
let(:coordinates) { [[{ "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }, { "lat" => 48.87273183590832, "lng" => 2.3850631713867183 }, { "lat" => 48.87081237174292, "lng" => 2.3809432983398438 }, { "lat" => 48.8712640169951, "lng" => 2.377510070800781 }, { "lat" => 48.87510283703279, "lng" => 2.3778533935546875 }, { "lat" => 48.87544154230615, "lng" => 2.382831573486328 }, { "lat" => 48.87442541960633, "lng" => 2.3859214782714844 }]] }
let(:champ_carte) { create(:champ_carte, value: coordinates.to_json, geo_areas: [geo_area]) } let(:serialized_champ) {
let(:champ) { champ_carte } {
type_de_champ: {
description: serialized_description,
id: serialized_id,
libelle: serialized_libelle,
order_place: serialized_order_place,
type_champ: serialized_type_champ
},
geo_areas: serialized_geo_areas,
value: geo_json
}.compact
}
let(:serialized_id) { -1 }
let(:serialized_description) { "" }
let(:serialized_order_place) { -1 }
let(:serialized_geo_areas) { nil }
context 'legacy champ user_geometry' do context 'and geo_area is selection_utilisateur' do
let(:champ) { champ_carte.user_geo_area } context 'old_api' do
let(:serialized_libelle) { "user geometry" }
let(:serialized_type_champ) { "user_geometry" }
it { let(:serializable_object) { champ.user_geo_area }
expect(subject).to include(
type_de_champ: { context 'when value is coordinates' do
descripton: "", let(:value) { coordinates.to_json }
id: -1,
libelle: "user geometry", it { expect(subject).to eq(serialized_champ) }
order_place: -1, end
type_champ: "user_geometry"
}, context 'when value is geojson' do
value: champ_carte.user_geometry let(:value) { geo_json }
)
} it { expect(subject).to eq(serialized_champ) }
end
end
context 'new_api' do
let(:geo_area) { nil }
let(:serialized_geo_areas) { [] }
let(:serialized_id) { champ.type_de_champ.stable_id }
let(:serialized_description) { champ.description }
let(:serialized_order_place) { champ.order_place }
let(:serialized_libelle) { champ.libelle }
let(:serialized_type_champ) { champ.type_champ }
context 'when value is coordinates' do
let(:value) { coordinates.to_json }
it { expect(subject).to eq(serialized_champ) }
end
context 'when value is geojson' do
let(:value) { geo_json }
it { expect(subject).to eq(serialized_champ) }
end
end
end end
context 'and geo_area is cadastre' do context 'and geo_area is cadastre' do
it { context 'new_api' do
expect(subject[:geo_areas].first).to include( it {
source: GeoArea.sources.fetch(:cadastre), expect(subject[:geo_areas].first).to include(
numero: '42', source: GeoArea.sources.fetch(:cadastre),
feuille: 'A11' geometry: parsed_geo_json,
) numero: '42',
expect(subject[:geo_areas].first.key?(:nom)).to be_falsey feuille: 'A11'
} )
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
end end
context 'and geo_area is quartier_prioritaire' do context 'and geo_area is quartier_prioritaire' do
let(:geo_area) { create(:geo_area, :quartier_prioritaire) } let(:geo_area) { create(:geo_area, :quartier_prioritaire, geometry: parsed_geo_json) }
it { context 'new_api' do
expect(subject[:geo_areas].first).to include( it {
source: GeoArea.sources.fetch(:quartier_prioritaire), expect(subject[:geo_areas].first).to include(
nom: 'XYZ', source: GeoArea.sources.fetch(:quartier_prioritaire),
commune: 'Paris' geometry: parsed_geo_json,
) nom: 'XYZ',
expect(subject[:geo_areas].first.key?(:numero)).to be_falsey commune: 'Paris'
} )
expect(subject[:geo_areas].first.key?(:numero)).to be_falsey
}
end
context 'old_api' do
let(:serializable_object) { champ.geo_areas.first }
let(:serialized_libelle) { "quartier prioritaire" }
let(:serialized_type_champ) { "quartier_prioritaire" }
it { expect(subject).to eq(serialized_champ) }
end
end end
end end