Skip to content

Commit 99711a2

Browse files
committed
test: add tests for composite primary key patch removal
chore: small improvements to error messages
1 parent a736c76 commit 99711a2

File tree

3 files changed

+103
-8
lines changed

3 files changed

+103
-8
lines changed

lib/ash_json_api/resource/transformers/require_primary_key.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule AshJsonApi.Resource.Transformers.RequirePrimaryKey do
1717
raise Spark.Error.DslError,
1818
module: __MODULE__,
1919
path: [:json_api, :primary_key],
20-
message: "AshJsonApi requires primary key when a resource has a composite key"
20+
message: "AshJsonApi requires primary key keys when a resource has a composite key"
2121
end
2222

2323
keys ->
@@ -31,8 +31,8 @@ defmodule AshJsonApi.Resource.Transformers.RequirePrimaryKey do
3131
false ->
3232
raise Spark.Error.DslError,
3333
module: __MODULE__,
34-
path: [:json_api, :primary_key],
35-
message: "AshJsonApi primary key must be from the resource's attributes"
34+
path: [:json_api, :primary_key, :keys],
35+
message: "AshJsonApi primary key keys must be from the resource's attributes"
3636
end
3737
end
3838
end

test/acceptance/json_primary_key_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ defmodule Test.Acceptance.ResourceTest do
217217
describe "module has composite key but does not define json primary key" do
218218
test "raises requires json primary key error" do
219219
assert_raise Spark.Error.DslError,
220-
~r/AshJsonApi requires primary key when a resource has a composite key/,
220+
~r/AshJsonApi requires primary key keys when a resource has a composite key/,
221221
fn ->
222222
defmodule BadResource do
223223
use Ash.Resource,
@@ -259,7 +259,7 @@ defmodule Test.Acceptance.ResourceTest do
259259
describe "json primary contains keys which are not the resource's attribute" do
260260
test "raises invalid primary keys error" do
261261
assert_raise Spark.Error.DslError,
262-
~r/AshJsonApi primary key must be from the resource's attributes/,
262+
~r/AshJsonApi primary key keys must be from the resource's attributes/,
263263
fn ->
264264
defmodule BadResource do
265265
use Ash.Resource,

test/acceptance/patch_test.exs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ defmodule Test.Acceptance.PatchTest do
2323
index :read
2424

2525
patch :delete_posts, route: "/:id/posts/delete"
26+
patch :delete_bios, route: "/:id/bios/delete"
27+
related :posts, :read
28+
related :bios, :read
2629
end
2730
end
2831

@@ -40,6 +43,17 @@ defmodule Test.Acceptance.PatchTest do
4043

4144
change(manage_relationship(:post_ids, :posts, type: :remove))
4245
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
4357
end
4458

4559
attributes do
@@ -52,6 +66,11 @@ defmodule Test.Acceptance.PatchTest do
5266
destination_attribute: :author_id,
5367
public?: true
5468
)
69+
70+
has_many(:bios, Test.Acceptance.PatchTest.Bio,
71+
destination_attribute: :author_id,
72+
public?: true
73+
)
5574
end
5675
end
5776

@@ -166,6 +185,41 @@ defmodule Test.Acceptance.PatchTest do
166185
end
167186
end
168187

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+
169223
defmodule Domain do
170224
use Ash.Domain,
171225
extensions: [
@@ -180,6 +234,7 @@ defmodule Test.Acceptance.PatchTest do
180234
resources do
181235
resource(Author)
182236
resource(Post)
237+
resource(Bio)
183238
end
184239
end
185240

@@ -394,7 +449,7 @@ defmodule Test.Acceptance.PatchTest do
394449
end
395450
end
396451

397-
describe "patch_removing_posts" do
452+
describe "patch removing posts" do
398453
setup do
399454
id = Ecto.UUID.generate()
400455

@@ -424,11 +479,51 @@ defmodule Test.Acceptance.PatchTest do
424479

425480
related =
426481
Domain
427-
|> get("/authors/#{author.id}/posts")
482+
|> get("/authors/#{author.id}/posts", status: 200)
428483
|> Map.get(:resp_body)
429484
|> Map.get("data")
430485

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
432527
end
433528
end
434529
end

0 commit comments

Comments
 (0)