Skip to content

Commit b818fa5

Browse files
device_disconnected_event + test
Signed-off-by: Gabriele Ghio <gabriele.ghio@secomind.com>
1 parent a39ec8b commit b818fa5

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.DeviceDisconnectedEvent do
20+
@moduledoc """
21+
This module provides generators for Astarte Trigger Simple Event DeviceDisconnectedEvent struct.
22+
"""
23+
use ExUnitProperties
24+
25+
alias Astarte.Core.Triggers.SimpleEvents.DeviceDisconnectedEvent
26+
27+
alias Astarte.Utilities.Map, as: MapUtilities
28+
29+
@spec device_disconnected_event() :: StreamData.t(DeviceDisconnectedEvent.t())
30+
def device_disconnected_event, do: constant(%DeviceDisconnectedEvent{})
31+
32+
@doc """
33+
Convert this struct/stream to changes
34+
"""
35+
@spec to_changes(DeviceDisconnectedEvent.t()) :: StreamData.t(map())
36+
def to_changes(data) when not is_struct(data, StreamData),
37+
do: data |> constant() |> to_changes()
38+
39+
@spec to_changes(StreamData.t(DeviceDisconnectedEvent.t())) :: StreamData.t(map())
40+
def to_changes(gen) do
41+
gen all device_disconnected_event <- gen do
42+
device_disconnected_event
43+
|> Map.from_struct()
44+
|> MapUtilities.clean()
45+
end
46+
end
47+
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.DeviceDisconnectedEventTest do
20+
use ExUnit.Case, async: true
21+
use ExUnitProperties
22+
23+
alias Astarte.Core.Triggers.SimpleEvents.DeviceDisconnectedEvent
24+
25+
alias Astarte.Core.Generators.Triggers.SimpleEvents.DeviceDisconnectedEvent,
26+
as: DeviceDisconnectedEventGenerator
27+
28+
@moduletag :trigger
29+
@moduletag :simple_event
30+
@moduletag :device_disconnected_event
31+
32+
@doc false
33+
describe "triggers device_disconnected_event generator" do
34+
@describetag :success
35+
@describetag :ut
36+
property "generates valid device_disconnected_event" do
37+
check all device_disconnected_event <-
38+
DeviceDisconnectedEventGenerator.device_disconnected_event() do
39+
assert %DeviceDisconnectedEvent{} = device_disconnected_event
40+
end
41+
end
42+
43+
property "generates valid changes using to_changes (gen)" do
44+
gen_device_disconnected_event_changes =
45+
DeviceDisconnectedEventGenerator.device_disconnected_event()
46+
|> DeviceDisconnectedEventGenerator.to_changes()
47+
48+
check all changes <- gen_device_disconnected_event_changes do
49+
assert is_map(changes)
50+
end
51+
end
52+
53+
property "generates valid changes using to_changes (struct)" do
54+
check all device_disconnected_event <-
55+
DeviceDisconnectedEventGenerator.device_disconnected_event(),
56+
changes <- DeviceDisconnectedEventGenerator.to_changes(device_disconnected_event) do
57+
assert is_map(changes)
58+
end
59+
end
60+
end
61+
end

0 commit comments

Comments
 (0)