Skip to content

Commit

Permalink
ConsolidateLEZsJob : ignore ressources sans historisation valide (#4372)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti authored Dec 11, 2024
1 parent 8a397a2 commit 9208de6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
11 changes: 7 additions & 4 deletions apps/transport/lib/jobs/consolidate_lez_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions apps/transport/test/transport/jobs/consolidate_lez_job_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 9208de6

Please sign in to comment.