|
| 1 | +defmodule DB.IRVEValidFileTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + setup do |
| 5 | + :ok = Ecto.Adapters.SQL.Sandbox.checkout(DB.Repo) |
| 6 | + end |
| 7 | + |
| 8 | + defp insert_file!(resource_id, attrs \\ %{}) do |
| 9 | + DB.Repo.insert!(%DB.IRVEValidFile{ |
| 10 | + datagouv_dataset_id: Map.get(attrs, :dataset_id, "dataset-#{resource_id}"), |
| 11 | + datagouv_resource_id: resource_id, |
| 12 | + checksum: Map.get(attrs, :checksum, "checksum-#{resource_id}"), |
| 13 | + dataset_title: Map.get(attrs, :dataset_title, "title-#{resource_id}"), |
| 14 | + datagouv_organization_or_owner: Map.get(attrs, :org, "org-#{resource_id}") |
| 15 | + }) |
| 16 | + end |
| 17 | + |
| 18 | + describe "existing_datagouv_resource_ids/0" do |
| 19 | + test "returns the distinct set of datagouv_resource_ids" do |
| 20 | + insert_file!("resource-a") |
| 21 | + insert_file!("resource-b") |
| 22 | + # same resource, another version: must be deduplicated |
| 23 | + insert_file!("resource-a", %{checksum: "checksum-a-2"}) |
| 24 | + |
| 25 | + assert DB.IRVEValidFile.existing_datagouv_resource_ids() == |
| 26 | + MapSet.new(["resource-a", "resource-b"]) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe "orphan_files/1" do |
| 31 | + test "returns metadata and PDC count for the given resource ids only" do |
| 32 | + path = System.tmp_dir!() |> Path.join("irve_orphan_#{Ecto.UUID.generate()}.csv") |
| 33 | + |
| 34 | + File.write!( |
| 35 | + path, |
| 36 | + DB.Factory.IRVE.to_csv_body([ |
| 37 | + DB.Factory.IRVE.generate_row(%{"id_pdc_itinerance" => "FRPAN99E00000001"}), |
| 38 | + DB.Factory.IRVE.generate_row(%{"id_pdc_itinerance" => "FRPAN99E00000002"}) |
| 39 | + ]) |
| 40 | + ) |
| 41 | + |
| 42 | + Transport.IRVE.DatabaseImporter.write_to_db( |
| 43 | + path, |
| 44 | + "orphan-dataset-id", |
| 45 | + "orphan-resource-id", |
| 46 | + "orphan-dataset-title", |
| 47 | + "orphan-org", |
| 48 | + "2024-01-01T10:00:00+00:00" |
| 49 | + ) |
| 50 | + |
| 51 | + insert_file!("kept-resource-id") |
| 52 | + |
| 53 | + assert [orphan] = DB.IRVEValidFile.orphan_files(["orphan-resource-id"]) |
| 54 | + |
| 55 | + assert %{ |
| 56 | + datagouv_dataset_id: "orphan-dataset-id", |
| 57 | + datagouv_resource_id: "orphan-resource-id", |
| 58 | + dataset_title: "orphan-dataset-title", |
| 59 | + datagouv_organization_or_owner: "orphan-org", |
| 60 | + pdc_count: 2 |
| 61 | + } = orphan |
| 62 | + |
| 63 | + assert DB.IRVEValidFile.orphan_files([]) == [] |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments