Skip to content

Commit 95c012a

Browse files
device_error_event + tests
Signed-off-by: Gabriele Ghio <gabriele.ghio@secomind.com>
1 parent b818fa5 commit 95c012a

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#
2+
# This file is part of Astarte.
3+
#
4+
# Copyright 2025 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
defmodule Astarte.Core.Generators.Triggers.SimpleEvents.DeviceErrorEvent do
20+
@moduledoc """
21+
This module provides generators for Astarte Trigger Simple Event DeviceErrorEvent struct.
22+
"""
23+
use ExUnitProperties
24+
25+
import Astarte.Generators.Utilities.ParamsGen
26+
27+
alias Astarte.Core.Triggers.SimpleEvents.DeviceErrorEvent
28+
29+
alias Astarte.Utilities.Map, as: MapUtilities
30+
31+
@spec device_error_event() :: StreamData.t(DeviceErrorEvent.t())
32+
@spec device_error_event(keyword :: keyword()) :: StreamData.t(DeviceErrorEvent.t())
33+
def device_error_event(params \\ []) do
34+
params gen all error_name <- error_name(),
35+
metadata <- metadata(),
36+
params: params do
37+
%DeviceErrorEvent{
38+
error_name: error_name,
39+
metadata: metadata
40+
}
41+
end
42+
end
43+
44+
@doc """
45+
Convert this struct/stream to changes
46+
"""
47+
@spec to_changes(DeviceErrorEvent.t()) :: StreamData.t(map())
48+
def to_changes(data) when not is_struct(data, StreamData),
49+
do: data |> constant() |> to_changes()
50+
51+
@spec to_changes(StreamData.t(DeviceErrorEvent.t())) :: StreamData.t(map())
52+
def to_changes(gen) do
53+
gen all %DeviceErrorEvent{
54+
error_name: error_name,
55+
metadata: metadata
56+
} <- gen do
57+
%{
58+
error_name: error_name,
59+
metadata: metadata
60+
}
61+
|> MapUtilities.clean()
62+
end
63+
end
64+
65+
defp error_name, do: one_of([nil, string(:utf8)])
66+
67+
defp metadata,
68+
do: map_of(string(:utf8), string(:utf8), min_length: 0, max_length: 10)
69+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# This file is part of Astarte.
3+
#
4+
# Copyright 2025 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
defmodule Astarte.Core.Generators.Triggers.SimpleEvents.DeviceErrorEventTest do
20+
use ExUnit.Case, async: true
21+
use ExUnitProperties
22+
23+
alias Astarte.Core.Triggers.SimpleEvents.DeviceErrorEvent
24+
25+
alias Astarte.Core.Generators.Triggers.SimpleEvents.DeviceErrorEvent,
26+
as: DeviceErrorEventGenerator
27+
28+
@moduletag :trigger
29+
@moduletag :simple_event
30+
@moduletag :device_error_event
31+
32+
@doc false
33+
describe "triggers device_error_event generator" do
34+
@describetag :success
35+
@describetag :ut
36+
property "generates valid device_error_event" do
37+
check all device_error_event <- DeviceErrorEventGenerator.device_error_event() do
38+
assert %DeviceErrorEvent{} = device_error_event
39+
end
40+
end
41+
42+
property "generates valid changes using to_changes (gen)" do
43+
gen_device_error_event_changes =
44+
DeviceErrorEventGenerator.device_error_event()
45+
|> DeviceErrorEventGenerator.to_changes()
46+
47+
check all changes <- gen_device_error_event_changes do
48+
assert is_map(changes)
49+
end
50+
end
51+
52+
property "generates valid changes using to_changes (struct)" do
53+
check all device_error_event <- DeviceErrorEventGenerator.device_error_event(),
54+
changes <- DeviceErrorEventGenerator.to_changes(device_error_event) do
55+
assert is_map(changes)
56+
end
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)