From e2d168e329c3b86e5e747ea4fc340ad88ef012c1 Mon Sep 17 00:00:00 2001 From: Barnabas Jovanovics Date: Tue, 10 Dec 2024 14:00:04 +0100 Subject: [PATCH] test: use a fragment in the test (#1650) --- test/manage_relationship_test.exs | 55 ++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/test/manage_relationship_test.exs b/test/manage_relationship_test.exs index 8e41b11ef..200c015c7 100644 --- a/test/manage_relationship_test.exs +++ b/test/manage_relationship_test.exs @@ -4,10 +4,11 @@ defmodule Ash.Test.ManageRelationshipTest do defmodule ParentResource do use Ash.Resource, - domain: Ash.Test.Domain + domain: Ash.Test.Domain, + data_layer: Ash.DataLayer.Ets actions do - defaults [:read, :update, :destroy] + defaults [:read, :destroy] create :create do accept [:name] @@ -15,6 +16,14 @@ defmodule Ash.Test.ManageRelationshipTest do change manage_relationship(:related_resource, :related_resource, type: :create) end + + update :update do + require_atomic? false + accept [:name] + argument :related_resource, :map, allow_nil?: false + + change manage_relationship(:related_resource, :related_resource, type: :direct_control) + end end attributes do @@ -28,9 +37,19 @@ defmodule Ash.Test.ManageRelationshipTest do end end + defmodule RelatedResourceFragment do + use Spark.Dsl.Fragment, of: Ash.Resource + + actions do + read :another_read + end + end + defmodule RelatedResource do use Ash.Resource, - domain: Ash.Test.Domain + domain: Ash.Test.Domain, + data_layer: Ash.DataLayer.Ets, + fragments: [RelatedResourceFragment] actions do defaults [:read, :destroy, create: :*, update: :*] @@ -38,7 +57,7 @@ defmodule Ash.Test.ManageRelationshipTest do attributes do uuid_primary_key :id - attribute :required_attribute, :string, allow_nil?: false + attribute :required_attribute, :string, allow_nil?: false, public?: true end relationships do @@ -63,4 +82,32 @@ defmodule Ash.Test.ManageRelationshipTest do }) |> Ash.create() end + + test "can create and update a related resource" do + assert {:ok, parent} = + ParentResource + |> Ash.Changeset.for_create(:create, %{ + name: "Test Parent Resource", + related_resource: %{ + required_attribute: "string" + } + }) + |> Ash.create!() + |> Ash.load(:related_resource) + + assert parent.related_resource.required_attribute == "string" + + assert {:ok, parent} = + parent + |> Ash.Changeset.for_update(:update, %{ + related_resource: %{ + id: parent.related_resource.id, + required_attribute: "other_string" + } + }) + |> Ash.update!() + |> Ash.load(:related_resource) + + assert parent.related_resource.required_attribute == "other_string" + end end