Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions test/after_transaction_mnesia_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# SPDX-FileCopyrightText: 2020 ash_graphql contributors <https://github.com/ash-project/ash_graphql/graphs.contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshGraphql.AfterTransactionMnesiaTest do
@moduledoc "Tests after_transaction with Mnesia (real transactions)"
use ExUnit.Case, async: false

setup do
:mnesia.start()
Ash.DataLayer.Mnesia.start(AshGraphql.Test.Domain, [AshGraphql.Test.AfterTransactionMnesia])
on_exit(fn -> :mnesia.clear_table(:after_transaction_mnesia_table) end)
end

describe "create mutation" do
test "after_transaction is called on success" do
"""
mutation CreateAfterTransactionMnesia($input: CreateAfterTransactionMnesiaInput!) {
createAfterTransactionMnesia(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "test"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :create, {:ok, _}}
end

test "after_transaction is called on error" do
"""
mutation CreateAfterTransactionMnesiaWithError($input: CreateAfterTransactionMnesiaWithErrorInput!) {
createAfterTransactionMnesiaWithError(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "test"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :create_with_error, {:error, _}}
end

test "record is not persisted when after_action fails" do
"""
mutation CreateAfterTransactionMnesiaWithError($input: CreateAfterTransactionMnesiaWithErrorInput!) {
createAfterTransactionMnesiaWithError(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "should_not_exist"}}
)

assert AshGraphql.Test.AfterTransactionMnesia |> Ash.read!() |> Enum.empty?()
end
end

describe "update mutation" do
test "after_transaction is called on success" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionMnesia($id: ID!, $input: UpdateAfterTransactionMnesiaInput!) {
updateAfterTransactionMnesia(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "updated"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :update, {:ok, _}}
end

test "after_transaction is called on error" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionMnesiaWithError($id: ID!, $input: UpdateAfterTransactionMnesiaWithErrorInput!) {
updateAfterTransactionMnesiaWithError(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "updated"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :update_with_error, {:error, _}}
end

test "record is not updated when after_action fails" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionMnesiaWithError($id: ID!, $input: UpdateAfterTransactionMnesiaWithErrorInput!) {
updateAfterTransactionMnesiaWithError(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "should_not_update"}}
)

assert Ash.get!(AshGraphql.Test.AfterTransactionMnesia, record.id).name == "original"
end
end

describe "destroy mutation" do
test "after_transaction is called on success" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "to_destroy"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionMnesia($id: ID!) {
destroyAfterTransactionMnesia(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :destroy, {:ok, _}}
end

test "after_transaction is called on error" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "to_destroy"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionMnesiaWithError($id: ID!) {
destroyAfterTransactionMnesiaWithError(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :destroy_with_error, {:error, _}}
end

test "record is not destroyed when after_action fails" do
record =
AshGraphql.Test.AfterTransactionMnesia
|> Ash.Changeset.for_create(:create, %{name: "should_still_exist"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionMnesiaWithError($id: ID!) {
destroyAfterTransactionMnesiaWithError(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id}
)

assert Ash.get!(AshGraphql.Test.AfterTransactionMnesia, record.id).name ==
"should_still_exist"
end
end
end
139 changes: 139 additions & 0 deletions test/after_transaction_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# SPDX-FileCopyrightText: 2020 ash_graphql contributors <https://github.com/ash-project/ash_graphql/graphs.contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshGraphql.AfterTransactionEts do
use ExUnit.Case, async: false

setup do
on_exit(fn -> AshGraphql.TestHelpers.stop_ets() end)
end

describe "create mutation" do
test "after_transaction is called on success" do
"""
mutation CreateAfterTransactionEts($input: CreateAfterTransactionEtsInput!) {
createAfterTransactionEts(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "test"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :create, {:ok, _}}
end

test "after_transaction is called on error" do
"""
mutation CreateAfterTransactionEtsWithError($input: CreateAfterTransactionEtsWithErrorInput!) {
createAfterTransactionEtsWithError(input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"input" => %{"name" => "test"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :create_with_error, {:error, _}}
end
end

describe "update mutation" do
test "after_transaction is called on success" do
record =
AshGraphql.Test.AfterTransactionEts
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionEts($id: ID!, $input: UpdateAfterTransactionEtsInput!) {
updateAfterTransactionEts(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "updated"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :update, {:ok, _}}
end

test "after_transaction is called on error" do
record =
AshGraphql.Test.AfterTransactionEts
|> Ash.Changeset.for_create(:create, %{name: "original"})
|> Ash.create!()

"""
mutation UpdateAfterTransactionEtsWithError($id: ID!, $input: UpdateAfterTransactionEtsWithErrorInput!) {
updateAfterTransactionEtsWithError(id: $id, input: $input) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id, "input" => %{"name" => "updated"}},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :update_with_error, {:error, _}}
end
end

describe "destroy mutation" do
test "after_transaction is called on success" do
record =
AshGraphql.Test.AfterTransactionEts
|> Ash.Changeset.for_create(:create, %{name: "to_destroy"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionEts($id: ID!) {
destroyAfterTransactionEts(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :destroy, {:ok, _}}
end

test "after_transaction is called on error" do
record =
AshGraphql.Test.AfterTransactionEts
|> Ash.Changeset.for_create(:create, %{name: "to_destroy"})
|> Ash.create!()

"""
mutation DestroyAfterTransactionEtsWithError($id: ID!) {
destroyAfterTransactionEtsWithError(id: $id) {
result { id name }
errors { message }
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema,
variables: %{"id" => record.id},
context: %{context: %{test_pid: self()}}
)

assert_receive {:after_transaction, :destroy_with_error, {:error, _}}
end
end
end
2 changes: 2 additions & 0 deletions test/support/domain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ defmodule AshGraphql.Test.Domain do
resource(AshGraphql.Test.Product)
resource(AshGraphql.Test.ResourceWithUnion)
resource(AshGraphql.Test.ResourceWithTypedStruct)
resource(AshGraphql.Test.AfterTransactionEts)
resource(AshGraphql.Test.AfterTransactionMnesia)
end
end
Loading
Loading