Skip to content

Commit

Permalink
Évite les resources avec à la fois un format de donnnées de transport…
Browse files Browse the repository at this point in the history
… public et un schéma de données (#3728)

* Add a validation to avoid weird resources

* Apply suggestions from code review

Co-authored-by: Antoine Augusti <antoine.augusti@transport.data.gouv.fr>

* syntax and test fixes

* add datagouv_id to test

---------

Co-authored-by: Antoine Augusti <antoine.augusti@transport.data.gouv.fr>
  • Loading branch information
vdegove and AntoineAugusti authored Jan 18, 2024
1 parent 7a766f1 commit cc35e69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions apps/transport/lib/db/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ defmodule DB.Resource do
]
)
|> validate_required([:url, :datagouv_id])
|> no_schema_name_for_public_transport()
end

@spec is_gtfs?(__MODULE__.t()) :: boolean()
Expand Down Expand Up @@ -371,4 +372,20 @@ defmodule DB.Resource do
"""
def proxy_namespace(%__MODULE__{format: "gbfs"}), do: "gbfs"
def proxy_namespace(%__MODULE__{}), do: "proxy"

def no_schema_name_for_public_transport(%Ecto.Changeset{} = changeset) do
schema_name = get_field(changeset, :schema_name)
format = get_field(changeset, :format)
public_transport_formats = ["GTFS", "gtfs-rt", "NeTEx", "SIRI", "SIRI Lite"]

if format in public_transport_formats and is_binary(schema_name) do
changeset
|> add_error(:schema_name, "Public transport formats can’t have a schema set",
resource_id: get_field(changeset, :id),
resource_datagouv_id: get_field(changeset, :datagouv_id)
)
else
changeset
end
end
end
9 changes: 9 additions & 0 deletions apps/transport/test/db/resource_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,13 @@ defmodule DB.ResourceTest do
"https://raw.githubusercontent.com/etalab/transport-base-nationale-covoiturage/898dc67fb19fae2464c24a85a0557e8ccce18791/bnlc-.csv"
}) == resource_url(TransportWeb.Endpoint, :download, id)
end

test "invalid resource" do
resource = %Resource{format: "GTFS", schema_name: "anything", datagouv_id: datagouv_id = Ecto.UUID.generate()}
assert %Ecto.Changeset{valid?: false} = changeset = Resource.changeset(resource, %{})

assert {"Public transport formats can’t have a schema set",
[{:resource_id, nil}, {:resource_datagouv_id, datagouv_id}]} ==
changeset.errors[:schema_name]
end
end

0 comments on commit cc35e69

Please sign in to comment.