2021-04-17 04:55:34 +12:00
|
|
|
defmodule AshGraphql.DestroyTest do
|
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
|
|
|
setup do
|
|
|
|
on_exit(fn ->
|
2022-02-01 08:55:14 +13:00
|
|
|
Application.delete_env(:ash_graphql, AshGraphql.Test.Api)
|
2021-09-12 18:13:19 +12:00
|
|
|
|
2021-04-17 04:55:34 +12:00
|
|
|
try do
|
2021-06-29 05:33:01 +12:00
|
|
|
Ash.DataLayer.Ets.stop(AshGraphql.Test.Post)
|
|
|
|
Ash.DataLayer.Ets.stop(AshGraphql.Test.Comment)
|
2021-04-17 04:55:34 +12:00
|
|
|
rescue
|
|
|
|
_ ->
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a destroy works" do
|
|
|
|
post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar"))
|
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation DeletePost($id: ID) {
|
|
|
|
deletePost(id: $id) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"id" => post.id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
assert %{data: %{"deletePost" => %{"result" => %{"text" => "foobar"}}}} = result
|
|
|
|
end
|
2021-04-17 05:49:51 +12:00
|
|
|
|
2021-05-22 02:43:54 +12:00
|
|
|
test "a soft destroy works" do
|
|
|
|
post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar"))
|
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation ArchivePost($id: ID) {
|
|
|
|
deletePost(id: $id) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"id" => post.id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
assert %{data: %{"deletePost" => %{"result" => %{"text" => "foobar"}}}} = result
|
|
|
|
end
|
|
|
|
|
2021-04-20 07:26:20 +12:00
|
|
|
test "a destroy with a configured read action and no identity works" do
|
|
|
|
AshGraphql.Test.Api.create!(
|
|
|
|
Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar", best: true)
|
|
|
|
)
|
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation DeleteBestPost {
|
|
|
|
deleteBestPost {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
|
|
|
assert %{data: %{"deleteBestPost" => %{"result" => %{"text" => "foobar"}}}} = result
|
|
|
|
end
|
|
|
|
|
2021-09-12 07:18:38 +12:00
|
|
|
test "a destroy with an error" do
|
|
|
|
post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar"))
|
|
|
|
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation DeleteWithError($id: ID) {
|
2021-09-12 18:13:19 +12:00
|
|
|
deletePostWithError(id: $id) {
|
2021-09-12 07:18:38 +12:00
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"id" => post.id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
assert %{
|
|
|
|
data: %{
|
2021-09-12 18:13:19 +12:00
|
|
|
"deletePostWithError" => %{
|
2021-09-12 07:18:38 +12:00
|
|
|
"errors" => [%{"message" => "could not be found"}],
|
|
|
|
"result" => nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} == result
|
|
|
|
end
|
|
|
|
|
2021-04-17 05:49:51 +12:00
|
|
|
test "destroying a non-existent record returns a not found error" do
|
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation DeletePost($id: ID) {
|
|
|
|
deletePost(id: $id) {
|
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
|
|
|
"id" => Ash.UUID.generate()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
refute Map.has_key?(result, :errors)
|
2021-06-29 05:33:01 +12:00
|
|
|
|
|
|
|
assert %{data: %{"deletePost" => %{"errors" => [%{"message" => "could not be found"}]}}} =
|
|
|
|
result
|
2021-04-17 05:49:51 +12:00
|
|
|
end
|
2021-09-12 17:02:08 +12:00
|
|
|
|
|
|
|
test "root level error on destroy" do
|
2022-02-01 08:55:14 +13:00
|
|
|
Application.put_env(:ash_graphql, AshGraphql.Test.Api,
|
2021-09-12 17:02:08 +12:00
|
|
|
graphql: [show_raised_errors?: true, root_level_errors?: true]
|
|
|
|
)
|
|
|
|
|
2021-09-12 18:13:19 +12:00
|
|
|
post = AshGraphql.Test.Api.create!(Ash.Changeset.new(AshGraphql.Test.Post, text: "foobar"))
|
|
|
|
|
2021-09-12 17:02:08 +12:00
|
|
|
resp =
|
|
|
|
"""
|
|
|
|
mutation DeletePost($id: ID) {
|
2021-09-12 18:13:19 +12:00
|
|
|
deletePostWithError(id: $id) {
|
2021-09-12 17:02:08 +12:00
|
|
|
result{
|
|
|
|
text
|
|
|
|
}
|
|
|
|
errors{
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|> Absinthe.run(AshGraphql.Test.Schema,
|
|
|
|
variables: %{
|
2021-09-12 18:13:19 +12:00
|
|
|
"id" => post.id
|
2021-09-12 17:02:08 +12:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {:ok, result} = resp
|
|
|
|
|
|
|
|
assert %{errors: [%{message: "could not be found"}]} = result
|
|
|
|
end
|
2021-04-17 04:55:34 +12:00
|
|
|
end
|