Skip to content

Commit 8836381

Browse files
committed
feat(astarte_core): add required field to Mapping schema
Add a new `required` boolean field to the Mapping schema with a default value of false. Signed-off-by: nedimtokic <nedim.tokic@secomind.com>
1 parent 61155fd commit 8836381

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

lib/astarte_core/mapping.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ defmodule Astarte.Core.Mapping do
4545
:explicit_timestamp,
4646
:description,
4747
:doc,
48-
:path
48+
:path,
49+
:required
4950
| @required_fields
5051
]
5152

@@ -68,6 +69,7 @@ defmodule Astarte.Core.Mapping do
6869
field :path, :string, virtual: true
6970
# Different input naming
7071
field :type, ValueType, virtual: true
72+
field :required, :boolean, default: false
7173
end
7274

7375
def changeset(%Mapping{} = mapping, %{} = params, opts) do
@@ -182,7 +184,8 @@ defmodule Astarte.Core.Mapping do
182184
allow_unset: allow_unset,
183185
explicit_timestamp: explicit_timestamp,
184186
endpoint_id: endpoint_id,
185-
interface_id: interface_id
187+
interface_id: interface_id,
188+
required: required
186189
} = db_result
187190

188191
database_retention_policy =
@@ -206,7 +209,8 @@ defmodule Astarte.Core.Mapping do
206209
endpoint_id: endpoint_id,
207210
interface_id: interface_id,
208211
doc: doc,
209-
description: description
212+
description: description,
213+
required: Kernel.||(required, false)
210214
}
211215
end
212216

@@ -259,7 +263,8 @@ defmodule Astarte.Core.Mapping do
259263
allow_unset: allow_unset,
260264
explicit_timestamp: explicit_timestamp,
261265
description: description,
262-
doc: doc
266+
doc: doc,
267+
required: required
263268
} = mapping
264269

265270
%{
@@ -275,6 +280,7 @@ defmodule Astarte.Core.Mapping do
275280
|> add_key_if_not_default(:explicit_timestamp, explicit_timestamp, false)
276281
|> add_key_if_not_nil(:description, description)
277282
|> add_key_if_not_nil(:doc, doc)
283+
|> add_key_if_not_default(:required, required, false)
278284
|> Jason.Encoder.Map.encode(options)
279285
end
280286

test/astarte_core/mapping_test.exs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ defmodule Astarte.Core.MappingTest do
130130
expiry: 0,
131131
database_retention_policy: :no_ttl,
132132
database_retention_ttl: nil,
133-
allow_unset: false
133+
allow_unset: false,
134+
required: false
134135
} = mapping
135136
end
136137

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

204+
test "required flag defaults to false" do
205+
opts = opts_fixture()
206+
207+
params = %{
208+
"endpoint" => "/valid",
209+
"type" => "string"
210+
}
211+
212+
changeset = Mapping.changeset(%Mapping{}, params, opts)
213+
{:ok, mapping} = Ecto.Changeset.apply_action(changeset, :insert)
214+
assert %Mapping{required: false} = mapping
215+
end
216+
217+
test "required flag can be set to true" do
218+
opts = opts_fixture()
219+
220+
params = %{
221+
"endpoint" => "/valid",
222+
"type" => "string",
223+
"required" => true
224+
}
225+
226+
assert %Ecto.Changeset{valid?: true} = Mapping.changeset(%Mapping{}, params, opts)
227+
end
228+
229+
test "required flag with invalid value fails" do
230+
opts = opts_fixture()
231+
232+
params = %{
233+
"endpoint" => "/valid",
234+
"type" => "string",
235+
"required" => "not_a_boolean"
236+
}
237+
238+
assert %Ecto.Changeset{valid?: false, errors: [required: _]} =
239+
Mapping.changeset(%Mapping{}, params, opts)
240+
end
241+
203242
test "mapping from legacy database result" do
204243
legacy_result = [
205244
endpoint: "/test",
@@ -215,7 +254,8 @@ defmodule Astarte.Core.MappingTest do
215254
allow_unset: false,
216255
explicit_timestamp: true,
217256
endpoint_id: <<24, 101, 36, 39, 201, 240, 51, 175, 45, 122, 166, 194, 132, 91, 176, 154>>,
218-
interface_id: <<3, 203, 231, 42, 212, 254, 30, 12, 159, 114, 187, 196, 29, 30, 5, 219>>
257+
interface_id: <<3, 203, 231, 42, 212, 254, 30, 12, 159, 114, 187, 196, 29, 30, 5, 219>>,
258+
required: nil
219259
]
220260

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

237278
assert Mapping.from_db_result!(legacy_result) == expected_mapping

0 commit comments

Comments
 (0)