Skip to content

Commit

Permalink
test: use a fragment in the test (#1650)
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabasJ authored Dec 10, 2024
1 parent c1f43c5 commit e2d168e
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions test/manage_relationship_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ 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]
argument :related_resource, :map, allow_nil?: false

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
Expand All @@ -28,17 +37,27 @@ 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: :*]
end

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
Expand All @@ -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

0 comments on commit e2d168e

Please sign in to comment.