Skip to content

Commit

Permalink
Add device tag handling in the GraphQL schema
Browse files Browse the repository at this point in the history
Show tags for the device object and allow updating them with the update_device
mutation. Add all the relevant tests.

Signed-off-by: Riccardo Binetti <riccardo.binetti@secomind.com>
  • Loading branch information
rbino committed May 26, 2022
1 parent ef461b4 commit 3245224
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
6 changes: 6 additions & 0 deletions backend/lib/edgehog_web/resolvers/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#

defmodule EdgehogWeb.Resolvers.Devices do
alias Edgehog.Astarte
alias Edgehog.Devices
alias Edgehog.Devices.HardwareType
alias Edgehog.Devices.SystemModel
Expand Down Expand Up @@ -168,4 +169,9 @@ defmodule EdgehogWeb.Resolvers.Devices do
_ -> {:ok, nil}
end
end

def extract_device_tags(%Astarte.Device{tags: tags}, _args, _context) do
tag_names = for t <- tags, do: t.name
{:ok, tag_names}
end
end
15 changes: 15 additions & 0 deletions backend/lib/edgehog_web/schema/astarte_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ defmodule EdgehogWeb.Schema.AstarteTypes do
the name of the device's hardware type.
"""
field :hardware_type_name, :string

@desc """
A string to match against the tags of the device.
The match is case-insensitive and tests whether the string is included in \
one of the tags of the device.
"""
field :tag, :string
end

@desc """
Expand Down Expand Up @@ -406,6 +413,11 @@ defmodule EdgehogWeb.Schema.AstarteTypes do
@desc "The system model of the device."
field :system_model, :system_model

@desc "The tags of the device"
field :tags, non_null(list_of(non_null(:string))) do
resolve &Resolvers.Devices.extract_device_tags/3
end

@desc "List of capabilities supported by the device."
field :capabilities, non_null(list_of(non_null(:device_capability))) do
resolve &Resolvers.Astarte.list_device_capabilities/3
Expand Down Expand Up @@ -535,6 +547,9 @@ defmodule EdgehogWeb.Schema.AstarteTypes do

@desc "The display name of the device."
field :name, :string

@desc "The tags of the device. These replace all the current tags."
field :tags, list_of(non_null(:string))
end

output do
Expand Down
36 changes: 34 additions & 2 deletions backend/test/edgehog_web/schema/mutation/update_device_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateDeviceTest do
use Edgehog.AstarteMockCase

import Edgehog.AstarteFixtures
alias Edgehog.Astarte

describe "updateDevice field" do
setup do
Expand All @@ -40,6 +41,7 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateDeviceTest do
device {
id
name
tags
}
}
}
Expand All @@ -52,7 +54,8 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateDeviceTest do
variables = %{
input: %{
device_id: Absinthe.Relay.Node.to_global_id(:device, device.id, EdgehogWeb.Schema),
name: "Some new name"
name: "Some new name",
tags: ["foo", "bar", "baz"]
}
}

Expand All @@ -62,7 +65,8 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateDeviceTest do
"data" => %{
"updateDevice" => %{
"device" => %{
"name" => "Some new name"
"name" => "Some new name",
"tags" => ["foo", "bar", "baz"]
}
}
}
Expand All @@ -82,5 +86,33 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateDeviceTest do

assert %{"errors" => _} = assert(json_response(conn, 200))
end

test "handles partial updates", %{
conn: conn,
api_path: api_path,
device: device
} do
{:ok, _} = Astarte.update_device(device, %{tags: ["not", "touched"]})

variables = %{
input: %{
device_id: Absinthe.Relay.Node.to_global_id(:device, device.id, EdgehogWeb.Schema),
name: "Some new name"
}
}

conn = post(conn, api_path, query: @query, variables: variables)

assert %{
"data" => %{
"updateDevice" => %{
"device" => %{
"name" => "Some new name",
"tags" => ["not", "touched"]
}
}
}
} = json_response(conn, 200)
end
end
end
27 changes: 27 additions & 0 deletions backend/test/edgehog_web/schema/query/device_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
import Edgehog.AstarteFixtures
import Edgehog.OSManagementFixtures

alias Edgehog.Astarte
alias Edgehog.Astarte.Device

describe "device query" do
Expand Down Expand Up @@ -144,5 +145,31 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
assert decoded_id == ota_operation.id
assert operation["status"] == "PENDING"
end

@query """
query ($id: ID!) {
device(id: $id) {
tags
}
}
"""

test "returns the tags", %{conn: conn, api_path: api_path, realm: realm} do
{:ok, %Device{id: id}} =
device_fixture(realm)
|> Astarte.update_device(%{tags: ["foo", "bar"]})

variables = %{id: Absinthe.Relay.Node.to_global_id(:device, id, EdgehogWeb.Schema)}

conn = get(conn, api_path, query: @query, variables: variables)

assert %{
"data" => %{
"device" => device
}
} = json_response(conn, 200)

assert device["tags"] == ["foo", "bar"]
end
end
end
32 changes: 32 additions & 0 deletions backend/test/edgehog_web/schema/query/devices_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule EdgehogWeb.Schema.Query.DevicesTest do
import Edgehog.DevicesFixtures
import Edgehog.AstarteFixtures

alias Edgehog.Astarte
alias Edgehog.Astarte.Device

describe "systemModels field" do
Expand Down Expand Up @@ -108,6 +109,37 @@ defmodule EdgehogWeb.Schema.Query.DevicesTest do
assert device["online"] == online
end

test "filters devices with tag", %{
conn: conn,
api_path: api_path,
realm: realm
} do
{:ok,
%Device{
name: name,
device_id: device_id,
online: online
}} =
device_fixture(realm, device_id: "INyxlnmUT3CEJHPAwWMi0A")
|> Astarte.update_device(%{tags: ["foobar"]})

_device_2 = device_fixture(realm, device_id: "1YmkqsFfSuWDZcYV3ceoBQ")

variables = %{filter: %{tag: "foo"}}

conn = post(conn, api_path, query: @query, variables: variables)

assert %{
"data" => %{
"devices" => [device]
}
} = json_response(conn, 200)

assert device["name"] == name
assert device["deviceId"] == device_id
assert device["online"] == online
end

test "returns system model description with default locale", %{
conn: conn,
api_path: api_path,
Expand Down

0 comments on commit 3245224

Please sign in to comment.