@@ -23,6 +23,9 @@ defmodule Test.Acceptance.PatchTest do
23
23
index :read
24
24
25
25
patch :delete_posts , route: "/:id/posts/delete"
26
+ patch :delete_bios , route: "/:id/bios/delete"
27
+ related :posts , :read
28
+ related :bios , :read
26
29
end
27
30
end
28
31
@@ -40,6 +43,17 @@ defmodule Test.Acceptance.PatchTest do
40
43
41
44
change ( manage_relationship ( :post_ids , :posts , type: :remove ) )
42
45
end
46
+
47
+ update :delete_bios do
48
+ accept ( [ ] )
49
+ require_atomic? ( false )
50
+
51
+ argument :bios , { :array , :map } do
52
+ allow_nil? ( false )
53
+ end
54
+
55
+ change ( manage_relationship ( :bios , type: :remove ) )
56
+ end
43
57
end
44
58
45
59
attributes do
@@ -52,6 +66,11 @@ defmodule Test.Acceptance.PatchTest do
52
66
destination_attribute: :author_id ,
53
67
public?: true
54
68
)
69
+
70
+ has_many ( :bios , Test.Acceptance.PatchTest.Bio ,
71
+ destination_attribute: :author_id ,
72
+ public?: true
73
+ )
55
74
end
56
75
end
57
76
@@ -166,6 +185,41 @@ defmodule Test.Acceptance.PatchTest do
166
185
end
167
186
end
168
187
188
+ defmodule Bio do
189
+ use Ash.Resource ,
190
+ domain: Test.Acceptance.PatchTest.Domain ,
191
+ data_layer: Ash.DataLayer.Ets ,
192
+ extensions: [
193
+ AshJsonApi.Resource
194
+ ]
195
+
196
+ ets do
197
+ private? ( true )
198
+ end
199
+
200
+ json_api do
201
+ type ( "bio" )
202
+
203
+ primary_key do
204
+ keys [ :pkey_a , :pkey_b ]
205
+ end
206
+ end
207
+
208
+ actions do
209
+ defaults ( [ :read , :destroy , create: :* , update: :* ] )
210
+ end
211
+
212
+ attributes do
213
+ attribute ( :pkey_a , :string , primary_key?: true , allow_nil?: false , public?: true )
214
+ attribute ( :pkey_b , :string , primary_key?: true , allow_nil?: false , public?: true )
215
+ attribute ( :bio , :string , public?: true )
216
+ end
217
+
218
+ relationships do
219
+ belongs_to ( :author , Author )
220
+ end
221
+ end
222
+
169
223
defmodule Domain do
170
224
use Ash.Domain ,
171
225
extensions: [
@@ -180,6 +234,7 @@ defmodule Test.Acceptance.PatchTest do
180
234
resources do
181
235
resource ( Author )
182
236
resource ( Post )
237
+ resource ( Bio )
183
238
end
184
239
end
185
240
@@ -394,7 +449,7 @@ defmodule Test.Acceptance.PatchTest do
394
449
end
395
450
end
396
451
397
- describe "patch_removing_posts " do
452
+ describe "patch removing posts " do
398
453
setup do
399
454
id = Ecto.UUID . generate ( )
400
455
@@ -424,11 +479,51 @@ defmodule Test.Acceptance.PatchTest do
424
479
425
480
related =
426
481
Domain
427
- |> get ( "/authors/#{ author . id } /posts" )
482
+ |> get ( "/authors/#{ author . id } /posts" , status: 200 )
428
483
|> Map . get ( :resp_body )
429
484
|> Map . get ( "data" )
430
485
431
- refute related
486
+ assert related == [ ]
487
+ end
488
+ end
489
+
490
+ describe "patch removing bios" do
491
+ setup do
492
+ author =
493
+ Author
494
+ |> Ash.Changeset . for_create ( :create , % { id: Ecto.UUID . generate ( ) , name: "John" } )
495
+ |> Ash . create! ( )
496
+
497
+ bios =
498
+ Enum . map ( 1 .. 2 , fn i ->
499
+ Bio
500
+ |> Ash.Changeset . for_create ( :create , % { pkey_a: "a#{ i } " , pkey_b: "b#{ i } " } )
501
+ |> Ash.Changeset . force_change_attribute ( :author_id , author . id )
502
+ |> Ash . create! ( )
503
+ end )
504
+
505
+ % { bios: bios , author: author }
506
+ end
507
+
508
+ test "patch to remove relationship with composite primary key" , % {
509
+ author: author ,
510
+ bios: bios
511
+ } do
512
+ assert % { status: 200 } =
513
+ Domain
514
+ |> patch ( "/authors/#{ author . id } /bios/delete" , % {
515
+ data: % {
516
+ attributes: % { bios: Enum . map ( bios , & % { pkey_a: & 1 . pkey_a , pkey_b: & 1 . pkey_b } ) }
517
+ }
518
+ } )
519
+
520
+ # related =
521
+ # Domain
522
+ # |> get("/authors/#{author.id}/bios", status: 200)
523
+ # |> Map.get(:resp_body)
524
+ # |> Map.get("data")
525
+
526
+ # refute related
432
527
end
433
528
end
434
529
end
0 commit comments