-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathafter_transaction_mnesia_test.exs
More file actions
142 lines (124 loc) · 4.37 KB
/
after_transaction_mnesia_test.exs
File metadata and controls
142 lines (124 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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
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
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
end
end