@@ -24,14 +24,29 @@ defmodule Test.Acceptance.PatchTest do
24
24
25
25
patch :delete_posts , route: "/:id/posts/delete"
26
26
patch :delete_bios , route: "/:id/bios/delete"
27
+
28
+ patch :update do
29
+ relationship_arguments ( [ :bios ] )
30
+ end
31
+
27
32
related :posts , :read
28
33
related :bios , :read
34
+ patch_relationship :bios
29
35
end
30
36
end
31
37
32
38
actions do
33
39
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
35
50
36
51
update :delete_posts do
37
52
accept ( [ ] )
@@ -201,7 +216,7 @@ defmodule Test.Acceptance.PatchTest do
201
216
type ( "bio" )
202
217
203
218
primary_key do
204
- keys [ :pkey_a , :pkey_b ]
219
+ keys [ :author_id , :pkey_b ]
205
220
end
206
221
end
207
222
@@ -210,13 +225,12 @@ defmodule Test.Acceptance.PatchTest do
210
225
end
211
226
212
227
attributes do
213
- attribute ( :pkey_a , :string , primary_key?: true , allow_nil?: false , public?: true )
214
228
attribute ( :pkey_b , :string , primary_key?: true , allow_nil?: false , public?: true )
215
229
attribute ( :bio , :string , public?: true )
216
230
end
217
231
218
232
relationships do
219
- belongs_to ( :author , Author )
233
+ belongs_to ( :author , Author , primary_key?: true , public?: true , allow_nil?: false )
220
234
end
221
235
end
222
236
@@ -497,7 +511,7 @@ defmodule Test.Acceptance.PatchTest do
497
511
bios =
498
512
Enum . map ( 1 .. 2 , fn i ->
499
513
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 } " } )
501
515
|> Ash.Changeset . force_change_attribute ( :author_id , author . id )
502
516
|> Ash . create! ( )
503
517
end )
@@ -513,7 +527,7 @@ defmodule Test.Acceptance.PatchTest do
513
527
Domain
514
528
|> patch ( "/authors/#{ author . id } /bios/delete" , % {
515
529
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 } ) }
517
531
}
518
532
} )
519
533
@@ -526,4 +540,36 @@ defmodule Test.Acceptance.PatchTest do
526
540
# refute related
527
541
end
528
542
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
529
575
end
0 commit comments