@@ -33,6 +33,7 @@ defmodule Transport.IRVE.ConsolidationTest do
3333 % {
3434 "dataset_id" => "another-dataset-id" ,
3535 "resource_id" => "another-resource-id" ,
36+ "resource_status" => "on_datagouv_not_in_db" ,
3637 "consolidation_status" => "file_level_errors" ,
3738 "error_type" => nil ,
3839 "estimated_pdc_count" => "1" ,
@@ -50,6 +51,7 @@ defmodule Transport.IRVE.ConsolidationTest do
5051 % {
5152 "dataset_id" => "individual-published-dataset-id" ,
5253 "resource_id" => "individual-published-resource-id" ,
54+ "resource_status" => "on_datagouv_not_in_db" ,
5355 "consolidation_status" => "producer_not_an_organization" ,
5456 "error_type" => nil ,
5557 "estimated_pdc_count" => "1" ,
@@ -63,6 +65,7 @@ defmodule Transport.IRVE.ConsolidationTest do
6365 % {
6466 "dataset_id" => "the-dataset-id" ,
6567 "resource_id" => "the-resource-id" ,
68+ "resource_status" => "on_datagouv_not_in_db" ,
6669 "consolidation_status" => "import_successful" ,
6770 "error_type" => nil ,
6871 "estimated_pdc_count" => "1" ,
@@ -79,6 +82,7 @@ defmodule Transport.IRVE.ConsolidationTest do
7982 |> Explorer.DataFrame . select ( [
8083 "dataset_id" ,
8184 "resource_id" ,
85+ "resource_status" ,
8286 "consolidation_status" ,
8387 "error_type" ,
8488 "estimated_pdc_count" ,
@@ -222,6 +226,78 @@ defmodule Transport.IRVE.ConsolidationTest do
222226 refute File . exists? ( System . tmp_dir! ( ) |> Path . join ( "irve-resource-the-resource-id.dat" ) )
223227 refute File . exists? ( System . tmp_dir! ( ) |> Path . join ( "irve-resource-another-resource-id.dat" ) )
224228 end
229+
230+ test "reports the presence delta between data.gouv.fr and the database" do
231+ mock_datagouv_resources ( )
232+ mock_resource_downloads ( )
233+
234+ on_exit ( fn ->
235+ ~w(
236+ consolidation_transport_irve_statique_rapport.csv
237+ consolidation_transport_avec_doublons_irve_statique.csv
238+ consolidation_transport_irve_statique.csv
239+ )
240+ |> Enum . each ( & File . rm / 1 )
241+ end )
242+
243+ # Still in the DB but no longer listed on data.gouv.fr, with 2 stored PDCs.
244+ seed_file_with_pdcs ( "deleted-dataset-id" , "deleted-resource-id" , 2 )
245+
246+ # Already in the DB (older version) and still listed on data.gouv.fr.
247+ DB.Repo . insert! ( % DB.IRVEValidFile {
248+ datagouv_dataset_id: "the-dataset-id" ,
249+ datagouv_resource_id: "the-resource-id" ,
250+ checksum: "old-checksum"
251+ } )
252+
253+ { :ok , report_df } = Transport.IRVE.Consolidation . process ( destination: :local_disk )
254+
255+ rows_by_id =
256+ report_df
257+ |> Explorer.DataFrame . to_rows ( )
258+ |> Map . new ( fn row -> { row [ "resource_id" ] , row } end )
259+
260+ # Had a version in the DB, imported a new one this run.
261+ assert rows_by_id [ "the-resource-id" ] [ "resource_status" ] == "on_datagouv_and_in_db"
262+ assert rows_by_id [ "the-resource-id" ] [ "consolidation_status" ] == "import_successful"
263+
264+ # Brand-new resources (nothing in the DB before this run).
265+ assert rows_by_id [ "another-resource-id" ] [ "resource_status" ] == "on_datagouv_not_in_db"
266+ assert rows_by_id [ "individual-published-resource-id" ] [ "resource_status" ] == "on_datagouv_not_in_db"
267+
268+ # Invariant: cannot be up-to-date without being in the DB.
269+ refute Enum . any? ( rows_by_id , fn { _id , row } ->
270+ row [ "resource_status" ] == "on_datagouv_not_in_db" and
271+ row [ "consolidation_status" ] == "already_up_to_date"
272+ end )
273+
274+ orphan = rows_by_id [ "deleted-resource-id" ]
275+ assert orphan [ "resource_status" ] == "in_db_deleted_from_datagouv"
276+ assert is_nil ( orphan [ "consolidation_status" ] )
277+ assert orphan [ "estimated_pdc_count" ] == 2
278+ assert orphan [ "dataset_id" ] == "deleted-dataset-id"
279+ end
280+ end
281+
282+ defp seed_file_with_pdcs ( dataset_id , resource_id , pdc_count ) do
283+ rows =
284+ for i <- 1 .. pdc_count do
285+ DB.Factory.IRVE . generate_row ( % { "id_pdc_itinerance" => "FRPAN99E0000000#{ i } " } )
286+ end
287+
288+ path = System . tmp_dir! ( ) |> Path . join ( "irve_seed_#{ Ecto.UUID . generate ( ) } .csv" )
289+ File . write! ( path , DB.Factory.IRVE . to_csv_body ( rows ) )
290+
291+ Transport.IRVE.DatabaseImporter . write_to_db (
292+ path ,
293+ dataset_id ,
294+ resource_id ,
295+ "#{ dataset_id } -title" ,
296+ "#{ dataset_id } -org" ,
297+ "2024-01-01T10:00:00+00:00"
298+ )
299+
300+ File . rm ( path )
225301 end
226302
227303 defp mock_datagouv_resources do
0 commit comments