Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion backend/lib/edgehog/labeling/tag.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2022 - 2025 SECO Mind Srl
# Copyright 2022 - 2026 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,6 +37,14 @@ defmodule Edgehog.Labeling.Tag do
graphql do
type :tag

subscriptions do
pubsub EdgehogWeb.Endpoint

subscribe :tag do
action_types [:create]
end
end

paginate_relationship_with device_tags: :relay
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#
# This file is part of Edgehog.
#
# Copyright 2026 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule EdgehogWeb.Schema.Subscriptions.Labeling.TagsSubscriptionsTest do
@moduledoc false
use EdgehogWeb.SubsCase

import Edgehog.LabelingFixtures

describe "Tag subscription" do
test "receive data on tag creation", %{socket: socket, tenant: tenant} do
subscribe(socket)

tag = tag_fixture(tenant: tenant)

assert_push "subscription:data", push

assert_created "tag", tag_data, push

assert tag_data["id"] == AshGraphql.Resource.encode_relay_id(tag)
assert tag_data["name"] == tag.name
end

defp subscribe(socket, opts \\ []) do
default_sub_gql = """
subscription Tag {
tag {
created {
id
name
}
}
}
"""

sub_gql = Keyword.get(opts, :query, default_sub_gql)

ref = push_doc(socket, sub_gql)
assert_reply ref, :ok, %{subscriptionId: subscription_id}

subscription_id
end
end
end
51 changes: 51 additions & 0 deletions backend/test/support/fixtures/labeling_fixtures.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# This file is part of Edgehog.
#
# Copyright 2026 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.LabelingFixtures do
@moduledoc """
This module defines test helpers for creating
entities via the `Edgehog.Labeling` context.
"""

@doc """
Generate a unique tag name.
"""
def unique_tag_name, do: "tag_#{System.unique_integer([:positive])}"

@doc """
Generate a tag.

## Options

* `:tenant` - Required. The tenant for the tag.
"""
def tag_fixture(opts \\ []) do
{tenant, opts} = Keyword.pop!(opts, :tenant)

params =
Enum.into(opts, %{
name: unique_tag_name()
})

Edgehog.Labeling.Tag
|> Ash.Changeset.for_create(:create, params, tenant: tenant)
|> Ash.create!()
end
end
Loading