Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!(pagination): paginate system_models #785

Merged
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
7 changes: 3 additions & 4 deletions backend/lib/edgehog/devices/devices.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 2025 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 All @@ -16,7 +15,6 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Devices do
@moduledoc """
Expand Down Expand Up @@ -60,7 +58,8 @@ defmodule Edgehog.Devices do

list SystemModel, :system_models, :read do
description "Returns a list of system models."
paginate_with nil
relay? true
paginate_with :keyset
end
end

Expand Down
6 changes: 3 additions & 3 deletions backend/lib/edgehog/devices/system_model/system_model.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 2025 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 All @@ -16,7 +15,6 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Devices.SystemModel do
@moduledoc false
Expand All @@ -43,6 +41,8 @@ defmodule Edgehog.Devices.SystemModel do

graphql do
type :system_model

paginate_relationship_with part_numbers: :relay
end

actions do
Expand Down
6 changes: 3 additions & 3 deletions backend/lib/edgehog/devices/system_model_part_number.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 2025 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 All @@ -16,7 +15,6 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Edgehog.Devices.SystemModelPartNumber do
@moduledoc false
Expand All @@ -28,6 +26,8 @@ defmodule Edgehog.Devices.SystemModelPartNumber do

graphql do
type :system_model_part_number

paginate_relationship_with devices: :relay
end

actions do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 2025 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 All @@ -16,7 +15,6 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule EdgehogWeb.Schema.Mutation.CreateSystemModelTest do
use EdgehogWeb.GraphqlCase, async: true
Expand Down Expand Up @@ -49,15 +47,15 @@ defmodule EdgehogWeb.Schema.Mutation.CreateSystemModelTest do
"name" => "Foobar",
"handle" => "foobar",
"pictureUrl" => nil,
"partNumbers" => part_numbers,
"partNumbers" => %{"edges" => part_numbers},
"hardwareType" => %{
"id" => ^hardware_type_id
}
} = system_model

assert length(part_numbers) == 2
assert %{"partNumber" => "123"} in part_numbers
assert %{"partNumber" => "456"} in part_numbers
assert %{"node" => %{"partNumber" => "123"}} in part_numbers
assert %{"node" => %{"partNumber" => "456"}} in part_numbers
end

test "allows passing localized descriptions", %{tenant: tenant} do
Expand Down Expand Up @@ -233,7 +231,11 @@ defmodule EdgehogWeb.Schema.Mutation.CreateSystemModelTest do
handle
pictureUrl
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
hardwareType {
id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 2025 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 All @@ -16,7 +15,6 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do
use EdgehogWeb.GraphqlCase, async: true
Expand Down Expand Up @@ -57,9 +55,13 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do
"id" => _id,
"name" => "Updated Name",
"handle" => "updatedhandle",
"partNumbers" => [
%{"partNumber" => "updated-1234"}
]
"partNumbers" => %{
"edges" => [
%{
"node" => %{"partNumber" => "updated-1234"}
}
]
}
} = system_model
end

Expand All @@ -78,13 +80,15 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do
assert %{
"name" => "Only Name Update",
"handle" => ^old_handle,
"partNumbers" => part_numbers
"partNumbers" => %{
"edges" => part_numbers
}
} = system_model

assert length(part_numbers) == length(old_part_numbers)

Enum.each(old_part_numbers, fn pn ->
assert %{"partNumber" => pn} in part_numbers
assert %{"node" => %{"partNumber" => pn}} in part_numbers
end)
end

Expand All @@ -100,10 +104,10 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do

system_model = extract_result!(result)

assert %{"partNumbers" => part_numbers} = system_model
assert %{"partNumbers" => %{"edges" => part_numbers}} = system_model
assert length(part_numbers) == 2
assert %{"partNumber" => "B"} in part_numbers
assert %{"partNumber" => "D"} in part_numbers
assert %{"node" => %{"partNumber" => "B"}} in part_numbers
assert %{"node" => %{"partNumber" => "D"}} in part_numbers
end

test "allows updating localized descriptions", %{tenant: tenant} do
Expand Down Expand Up @@ -387,7 +391,11 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateSystemModelTest do
handle
pictureUrl
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
Expand Down
15 changes: 10 additions & 5 deletions backend/test/edgehog_web/schema/query/device_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 2025 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 All @@ -16,7 +15,6 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule EdgehogWeb.Schema.Query.DeviceTest do
use EdgehogWeb.GraphqlCase, async: true
Expand Down Expand Up @@ -58,7 +56,11 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
systemModel {
id
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
Expand All @@ -71,7 +73,10 @@ defmodule EdgehogWeb.Schema.Query.DeviceTest do
|> extract_result!()

assert device["systemModel"]["id"] == system_model_id
assert device["systemModel"]["partNumbers"] == [%{"partNumber" => part_number}]

assert device["systemModel"]["partNumbers"]["edges"] == [
%{"node" => %{"partNumber" => part_number}}
]
end

test "queries associated OTA operations", %{tenant: tenant} do
Expand Down
29 changes: 18 additions & 11 deletions backend/test/edgehog_web/schema/query/devices_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 2025 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 All @@ -16,7 +15,6 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule EdgehogWeb.Schema.Query.DevicesTest do
use EdgehogWeb.GraphqlCase, async: true
Expand Down Expand Up @@ -57,27 +55,36 @@ defmodule EdgehogWeb.Schema.Query.DevicesTest do
systemModel {
id
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
}
}
}
"""

