From 9208de61696028725b2ed9cd4abdf3bb7f9fb228 Mon Sep 17 00:00:00 2001 From: Antoine Augusti Date: Wed, 11 Dec 2024 15:44:11 +0100 Subject: [PATCH] ConsolidateLEZsJob : ignore ressources sans historisation valide (#4372) --- .../transport/lib/jobs/consolidate_lez_job.ex | 11 ++-- .../jobs/consolidate_lez_job_test.exs | 60 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/apps/transport/lib/jobs/consolidate_lez_job.ex b/apps/transport/lib/jobs/consolidate_lez_job.ex index 0c9563d4e9..c90bad864b 100644 --- a/apps/transport/lib/jobs/consolidate_lez_job.ex +++ b/apps/transport/lib/jobs/consolidate_lez_job.ex @@ -130,13 +130,16 @@ defmodule Transport.Jobs.ConsolidateLEZsJob do def consolidate_features(resources) do %{ type: "FeatureCollection", - features: resources |> Enum.flat_map(&content_features/1) + features: + resources + |> Enum.map(fn %Resource{} = resource -> {resource, latest_valid_resource_history(resource)} end) + # Do not consolidate resources without a valid resource history + |> Enum.reject(fn {_, resource_history} -> is_nil(resource_history) end) + |> Enum.flat_map(&content_features/1) } end - defp content_features(%Resource{} = resource) do - %ResourceHistory{payload: %{"permanent_url" => url}} = latest_valid_resource_history(resource) - + defp content_features({%Resource{} = resource, %ResourceHistory{payload: %{"permanent_url" => url}}}) do %HTTPoison.Response{status_code: 200, body: body} = http_client().get!(url, [], follow_redirect: true) body diff --git a/apps/transport/test/transport/jobs/consolidate_lez_job_test.exs b/apps/transport/test/transport/jobs/consolidate_lez_job_test.exs index 6bae3507f1..5baa18bdd8 100644 --- a/apps/transport/test/transport/jobs/consolidate_lez_job_test.exs +++ b/apps/transport/test/transport/jobs/consolidate_lez_job_test.exs @@ -180,6 +180,66 @@ defmodule Transport.Test.Transport.Jobs.ConsolidateLEZsJobTest do ] == ConsolidateLEZsJob.consolidate() end + test "consolidate_features ignores resources without a valid resource history" do + aom = insert(:aom, siren: "253800825", nom: "SMM de l’Aire Grenobloise", forme_juridique: "Métropole") + dataset = insert(:dataset, type: "low-emission-zones", organization: "Sample", aom: aom) + + zfe_aire = + insert(:resource, + dataset: dataset, + url: "https://example.com/aires.geojson", + schema_name: "etalab/schema-zfe" + ) + + zfe_voies = + insert(:resource, + dataset: dataset, + url: "https://example.com/voies.geojson", + schema_name: "etalab/schema-zfe" + ) + + resource_history_aire = + insert(:resource_history, + resource_id: zfe_aire.id, + payload: %{ + "permanent_url" => permanent_url_aires = "https://example.com/permanent_url/aires" + } + ) + + # Should be ignored as we will create an invalid MultiValidation linked to it + resource_history_voies = + insert(:resource_history, + resource_id: zfe_voies.id, + payload: %{ + "permanent_url" => "https://example.com/permanent_url/voies" + } + ) + + insert(:multi_validation, resource_history_id: resource_history_aire.id, result: %{"has_errors" => false}) + insert(:multi_validation, resource_history_id: resource_history_voies.id, result: %{"has_errors" => true}) + + Transport.HTTPoison.Mock + |> expect(:get!, fn ^permanent_url_aires, [], follow_redirect: true -> + %HTTPoison.Response{status_code: 200, body: ~s({"features": [{"properties": {"foo": "bar"}}]})} + end) + + assert %{ + features: [ + %{ + "properties" => %{"foo" => "bar"}, + "publisher" => %{ + "forme_juridique" => "Métropole", + "nom" => "SMM de l’Aire Grenobloise", + "siren" => "253800825", + "zfe_id" => "GRENOBLE" + } + } + ], + type: "FeatureCollection" + } == + ConsolidateLEZsJob.consolidate_features([zfe_aire, zfe_voies]) + end + describe "publisher_details" do test "with an AOM" do aom = insert(:aom, siren: "253800825", nom: "SMM de l’Aire Grenobloise", forme_juridique: "Métropole")