-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathafter_transaction_test.exs
More file actions
139 lines (121 loc) · 4.1 KB
/
after_transaction_test.exs
File metadata and controls
139 lines (121 loc) · 4.1 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
# 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