Skip to content

Commit

Permalink
Add a presence validation for a few dataset fields
Browse files Browse the repository at this point in the history
  • Loading branch information
vdegove committed Oct 28, 2024
1 parent 16f8efd commit 7023c66
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
11 changes: 9 additions & 2 deletions apps/transport/lib/db/dataset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,25 @@ defmodule DB.Dataset do
|> cast_datagouv_zone(params, territory_name)
|> cast_nation_dataset(params)
|> cast_assoc(:resources)
|> validate_required([:slug])
|> validate_siren()
|> validate_territory_mutual_exclusion()
|> maybe_overwrite_licence()
|> has_real_time()
|> set_is_hidden()
|> validate_organization_type()
|> add_organization(params)
|> validate_required([:organization_id])
|> maybe_set_custom_logo_changed_at()
|> put_assoc(:legal_owners_aom, legal_owners_aom)
|> put_assoc(:legal_owners_region, legal_owners_region)
|> validate_required([
:datagouv_id,
:custom_title,
:licence,
:slug,
:datagouv_title,
:type,
:organization_id
])
|> case do
%{valid?: false, changes: changes} = changeset when changes == %{} ->
{:ok, %{changeset | action: :ignore}}
Expand Down
50 changes: 50 additions & 0 deletions apps/transport/test/db/db_dataset_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ defmodule DB.DatasetDBTest do
assert {:ok, _} =
Dataset.changeset(%{
"datagouv_id" => "1",
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "ma_limace",
"insee" => "38185",
"organization_id" => Ecto.UUID.generate()
Expand All @@ -72,6 +76,10 @@ defmodule DB.DatasetDBTest do
assert {:ok, _} =
Dataset.changeset(%{
"datagouv_id" => "1",
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "ma_limace",
"zones" => ["38185"],
"associated_territory_name" => "paris",
Expand All @@ -83,6 +91,10 @@ defmodule DB.DatasetDBTest do
assert {:ok, _} =
Dataset.changeset(%{
"datagouv_id" => "1",
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "ma_limace",
"national_dataset" => "true",
"organization_id" => Ecto.UUID.generate()
Expand All @@ -109,6 +121,10 @@ defmodule DB.DatasetDBTest do
assert {:ok, %Ecto.Changeset{changes: %{aom_id: nil, region_id: 1}}} =
Dataset.changeset(%{
"datagouv_id" => datagouv_id,
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"national_dataset" => "true",
"insee" => nil
})
Expand All @@ -118,6 +134,10 @@ defmodule DB.DatasetDBTest do
changeset =
Dataset.changeset(%{
"datagouv_id" => "1",
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "ma_limace",
"insee" => "38185",
"resources" => [
Expand All @@ -134,6 +154,10 @@ defmodule DB.DatasetDBTest do
changeset =
Dataset.changeset(%{
"datagouv_id" => "1",
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"organization_id" => Ecto.UUID.generate(),
"slug" => "ma_limace",
"insee" => "38185",
Expand All @@ -147,6 +171,10 @@ defmodule DB.DatasetDBTest do
assert {:ok, %Ecto.Changeset{changes: %{is_hidden: true}}} =
Dataset.changeset(%{
"datagouv_id" => "1",
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "ma_limace",
"insee" => "38185",
"custom_tags" => ["masqué"],
Expand All @@ -158,6 +186,10 @@ defmodule DB.DatasetDBTest do
assert {:ok, %Ecto.Changeset{changes: %{is_hidden: false}}} =
Dataset.changeset(%{
"datagouv_id" => "1",
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "ma_limace",
"insee" => "38185",
"custom_tags" => ["not_hidden"],
Expand All @@ -170,6 +202,8 @@ defmodule DB.DatasetDBTest do
with_log(fn ->
Dataset.changeset(%{
"datagouv_id" => "1",
"datagouv_title" => "title",
"custom_title" => "custom title",
"slug" => "slug",
"national_dataset" => "true",
"legal_owner_company_siren" => "123456789"
Expand All @@ -181,6 +215,10 @@ defmodule DB.DatasetDBTest do
assert {:ok, %Ecto.Changeset{}} =
Dataset.changeset(%{
"datagouv_id" => "1",
"custom_title" => "custom title",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "slug",
"national_dataset" => "true",
"legal_owner_company_siren" => "552049447",
Expand All @@ -192,6 +230,9 @@ defmodule DB.DatasetDBTest do
assert {:ok, %Ecto.Changeset{changes: %{custom_title: "Foo"}}} =
Dataset.changeset(%{
"datagouv_id" => "1",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "slug",
"national_dataset" => "true",
"custom_title" => " Foo ",
Expand All @@ -204,6 +245,9 @@ defmodule DB.DatasetDBTest do
with_log(fn ->
DB.Dataset.changeset(%{
"datagouv_id" => "1",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "slug",
"national_dataset" => "true",
"custom_title" => "Foo"
Expand All @@ -219,6 +263,9 @@ defmodule DB.DatasetDBTest do
{:ok, %Ecto.Changeset{changes: changes}} =
Dataset.changeset(%{
"datagouv_id" => "1",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "slug",
"national_dataset" => "true",
"custom_title" => "foo",
Expand All @@ -232,6 +279,9 @@ defmodule DB.DatasetDBTest do
assert {:ok, %Ecto.Changeset{changes: %{custom_logo_changed_at: custom_logo_changed_at}}} =
Dataset.changeset(%{
"datagouv_id" => "1",
"datagouv_title" => "title",
"type" => "public-transit",
"licence" => "lov2",
"slug" => "slug",
"national_dataset" => "true",
"custom_title" => "foo",
Expand Down

0 comments on commit 7023c66

Please sign in to comment.