Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/astarte_core/mapping.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ defmodule Astarte.Core.Mapping do
:explicit_timestamp,
:description,
:doc,
:path
:path,
:required
| @required_fields
]

Expand All @@ -64,6 +65,7 @@ defmodule Astarte.Core.Mapping do
field :doc
field :endpoint_id, :binary
field :interface_id, :binary
field :required, :boolean, default: false
# Legacy support
field :path, :string, virtual: true
# Different input naming
Expand Down Expand Up @@ -182,7 +184,8 @@ defmodule Astarte.Core.Mapping do
allow_unset: allow_unset,
explicit_timestamp: explicit_timestamp,
endpoint_id: endpoint_id,
interface_id: interface_id
interface_id: interface_id,
required: required
} = db_result

database_retention_policy =
Expand All @@ -206,7 +209,8 @@ defmodule Astarte.Core.Mapping do
endpoint_id: endpoint_id,
interface_id: interface_id,
doc: doc,
description: description
description: description,
required: Kernel.||(required, false)
}
end

Expand Down Expand Up @@ -259,7 +263,8 @@ defmodule Astarte.Core.Mapping do
allow_unset: allow_unset,
explicit_timestamp: explicit_timestamp,
description: description,
doc: doc
doc: doc,
required: required
} = mapping

%{
Expand All @@ -275,6 +280,7 @@ defmodule Astarte.Core.Mapping do
|> add_key_if_not_default(:explicit_timestamp, explicit_timestamp, false)
|> add_key_if_not_nil(:description, description)
|> add_key_if_not_nil(:doc, doc)
|> add_key_if_not_default(:required, required, false)
|> Jason.Encoder.Map.encode(options)
end

Expand Down
47 changes: 44 additions & 3 deletions test/astarte_core/mapping_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ defmodule Astarte.Core.MappingTest do
expiry: 0,
database_retention_policy: :no_ttl,
database_retention_ttl: nil,
allow_unset: false
allow_unset: false,
required: false
} = mapping
end

Expand Down Expand Up @@ -200,6 +201,44 @@ defmodule Astarte.Core.MappingTest do
} = Mapping.changeset(%Mapping{}, params, opts)
end

test "required flag defaults to false" do
opts = opts_fixture()

params = %{
"endpoint" => "/valid",
"type" => "string"
}

changeset = Mapping.changeset(%Mapping{}, params, opts)
{:ok, mapping} = Ecto.Changeset.apply_action(changeset, :insert)
assert %Mapping{required: false} = mapping
end

test "required flag can be set to true" do
opts = opts_fixture()

params = %{
"endpoint" => "/valid",
"type" => "string",
"required" => true
}

assert %Ecto.Changeset{valid?: true} = Mapping.changeset(%Mapping{}, params, opts)
end

test "required flag with invalid value fails" do
opts = opts_fixture()

params = %{
"endpoint" => "/valid",
"type" => "string",
"required" => "not_a_boolean"
}

assert %Ecto.Changeset{valid?: false, errors: [required: _]} =
Mapping.changeset(%Mapping{}, params, opts)
end

test "mapping from legacy database result" do
legacy_result = [
endpoint: "/test",
Expand All @@ -215,7 +254,8 @@ defmodule Astarte.Core.MappingTest do
allow_unset: false,
explicit_timestamp: true,
endpoint_id: <<24, 101, 36, 39, 201, 240, 51, 175, 45, 122, 166, 194, 132, 91, 176, 154>>,
interface_id: <<3, 203, 231, 42, 212, 254, 30, 12, 159, 114, 187, 196, 29, 30, 5, 219>>
interface_id: <<3, 203, 231, 42, 212, 254, 30, 12, 159, 114, 187, 196, 29, 30, 5, 219>>,
required: nil
]

expected_mapping = %Mapping{
Expand All @@ -231,7 +271,8 @@ defmodule Astarte.Core.MappingTest do
description: nil,
doc: nil,
endpoint_id: <<24, 101, 36, 39, 201, 240, 51, 175, 45, 122, 166, 194, 132, 91, 176, 154>>,
interface_id: <<3, 203, 231, 42, 212, 254, 30, 12, 159, 114, 187, 196, 29, 30, 5, 219>>
interface_id: <<3, 203, 231, 42, 212, 254, 30, 12, 159, 114, 187, 196, 29, 30, 5, 219>>,
required: false
}

assert Mapping.from_db_result!(legacy_result) == expected_mapping
Expand Down
Loading