-
Notifications
You must be signed in to change notification settings - Fork 2
/
easy-dgraph.test.ts
412 lines (357 loc) · 16.1 KB
/
easy-dgraph.test.ts
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import { Dgraph } from "./easy-dgraph";
import { EnumType } from "./jsonToGraphQLQuery";
describe('Query Tests', () => {
// GET
it('Empty Query', () => {
const d = new Dgraph('lesson').get({}).build();
expect(d).toBe(`query { getLesson }`);
});
it('Get Query', () => {
const d = new Dgraph('lesson').get({
me: 1
}).build();
expect(d).toBe(`query { getLesson { me } }`);
});
it('Get Query with Filter', () => {
const d = new Dgraph('lesson').get({
me: 1
}).filter('2223a').build();
expect(d).toBe(`query { getLesson(id: "2223a") { me } }`);
});
it('Get Query with New Id Field with Filter', () => {
const d = new Dgraph('lesson').get({
me: 1
}).idField('post').filter('2223a').build();
expect(d).toBe(`query { getLesson(post: "2223a") { me } }`);
});
it('Get Query with New Id Field in Filter', () => {
const d = new Dgraph('lesson').get({
me: 1
}).filter({ post: '2223a' }).build();
expect(d).toBe(`query { getLesson(post: "2223a") { me } }`);
});
it('Get Query with Several Fields in Filter', () => {
const d = new Dgraph('lesson').get({
me: 1
}).filter({ post: '2223a', help: 'a-lot' }).build();
expect(d).toBe(`query { getLesson(post: "2223a", help: "a-lot") { me } }`);
});
// AGGREGATE
it('Get Query with Several Fields in Filter', () => {
const d = new Dgraph('lesson').aggregate({
name: 1,
postsAggregate: {
scoreMin: 1,
scoreMax: 1,
scoreAvg: 1
}
}).filter({
score: {
gt: 10
}
}).build();
expect(d).toBe(`query { aggregateLesson(filter: { score: { gt: 10 } }) { name postsAggregate { scoreMin scoreMax scoreAvg } } }`);
});
// QUERY
it('Get Query with Order Filter', () => {
const d = new Dgraph('lesson').query({
me: 1
}).order({ asc: 'bob', then: { desc: 'jill' } }).filter({ post: '2223a' }).build();
expect(d).toBe(`query { queryLesson(order: { asc: bob, then: { desc: jill } }, filter: { post: "2223a" }) { me } }`);
});
it('Get Query with Complex Filter', () => {
const d = new Dgraph('lesson').query({
me: 1
}).filter({
or: [
{ and: [{ title: { allofterms: "GraphQL" } }, { not: { tags: { eq: "GraphQL" } } }] },
{ and: [{ title: { allofterms: "Dgraph" } }, { not: { tags: { eq: "Dgraph" } } }] }
]
}).build();
expect(d).toBe(`query { queryLesson(filter: { or: [{ and: [{ title: { allofterms: "GraphQL" } }, { not: { tags: { eq: "GraphQL" } } }] }, { and: [{ title: { allofterms: "Dgraph" } }, { not: { tags: { eq: "Dgraph" } } }] }] }) { me } }`);
});
it('Multiple Queries', () => {
const d = new Dgraph('lesson').query({
me: 1
}).type('lesson', 'bob').query({
you: 1,
him: 1
}).build();
expect(d).toBe(`query { queryLesson { me } bob: queryLesson { you him } }`);
});
it('Multiple Queries Subscription', () => {
const d = new Dgraph('lesson').query({
me: 1
}).type('lesson', 'bob').query({
you: 1,
him: 1
}).buildSubscription();
expect(d).toBe(`subscription { queryLesson { me } bob: queryLesson { you him } }`);
});
it('Cascade Pagination', () => {
const d = new Dgraph('task').query({
me: 1
}).cascade().first(5).offset(2).build();
expect(d).toBe(`query { queryTask(first: 5, offset: 2) @cascade { me } }`);
});
it('Cascade Pagination with Fields', () => {
const d = new Dgraph('task').query({
tom: 1
}).cascade('name', 'me').first(5).offset(2).build();
expect(d).toBe(`query { queryTask(first: 5, offset: 2) @cascade(fields: ["name", "me"]) { tom } }`);
});
it('Cascade Pagination with Fields Array', () => {
const d = new Dgraph('task').query({
tom: 1
}).cascade(['name', 'me']).first(5).offset(2).build();
expect(d).toBe(`query { queryTask(first: 5, offset: 2) @cascade(fields: ["name", "me"]) { tom } }`);
});
it('Nested Cascade Pagination', () => {
const d = new Dgraph('task').query({
tom: {
__cascade: 1,
name: 1,
gun: 1
}
}).first(5).offset(2).build();
expect(d).toBe(`query { queryTask(first: 5, offset: 2) { tom @cascade { name gun } } }`);
});
it('Nested Cascade Pagination with Fields', () => {
const d = new Dgraph('task').query({
tom: {
__cascade: ["tom", "bill"],
name: 1,
gun: 1
}
}).first(5).offset(2).build();
expect(d).toBe(`query { queryTask(first: 5, offset: 2) { tom @cascade(fields: ["tom", "bill"]) { name gun } } }`);
});
it('Nested Cascade Pagination with Fields', () => {
const d = new Dgraph('task').query({
tom: {
__cascade: "tom",
name: 1,
gun: 1
}
}).first(5).offset(2).build();
expect(d).toBe(`query { queryTask(first: 5, offset: 2) { tom @cascade(fields: ["tom"]) { name gun } } }`);
});
it('Nested Filter', () => {
const d = new Dgraph('task').query({
tom: {
__filter: {
title: {
allofterms: "GraphQL"
}
},
name: 1,
gun: 1
}
}).first(5).offset(2).build();
expect(d).toBe(`query { queryTask(first: 5, offset: 2) { tom(filter: { title: { allofterms: "GraphQL" } }) { name gun } } }`);
});
it('Regular Expression Query', () => {
const d = new Dgraph('task').query({
tom: 1
}).filter({ name: { regexp: "/.*iggy.*/" } }).first(5).offset(2).build();
expect(d).toBe(`query { queryTask(first: 5, offset: 2, filter: { name: { regexp: "/.*iggy.*/" } }) { tom } }`);
});
it('Nested Numbers', () => {
const d = new Dgraph('task').query({
name: 1
}).filter({
location: {
near: {
coordinate: {
latitute: 37.771935,
longitude: -122.469829
},
distance: 1000
}
}
}).build();
expect(d).toBe(`query { queryTask(filter: { location: { near: { coordinate: { latitute: 37.771935, longitude: -122.469829 }, distance: 1000 } } }) { name } }`);
});
it('Enum Type Query', () => {
const d = new Dgraph('post').query({
tom: 1
}).filter({ tags: { eq: new EnumType('GraphQL') } }).build();
expect(d).toBe(`query { queryPost(filter: { tags: { eq: GraphQL } }) { tom } }`);
});
it('Multiple Enum Type Query', () => {
const d = new Dgraph('post').query({
tom: 1
}).filter({ tags: { eq: [new EnumType('GraphQL'), new EnumType('Dgraph')] } }).build();
expect(d).toBe(`query { queryPost(filter: { tags: { eq: [GraphQL, Dgraph] } }) { tom } }`);
});
it('Boolean Types with Different Case Type', () => {
const d = new Dgraph('pOSt').query({
tom: 1,
becca: 1
}).filter({ sum: true, me: false }).build();
expect(d).toBe(`query { queryPOSt(filter: { sum: true, me: false }) { tom becca } }`);
});
it('Query `has` Filter', () => {
const d = new Dgraph('post').query({
tom: 1
}).filter({ has: 'name' }).build();
expect(d).toBe(`query { queryPost(filter: { has: name }) { tom } }`);
});
it('Query `has` Multiple Filter', () => {
const d = new Dgraph('post').query({
tom: 1
}).filter({ has: ['name', 'email'] }).build();
expect(d).toBe(`query { queryPost(filter: { has: [name, email] }) { tom } }`);
});
it('Query `has` Multiple Nested Filter', () => {
const d = new Dgraph('post').query({
tom: 1,
brandy: {
__filter: {
has: ['name', 'email']
}
}
}).build();
expect(d).toBe(`query { queryPost { tom brandy(filter: { has: [name, email] }) } }`);
});
it('variable CaseNames', () => {
const d = new Dgraph('snakeMan').query({
hint: 1
}).filter({ h: false }).build();
expect(d).toBe(`query { querySnakeMan(filter: { h: false }) { hint } }`)
});
});
describe('Mutation Tests', () => {
// ADD
it('Add Mutation', () => {
const d = new Dgraph('lesson').add({
me: 1
}).build();
expect(d).toBe(`mutation { addLesson { lesson { me } numUids } }`);
});
it('Add Mutation with Set', () => {
const d = new Dgraph('lesson').add({
me: 1
}).set({ bob: true }).build();
expect(d).toBe(`mutation { addLesson(input: { bob: true }) { lesson { me } numUids } }`);
});
it('Add Mutation with Set', () => {
const d = new Dgraph('lesson').upsert({
me: 1
}).set({ bob: true }).build();
expect(d).toBe(`mutation { addLesson(input: { bob: true }, upsert: true) { lesson { me } numUids } }`);
});
// UPDATE
it('Update Mutation Set', () => {
const d = new Dgraph('lesson').update({
me: 1,
cards: {
buddy: 1
}
}).filter({ bob: true }).set({ tim: false }).build();
expect(d).toBe(`mutation { updateLesson(input: { filter: { bob: true }, set: { tim: false } }) { lesson { me cards { buddy } } numUids } }`);
});
it('Update Mutation Remove', () => {
const d = new Dgraph('lesson').update({
me: 1
}).filter({ bob: true }).remove({ bob: false }).build();
expect(d).toBe(`mutation { updateLesson(input: { filter: { bob: true }, remove: { bob: false } }) { lesson { me } numUids } }`);
});
// DELETE
it('Delete Mutation', () => {
const d = new Dgraph('lesson').delete({
me: 1
}).filter('222g').build();
expect(d).toBe(`mutation { deleteLesson(filter: { id: "222g" }) { lesson { me } numUids msg } }`);
});
});
describe('Custom Tests', () => {
// Custom
it('Custom Query', () => {
const d = new Dgraph('getCustomTwitterUser').customQuery({
location: 1,
description: 1,
count: 1
}).filter({ name: '222g' }).build();
expect(d).toBe(`query { getCustomTwitterUser(name: "222g") { location description count } }`);
});
it('Custom Mutation', () => {
const d = new Dgraph('newCustomTwitterUser').customMutation().filter({ name: '222g', bug: 'spider' }).build();
expect(d).toBe(`mutation { newCustomTwitterUser(name: "222g", bug: "spider") }`);
});
});
describe('easy-dgraph Functions', () => {
// Deep Mutations
it('Update Deep Mutation with Multiple Records and New Deep Add', () => {
const d = new Dgraph('lesson').deep({ field: 'cards', type: 'card' }).update({
me: 1,
cards: {
id: 1,
tommy: 1
}
}).filter({ id: '12345' }).set({ me: false, cards: [{ tommy: 'son' }, { id: '2', tommy: 'bill' }] }).build();
expect(d).toBe(`mutation { updateCard1: updateCard(input: { filter: { id: "2" }, set: { tommy: "bill" } }) { numUids } updateLesson(input: { filter: { id: "12345" }, set: { me: false, cards: [{ tommy: "son" }] } }) { lesson { me cards { id tommy } } numUids } }`);
});
it('Update Deep Mutation with Multiple Records and New Deep Add with Different ID Field', () => {
const d = new Dgraph('lesson').deep({ field: 'cards', type: 'card', idField: 'dreamId' }).update({
me: 1,
cards: {
id: 1,
tommy: 1
}
}).filter({ id: '12345' }).set({ me: false, cards: [{ tommy: 'son' }, { dreamId: '2', tommy: 'bill' }] }).build();
expect(d).toBe(`mutation { updateCard1: updateCard(input: { filter: { dreamId: "2" }, set: { tommy: "bill" } }) { numUids } updateLesson(input: { filter: { id: "12345" }, set: { me: false, cards: [{ tommy: "son" }] } }) { lesson { me cards { id tommy } } numUids } }`);
});
it('Update Deep Mutation with Multiple Records', () => {
const d = new Dgraph('lesson').deep({ field: 'cards', type: 'card' }).update({
me: 1,
cards: {
id: 1,
tommy: 1
}
}).filter({ id: '12345' }).set({ me: false, cards: [{ id: '1', tommy: 'son' }, { id: '2', tommy: 'bill' }] }).build();
expect(d).toBe(`mutation { updateCard0: updateCard(input: { filter: { id: "1" }, set: { tommy: "son" } }) { numUids } updateCard1: updateCard(input: { filter: { id: "2" }, set: { tommy: "bill" } }) { numUids } updateLesson(input: { filter: { id: "12345" }, set: { me: false } }) { lesson { me cards { id tommy } } numUids } }`);
});
it('Update Deep Mutation with Multiple Records, Automatic ID', () => {
const d = new Dgraph('lesson').deep({ field: 'cards', type: 'card' }).update({
me: 1,
cards: {
id: 1,
tommy: 1
}
}).filter({ id: '12345' }).set({ me: false, cards: [{ tommy: 'son' }, { id: '2', tommy: 'bill' }] }).build();
expect(d).toBe(`mutation { updateCard1: updateCard(input: { filter: { id: "2" }, set: { tommy: "bill" } }) { numUids } updateLesson(input: { filter: { id: "12345" }, set: { me: false, cards: [{ tommy: "son" }] } }) { lesson { me cards { id tommy } } numUids } }`);
});
it('Update Deep Mutation with Multiple Records @id Type', () => {
const d = new Dgraph('lesson').deep({ field: 'cards', type: 'card', idDirective: true, idField: 'nest' }).update({
me: 1,
cards: {
nest: 1,
tommy: 1
}
}).filter({ id: '12345' }).set({ me: false, cards: [{ nest: '1', tommy: 'son' }, { nest: '2', tommy: 'bill' }] }).build();
expect(d).toBe(`mutation { updateCard0: updateCard(input: { filter: { nest: { eq: "1" } }, set: { tommy: "son" } }) { numUids } updateCard1: updateCard(input: { filter: { nest: { eq: "2" } }, set: { tommy: "bill" } }) { numUids } updateLesson(input: { filter: { id: "12345" }, set: { me: false } }) { lesson { me cards { nest tommy } } numUids } }`);
});
it('Update Multiple Deep Mutation with Multiple Records', () => {
const d = new Dgraph('lesson').deep([{ field: 'cards', type: 'card' }, { field: 'pages', type: 'page', idField: 'pageId', idDirective: true }]).update({
me: 1,
cards: {
id: 1,
tommy: 1
},
pages: {
pageId: 1,
words: 1
}
}).filter({ id: '12345' }).set({ me: false, cards: [{ id: '1', tommy: 'son' }, { id: '2', tommy: 'bill' }], pages: { pageId: 1, words: '23' } }).build();
expect(d).toBe(`mutation { updateCard0: updateCard(input: { filter: { id: "1" }, set: { tommy: "son" } }) { numUids } updateCard1: updateCard(input: { filter: { id: "2" }, set: { tommy: "bill" } }) { numUids } updatePage0: updatePage(input: { filter: { pageId: { eq: 1 } }, set: { words: "23" } }) { numUids } updateLesson(input: { filter: { id: "12345" }, set: { me: false } }) { lesson { me cards { id tommy } pages { pageId words } } numUids } }`);
});
it('Type Prefixes', () => {
const d = new Dgraph('buddy').prefix('sound_').update({ id: 1 }).filter({ id: '12345' }).set({ adder: 't', subber: 'me' }).build();
expect(d).toBe(`mutation { updateSound_buddy(input: { filter: { id: "12345" }, set: { adder: "t", subber: "me" } }) { sound_buddy { id } numUids } }`);
})
// TODO
it.todo(`Add Multiple Set Input Capabilities`);
it.todo(`Deep Deep Updates?... deep({ field: 'cards.lesson' })`);
it.todo('Aggregate Counts with Filters');
});