-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration_test.exs
191 lines (161 loc) · 4.41 KB
/
integration_test.exs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
defmodule EctographTest.Schema do
use ExUnit.Case
doctest Ectograph.Type
alias EctographTest.Schemas.{Author, Quote}
alias Ectograph.{Definitions}
setup_all do
create_quote_args = ~w(quote revision rating related_authors)a
create_author_args = ~w(name info)a
extra_arg = %{ extra_arg: %{ type: %GraphQL.Type.String{} }}
extra_fld = %{ extra_fld: %{ type: %GraphQL.Type.String{} }}
add_author = fn(d) ->
Definitions.add_association(d, Author, :author)
end
schema = %GraphQL.Schema{
query: %GraphQL.Type.ObjectType{
name: "Queries",
description: "GraphQL Queries",
fields: %{
quotes: Definitions.build(Quote, :all, ~w()a ) |> add_author.(),
quote: Definitions.build(Quote, :get, ~w(id)a ) |> add_author.(),
},
},
mutation: %GraphQL.Type.ObjectType{
name: "Mutations",
description: "GraphQL Mutations",
fields: %{
createQuote: (
Definitions.build(Quote, :create, create_quote_args)
|> Definitions.extend_arguments(extra_arg)
|> Definitions.extend_type_fields(extra_fld)
),
createAuthor: (
Definitions.build(Author, :create, create_author_args)
)
},
},
}
# context
{ :ok, %{
schema: schema,
}}
end
test "should be able to get quotes", context do
query = ~S[
query _ {
quotes {
id, revision, rating, quote,
related_authors, inserted_at, updated_at
}
}]
# execute
{ state, result } = GraphQL.execute(context.schema, query)
quotes = result.data["quotes"]
first_quote = List.first(quotes)
# assertions
assert state == :ok
assert is_binary(first_quote["inserted_at"])
assert first_quote["id"] == "1"
assert first_quote["revision"] == 1
assert first_quote["rating"] == 0.75
assert first_quote["quote"] == List.first(Quote.test_data).quote
assert List.first(first_quote["related_authors"]) == 20
end
test "should be able to get a quote", context do
query = ~S[
query _ ($id: Int) {
quote (id: $id) {
id, revision, rating, quote,
related_authors, inserted_at, updated_at
}
}]
# execute
{ state, result } = GraphQL.execute(
context.schema,
query,
%{ id: 2 }
)
q = result.data["quote"]
# assertions
assert state == :ok
assert is_binary(q["inserted_at"])
assert q["id"] == "2"
assert q["revision"] == 2
assert q["rating"] == 0.25
assert q["quote"] == "One must steer, not talk."
assert List.first(q["related_authors"]) == 30
end
test "should be able to create a quote", context do
query = ~S[
mutation _ (
$revision: Int,
$rating: Int,
$quote: String,
$related_authors: Array,
$extra_arg: String
) {
createQuote (
revision: $revision,
rating: $rating,
quote: $quote,
related_authors: $related_authors,
extra_arg: $extra_arg
) {
id, revision, rating, quote,
related_authors, inserted_at, updated_at,
extra_fld
}
}]
# execute
{ state, result } = GraphQL.execute(
context.schema,
query,
%{
revision: 10,
rating: 1,
quote: "Integration",
related_authors: [20],
extra_arg: "HI FROM TEST",
}
)
q = result.data["createQuote"]
# assertions
assert state == :ok
assert is_binary(q["inserted_at"])
assert q["id"] == "30"
assert q["revision"] == 10
assert q["rating"] == 1
assert q["quote"] == "Integration"
assert q["extra_fld"] == "HI FROM TEST"
assert List.first(q["related_authors"]) == 20
end
test "should be able to create an author", context do
query = ~S[
mutation _ (
$name: String,
$info: Map
) {
createAuthor (
name: $name,
info: $info
) {
id, name, info
}
}]
# execute
{ state, result } = GraphQL.execute(
context.schema,
query,
%{
name: "Test Author",
info: %{ info: "test" }
}
)
q = result.data["createAuthor"]
# assertions
assert state == :ok
assert q["id"] == "30"
assert q["name"] == "Test Author"
assert q["info"][:info] == "test"
end
end