-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore.test.js
457 lines (419 loc) · 13.8 KB
/
datastore.test.js
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
const {
assertSuccess,
assertFailure,
payload,
isSuccess,
isFailure,
meta,
} = require(`@pheasantplucker/failables`)
const assert = require('assert')
const equal = assert.deepEqual
const {
createDatastoreClient,
makeDatastoreKey,
makeEntityByName,
writeEntity,
deleteEntity,
readEntities,
formatResponse,
createQueryObj,
runQuery,
runQueryKeysOnly,
deleteByKey,
getRawEntitiesByKeys,
formatKeyResponse,
getDatastoreKeySymbol,
makeArray,
lookup,
convertKeyToV1,
make_entity,
insert,
batch_get,
batch_set,
batch_delete,
} = require('./datastore')
const uuid = require('uuid')
// *********************************************
//
// SETUP
//
// *********************************************
const { GC_PROJECT_ID } = process.env
const kind = 'testKind'
const kind2 = 'testKind2'
const namespace = 'testNamespace'
const entityName1 = 'testEntity1'
const entityName2 = 'testEntity2'
const nonexistantEntityName = 'ERROR_ERROR_IF_THIS_IS_WRITEN_BAD'
const testData1 = { description: 'no where now here when ew' }
const testData2 = { description: 'how now brown cow' }
const result = createDatastoreClient(GC_PROJECT_ID)
assertSuccess(result)
const testKey1 = payload(makeDatastoreKey(kind, entityName1))
const entity1 = payload(makeEntityByName(kind, entityName1, testData1))
const testKey2 = payload(makeDatastoreKey(kind, entityName2))
const entity2 = payload(makeEntityByName(kind, entityName2, testData2))
const nonexistantKey = payload(makeDatastoreKey(kind, nonexistantEntityName))
const separateKindEntity = payload(
makeEntityByName(kind2, entityName1, testData1)
)
describe(`datastore.js`, function() {
this.timeout(4 * 1000)
describe('writeEntity()', () => {
it('should write an entity', async () => {
const writeResult = await writeEntity([
entity1,
entity2,
separateKindEntity,
])
assertSuccess(writeResult)
})
})
describe('makeDatastoreKey()', () => {
it('should return a valid key', () => {
const result = makeDatastoreKey(kind, entityName1)
assertSuccess(result)
const key = payload(result)
equal(key.name, entityName1)
equal(key.kind, kind)
})
it('should fail with missing data', () => {
const result = makeDatastoreKey('', '')
assertFailure(result)
const resultKind = makeDatastoreKey(kind, '')
assertFailure(resultKind)
const resultName = makeDatastoreKey('', entityName1)
assertFailure(resultName)
})
})
describe('makeEntityByName()', () => {
it('should make an entity to execute', () => {
const result = makeEntityByName(kind, entityName1, testData1)
assertSuccess(result)
const entity = payload(result)
equal(entity.key, testKey1)
equal(entity.data, testData1)
})
})
describe(`deleteEntity()`, async () => {
it(`should remove an entity from the DB`, async () => {
const entityNameDel = 'deleteTest'
const deleteKey = payload(makeDatastoreKey(kind, entityNameDel))
const entityRead = await getRawEntitiesByKeys(deleteKey)
const entityDel = payload(makeEntityByName(kind, entityNameDel, 'delete'))
if (isFailure(entityRead)) {
const writeResult = await writeEntity(entityDel)
assertSuccess(writeResult)
const result = await deleteEntity(entityDel)
assertSuccess(result)
} else {
const result = await deleteEntity(entityDel)
assertSuccess(result)
}
})
})
describe(`deleteByKey()`, async () => {
it(`should remove an entity from the DB by key`, async () => {
const writeResult = await writeEntity(entity1)
assertSuccess(writeResult)
const result = await deleteByKey(testKey1)
assertSuccess(result)
const entity1Read = await getRawEntitiesByKeys(testKey1)
assertFailure(entity1Read)
})
})
describe('writeEntity()', () => {
it('should write an entity to Datastore', async () => {
const result = await writeEntity(entity1)
assertSuccess(result)
const entity1Read = await getRawEntitiesByKeys(testKey1)
assertSuccess(entity1Read)
})
it('should fail with no entity', async () => {
const result = await writeEntity()
assertFailure(result)
})
it('should be able to write test data to a local path', async () => {
const localPath = './testdata/'
const result = await writeEntity(entity1, localPath)
assertSuccess(result)
const entity1Read = await getRawEntitiesByKeys(testKey1)
assertSuccess(entity1Read)
})
})
describe(`getDatastoreKeySymbol()`, () => {
it(`should Create a client if none exists`, () => {
const result = getDatastoreKeySymbol()
assertSuccess(result)
})
it(`should return the symbol`, () => {
const result = getDatastoreKeySymbol()
assertSuccess(result)
const key = payload(result)
equal('symbol', typeof key)
})
})
describe('formatResponse()', () => {
it(`should convert the raw response to a cleaner struct`, async () => {
const getResponse = await getRawEntitiesByKeys(testKey1)
assertSuccess(getResponse)
const key1Data = payload(getResponse)
const result = formatResponse(key1Data)
const cleanReturn = payload(result)
assertSuccess(result)
equal({ [entityName1]: testData1 }, cleanReturn)
})
it(`should return multiple entities as object attributes`, async () => {
const getResponse = await getRawEntitiesByKeys([testKey1, testKey2])
assertSuccess(getResponse)
const keysData = payload(getResponse)
const result = formatResponse(keysData)
const cleanReturn = payload(result)
assertSuccess(result)
equal(
{
[entityName1]: testData1,
[entityName2]: testData2,
},
cleanReturn
)
})
})
describe('formatKeyResponse()', () => {
it(`should convert the raw response to a cleaner struct`, async () => {
const getResponse = await getRawEntitiesByKeys(testKey1)
assertSuccess(getResponse)
const key1Data = payload(getResponse)
const result = formatKeyResponse(key1Data)
const cleanReturn = payload(result)
assertSuccess(result)
equal({ [entityName1]: testKey1 }, cleanReturn)
})
it(`should return multiple entities as object attributes`, async () => {
const getResponse = await getRawEntitiesByKeys([testKey1, testKey2])
assertSuccess(getResponse)
const keysData = payload(getResponse)
const result = formatResponse(keysData)
const cleanReturn = payload(result)
assertSuccess(result)
equal(
{
[entityName1]: testData1,
[entityName2]: testData2,
},
cleanReturn
)
})
})
describe(`createQueryObj()`, () => {
it(`should return a query object`, () => {
const result = createQueryObj(kind)
assertSuccess(result)
const queryObj = payload(result)
equal(typeof queryObj, 'object')
})
it('should fail with no kind', () => {
const result = createQueryObj('')
assertFailure(result)
})
it(`should set the namespace if given 2 args`, () => {
const result = createQueryObj(kind, namespace)
assertSuccess(result)
const queryObj = payload(result)
equal(queryObj.namespace, namespace)
equal(queryObj.kinds[0], kind)
})
// it.skip(`COULD also set the datastore object scope`, () => {
// //but I don't know why you need that!
// })
})
describe(`lookup()`, () => {
it(`should return an array of objects found in the DB Keys removing non-written keys`, async () => {
const testKeys = [testKey1, nonexistantKey]
const result = await lookup(testKeys)
assertSuccess(result)
const returnKeys = payload(result)
assert(returnKeys.found.length, 1)
assert(returnKeys.missing.length, 1)
})
})
describe(`convertKeyToV1()`, () => {
it(`should replace path with the nested abomination of kind,name`, () => {
const result = convertKeyToV1(testKey1)
const correctPath = [{ kind: kind, name: entityName1 }]
equal(result.path, correctPath)
})
})
describe(`runQuery()`, () => {
it(`should fail with no query`, async () => {
const result = await runQuery()
assertFailure(result)
})
it(`should return all elements from a query`, async () => {
const query = payload(createQueryObj(kind))
const result = await runQuery(query)
assertSuccess(result)
const responses = payload(result)
const metadata = meta(result)
assert(
metadata.queryEndDetails.moreResults,
'This attribute should exist.'
)
// not sure what other tests are creating/deleting objects
equal(Object.keys(responses).length > 1, true)
})
})
describe(`runQueryKeysOnly`, () => {
it(`should return only the keys which are cheaper`, async () => {
const query = payload(createQueryObj(kind))
const result = await runQueryKeysOnly(query)
assertSuccess(result)
const keys = payload(result)
assert(Object.keys(keys).length > 1)
})
})
describe(`makeArray()`, () => {
it(`should return a success array from an array or string or fail`, () => {
const result = makeArray('fling')
assert(typeof result, 'array')
})
})
describe('readEntities()', async () => {
it(`should return a nice formatted list with response stuff in metadata`, async () => {
const result = await readEntities(testKey1)
assertSuccess(result)
const keyData = payload(result)
equal(
{
[entityName1]: testData1,
},
keyData
)
})
it(`should work with an array`, async () => {
const testKeys = [testKey1, testKey2]
const result = await readEntities(testKeys)
assertSuccess(result)
const keyData = payload(result)
equal(testData1, keyData[entityName1])
equal(testData2, keyData[entityName2])
})
it(`should return undefined if it doesn't read`, async () => {
const testKeys = [testKey1, testKey2, nonexistantKey]
const result = await readEntities(testKeys)
assertSuccess(result)
const keyData = payload(result)
equal(testData1, keyData[entityName1])
equal(testData2, keyData[entityName2])
equal(undefined, keyData[nonexistantKey])
})
it('should fail if the key doesnt exist', async () => {
const result = await readEntities([nonexistantKey])
assertFailure(result)
})
})
describe('getRawEntitiesByKeys()', () => {
it('should return the correct entity', async () => {
const writeResult = await writeEntity(entity1)
assertSuccess(writeResult)
if (isSuccess(writeResult)) {
const result = await getRawEntitiesByKeys(testKey1)
assertSuccess(result)
const rawResponse = payload(result)
const responseData = rawResponse[0]
equal(responseData, testData1)
}
})
it('should fail with an entity not in the DS', async () => {
const result = await getRawEntitiesByKeys(nonexistantKey)
assertFailure(result)
})
it('should return both entities if given an array', async () => {
const keys = [testKey1, testKey2]
const result = await getRawEntitiesByKeys(keys)
assertSuccess(result)
const keysData = payload(result)
equal(keysData.length, keys.length)
})
})
describe('insert', () => {
it('should insert a list of entities', async () => {
const kind = 'insert_kind'
const name1 = uuid.v4()
const name2 = uuid.v4()
const e1 = make_entity(kind, name1, { foo: 1 })
const e2 = make_entity(kind, name2, { foo: 2 })
const expected = { inserted: [name2], existed: [name1] }
const r1 = await writeEntity(e1)
assertSuccess(r1)
const r2 = await insert([e1, e2])
assertSuccess(r2)
const p = payload(r2)
equal(p, expected)
})
})
describe(`batch`, () => {
it('make some data', async () => {
const namespace = 'namespace1'
const meta = {
excludeFromIndexes: ['hash', 'title'],
method: 'insert',
}
const keys_and_data = [
['test_kind', 'bg1', { hash: '1', title: 'one' }],
['test_kind', 'bg2', { hash: '2', title: 'two' }],
['test_kind', 'bg3', { hash: '3', title: 'thr' }],
]
const result = await batch_set(namespace, keys_and_data, meta)
//console.log(payload(result))
assertSuccess(result)
})
it('get the data as a map', async () => {
const namespace = 'namespace1'
const keys = [
['test_kind', 'bg1'],
['test_kind', 'bg2'],
['test_kind', 'bg3'],
['test_kind', 'bg4'],
]
const fields = ['hash']
const result = await batch_get(namespace, keys, fields)
assertSuccess(result)
equal(payload(result), {
items: {
bg1: { hash: '1', title: 'one' },
bg2: { hash: '2', title: 'two' },
bg3: { hash: '3', title: 'thr' },
},
found: ['bg1', 'bg2', 'bg3'],
missing: ['bg4'],
})
})
it('try getting keys that are not in the database', async () => {
const namespace = 'namespace1'
const keys = [['test_kind', 'bg5'], ['test_kind', 'bg6']]
const fields = ['hash']
const result = await batch_get(namespace, keys, fields)
assertSuccess(result)
const p = payload(result)
equal(p, {
items: {},
found: [],
missing: ['bg5', 'bg6'],
})
})
it('delete the data', async () => {
const namespace = 'namespace1'
const keys = [
['test_kind', 'bg1'],
['test_kind', 'bg2'],
['test_kind', 'bg3'],
['test_kind', 'bg4'],
]
const result = await batch_delete(namespace, keys)
assertSuccess(result)
const p = payload(result)
equal(p, { count: 3 })
})
})
})