Skip to content

Commit 7c5fd6f

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

File tree

2 files changed

+159
-38
lines changed

2 files changed

+159
-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: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
# helper_name = realm_string
49+
50+
consumer_name = String.to_atom("consumer_#{realm_string}")
51+
# realm = String.to_atom(realm_string)
52+
53+
{:ok, _pid} = AMQPTestHelper.start_link(name: helper_name, realm: realm_string)
54+
55+
{:ok, _consumer_pid} =
56+
AMQPTestHelper.start_events_consumer(
57+
name: consumer_name,
58+
realm: realm_string,
59+
helper_name: helper_name
60+
)
61+
62+
{:ok, %{realm: realm_string, helper_name: helper_name}}
63+
end
64+
65+
test "Unset values from interface properties", %{
66+
realm: realm,
67+
helper_name: helper_name
68+
} do
69+
AMQPTestHelper.clean_queue(helper_name)
70+
71+
encoded_device_id = "f0VMRgIBAQAAAAAAAAAAAA"
72+
keyspace_name = Realm.keyspace_name(realm)
73+
{:ok, device_id} = Device.decode_device_id(encoded_device_id)
74+
volatile_changed_trigger_id = :crypto.strong_rand_bytes(16)
75+
76+
received_msgs = 45000
77+
received_bytes = 4_500_000
78+
existing_introspection_map = %{"com.test.LCDMonitor" => 1, "com.test.SimpleStreamTest" => 1}
79+
80+
insert_opts = [
81+
introspection: existing_introspection_map,
82+
total_received_msgs: received_msgs,
83+
total_received_bytes: received_bytes,
84+
groups: ["group1"]
85+
]
86+
87+
DatabaseTestHelper.insert_device(realm, device_id, insert_opts)
88+
89+
assert DataUpdater.handle_delete_volatile_trigger(
90+
realm,
91+
encoded_device_id,
92+
volatile_changed_trigger_id
93+
) == :ok
94+
95+
DataUpdater.handle_data(
96+
realm,
97+
encoded_device_id,
98+
"com.test.LCDMonitor",
99+
"/weekSchedule/10/start",
100+
<<>>,
101+
gen_tracking_id(),
102+
make_timestamp("2017-10-09T15:10:32+00:00")
103+
)
104+
105+
DataUpdater.dump_state(realm, encoded_device_id)
106+
107+
endpoint_id =
108+
retrieve_endpoint_id(realm, "com.test.LCDMonitor", 1, "/weekSchedule/10/start")
109+
110+
value_query =
111+
from ip in IndividualProperty,
112+
prefix: ^keyspace_name,
113+
where:
114+
ip.device_id == ^device_id and
115+
ip.interface_id == ^CQLUtils.interface_id("com.test.LCDMonitor", 1) and
116+
ip.endpoint_id == ^endpoint_id and
117+
ip.path == "/weekSchedule/10/start",
118+
select: ip.longinteger_value
119+
120+
value = Repo.one(value_query)
121+
122+
assert value == nil
123+
end
124+
125+
defp retrieve_endpoint_id(realm_name, interface_name, interface_major, path) do
126+
keyspace_name = Realm.keyspace_name(realm_name)
127+
128+
query =
129+
from i in Interface,
130+
prefix: ^keyspace_name,
131+
where: i.name == ^interface_name and i.major_version == ^interface_major,
132+
select: %{
133+
automaton_transitions: i.automaton_transitions,
134+
automaton_accepting_states: i.automaton_accepting_states
135+
}
136+
137+
interface_row = Repo.one!(query)
138+
139+
automaton =
140+
{:erlang.binary_to_term(interface_row[:automaton_transitions]),
141+
:erlang.binary_to_term(interface_row[:automaton_accepting_states])}
142+
143+
{:ok, endpoint_id} = Astarte.Core.Mapping.EndpointsAutomaton.resolve_path(path, automaton)
144+
145+
endpoint_id
146+
end
147+
148+
defp make_timestamp(timestamp_string) do
149+
{:ok, date_time, _} = DateTime.from_iso8601(timestamp_string)
150+
151+
DateTime.to_unix(date_time, :millisecond) * 10000
152+
end
153+
154+
defp gen_tracking_id() do
155+
message_id = :erlang.unique_integer([:monotonic]) |> Integer.to_string()
156+
delivery_tag = {:injected_msg, make_ref()}
157+
{message_id, delivery_tag}
158+
end
159+
end

0 commit comments

Comments
 (0)