Skip to content

Commit

Permalink
test: add tests for composite primary key patch removal
Browse files Browse the repository at this point in the history
chore: small improvements to error messages
  • Loading branch information
zachdaniel committed Jul 13, 2024
1 parent a736c76 commit 99711a2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/ash_json_api/resource/transformers/require_primary_key.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule AshJsonApi.Resource.Transformers.RequirePrimaryKey do
raise Spark.Error.DslError,
module: __MODULE__,
path: [:json_api, :primary_key],
message: "AshJsonApi requires primary key when a resource has a composite key"
message: "AshJsonApi requires primary key keys when a resource has a composite key"
end

keys ->
Expand All @@ -31,8 +31,8 @@ defmodule AshJsonApi.Resource.Transformers.RequirePrimaryKey do
false ->
raise Spark.Error.DslError,
module: __MODULE__,
path: [:json_api, :primary_key],
message: "AshJsonApi primary key must be from the resource's attributes"
path: [:json_api, :primary_key, :keys],
message: "AshJsonApi primary key keys must be from the resource's attributes"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/json_primary_key_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ defmodule Test.Acceptance.ResourceTest do
describe "module has composite key but does not define json primary key" do
test "raises requires json primary key error" do
assert_raise Spark.Error.DslError,
~r/AshJsonApi requires primary key when a resource has a composite key/,
~r/AshJsonApi requires primary key keys when a resource has a composite key/,
fn ->
defmodule BadResource do
use Ash.Resource,
Expand Down Expand Up @@ -259,7 +259,7 @@ defmodule Test.Acceptance.ResourceTest do
describe "json primary contains keys which are not the resource's attribute" do
test "raises invalid primary keys error" do
assert_raise Spark.Error.DslError,
~r/AshJsonApi primary key must be from the resource's attributes/,
~r/AshJsonApi primary key keys must be from the resource's attributes/,
fn ->
defmodule BadResource do
use Ash.Resource,
Expand Down
101 changes: 98 additions & 3 deletions test/acceptance/patch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ defmodule Test.Acceptance.PatchTest do
index :read

patch :delete_posts, route: "/:id/posts/delete"
patch :delete_bios, route: "/:id/bios/delete"
related :posts, :read
related :bios, :read
end
end

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

change(manage_relationship(:post_ids, :posts, type: :remove))
end

update :delete_bios do
accept([])
require_atomic?(false)

argument :bios, {:array, :map} do
allow_nil?(false)
end

change(manage_relationship(:bios, type: :remove))
end
end

attributes do
Expand All @@ -52,6 +66,11 @@ defmodule Test.Acceptance.PatchTest do
destination_attribute: :author_id,
public?: true
)

has_many(:bios, Test.Acceptance.PatchTest.Bio,
destination_attribute: :author_id,
public?: true
)
end
end

Expand Down Expand Up @@ -166,6 +185,41 @@ defmodule Test.Acceptance.PatchTest do
end
end

defmodule Bio do
use Ash.Resource,
domain: Test.Acceptance.PatchTest.Domain,
data_layer: Ash.DataLayer.Ets,
extensions: [
AshJsonApi.Resource
]

ets do
private?(true)
end

json_api do
type("bio")

primary_key do
keys [:pkey_a, :pkey_b]
end
end

actions do
defaults([:read, :destroy, create: :*, update: :*])
end

attributes do
attribute(:pkey_a, :string, primary_key?: true, allow_nil?: false, public?: true)
attribute(:pkey_b, :string, primary_key?: true, allow_nil?: false, public?: true)
attribute(:bio, :string, public?: true)
end

relationships do
belongs_to(:author, Author)
end
end

defmodule Domain do
use Ash.Domain,
extensions: [
Expand All @@ -180,6 +234,7 @@ defmodule Test.Acceptance.PatchTest do
resources do
resource(Author)
resource(Post)
resource(Bio)
end
end

Expand Down Expand Up @@ -394,7 +449,7 @@ defmodule Test.Acceptance.PatchTest do
end
end

describe "patch_removing_posts" do
describe "patch removing posts" do
setup do
id = Ecto.UUID.generate()

Expand Down Expand Up @@ -424,11 +479,51 @@ defmodule Test.Acceptance.PatchTest do

related =
Domain
|> get("/authors/#{author.id}/posts")
|> get("/authors/#{author.id}/posts", status: 200)
|> Map.get(:resp_body)
|> Map.get("data")

refute related
assert related == []
end
end

describe "patch removing bios" do
setup do
author =
Author
|> Ash.Changeset.for_create(:create, %{id: Ecto.UUID.generate(), name: "John"})
|> Ash.create!()

bios =
Enum.map(1..2, fn i ->
Bio
|> Ash.Changeset.for_create(:create, %{pkey_a: "a#{i}", pkey_b: "b#{i}"})
|> Ash.Changeset.force_change_attribute(:author_id, author.id)
|> Ash.create!()
end)

%{bios: bios, author: author}
end

test "patch to remove relationship with composite primary key", %{
author: author,
bios: bios
} do
assert %{status: 200} =
Domain
|> patch("/authors/#{author.id}/bios/delete", %{
data: %{
attributes: %{bios: Enum.map(bios, &%{pkey_a: &1.pkey_a, pkey_b: &1.pkey_b})}
}
})

# related =
# Domain
# |> get("/authors/#{author.id}/bios", status: 200)
# |> Map.get(:resp_body)
# |> Map.get("data")

# refute related
end
end
end

0 comments on commit 99711a2

Please sign in to comment.