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

add a manage_relationship test that creates resources #1650

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
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
Loading