Skip to content

Commit

Permalink
Import de données : meilleure détection type documentation (#4309)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti authored Nov 13, 2024
1 parent d5f7d74 commit bf175e4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
21 changes: 9 additions & 12 deletions apps/transport/lib/transport/import_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ defmodule Transport.ImportData do
@spec get_valid_documentation_resources([map()]) :: [map()]
def get_valid_documentation_resources(resources) do
resources
|> Enum.filter(&(documentation?(&1) or documentation_format?(&1)))
|> Enum.filter(&documentation?/1)
|> Enum.map(fn resource -> %{resource | "type" => "documentation"} end)
end

Expand Down Expand Up @@ -539,40 +539,37 @@ defmodule Transport.ImportData do
def gtfs_rt?(_), do: false

@doc """
iex> documentation?(%{"format" => "gtfs"})
iex> documentation?(%{"title" => "Fichier", "format" => "gtfs"})
false
iex> documentation?(%{"format" => "csv"})
false
iex> documentation?(%{"format" => "PDF"})
iex> documentation?(%{"title" => "Fichier", "format" => "csv"})
false
iex> documentation?(%{"title" => "Description des données", "format" => "PDF"})
true
iex> documentation?(%{"type" => "documentation", "format" => "docx"})
true
iex> documentation?(nil)
false
iex> documentation?("pdf")
false
iex> documentation?(%{"type" => "main", "title" => "Documentation SIRI"})
iex> documentation?(%{"type" => "main", "title" => "Documentation SIRI", "format" => "xlsx"})
true
"""
@spec documentation?(any()) :: boolean()
def documentation?(%{"type" => "documentation"}), do: true

def documentation?(%{"title" => resource_title}) do
String.match?(resource_title, ~r/\bdocumentation\b/i)
def documentation?(%{"title" => resource_title, "format" => _} = args) do
String.match?(resource_title, ~r/\bdocumentation\b/i) or documentation_format?(args)
end

def documentation?(_), do: false

@doc """
Determines if a format is likely a documentation format.
Only used for the `public-transit` type, other types use
`documentation?/1` which is stricter.
iex> documentation_format?("PDF")
true
iex> documentation_format?("GTFS")
false
"""
@spec documentation_format?(map() | binary() | nil) :: boolean()
def documentation_format?(%{"format" => format}), do: documentation_format?(format)

def documentation_format?(format) do
Expand Down
8 changes: 4 additions & 4 deletions apps/transport/test/transport/import_data_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,13 @@ defmodule Transport.ImportDataTest do
test "get_valid_resources for public-transit detects documentation resources" do
resources = %{
"resources" => [
%{"type" => "main", "format" => "gtfs"},
%{"type" => "main", "format" => "geojson"},
%{"type" => "main", "format" => "svg"}
%{"type" => "main", "title" => "Fichier", "format" => "gtfs"},
%{"type" => "main", "title" => "Fichier", "format" => "geojson"},
%{"type" => "main", "title" => "Fichier", "format" => "svg"}
]
}

assert [%{"format" => "gtfs", "type" => "main"}, %{"format" => "svg", "type" => "documentation"}] ==
assert [%{"format" => "gtfs", "type" => "main"}, %{"format" => "svg", "type" => "documentation"}] =
ImportData.get_valid_resources(resources, "public-transit")
end

Expand Down

0 comments on commit bf175e4

Please sign in to comment.