From 278e5b1571d666cead03a389966e8af0422d99ef Mon Sep 17 00:00:00 2001 From: Luca Zaninotto Date: Fri, 13 Dec 2024 10:59:00 +0100 Subject: [PATCH] chore(update_channels): allow empty targets Update channels can have zero target device groups. Closes #399 and Closes #518. Signed-off-by: Luca Zaninotto --- .../update_channel/update_channel.ex | 5 -- .../mutation/create_update_channel_test.exs | 52 ++++++++++--------- .../mutation/update_update_channel_test.exs | 13 ++--- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/backend/lib/edgehog/update_campaigns/update_channel/update_channel.ex b/backend/lib/edgehog/update_campaigns/update_channel/update_channel.ex index c098fe8aa..99daa97aa 100644 --- a/backend/lib/edgehog/update_campaigns/update_channel/update_channel.ex +++ b/backend/lib/edgehog/update_campaigns/update_channel/update_channel.ex @@ -58,9 +58,6 @@ defmodule Edgehog.UpdateCampaigns.UpdateChannel do description """ The IDs of the target groups that are targeted by this update channel. """ - - allow_nil? false - constraints min_length: 1 end change Changes.RelateTargetGroups do @@ -78,8 +75,6 @@ defmodule Edgehog.UpdateCampaigns.UpdateChannel do description """ The IDs of the target groups that are targeted by this update channel. """ - - constraints min_length: 1 end # Needed because manage_relationship is not atomic diff --git a/backend/test/edgehog_web/schema/mutation/create_update_channel_test.exs b/backend/test/edgehog_web/schema/mutation/create_update_channel_test.exs index 9a2471e2b..1f949ccd0 100644 --- a/backend/test/edgehog_web/schema/mutation/create_update_channel_test.exs +++ b/backend/test/edgehog_web/schema/mutation/create_update_channel_test.exs @@ -49,6 +49,34 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateChannelTest do assert target_group_data["handle"] == target_group.handle end + test "creates update_channel with nil target_group_ids", %{tenant: tenant} do + name = unique_update_channel_name() + handle = unique_update_channel_handle() + + result = + [target_group_ids: nil, tenant: tenant, name: name, handle: handle] + |> create_update_channel_mutation() + |> extract_result!() + + assert result["name"] == name + assert result["handle"] == handle + assert result["targetGroups"] == [] + end + + test "creates update_channel with empty target_group_ids", %{tenant: tenant} do + name = unique_update_channel_name() + handle = unique_update_channel_handle() + + result = + [target_group_ids: [], tenant: tenant, name: name, handle: handle] + |> create_update_channel_mutation() + |> extract_result!() + + assert result["name"] == name + assert result["handle"] == handle + assert result["targetGroups"] == [] + end + test "fails with missing name", %{tenant: tenant} do error = [name: nil, tenant: tenant] @@ -143,30 +171,6 @@ defmodule EdgehogWeb.Schema.Mutation.CreateUpdateChannelTest do } = error end - test "fails with missing target_group_ids", %{tenant: tenant} do - error = - [target_group_ids: nil, tenant: tenant] - |> create_update_channel_mutation() - |> extract_error!() - - assert %{message: message} = error - assert message =~ ~s - end - - test "fails with empty target_group_ids", %{tenant: tenant} do - error = - [target_group_ids: [], tenant: tenant] - |> create_update_channel_mutation() - |> extract_error!() - - assert %{ - path: ["createUpdateChannel"], - fields: [:target_group_ids], - message: "must have 1 or more items", - code: "invalid_argument" - } = error - end - test "fails when trying to use a non-existing target group", %{tenant: tenant} do target_group_id = non_existing_device_group_id(tenant) diff --git a/backend/test/edgehog_web/schema/mutation/update_update_channel_test.exs b/backend/test/edgehog_web/schema/mutation/update_update_channel_test.exs index 1646254d4..522755382 100644 --- a/backend/test/edgehog_web/schema/mutation/update_update_channel_test.exs +++ b/backend/test/edgehog_web/schema/mutation/update_update_channel_test.exs @@ -130,18 +130,13 @@ defmodule EdgehogWeb.Schema.Mutation.UpdateUpdateChannelTest do } = error end - test "fails with empty target_group_ids", %{tenant: tenant, id: id} do - error = + test "updates update_channel with empty target_group_ids", %{tenant: tenant, id: id} do + result = [id: id, target_group_ids: [], tenant: tenant] |> update_update_channel_mutation() - |> extract_error!() + |> extract_result!() - assert %{ - path: ["updateUpdateChannel"], - fields: [:target_group_ids], - code: "invalid_argument", - message: "must have 1 or more items" - } = error + assert result["id"] == id end test "fails when trying to use a non-existing target group id", %{tenant: tenant, id: id} do