devices =
system_models =
[document: document, tenant: tenant]
|> devices_query()
|> extract_result!()
|> Enum.flat_map(fn device ->
system_model = get_in(device, ["systemModel", "partNumbers", "edges"])

assert Enum.count(devices, fn device ->
%{"partNumber" => part_number_1} in (device["systemModel"]["partNumbers"] || [])
if system_model,
do: system_model,
else: []
end)

assert Enum.count(system_models, fn element ->
element == %{"node" => %{"partNumber" => part_number_1}}
end) == 1

assert Enum.count(devices, fn device ->
%{"partNumber" => part_number_2} in (device["systemModel"]["partNumbers"] || [])
assert Enum.count(system_models, fn element ->
element == %{"node" => %{"partNumber" => part_number_2}}
end) == 2

assert Enum.count(devices, fn device -> device["systemModel"] == nil end) == 1
end

test "allows filtering", %{tenant: tenant} do
Expand Down
14 changes: 8 additions & 6 deletions backend/test/edgehog_web/schema/query/system_model_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2024 SECO Mind Srl
# Copyright 2021 - 2025 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 All @@ -16,7 +15,6 @@
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule EdgehogWeb.Schema.Query.SystemModelTest do
use EdgehogWeb.GraphqlCase, async: true
Expand Down Expand Up @@ -46,10 +44,10 @@ defmodule EdgehogWeb.Schema.Query.SystemModelTest do
assert system_model["name"] == fixture.name
assert system_model["handle"] == fixture.handle
assert system_model["pictureUrl"] == fixture.picture_url
assert length(system_model["partNumbers"]) == length(fixture.part_number_strings)
assert length(system_model["partNumbers"]["edges"]) == length(fixture.part_number_strings)

Enum.each(fixture.part_number_strings, fn pn ->
assert(%{"partNumber" => pn} in system_model["partNumbers"])
assert(%{"node" => %{"partNumber" => pn}} in system_model["partNumbers"]["edges"])
end)

assert system_model["hardwareType"]["id"] ==
Expand Down Expand Up @@ -164,7 +162,11 @@ defmodule EdgehogWeb.Schema.Query.SystemModelTest do
handle
pictureUrl
partNumbers {
partNumber
edges {
node {
partNumber
}
}
}
hardwareType {
id
Expand Down
Loading
Loading