perf(carto): use json schema to validate geojson instead of rgeo

This commit is contained in:
Paul Chavard 2023-04-07 15:24:28 +02:00
parent 3d3b90c4fb
commit c4432b1b47
11 changed files with 1312 additions and 99 deletions

View file

@ -46,7 +46,7 @@ describe Champs::CarteController, type: :controller do
end
context 'error' do
let(:feature) { attributes_for(:geo_area, :invalid_right_hand_rule_polygon) }
let(:feature) { attributes_for(:geo_area, :invalid_point) }
let(:params) do
{
champ_id: champ.id,
@ -92,7 +92,7 @@ describe Champs::CarteController, type: :controller do
end
context 'error' do
let(:feature) { attributes_for(:geo_area, :invalid_right_hand_rule_polygon) }
let(:feature) { attributes_for(:geo_area, :invalid_point) }
it { expect(response.status).to eq 422 }
end

View file

@ -116,6 +116,15 @@ FactoryBot.define do
end
end
trait :invalid_point do
geometry do
{
"type": "Point",
"coordinates": [46.538476837725796]
}
end
end
trait :multi_polygon do
geometry do
{

View file

@ -23,36 +23,6 @@ RSpec.describe GeoArea, type: :model do
it { expect(geo_area.location).to eq("46°32'19\"N 2°25'42\"E") }
end
describe '#geometry' do
let(:geo_area) { build(:geo_area, :polygon, champ: nil) }
let(:polygon) do
{
"type" => "Polygon",
"coordinates" => [
[
[2.428439855575562, 46.538476837725796],
[2.4284291267395024, 46.53842148758162],
[2.4282521009445195, 46.53841410755813],
[2.42824137210846, 46.53847314771794],
[2.428284287452698, 46.53847314771794],
[2.428364753723145, 46.538487907747864],
[2.4284291267395024, 46.538491597754714],
[2.428439855575562, 46.538476837725796]
]
]
}
end
it { expect(geo_area.geometry).to eq(polygon) }
context 'polygon_with_extra_coordinate' do
let(:geo_area) { build(:geo_area, :polygon_with_extra_coordinate, champ: nil) }
before { geo_area.valid? }
it { expect(geo_area.geometry).to eq(polygon) }
end
end
describe 'validations' do
context 'geometry' do
subject! { geo_area.validate }
@ -62,11 +32,6 @@ RSpec.describe GeoArea, type: :model do
it { expect(geo_area.errors).not_to have_key(:geometry) }
end
context 'hourglass_polygon' do
let(:geo_area) { build(:geo_area, :hourglass_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
context 'line_string' do
let(:geo_area) { build(:geo_area, :line_string, champ: nil) }
it { expect(geo_area.errors).not_to have_key(:geometry) }
@ -77,9 +42,9 @@ RSpec.describe GeoArea, type: :model do
it { expect(geo_area.errors).not_to have_key(:geometry) }
end
context 'invalid_right_hand_rule_polygon' do
let(:geo_area) { build(:geo_area, :invalid_right_hand_rule_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
context "allow empty {}" do
let(:geo_area) { build(:geo_area, geometry: {}) }
it { expect(geo_area.errors).not_to have_key(:geometry) }
end
context "nil" do
@ -87,9 +52,19 @@ RSpec.describe GeoArea, type: :model do
it { expect(geo_area.errors).to have_key(:geometry) }
end
context "allow empty {}" do
let(:geo_area) { build(:geo_area, geometry: {}) }
it { expect(geo_area.errors).not_to have_key(:geometry) }
context 'invalid point' do
let(:geo_area) { build(:geo_area, :invalid_point, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
context.skip 'invalid_right_hand_rule_polygon' do
let(:geo_area) { build(:geo_area, :invalid_right_hand_rule_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
context.skip 'hourglass_polygon' do
let(:geo_area) { build(:geo_area, :hourglass_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
end
end