Skip to content

Commit 3e09933

Browse files
committed
feat: add device group subscription
- add GraphQL subscription for device group creation and updates - update tests to verify subscription functionality Signed-off-by: Osman Hadzic <osman.hadzic@secomind.com>
1 parent 4ccb2cd commit 3e09933

3 files changed

Lines changed: 128 additions & 0 deletions

File tree

backend/lib/edgehog/groups/device_group/device_group.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ defmodule Edgehog.Groups.DeviceGroup do
3333
type :device_group
3434

3535
# TODO: paginate `device` relationship with relay
36+
37+
subscriptions do
38+
pubsub EdgehogWeb.Endpoint
39+
40+
subscribe :device_group_event do
41+
action_types [:create, :update, :destroy]
42+
end
43+
end
3644
end
3745

3846
actions do
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#
2+
# This file is part of Edgehog.
3+
#
4+
# Copyright 2026 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+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule EdgehogWeb.Schema.Subscriptions.DeviceGroup.DeviceGroupSubscriptionsTest do
22+
@moduledoc false
23+
use EdgehogWeb.SubsCase
24+
25+
import Edgehog.GroupsFixtures
26+
27+
describe "DeviceGroup subscription" do
28+
test "receive data on device group creation", %{socket: socket, tenant: tenant} do
29+
subscribe(socket)
30+
31+
device_group = device_group_fixture(tenant: tenant)
32+
33+
assert_push "subscription:data", push
34+
35+
assert_device_group_created(device_group_data, push)
36+
37+
assert device_group_data["id"] == AshGraphql.Resource.encode_relay_id(device_group)
38+
assert device_group_data["name"] == device_group.name
39+
assert device_group_data["handle"] == device_group.handle
40+
assert device_group_data["selector"] == device_group.selector
41+
end
42+
43+
test "receive data on device group update", %{socket: socket, tenant: tenant} do
44+
device_group = device_group_fixture(tenant: tenant)
45+
subscribe(socket)
46+
47+
new_name = "new_name_#{System.unique_integer()}"
48+
49+
device_group
50+
|> Ash.Changeset.for_update(:update, %{name: new_name})
51+
|> Ash.update!(tenant: tenant)
52+
53+
assert_push "subscription:data", push
54+
assert_device_group_updated(device_group_data, push)
55+
56+
assert device_group_data["id"] == AshGraphql.Resource.encode_relay_id(device_group)
57+
assert device_group_data["name"] == new_name
58+
assert device_group_data["handle"] == device_group.handle
59+
assert device_group_data["selector"] == device_group.selector
60+
end
61+
end
62+
63+
defp subscribe(socket, opts \\ []) do
64+
default_sub_gql = """
65+
subscription {
66+
deviceGroupChanged {
67+
created {
68+
id
69+
name
70+
handle
71+
selector
72+
}
73+
updated {
74+
id
75+
name
76+
handle
77+
selector
78+
}
79+
}
80+
}
81+
"""
82+
83+
sub_gql = Keyword.get(opts, :query, default_sub_gql)
84+
85+
ref = push_doc(socket, sub_gql)
86+
assert_reply ref, :ok, %{subscriptionId: subscription_id}
87+
88+
subscription_id
89+
end
90+
end

backend/test/support/assertions.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,34 @@ defmodule Edgehog.Assertions do
5353
} = var!(unquote(push))
5454
end
5555
end
56+
57+
defmacro assert_device_group_created(created_data, push) do
58+
quote do
59+
assert %{
60+
result: %{
61+
data: %{
62+
"deviceGroupChanged" => %{
63+
"created" => var!(unquote(created_data)),
64+
"updated" => nil
65+
}
66+
}
67+
}
68+
} = var!(unquote(push))
69+
end
70+
end
71+
72+
defmacro assert_device_group_updated(updated_data, push) do
73+
quote do
74+
assert %{
75+
result: %{
76+
data: %{
77+
"deviceGroupChanged" => %{
78+
"created" => nil,
79+
"updated" => var!(unquote(updated_data))
80+
}
81+
}
82+
}
83+
} = var!(unquote(push))
84+
end
85+
end
5686
end

0 commit comments

Comments
 (0)