Skip to content

Commit f653c60

Browse files
committed
tests for patch relationship actions with composite pkey
1 parent c0258eb commit f653c60

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

test/acceptance/patch_test.exs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,29 @@ defmodule Test.Acceptance.PatchTest do
2424

2525
patch :delete_posts, route: "/:id/posts/delete"
2626
patch :delete_bios, route: "/:id/bios/delete"
27+
28+
patch :update do
29+
relationship_arguments([:bios])
30+
end
31+
2732
related :posts, :read
2833
related :bios, :read
34+
patch_relationship :bios
2935
end
3036
end
3137

3238
actions do
3339
default_accept(:*)
34-
defaults([:create, :read, :update, :destroy])
40+
defaults([:create, :read, :destroy])
41+
42+
update :update do
43+
primary? true
44+
require_atomic?(false)
45+
46+
argument(:bios, {:array, :map})
47+
48+
change manage_relationship(:bios, type: :append_and_remove)
49+
end
3550

3651
update :delete_posts do
3752
accept([])
@@ -201,7 +216,7 @@ defmodule Test.Acceptance.PatchTest do
201216
type("bio")
202217

203218
primary_key do
204-
keys [:pkey_a, :pkey_b]
219+
keys [:author_id, :pkey_b]
205220
end
206221
end
207222

@@ -210,13 +225,12 @@ defmodule Test.Acceptance.PatchTest do
210225
end
211226

212227
attributes do
213-
attribute(:pkey_a, :string, primary_key?: true, allow_nil?: false, public?: true)
214228
attribute(:pkey_b, :string, primary_key?: true, allow_nil?: false, public?: true)
215229
attribute(:bio, :string, public?: true)
216230
end
217231

218232
relationships do
219-
belongs_to(:author, Author)
233+
belongs_to(:author, Author, primary_key?: true, public?: true, allow_nil?: false)
220234
end
221235
end
222236

@@ -497,7 +511,7 @@ defmodule Test.Acceptance.PatchTest do
497511
bios =
498512
Enum.map(1..2, fn i ->
499513
Bio
500-
|> Ash.Changeset.for_create(:create, %{pkey_a: "a#{i}", pkey_b: "b#{i}"})
514+
|> Ash.Changeset.for_create(:create, %{author_id: author.id, pkey_b: "b#{i}"})
501515
|> Ash.Changeset.force_change_attribute(:author_id, author.id)
502516
|> Ash.create!()
503517
end)
@@ -513,7 +527,7 @@ defmodule Test.Acceptance.PatchTest do
513527
Domain
514528
|> patch("/authors/#{author.id}/bios/delete", %{
515529
data: %{
516-
attributes: %{bios: Enum.map(bios, &%{pkey_a: &1.pkey_a, pkey_b: &1.pkey_b})}
530+
attributes: %{bios: Enum.map(bios, &%{author_id: author.id, pkey_b: &1.pkey_b})}
517531
}
518532
})
519533

@@ -526,4 +540,36 @@ defmodule Test.Acceptance.PatchTest do
526540
# refute related
527541
end
528542
end
543+
544+
describe "patch updating bios" do
545+
setup do
546+
author =
547+
Author
548+
|> Ash.Changeset.for_create(:create, %{id: Ecto.UUID.generate(), name: "John"})
549+
|> Ash.create!()
550+
551+
bios =
552+
Enum.map(1..2, fn i ->
553+
Bio
554+
|> Ash.Changeset.for_create(:create, %{author_id: author.id, pkey_b: "b#{i}"})
555+
|> Ash.Changeset.force_change_attribute(:author_id, author.id)
556+
|> Ash.create!()
557+
end)
558+
559+
%{bios: bios, author: author}
560+
end
561+
562+
test "patch to update relationship with composite primary key", %{
563+
author: author,
564+
bios: bios
565+
} do
566+
assert %{status: 200} =
567+
Domain
568+
|> patch("/authors/#{author.id}/relationships/bios", %{
569+
data: [
570+
%{type: "bio", author_id: author.id, pkey_b: "b1", bio: "new bio"}
571+
]
572+
})
573+
end
574+
end
529575
end

0 commit comments

Comments
 (0)