-
Notifications
You must be signed in to change notification settings - Fork 5
/
SociableWeaverBuilderTests.swift
278 lines (238 loc) · 9.59 KB
/
SociableWeaverBuilderTests.swift
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import XCTest
@testable import SociableWeaver
final class SociableWeaverBuilderTests: XCTestCase {
func testBuildField() {
let query = Weave(.query) {
Field(Comment.CodingKeys.id)
}
let expected = "query { id }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObject() {
let query = Weave(.query) {
Object(Post.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
}
let expected = "query { post { id name } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithAlias() {
let query = Weave(.query) {
Object(Post.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
.alias("testAlias")
}
let expected = "query { testAlias: post { id name } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithSchemaName() {
let query = Weave(.query) {
Object(Post.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
.schemaName("testSchema")
}
let expected = "query { testSchema { id name } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithArgument() {
let query = Weave(.query) {
Object(Post.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
.argument(key: "testArgument", value: "test")
}
let expected = "query { post(testArgument: \"test\") { id name } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithNilArgument() {
let value: String? = nil
let query = Weave(.query) {
Object(Post.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
.argument(key: "testArgument", value: value)
}
let expected = "query { post { id name } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithCaseStyle() {
let query = Weave(.query) {
Object(Post.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
.caseStyle(.capitalized)
}
let expected = "query { Post { id name } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithFalseInclude() {
let query = Weave(.query) {
Object(Post.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
Object(Author.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
.include(if: false)
}
let expected = "query { post { id name } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithTrueSkip() {
let query = Weave(.query) {
Object(Post.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
Object(Author.self) {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
}
.skip(if: true)
}
let expected = "query { post { id name } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithSlice() {
let query = Weave(.query) {
Object(Post.CodingKeys.comments) {
Field(Comment.CodingKeys.id)
Field(Comment.CodingKeys.author)
Field(Comment.CodingKeys.content)
}
.slice(amount: 2)
}
let expected = "query { comments(first: 2) { id author content } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithSliceAndOffset() {
let query = Weave(.query) {
Object(Post.CodingKeys.comments) {
Field(Comment.CodingKeys.id)
Field(Comment.CodingKeys.author)
Field(Comment.CodingKeys.content)
}
.slice(amount: 2, offset: 2)
}
let expected = "query { comments(first: 2, offset: 2) { id author content } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithSliceAfterKey() {
let query = Weave(.query) {
Object(Post.CodingKeys.comments) {
Field(Comment.CodingKeys.id)
Field(Comment.CodingKeys.author)
Field(Comment.CodingKeys.content)
}
.slice(amount: 2, after: "123")
}
let expected = "query { comments(first: 2, after: \"123\") { id author content } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithSliceWithEdges() {
let query = Weave(.query) {
Object(Post.CodingKeys.comments) {
Field(Comment.CodingKeys.id)
Field(Comment.CodingKeys.author)
Field(Comment.CodingKeys.content)
}
.slice(amount: 2)
.paginationType(.cursor)
}
let expected = "query { comments(first: 2) { edges { cursor node { id author content } } } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithNoSliceAndEdges() {
let query = Weave(.query) {
Object(Post.CodingKeys.comments) {
Field(Comment.CodingKeys.id)
Field(Comment.CodingKeys.author)
Field(Comment.CodingKeys.content)
}
.paginationType(.cursor)
}
let expected = "query { comments { id author content } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithEdgesAndPageInfo() {
let query = Weave(.query) {
Object(Post.CodingKeys.comments) {
Field(Comment.CodingKeys.id)
Field(Comment.CodingKeys.author)
Field(Comment.CodingKeys.content)
}
.slice(amount: 2)
.paginationType(.cursor)
.pageInfo(type: PageInfo.self,
keys: PageInfo.CodingKeys.startCursor,
PageInfo.CodingKeys.endCursor,
PageInfo.CodingKeys.hasNextPage)
}
let expected = "query { comments(first: 2) { edges { cursor node { id author content } } pageInfo { startCursor endCursor hasNextPage } } }"
XCTAssertEqual(String(describing: query), expected)
}
func testBuildObjectWithEdgesAndPageInfoStrings() {
let query = Weave(.query) {
Object(Post.CodingKeys.comments) {
Field(Comment.CodingKeys.id)
Field(Comment.CodingKeys.author)
Field(Comment.CodingKeys.content)
}
.slice(amount: 2)
.paginationType(.cursor)
.pageInfo(name: "pageInfo",
keys: "startCursor",
"endCursor",
"hasNextPage")
}
let expected = "query { comments(first: 2) { edges { cursor node { id author content } } pageInfo { startCursor endCursor hasNextPage } } }"
XCTAssertEqual(String(describing: query), expected)
}
func testForEachWeavable() {
let authors = [
Author(id: "1", name: "John", age: 17, birthplace: [:]),
Author(id: "2", name: "Jane", age: 29, birthplace: [:]),
Author(id: "3", name: "Adam", age: 41, birthplace: [:])
]
let query = Weave(.query) {
ForEachWeavable(authors) { author in
Object("postsForAuthor") {
Field(Author.CodingKeys.id)
Field(Author.CodingKeys.name)
Field(Author.CodingKeys.age)
Field(Author.CodingKeys.birthplace)
}
.argument(key: "id", value: author.id)
}
}
let expected = "query { postsForAuthor(id: \"1\") { id name age birthplace } postsForAuthor(id: \"2\") { id name age birthplace } postsForAuthor(id: \"3\") { id name age birthplace } }"
XCTAssertEqual(String(describing: query), expected)
}
static var allTests = [
("testBuildField", testBuildField),
("testBuildObject", testBuildObject),
("testBuildObjectWithAlias", testBuildObjectWithAlias),
("testBuildObjectWithSchemaName", testBuildObjectWithSchemaName),
("testBuildObjectWithArgument", testBuildObjectWithArgument),
("testBuildObjectWithNilArgument", testBuildObjectWithNilArgument),
("testBuildObjectWithCaseStyle", testBuildObjectWithCaseStyle),
("testBuildObjectWithRemove", testBuildObjectWithFalseInclude),
("testBuildObjectWithTrueSkip", testBuildObjectWithTrueSkip),
("testBuildObjectWithSlice", testBuildObjectWithSlice),
("testBuildObjectWithSliceAndOffset", testBuildObjectWithSliceAndOffset),
("testBuildObjectWithNoSliceAndEdges", testBuildObjectWithNoSliceAndEdges),
("testBuildObjectWithEdgesAndPageInfo", testBuildObjectWithEdgesAndPageInfo),
("testBuildObjectWithEdgesAndPageInfoStrings", testBuildObjectWithEdgesAndPageInfoStrings),
]
}