Skip to content

Commit 0569b29

Browse files
committed
test(dup): Extract Interface proprieties unset test from simple flow
1 parent ee0f545 commit 0569b29

File tree

2 files changed

+157
-38
lines changed

2 files changed

+157
-38
lines changed

apps/astarte_data_updater_plant/test/astarte_data_updater_plant/data_updater_test.exs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,44 +1290,6 @@ defmodule Astarte.DataUpdaterPlant.DataUpdaterTest do
12901290
value = Repo.one(value_query)
12911291

12921292
assert value == 5
1293-
1294-
# Unset subtest
1295-
1296-
# Delete it otherwise it gets raised
1297-
assert DataUpdater.handle_delete_volatile_trigger(
1298-
realm,
1299-
encoded_device_id,
1300-
volatile_changed_trigger_id
1301-
) == :ok
1302-
1303-
DataUpdater.handle_data(
1304-
realm,
1305-
encoded_device_id,
1306-
"com.test.LCDMonitor",
1307-
"/weekSchedule/10/start",
1308-
<<>>,
1309-
gen_tracking_id(),
1310-
make_timestamp("2017-10-09T15:10:32+00:00")
1311-
)
1312-
1313-
DataUpdater.dump_state(realm, encoded_device_id)
1314-
1315-
endpoint_id =
1316-
retrieve_endpoint_id(realm, "com.test.LCDMonitor", 1, "/weekSchedule/10/start")
1317-
1318-
value_query =
1319-
from ip in IndividualProperty,
1320-
prefix: ^keyspace_name,
1321-
where:
1322-
ip.device_id == ^device_id and
1323-
ip.interface_id == ^CQLUtils.interface_id("com.test.LCDMonitor", 1) and
1324-
ip.endpoint_id == ^endpoint_id and
1325-
ip.path == "/weekSchedule/10/start",
1326-
select: ip.longinteger_value
1327-
1328-
value = Repo.one(value_query)
1329-
1330-
assert value == nil
13311293
end
13321294

13331295
test "empty introspection is updated correctly", %{realm: realm, helper_name: helper_name} do
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#
2+
# This file is part of Astarte.
3+
#
4+
# Copyright 2017 - 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 Astate.DataUpdaterPlant.UnsetTest do
20+
use ExUnit.Case, async: true
21+
import Mox
22+
23+
alias Astarte.DataUpdaterPlant.DatabaseTestHelper
24+
alias Astarte.DataUpdaterPlant.AMQPTestHelper
25+
alias Astarte.Core.Device
26+
alias Astarte.DataUpdaterPlant.DataUpdater
27+
alias Astarte.DataAccess.Realms.Realm
28+
alias Astarte.DataAccess.Repo
29+
alias Astarte.DataAccess.Realms.IndividualProperty
30+
alias Astarte.DataAccess.Realms.Interface
31+
alias Astarte.DataUpdaterPlant.DatabaseTestHelper
32+
33+
alias Astarte.Core.CQLUtils
34+
35+
import Ecto.Query
36+
37+
setup :verify_on_exit!
38+
39+
setup do
40+
realm_string = "autotestrealm#{System.unique_integer([:positive])}"
41+
{:ok, _keyspace_name} = DatabaseTestHelper.create_test_keyspace(realm_string)
42+
43+
on_exit(fn ->
44+
DatabaseTestHelper.destroy_local_test_keyspace(realm_string)
45+
end)
46+
47+
helper_name = String.to_atom("helper_#{realm_string}")
48+
49+
consumer_name = String.to_atom("consumer_#{realm_string}")
50+
51+
{:ok, _pid} = AMQPTestHelper.start_link(name: helper_name, realm: realm_string)
52+
53+
{:ok, _consumer_pid} =
54+
AMQPTestHelper.start_events_consumer(
55+
name: consumer_name,
56+
realm: realm_string,
57+
helper_name: helper_name
58+
)
59+
60+
{:ok, %{realm: realm_string, helper_name: helper_name}}
61+
end
62+
63+
test "Unset values from interface properties", %{
64+
realm: realm,
65+
helper_name: helper_name
66+
} do
67+
AMQPTestHelper.clean_queue(helper_name)
68+
69+
encoded_device_id = "f0VMRgIBAQAAAAAAAAAAAA"
70+
keyspace_name = Realm.keyspace_name(realm)
71+
{:ok, device_id} = Device.decode_device_id(encoded_device_id)
72+
volatile_changed_trigger_id = :crypto.strong_rand_bytes(16)
73+
74+
received_msgs = 45000
75+
received_bytes = 4_500_000
76+
existing_introspection_map = %{"com.test.LCDMonitor" => 1, "com.test.SimpleStreamTest" => 1}
77+
78+
insert_opts = [
79+
introspection: existing_introspection_map,
80+
total_received_msgs: received_msgs,
81+
total_received_bytes: received_bytes,
82+
groups: ["group1"]
83+
]
84+
85+
DatabaseTestHelper.insert_device(realm, device_id, insert_opts)
86+
87+
assert DataUpdater.handle_delete_volatile_trigger(
88+
realm,
89+
encoded_device_id,
90+
volatile_changed_trigger_id
91+
) == :ok
92+
93+
DataUpdater.handle_data(
94+
realm,
95+
encoded_device_id,
96+
"com.test.LCDMonitor",
97+
"/weekSchedule/10/start",
98+
<<>>,
99+
gen_tracking_id(),
100+
make_timestamp("2017-10-09T15:10:32+00:00")
101+
)
102+
103+
DataUpdater.dump_state(realm, encoded_device_id)
104+
105+
endpoint_id =
106+
retrieve_endpoint_id(realm, "com.test.LCDMonitor", 1, "/weekSchedule/10/start")
107+
108+
value_query =
109+
from ip in IndividualProperty,
110+
prefix: ^keyspace_name,
111+
where:
112+
ip.device_id == ^device_id and
113+
ip.interface_id == ^CQLUtils.interface_id("com.test.LCDMonitor", 1) and
114+
ip.endpoint_id == ^endpoint_id and
115+
ip.path == "/weekSchedule/10/start",
116+
select: ip.longinteger_value
117+
118+
value = Repo.one(value_query)
119+
120+
assert value == nil
121+
end
122+
123+
defp retrieve_endpoint_id(realm_name, interface_name, interface_major, path) do
124+
keyspace_name = Realm.keyspace_name(realm_name)
125+
126+
query =
127+
from i in Interface,
128+
prefix: ^keyspace_name,
129+
where: i.name == ^interface_name and i.major_version == ^interface_major,
130+
select: %{
131+
automaton_transitions: i.automaton_transitions,
132+
automaton_accepting_states: i.automaton_accepting_states
133+
}
134+
135+
interface_row = Repo.one!(query)
136+
137+
automaton =
138+
{:erlang.binary_to_term(interface_row[:automaton_transitions]),
139+
:erlang.binary_to_term(interface_row[:automaton_accepting_states])}
140+
141+
{:ok, endpoint_id} = Astarte.Core.Mapping.EndpointsAutomaton.resolve_path(path, automaton)
142+
143+
endpoint_id
144+
end
145+
146+
defp make_timestamp(timestamp_string) do
147+
{:ok, date_time, _} = DateTime.from_iso8601(timestamp_string)
148+
149+
DateTime.to_unix(date_time, :millisecond) * 10000
150+
end
151+
152+
defp gen_tracking_id() do
153+
message_id = :erlang.unique_integer([:monotonic]) |> Integer.to_string()
154+
delivery_tag = {:injected_msg, make_ref()}
155+
{message_id, delivery_tag}
156+
end
157+
end

0 commit comments

Comments
 (0)