@sprucelabs/data-stores 19.3.3 → 20.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,16 +6,109 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const schema_1 = require("@sprucelabs/schema");
7
7
  const test_utils_1 = require("@sprucelabs/test-utils");
8
8
  const test_utils_2 = require("@sprucelabs/test-utils");
9
- const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
10
9
  const generateId_1 = __importDefault(require("../utilities/generateId"));
11
- const pluckAssertionMethods_1 = __importDefault(require("./pluckAssertionMethods"));
12
10
  const databaseAssertUtil = {
13
11
  collectionName: 'test_collection',
14
- async runSuite(connect) {
12
+ async runSuite(connect, tests) {
15
13
  (0, schema_1.assertOptions)({ connect }, ['connect']);
16
- const methods = (0, pluckAssertionMethods_1.default)(this);
17
- //@ts-ignore
18
- await Promise.all(methods.map((method) => this[method](connect)));
14
+ const methods = [
15
+ //connecting
16
+ 'assertThrowsWithInvalidConnectionString',
17
+ 'assertThrowsWhenCantConnect',
18
+ 'assertThrowsWithBadDatabaseName',
19
+ //inserting
20
+ 'assertEmptyDatabaseReturnsEmptyArray',
21
+ 'assertKnowsIfConnectionClosed',
22
+ 'assertFindOneOnEmptyDatabaseReturnsNull',
23
+ 'assertCanSortDesc',
24
+ 'assertCanSortAsc',
25
+ 'assertCanSortById',
26
+ 'assertCanQueryWithOr',
27
+ 'generateIdDifferentEachTime',
28
+ 'assertInsertingGeneratesId',
29
+ 'assertCanCreateMany',
30
+ 'assertCanLimitResults',
31
+ 'assertCanCreateWithObjectField',
32
+ //counting
33
+ 'assertCanCountOnId',
34
+ 'assertCanCount',
35
+ //updating
36
+ 'assertThrowsWhenUpdatingRecordNotFound',
37
+ 'assertCanUpdate',
38
+ 'assertCanUpdateMany',
39
+ 'assertCanPushOntoArrayValue',
40
+ 'canUpdateWithObjectField',
41
+ 'canUpdateFieldInObjectFieldWithTargettedWhere',
42
+ 'assertCanSaveAndGetNullAndUndefined',
43
+ //upserting
44
+ 'assertCanUpsertOne',
45
+ 'assertCanUpsertNull',
46
+ 'assertCanPushToArrayOnUpsert',
47
+ //finding
48
+ 'assertEmptyDatabaseReturnsEmptyArray',
49
+ 'assertFindOneOnEmptyDatabaseReturnsNull',
50
+ 'assertCanLimitResults',
51
+ 'assertCanLimitResultsToZero',
52
+ 'assertCanFindWithBooleanField',
53
+ 'assertCanQueryByGtLtGteLte',
54
+ 'assertCanQueryPathWithDotSyntax',
55
+ 'assertCanReturnOnlySelectFields',
56
+ 'assertCanSearchByRegex',
57
+ 'assertCanFindWithNe',
58
+ 'assertCanFindWithIn',
59
+ //deleting
60
+ 'assertCanDeleteRecord',
61
+ 'assertCanDeleteOne',
62
+ //indexing
63
+ 'assertHasNoUniqueIndexToStart',
64
+ 'assertCanCreateUniqueIndex',
65
+ 'assertCanCreateMultiFieldUniqueIndex',
66
+ 'assertCantCreateUniqueIndexTwice',
67
+ 'assertCanDropUniqueIndex',
68
+ 'assertCanDropCompoundUniqueIndex',
69
+ 'assertCantDropUniqueIndexThatDoesntExist',
70
+ 'assertCantDropIndexWhenNoIndexExists',
71
+ 'assertCantDropCompoundUniqueIndexThatDoesntExist',
72
+ 'assertSyncingUniqueIndexsAddsMissingIndexes',
73
+ 'assertSyncingUniqueIndexsSkipsExistingIndexs',
74
+ 'assertSyncingUniqueIndexesRemovesExtraIndexes',
75
+ 'assertSyncingUniqueIndexesIsRaceProof',
76
+ 'assertSyncingIndexesDoesNotAddAndRemove',
77
+ 'assertUniqueIndexBlocksDuplicates',
78
+ 'assertDuplicateKeyThrowsOnInsert',
79
+ 'assertSettingUniqueIndexViolationThrowsSpruceError',
80
+ 'assertCanCreateUniqueIndexOnNestedField',
81
+ 'assertUpsertWithUniqueIndex',
82
+ 'assertNestedFieldIndexUpdates',
83
+ 'assertHasNoIndexToStart',
84
+ 'assertCanCreateIndex',
85
+ 'assertCantCreateSameIndexTwice',
86
+ 'assertCanCreateMultiFieldIndex',
87
+ 'assertCanDropIndex',
88
+ 'assertCanDropCompoundIndex',
89
+ 'assertCantDropCompoundIndexThatDoesNotExist',
90
+ 'assertSyncIndexesSkipsExisting',
91
+ 'assertSyncIndexesRemovesExtraIndexes',
92
+ 'assertSyncIndexesHandlesRaceConditions',
93
+ 'assertSyncIndexesDoesNotRemoveExisting',
94
+ 'assertDuplicateFieldsWithMultipleUniqueIndexesWorkAsExpected',
95
+ ];
96
+ const db = await connectToDabatase(connect);
97
+ await db.dropDatabase();
98
+ await this.shutdown(db);
99
+ const toRun = tests !== null && tests !== void 0 ? tests : methods;
100
+ for (const method of toRun) {
101
+ try {
102
+ //@ts-ignore
103
+ await this[method](connect);
104
+ }
105
+ catch (err) {
106
+ const prefix = `Error in ${method}:\n\n `;
107
+ err.message = `${prefix} ${err.message}`;
108
+ err.stack = `${prefix} ${err.stack}`;
109
+ throw err;
110
+ }
111
+ }
19
112
  },
20
113
  async _getFilteredIndexes(db) {
21
114
  return this._filterIdIndex(await db.getIndexes(this.collectionName));
@@ -25,15 +118,32 @@ const databaseAssertUtil = {
25
118
  },
26
119
  async _assertUpdateUpdatedRightNumberOfRecords(db, search, updates, expectedUpdateCount) {
27
120
  const updatedCount = await db.update(this.collectionName, search, updates);
28
- test_utils_1.assert.isEqual(updatedCount, expectedUpdateCount);
121
+ test_utils_1.assert.isEqual(updatedCount, expectedUpdateCount, 'db.update() did not update the expected amount of records! Make sure it returns an integer of the number of records updated.');
29
122
  const count = await db.count(this.collectionName, updates);
30
123
  test_utils_1.assert.isEqual(count, expectedUpdateCount);
31
124
  },
125
+ async generateIdDifferentEachTime(connect) {
126
+ const db = await connectToDabatase(connect);
127
+ const id1 = db.generateId();
128
+ const id2 = db.generateId();
129
+ test_utils_1.assert.isNotEqual(id1, id2, 'generateId() must generate a different id each time');
130
+ await this.shutdown(db);
131
+ },
32
132
  async assertCanSortDesc(connect) {
33
- const db = await connect();
34
- await db.createOne(this.collectionName, { name: 'second', count: 1 });
35
- await db.createOne(this.collectionName, { name: 'third', count: 5 });
36
- await db.createOne(this.collectionName, { name: 'first', count: -1 });
133
+ const db = await connectToDabatase(connect);
134
+ const second = await db.createOne(this.collectionName, {
135
+ name: 'second',
136
+ count: 1,
137
+ });
138
+ const third = await db.createOne(this.collectionName, {
139
+ name: 'third',
140
+ count: 5,
141
+ });
142
+ const first = await db.createOne(this.collectionName, {
143
+ name: 'first',
144
+ count: -1,
145
+ });
146
+ test_utils_1.assert.isFunction(db.find, `db.find() must be a function.`);
37
147
  const results = await db.find(this.collectionName, {}, {
38
148
  sort: [
39
149
  {
@@ -42,18 +152,23 @@ const databaseAssertUtil = {
42
152
  },
43
153
  ],
44
154
  });
45
- test_utils_1.assert.isEqual(results[0].name, 'third');
46
- test_utils_1.assert.isEqual(results[1].name, 'second');
47
- test_utils_1.assert.isEqual(results[2].name, 'first');
155
+ test_utils_1.assert.isBelow(results.length, 4, `You are not resetting data when .dropDatabase() is called. If you can't drop the database in your env, then you need to clear at all rows in all tables! Got back ${results.length} records!`);
156
+ test_utils_1.assert.isLength(results, 3, `Should have 3 records back from find() after 3 createOne()s. Make sure your createOne() is inserting records! I only got back ${results.length} records!`);
157
+ test_utils_1.assert.isEqual(results[0].name, 'third', `first record should be named "third" when sorting desc by name. Make sure you are sorting using the "sort" option.`);
158
+ test_utils_1.assert.isEqual(results[1].name, 'second', `second record should be named "second" when sorting desc by name.`);
159
+ test_utils_1.assert.isEqual(results[2].name, 'first', `third record should be named "first" when sorting desc by name.`);
160
+ test_utils_1.assert.isEqualDeep(results[0], third, `createOne() is not returning the record that was created!`);
161
+ test_utils_1.assert.isEqualDeep(results[1], second, `createOne() is not returning the record that was created!`);
162
+ test_utils_1.assert.isEqualDeep(results[2], first, `createOne() is not returning the record that was created!`);
48
163
  const result = await db.findOne(this.collectionName, undefined, {
49
164
  sort: [{ field: 'count', direction: 'desc' }],
50
165
  });
51
- test_utils_1.assert.isTruthy(result);
52
- test_utils_1.assert.isEqual(result.name, 'third');
166
+ test_utils_1.assert.isTruthy(result, 'findOne() should return a record!');
167
+ test_utils_1.assert.isEqual(result.name, 'third', 'findOne() should honor sort!');
53
168
  await this.shutdown(db);
54
169
  },
55
170
  async assertCanSortAsc(connect) {
56
- const db = await connect();
171
+ const db = await connectToDabatase(connect);
57
172
  await db.createOne(this.collectionName, { name: 'second', count: 1 });
58
173
  await db.createOne(this.collectionName, { name: 'third', count: 5 });
59
174
  await db.createOne(this.collectionName, { name: 'first', count: -1 });
@@ -65,9 +180,9 @@ const databaseAssertUtil = {
65
180
  },
66
181
  ],
67
182
  });
68
- test_utils_1.assert.isEqual(results[0].name, 'first');
69
- test_utils_1.assert.isEqual(results[1].name, 'second');
70
- test_utils_1.assert.isEqual(results[2].name, 'third');
183
+ test_utils_1.assert.isEqual(results[0].name, 'first', 'find is not sorting ascending');
184
+ test_utils_1.assert.isEqual(results[1].name, 'second', 'find is not sorting ascending');
185
+ test_utils_1.assert.isEqual(results[2].name, 'third', 'find is not sorting ascending');
71
186
  const result = await db.findOne(this.collectionName, undefined, {
72
187
  sort: [{ field: 'count', direction: 'asc' }],
73
188
  });
@@ -76,74 +191,84 @@ const databaseAssertUtil = {
76
191
  await this.shutdown(db);
77
192
  },
78
193
  async assertInsertingGeneratesId(connect) {
79
- const db = await connect();
194
+ const db = await connectToDabatase(connect);
195
+ const name = (0, generateId_1.default)();
80
196
  const inserted = await db.createOne(this.collectionName, {
81
- name: 'first',
197
+ name,
82
198
  });
83
- test_utils_1.assert.isTruthy(inserted);
84
- test_utils_1.assert.isString(inserted.id);
85
- test_utils_1.assert.isEqual(inserted.name, 'first');
199
+ test_utils_1.assert.isTruthy(inserted, "createOne() didn't return anything!");
200
+ test_utils_1.assert.isTruthy(inserted.id, 'createOne() record id is missing!');
201
+ test_utils_1.assert.isEqual(inserted.name, name, 'createOne() record name field was not set to ' + name);
86
202
  await this.shutdown(db);
87
203
  },
88
204
  async assertThrowsWhenUpdatingRecordNotFound(connect) {
89
- const db = await connect();
90
- const err = (await test_utils_1.assert.doesThrowAsync(() => db.updateOne('unknown', { id: db.generateId() }, { foo: 'bar' })));
91
- test_utils_2.errorAssert.assertError(err, 'RECORD_NOT_FOUND');
205
+ const db = await connectToDabatase(connect);
206
+ test_utils_1.assert.isFunction(db.generateId, `db.generateId() must be a function that returns a unique identifier.`);
207
+ test_utils_1.assert.isFunction(db.updateOne, 'You must implement updateOne() in your database adapter.');
208
+ await this._assertThrowsExpectedNotFoundOnUpdateOne(db, {
209
+ id: db.generateId(),
210
+ });
211
+ await this._assertThrowsExpectedNotFoundOnUpdateOne(db, { count: 10 });
92
212
  await this.shutdown(db);
93
213
  },
94
214
  async shutdown(db) {
95
- await db.dropDatabase();
96
- await db.close();
215
+ if (db.isConnected()) {
216
+ await db.dropDatabase();
217
+ await db.close();
218
+ }
97
219
  },
98
220
  async assertCanCreateUniqueIndex(connect) {
99
- const db = await connect();
221
+ const db = await connectToDabatase(connect);
100
222
  await db.createUniqueIndex(this.collectionName, ['uniqueField']);
101
223
  let indexes = await db.getUniqueIndexes(this.collectionName);
102
- test_utils_1.assert.isLength(indexes, 1);
103
- test_utils_1.assert.isLength(indexes[0], 1);
104
- test_utils_1.assert.isEqual(indexes[0][0], 'uniqueField');
224
+ test_utils_1.assert.isLength(indexes, 1, 'getUniqueIndexes() did not return the unique index I tried to create!');
225
+ test_utils_1.assert.isLength(indexes[0], 1, 'getUniqueIndexes() needs to return an array of arrays. Each item in the array should be an array of fields that make up the unique index.');
226
+ test_utils_1.assert.isEqual(indexes[0][0].toLowerCase(), 'uniqueField'.toLowerCase(), 'getUniqueIndexes() did not add the expected field to the first unique index.');
105
227
  await db.createUniqueIndex(this.collectionName, ['uniqueField2']);
106
228
  indexes = await db.getUniqueIndexes(this.collectionName);
107
229
  test_utils_1.assert.isLength(indexes, 2);
108
- test_utils_1.assert.isEqual(indexes[1][0], 'uniqueField2');
230
+ test_utils_1.assert.isEqual(indexes[1][0].toLowerCase(), 'uniqueField2'.toLowerCase());
109
231
  await db.createUniqueIndex(this.collectionName, [
110
232
  'uniqueField3',
111
233
  'uniqueField4',
112
234
  ]);
113
235
  indexes = await db.getUniqueIndexes(this.collectionName);
114
236
  test_utils_1.assert.isLength(indexes, 3);
115
- test_utils_1.assert.isEqual(indexes[2][0], 'uniqueField3');
116
- test_utils_1.assert.isEqual(indexes[2][1], 'uniqueField4');
237
+ test_utils_1.assert.isEqual(indexes[2][0].toLowerCase(), 'uniqueField3'.toLowerCase());
238
+ test_utils_1.assert.isEqual(indexes[2][1].toLowerCase(), 'uniqueField4'.toLowerCase());
117
239
  await this.shutdown(db);
118
240
  },
119
241
  async assertHasNoUniqueIndexToStart(connect) {
120
- const db = await connect();
242
+ const db = await connectToDabatase(connect);
121
243
  const indexes = await db.getUniqueIndexes(this.collectionName);
122
- test_utils_1.assert.isLength(indexes, 0);
244
+ test_utils_1.assert.isLength(indexes, 0, "getUniqueIndexes() should return an empty array when there are no unique indexes (primary key indexes don't count!). Also, make sure that dropDatabase() in your adapter clears out any unique indexes you've created.");
123
245
  await this.shutdown(db);
124
246
  },
125
- async assertCanShutdown(connect) {
126
- const db = await connect();
247
+ async assertCanUpsertOne(connect) {
248
+ const db = await connectToDabatase(connect);
127
249
  const id = db.generateId();
128
250
  const created = await db.upsertOne(this.collectionName, { id }, {
129
251
  id,
130
252
  name: 'first',
131
253
  });
132
- test_utils_1.assert.isTruthy(created);
254
+ test_utils_1.assert.isTruthy(created, 'upsertOne() should return the record it created!');
133
255
  test_utils_1.assert.isEqual(created.name, 'first');
134
- test_utils_1.assert.isEqual(created.id, id);
256
+ test_utils_1.assert.isEqual(`${created.id}`, `${id}`, 'ids do not match!');
135
257
  const upserted = await db.upsertOne(this.collectionName, { id }, { name: 'second' });
136
- await db.upsertOne(this.collectionName, { id: db.generateId() }, { name: 'second' });
258
+ const id2 = db.generateId();
259
+ await db.upsertOne(this.collectionName, { id: id2 }, { name: 'second', id: id2 });
137
260
  test_utils_1.assert.isTruthy(upserted);
138
261
  test_utils_1.assert.isEqual(created.id, upserted.id);
139
262
  test_utils_1.assert.isEqual(upserted.name, 'second');
140
263
  const upserted2 = await db.upsertOne(this.collectionName, { id }, { name: 'third' });
141
264
  test_utils_1.assert.isTruthy(upserted2);
142
265
  test_utils_1.assert.isEqual(upserted2.name, 'third');
266
+ const match = await db.findOne(this.collectionName, { id });
267
+ test_utils_1.assert.isEqualDeep(match, upserted2, 'upsertOne() did not update the record!');
143
268
  await this.shutdown(db);
144
269
  },
145
270
  async assertCanDeleteOne(connect) {
146
- const db = await connect();
271
+ const db = await connectToDabatase(connect);
147
272
  await db.create(this.collectionName, [
148
273
  {
149
274
  name: 'a record',
@@ -157,11 +282,11 @@ const databaseAssertUtil = {
157
282
  ]);
158
283
  await db.deleteOne(this.collectionName, { name: 'a record' });
159
284
  const count = await db.count(this.collectionName);
160
- test_utils_1.assert.isEqual(count, 2);
285
+ test_utils_1.assert.isEqual(count, 2, 'deleteOne() did not delete a single record!');
161
286
  await this.shutdown(db);
162
287
  },
163
288
  async assertCanDeleteRecord(connect) {
164
- const db = await connect();
289
+ const db = await connectToDabatase(connect);
165
290
  const created = await db.createOne(this.collectionName, {
166
291
  id: db.generateId(),
167
292
  name: 'first',
@@ -178,7 +303,7 @@ const databaseAssertUtil = {
178
303
  const matchedAfterDelete = await db.findOne(this.collectionName, {
179
304
  id: created.id,
180
305
  });
181
- test_utils_1.assert.isFalsy(matchedAfterDelete);
306
+ test_utils_1.assert.isFalsy(matchedAfterDelete, `Record with the id of ${created.id} was not deleted!`);
182
307
  await db.create(this.collectionName, [
183
308
  {
184
309
  name: 'a record',
@@ -191,11 +316,11 @@ const databaseAssertUtil = {
191
316
  },
192
317
  ]);
193
318
  const manyDeleted = await db.delete(this.collectionName, {});
194
- test_utils_1.assert.isEqual(manyDeleted, 3);
319
+ test_utils_1.assert.isEqual(manyDeleted, 3, `delete() did not return the total recrods deleted!`);
195
320
  await this.shutdown(db);
196
321
  },
197
322
  async assertCanLimitResultsToZero(connect) {
198
- const db = await connect();
323
+ const db = await connectToDabatase(connect);
199
324
  await db.createOne(this.collectionName, {
200
325
  id: db.generateId(),
201
326
  name: 'first',
@@ -216,7 +341,7 @@ const databaseAssertUtil = {
216
341
  await this.shutdown(db);
217
342
  },
218
343
  async assertCanLimitResults(connect) {
219
- const db = await connect();
344
+ const db = await connectToDabatase(connect);
220
345
  await db.createOne(this.collectionName, {
221
346
  id: db.generateId(),
222
347
  name: 'first',
@@ -239,21 +364,22 @@ const databaseAssertUtil = {
239
364
  await this.shutdown(db);
240
365
  },
241
366
  async assertFindOneOnEmptyDatabaseReturnsNull(connect) {
242
- const db = await connect();
367
+ const db = await connectToDabatase(connect);
243
368
  const results = await db.findOne(this.collectionName, { id: '111' });
244
369
  test_utils_1.assert.isFalsy(results);
245
370
  await this.shutdown(db);
246
371
  },
247
372
  async assertEmptyDatabaseReturnsEmptyArray(connect) {
248
- const db = await connect();
373
+ const db = await connectToDabatase(connect);
249
374
  const results = await db.find(this.collectionName, { id: '111' });
250
375
  test_utils_1.assert.isLength(results, 0);
251
376
  await this.shutdown(db);
252
377
  },
253
378
  async assertCanPushOntoArrayValue(connect) {
254
- const db = await connect();
379
+ const db = await connectToDabatase(connect);
255
380
  const inserted = await db.createOne(this.collectionName, {
256
381
  id: db.generateId(),
382
+ name: 'nope',
257
383
  names: ['first'],
258
384
  });
259
385
  const updated = await db.updateOne(this.collectionName, { id: inserted.id }, {
@@ -265,13 +391,72 @@ const databaseAssertUtil = {
265
391
  test_utils_1.assert.isEqualDeep(matched.names, ['first', 'second']);
266
392
  await this.shutdown(db);
267
393
  },
394
+ async assertCanCreateWithObjectField(connect) {
395
+ const db = await connectToDabatase(connect);
396
+ const values = {
397
+ name: 'first',
398
+ target: {
399
+ organizationId: (0, generateId_1.default)(),
400
+ locationId: (0, generateId_1.default)(),
401
+ },
402
+ };
403
+ let created;
404
+ try {
405
+ created = await db.createOne(this.collectionName, values);
406
+ }
407
+ catch (_a) {
408
+ test_utils_1.assert.fail('createOne() tried to create a record with an object field. Make sure the target field handles objects!');
409
+ }
410
+ const matched = await db.findOne(this.collectionName, { id: created.id });
411
+ test_utils_1.assert.isEqualDeep(matched, Object.assign(Object.assign({}, created), { id: matched.id }));
412
+ await this.shutdown(db);
413
+ },
414
+ async canUpdateWithObjectField(connect) {
415
+ const db = await connectToDabatase(connect);
416
+ const values = {
417
+ name: 'first',
418
+ target: {
419
+ organizationId: (0, generateId_1.default)(),
420
+ locationId: (0, generateId_1.default)(),
421
+ },
422
+ };
423
+ const created = await db.createOne(this.collectionName, values);
424
+ const target = {
425
+ organizationId: 'hey',
426
+ locationId: 'there',
427
+ };
428
+ await db.updateOne(this.collectionName, { id: created.id }, {
429
+ target,
430
+ });
431
+ const matched = await db.findOne(this.collectionName, { id: created.id });
432
+ test_utils_1.assert.isEqualDeep(matched.target, target);
433
+ await this.shutdown(db);
434
+ },
435
+ async canUpdateFieldInObjectFieldWithTargettedWhere(connect) {
436
+ const db = await connectToDabatase(connect);
437
+ const target = {
438
+ organizationId: (0, generateId_1.default)(),
439
+ locationId: (0, generateId_1.default)(),
440
+ };
441
+ const values = {
442
+ name: 'first',
443
+ target,
444
+ };
445
+ const created = await db.createOne(this.collectionName, values);
446
+ await db.updateOne(this.collectionName, { id: created.id }, {
447
+ 'target.organizationId': 'oy oy',
448
+ });
449
+ const matched = await db.findOne(this.collectionName, { id: created.id });
450
+ test_utils_1.assert.isEqualDeep(matched.target, Object.assign(Object.assign({}, target), { organizationId: 'oy oy' }));
451
+ await this.shutdown(db);
452
+ },
268
453
  async assertCanCreateMany(connect) {
269
- const db = await connect();
454
+ const db = await connectToDabatase(connect);
270
455
  const values = [
271
- { first: 'ry' },
272
- { first: 'tay' },
273
- { first: 'bill' },
274
- { first: 'bob' },
456
+ { name: 'ry' },
457
+ { name: 'tay' },
458
+ { name: 'bill' },
459
+ { name: 'bob' },
275
460
  ];
276
461
  const results = await db.create(this.collectionName, values);
277
462
  test_utils_1.assert.isLength(results, values.length);
@@ -281,7 +466,7 @@ const databaseAssertUtil = {
281
466
  await this.shutdown(db);
282
467
  },
283
468
  async assertCanUpdateMany(connect) {
284
- const db = await connect();
469
+ const db = await connectToDabatase(connect);
285
470
  await db.create(this.collectionName, [
286
471
  {
287
472
  name: 'one',
@@ -301,41 +486,42 @@ const databaseAssertUtil = {
301
486
  await this.shutdown(db);
302
487
  },
303
488
  async assertCanQueryWithOr(connect) {
304
- const db = await connect();
305
- const id1 = (0, generateId_1.default)();
306
- const id2 = (0, generateId_1.default)();
489
+ const db = await connectToDabatase(connect);
490
+ const name1 = (0, generateId_1.default)();
491
+ const name2 = (0, generateId_1.default)();
492
+ test_utils_1.assert.isFunction(db.create, 'db.create() must be a function that creates records and returns it.');
307
493
  await db.create(this.collectionName, [
308
494
  {
309
495
  isPublic: true,
310
- id: id1,
496
+ name: name1,
311
497
  },
312
498
  {
313
- id: id2,
499
+ name: name2,
314
500
  },
315
501
  ]);
316
- const matches = await db.find(this.collectionName, {
317
- $or: [{ isPublic: true }, { id: id2 }],
318
- });
319
- test_utils_1.assert.isLength(matches, 2);
502
+ await this._assert$orReturnsExpectedTotalRecords(db, [{ isPublic: true }, { name: name2 }], 2);
503
+ await this._assert$orReturnsExpectedTotalRecords(db, [{ isPublic: true }], 1);
320
504
  await this.shutdown(db);
321
505
  },
322
506
  async assertCanUpdate(connect) {
323
- const db = await connect();
507
+ const db = await connectToDabatase(connect);
324
508
  const inserted = await db.createOne(this.collectionName, {
325
509
  id: db.generateId(),
326
510
  name: 'first',
327
511
  });
328
- test_utils_1.assert.isString(inserted.id);
329
- test_utils_1.assert.isEqual(inserted.name, 'first');
512
+ test_utils_1.assert.isTruthy(inserted, 'createOne needs to return the record that was inserted!');
513
+ test_utils_1.assert.isTruthy(inserted.id, `an id must be return with the record from createOne!`);
514
+ test_utils_1.assert.isEqual(inserted.name, 'first', 'name did not set as expected on the inserted record');
330
515
  const updated = await db.updateOne(this.collectionName, { id: inserted.id }, {
331
516
  name: 'updated',
332
517
  });
518
+ test_utils_1.assert.isTruthy(updated, `updateOne needs to return the record that was updated!`);
333
519
  test_utils_1.assert.isEqual(updated.id, inserted.id);
334
520
  test_utils_1.assert.isEqual(updated.name, 'updated');
335
521
  await this.shutdown(db);
336
522
  },
337
523
  async assertCanSortById(connect) {
338
- const db = await connect();
524
+ const db = await connectToDabatase(connect);
339
525
  await db.createOne(this.collectionName, {
340
526
  id: db.generateId(),
341
527
  name: 'first',
@@ -367,26 +553,33 @@ const databaseAssertUtil = {
367
553
  await this.shutdown(db);
368
554
  },
369
555
  async assertCanCreateMultiFieldUniqueIndex(connect) {
370
- const db = await connect();
556
+ const db = await connectToDabatase(connect);
371
557
  await db.createUniqueIndex(this.collectionName, [
372
558
  'uniqueField',
373
559
  'uniqueField2',
374
560
  ]);
375
561
  await db.createOne(this.collectionName, {
562
+ name: (0, generateId_1.default)(),
376
563
  uniqueField: 'hello world',
377
564
  uniqueField2: 'hello again',
378
565
  });
379
566
  await db.createOne(this.collectionName, {
567
+ name: (0, generateId_1.default)(),
380
568
  uniqueField: 'hello world',
381
569
  uniqueField2: 'unique',
382
570
  });
383
571
  let err = (await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, {
572
+ name: (0, generateId_1.default)(),
384
573
  uniqueField: 'hello world',
385
574
  uniqueField2: 'unique',
386
- })));
575
+ }), undefined, `createOne() should throw a SpruceError with the code: DUPLICATE_RECORD`));
576
+ lowerCaseErrorDuplicateFields(err);
387
577
  test_utils_2.errorAssert.assertError(err, 'DUPLICATE_RECORD', {
388
578
  collectionName: this.collectionName,
389
- duplicateFields: ['uniqueField', 'uniqueField2'],
579
+ duplicateFields: [
580
+ 'uniqueField'.toLowerCase(),
581
+ 'uniqueField2'.toLowerCase(),
582
+ ],
390
583
  duplicateValues: ['hello world', 'unique'],
391
584
  action: 'create',
392
585
  });
@@ -394,6 +587,7 @@ const databaseAssertUtil = {
394
587
  uniqueField: 'hello world',
395
588
  uniqueField2: 'unique',
396
589
  }, {
590
+ name: (0, generateId_1.default)(),
397
591
  uniqueField: 'hello world',
398
592
  uniqueField2: 'unique2',
399
593
  });
@@ -401,45 +595,60 @@ const databaseAssertUtil = {
401
595
  uniqueField: 'hello world',
402
596
  uniqueField2: 'unique2',
403
597
  }, {
598
+ name: (0, generateId_1.default)(),
404
599
  uniqueField: 'hello world',
405
600
  uniqueField2: 'hello again',
406
- })));
601
+ }), undefined, 'upsertOne() should throw a SpruceError with the code: DUPLICATE_RECORD'));
602
+ lowerCaseErrorDuplicateFields(err);
407
603
  test_utils_2.errorAssert.assertError(err, 'DUPLICATE_RECORD', {
408
604
  collectionName: this.collectionName,
409
- duplicateFields: ['uniqueField', 'uniqueField2'],
605
+ duplicateFields: [
606
+ 'uniqueField'.toLowerCase(),
607
+ 'uniqueField2'.toLowerCase(),
608
+ ],
410
609
  duplicateValues: ['hello world', 'hello again'],
411
610
  action: 'upsertOne',
412
611
  });
413
612
  await this.shutdown(db);
414
613
  },
415
614
  async assertSettingUniqueIndexViolationThrowsSpruceError(connect) {
416
- const db = await connect();
615
+ const db = await connectToDabatase(connect);
417
616
  await db.createUniqueIndex(this.collectionName, ['randomUniqueField']);
418
617
  await db.createOne(this.collectionName, {
419
618
  uniqueField: 'hello world',
420
619
  randomUniqueField: '1',
620
+ name: (0, generateId_1.default)(),
421
621
  });
422
622
  await db.createOne(this.collectionName, {
423
623
  uniqueField: 'hello world',
424
624
  randomUniqueField: '2',
625
+ name: (0, generateId_1.default)(),
425
626
  });
426
627
  let err = await test_utils_1.assert.doesThrowAsync(() => db.syncUniqueIndexes(this.collectionName, [['uniqueField']]));
427
- test_utils_1.assert.isTrue(err instanceof SpruceError_1.default);
428
628
  test_utils_2.errorAssert.assertError(err, 'DUPLICATE_KEY');
429
629
  await this.shutdown(db);
430
630
  },
431
631
  async assertDuplicateKeyThrowsOnInsert(connect) {
432
- const db = await connect();
632
+ const db = await connectToDabatase(connect);
433
633
  await db.createUniqueIndex(this.collectionName, ['uniqueField']);
434
634
  await db.createOne(this.collectionName, {
435
635
  uniqueField: 'hello world',
636
+ name: (0, generateId_1.default)(),
637
+ });
638
+ let err = await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, {
639
+ uniqueField: 'hello world',
640
+ name: (0, generateId_1.default)(),
641
+ }));
642
+ lowerCaseErrorDuplicateFields(err);
643
+ test_utils_2.errorAssert.assertError(err, 'DUPLICATE_RECORD', {
644
+ duplicateFields: ['uniquefield'],
645
+ duplicateValues: ['hello world'],
646
+ collectionName: this.collectionName,
436
647
  });
437
- let err = await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, { uniqueField: 'hello world' }));
438
- test_utils_1.assert.isTrue(err instanceof SpruceError_1.default);
439
648
  await this.shutdown(db);
440
649
  },
441
650
  async assertSyncingIndexesDoesNotAddAndRemove(connect) {
442
- const db = await connect();
651
+ const db = await connectToDabatase(connect);
443
652
  await db.createUniqueIndex(this.collectionName, ['otherField']);
444
653
  await db.createUniqueIndex(this.collectionName, ['someField']);
445
654
  db.createUniqueIndex = () => {
@@ -455,22 +664,22 @@ const databaseAssertUtil = {
455
664
  await this.shutdown(db);
456
665
  },
457
666
  async assertSyncingUniqueIndexesRemovesExtraIndexes(connect) {
458
- const db = await connect();
667
+ const db = await connectToDabatase(connect);
459
668
  await db.syncUniqueIndexes(this.collectionName, [
460
669
  ['uniqueField'],
461
670
  ['someField'],
462
671
  ['otherField', 'otherField2'],
463
672
  ]);
464
673
  let indexes = await db.getUniqueIndexes(this.collectionName);
465
- test_utils_1.assert.isLength(indexes, 3);
674
+ test_utils_1.assert.isLength(indexes, 3, 'syncUniqueIndexes() should have created 3 indexes.');
466
675
  await db.syncUniqueIndexes(this.collectionName, [['uniqueField']]);
467
676
  indexes = await db.getUniqueIndexes(this.collectionName);
468
677
  test_utils_1.assert.isLength(indexes, 1);
469
- test_utils_1.assert.isEqual(indexes[0][0], 'uniqueField');
678
+ test_utils_1.assert.isEqual(indexes[0][0].toLowerCase(), 'uniquefield');
470
679
  await this.shutdown(db);
471
680
  },
472
681
  async assertSyncingUniqueIndexsSkipsExistingIndexs(connect) {
473
- const db = await connect();
682
+ const db = await connectToDabatase(connect);
474
683
  await db.syncUniqueIndexes(this.collectionName, [['uniqueField']]);
475
684
  let indexes = await db.getUniqueIndexes(this.collectionName);
476
685
  test_utils_1.assert.isLength(indexes, 1);
@@ -484,46 +693,58 @@ const databaseAssertUtil = {
484
693
  await this.shutdown(db);
485
694
  },
486
695
  async assertSyncingUniqueIndexsAddsMissingIndexes(connect) {
487
- const db = await connect();
696
+ const db = await connectToDabatase(connect);
488
697
  await db.syncUniqueIndexes(this.collectionName, [['uniqueField']]);
489
698
  let indexes = await db.getUniqueIndexes(this.collectionName);
490
- test_utils_1.assert.isLength(indexes, 1);
699
+ test_utils_1.assert.isLength(indexes, 1, 'There should be 1 index after syncing.');
491
700
  await db.syncUniqueIndexes(this.collectionName, [['someField']]);
492
701
  indexes = await db.getUniqueIndexes(this.collectionName);
493
- test_utils_1.assert.isLength(indexes, 1);
702
+ test_utils_1.assert.isLength(indexes, 1, 'There should now be 2 indexes after syncing.');
494
703
  await db.syncUniqueIndexes(this.collectionName, [
495
704
  ['uniqueField'],
496
705
  ['someField'],
497
706
  ]);
498
707
  indexes = await db.getUniqueIndexes(this.collectionName);
499
- test_utils_1.assert.isLength(indexes, 2);
708
+ test_utils_1.assert.isLength(indexes, 2, 'There should still be 2 indexes after syncing.');
500
709
  await this.shutdown(db);
501
710
  },
502
711
  async assertCantDropCompoundUniqueIndexThatDoesntExist(connect) {
503
- const db = await connect();
712
+ const db = await connectToDabatase(connect);
504
713
  await db.createUniqueIndex(this.collectionName, [
505
714
  'someField',
506
715
  'someOtherField',
507
716
  ]);
508
717
  const err = await test_utils_1.assert.doesThrowAsync(() => db.dropIndex(this.collectionName, ['uniqueField', 'someOtherField']));
509
- test_utils_2.errorAssert.assertError(err, 'INDEX_NOT_FOUND');
718
+ lowerCaseMissingIndexValues(err);
719
+ test_utils_2.errorAssert.assertError(err, 'INDEX_NOT_FOUND', {
720
+ collectionName: this.collectionName,
721
+ missingIndex: ['uniquefield', 'someotherfield'],
722
+ });
510
723
  await this.shutdown(db);
511
724
  },
512
725
  async assertCantDropIndexWhenNoIndexExists(connect) {
513
- const db = await connect();
726
+ const db = await connectToDabatase(connect);
514
727
  const err = await test_utils_1.assert.doesThrowAsync(() => db.dropIndex(this.collectionName, ['someOtherField']));
515
- test_utils_2.errorAssert.assertError(err, 'INDEX_NOT_FOUND');
728
+ lowerCaseMissingIndexValues(err);
729
+ test_utils_2.errorAssert.assertError(err, 'INDEX_NOT_FOUND', {
730
+ collectionName: this.collectionName,
731
+ missingIndex: ['someotherfield'],
732
+ });
516
733
  await this.shutdown(db);
517
734
  },
518
735
  async assertCantDropUniqueIndexThatDoesntExist(connect) {
519
- const db = await connect();
736
+ const db = await connectToDabatase(connect);
520
737
  await db.createUniqueIndex(this.collectionName, ['someField']);
521
738
  const err = await test_utils_1.assert.doesThrowAsync(() => db.dropIndex(this.collectionName, ['someOtherField']));
522
- test_utils_2.errorAssert.assertError(err, 'INDEX_NOT_FOUND');
739
+ lowerCaseMissingIndexValues(err);
740
+ test_utils_2.errorAssert.assertError(err, 'INDEX_NOT_FOUND', {
741
+ collectionName: this.collectionName,
742
+ missingIndex: ['someotherfield'],
743
+ });
523
744
  await this.shutdown(db);
524
745
  },
525
746
  async assertCanDropCompoundUniqueIndex(connect) {
526
- const db = await connect();
747
+ const db = await connectToDabatase(connect);
527
748
  await db.createUniqueIndex(this.collectionName, ['someField', 'otherField']);
528
749
  await db.dropIndex(this.collectionName, ['someField', 'otherField']);
529
750
  let indexes = await db.getUniqueIndexes(this.collectionName);
@@ -536,11 +757,11 @@ const databaseAssertUtil = {
536
757
  await this.shutdown(db);
537
758
  },
538
759
  async assertCanDropUniqueIndex(connect) {
539
- const db = await connect();
760
+ const db = await connectToDabatase(connect);
540
761
  await db.createUniqueIndex(this.collectionName, ['someField']);
541
762
  await db.dropIndex(this.collectionName, ['someField']);
542
763
  let indexes = await db.getUniqueIndexes(this.collectionName);
543
- test_utils_1.assert.isLength(indexes, 0);
764
+ test_utils_1.assert.isLength(indexes, 0, 'getUniqueIndexes() still returned an index even though it should not have after dropIndex().');
544
765
  await db.createUniqueIndex(this.collectionName, ['someField2']);
545
766
  await db.createUniqueIndex(this.collectionName, ['someField3']);
546
767
  await db.dropIndex(this.collectionName, ['someField3']);
@@ -549,16 +770,12 @@ const databaseAssertUtil = {
549
770
  await this.shutdown(db);
550
771
  },
551
772
  async assertCantCreateUniqueIndexTwice(connect) {
552
- const db = await connect();
553
- await db.createUniqueIndex(this.collectionName, ['uniqueField']);
554
- let indexes = await db.getUniqueIndexes(this.collectionName);
555
- test_utils_1.assert.isLength(indexes, 1);
556
- const err = await test_utils_1.assert.doesThrowAsync(() => db.createUniqueIndex(this.collectionName, ['uniqueField']));
557
- test_utils_2.errorAssert.assertError(err, 'INDEX_EXISTS');
773
+ const db = await connectToDabatase(connect);
774
+ await assertCantCreateUniqueIndexTwice(db, this.collectionName, ['uniqueField'], 1);
558
775
  await this.shutdown(db);
559
776
  },
560
777
  async assertSyncingUniqueIndexesIsRaceProof(connect) {
561
- const db = await connect();
778
+ const db = await connectToDabatase(connect);
562
779
  const syncs = [
563
780
  db.syncUniqueIndexes(this.collectionName, [
564
781
  ['otherField', 'otherField2'],
@@ -595,25 +812,32 @@ const databaseAssertUtil = {
595
812
  await this.shutdown(db);
596
813
  },
597
814
  async assertUniqueIndexBlocksDuplicates(connect) {
598
- const db = await connect();
815
+ const db = await connectToDabatase(connect);
599
816
  await db.createUniqueIndex(this.collectionName, ['uniqueField']);
600
817
  await db.createOne(this.collectionName, {
601
818
  uniqueField: 'hello world',
819
+ name: 'hello world',
602
820
  });
603
- let err = (await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, { uniqueField: 'hello world' })));
821
+ let err = (await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, {
822
+ uniqueField: 'hello world',
823
+ name: 'hello world',
824
+ })));
825
+ lowerCaseStringArray(err, 'duplicateFields');
604
826
  test_utils_2.errorAssert.assertError(err, 'DUPLICATE_RECORD', {
605
827
  collectionName: this.collectionName,
606
- duplicateFields: ['uniqueField'],
828
+ duplicateFields: ['uniquefield'],
607
829
  duplicateValues: ['hello world'],
608
830
  action: 'create',
609
831
  });
610
832
  const created = await db.createOne(this.collectionName, {
611
833
  uniqueField: 'pass',
834
+ name: 'hello world',
612
835
  });
613
836
  err = (await test_utils_1.assert.doesThrowAsync(() => db.updateOne(this.collectionName, { id: created.id }, { uniqueField: 'hello world' })));
837
+ lowerCaseStringArray(err, 'duplicateFields');
614
838
  test_utils_2.errorAssert.assertError(err, 'DUPLICATE_RECORD', {
615
839
  collectionName: this.collectionName,
616
- duplicateFields: ['uniqueField'],
840
+ duplicateFields: ['uniquefield'],
617
841
  duplicateValues: ['hello world'],
618
842
  action: 'updateOne',
619
843
  });
@@ -621,17 +845,20 @@ const databaseAssertUtil = {
621
845
  for (let c = 0; c <= 10; c++) {
622
846
  promises.push(db.createOne(this.collectionName, {
623
847
  uniqueField: 'fast',
848
+ name: 'hello world',
624
849
  }));
625
850
  }
626
851
  err = (await test_utils_1.assert.doesThrowAsync(() => Promise.all(promises)));
852
+ lowerCaseStringArray(err, 'duplicateFields');
627
853
  test_utils_2.errorAssert.assertError(err, 'DUPLICATE_RECORD', {
628
- duplicateFields: ['uniqueField'],
854
+ duplicateFields: ['uniquefield'],
629
855
  duplicateValues: ['fast'],
630
856
  });
631
857
  promises = [];
632
858
  for (let c = 0; c <= 10; c++) {
633
859
  promises.push(db.upsertOne(this.collectionName, { uniqueField: `${c}` }, {
634
860
  uniqueField: 'upsertFast',
861
+ name: 'hello world',
635
862
  }));
636
863
  }
637
864
  err = (await test_utils_1.assert.doesThrowAsync(() => Promise.all(promises)));
@@ -639,12 +866,20 @@ const databaseAssertUtil = {
639
866
  await this.shutdown(db);
640
867
  },
641
868
  async assertCanCreateUniqueIndexOnNestedField(connect) {
642
- const db = await connect();
643
- await db.createUniqueIndex(this.collectionName, [
644
- 'target.organizationId',
645
- 'slug',
646
- ]);
869
+ var _a;
870
+ const db = await connectToDabatase(connect);
871
+ try {
872
+ await db.createUniqueIndex(this.collectionName, [
873
+ 'target.organizationId',
874
+ 'slug',
875
+ ]);
876
+ }
877
+ catch (err) {
878
+ test_utils_1.assert.fail((_a = `Trying to create a unique index on target.organizationId and slug failed.\n\n` +
879
+ err.stack) !== null && _a !== void 0 ? _a : err.message);
880
+ }
647
881
  await db.createOne(this.collectionName, {
882
+ name: (0, generateId_1.default)(),
648
883
  target: {
649
884
  organizationId: 'go!',
650
885
  locationId: null,
@@ -652,6 +887,7 @@ const databaseAssertUtil = {
652
887
  slug: 'a slug',
653
888
  });
654
889
  const err = await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, {
890
+ name: (0, generateId_1.default)(),
655
891
  target: {
656
892
  organizationId: 'go!',
657
893
  locationId: null,
@@ -669,6 +905,7 @@ const databaseAssertUtil = {
669
905
  organizationId: 'go!',
670
906
  },
671
907
  }, {
908
+ name: 'test',
672
909
  target: {
673
910
  organizationId: 'go 2!',
674
911
  locationId: null,
@@ -678,8 +915,11 @@ const databaseAssertUtil = {
678
915
  await this.shutdown(db);
679
916
  },
680
917
  async assertCanPushToArrayOnUpsert(connect) {
681
- const db = await connect();
682
- const record = await db.createOne(this.collectionName, { names: [] });
918
+ const db = await connectToDabatase(connect);
919
+ const record = await db.createOne(this.collectionName, {
920
+ name: 'test',
921
+ names: [],
922
+ });
683
923
  await db.updateOne(this.collectionName, {
684
924
  id: record.id,
685
925
  }, {
@@ -690,23 +930,23 @@ const databaseAssertUtil = {
690
930
  await this.shutdown(db);
691
931
  },
692
932
  async assertCanSearchByRegex(connect) {
693
- const db = await connect();
933
+ const db = await connectToDabatase(connect);
694
934
  await db.create(this.collectionName, [
695
935
  {
696
936
  name: 'first',
697
- subObject: {
937
+ target: {
698
938
  score: 1,
699
939
  },
700
940
  },
701
941
  {
702
942
  name: 'second',
703
- subObject: {
943
+ target: {
704
944
  score: 2,
705
945
  },
706
946
  },
707
947
  {
708
948
  name: 'third',
709
- subObject: {
949
+ target: {
710
950
  score: 2,
711
951
  },
712
952
  },
@@ -716,27 +956,27 @@ const databaseAssertUtil = {
716
956
  {
717
957
  name: 'first',
718
958
  },
719
- ]);
959
+ ], 'Searching by regex failed.!');
720
960
  await this.shutdown(db);
721
961
  },
722
962
  async assertCanReturnOnlySelectFields(connect) {
723
- const db = await connect();
963
+ const db = await connectToDabatase(connect);
724
964
  await db.create(this.collectionName, [
725
965
  {
726
966
  name: 'first',
727
- subObject: {
967
+ target: {
728
968
  score: 1,
729
969
  },
730
970
  },
731
971
  {
732
972
  name: 'second',
733
- subObject: {
973
+ target: {
734
974
  score: 2,
735
975
  },
736
976
  },
737
977
  {
738
978
  name: 'third',
739
- subObject: {
979
+ target: {
740
980
  score: 2,
741
981
  },
742
982
  },
@@ -753,22 +993,26 @@ const databaseAssertUtil = {
753
993
  name: 'third',
754
994
  },
755
995
  ]);
756
- const first = await db.findOne(this.collectionName, {}, { includeFields: ['subObject'] });
996
+ const first = await db.findOne(this.collectionName, {}, { includeFields: ['target'] });
757
997
  test_utils_1.assert.isEqualDeep(first, {
758
- subObject: {
998
+ target: {
759
999
  score: 1,
760
1000
  },
761
1001
  });
762
1002
  await this.shutdown(db);
763
1003
  },
764
- async assertThrowsWithoutDatabaseName(connect) {
765
- const err = await test_utils_1.assert.doesThrowAsync(() => connect(undefined, 'undefined'));
1004
+ async assertThrowsWithBadDatabaseName(connect) {
1005
+ const { db, connectionStringWithRandomBadDatabaseName: connectionStringWithRandomBadDatabaseeName, badDatabaseName, } = await connect();
1006
+ await this.shutdown(db);
1007
+ const err = await test_utils_1.assert.doesThrowAsync(() => connect(connectionStringWithRandomBadDatabaseeName));
766
1008
  test_utils_2.errorAssert.assertError(err, 'INVALID_DATABASE_NAME', {
767
- suppliedName: 'undefined',
1009
+ suppliedName: badDatabaseName,
768
1010
  });
769
1011
  },
770
1012
  async assertThrowsWhenCantConnect(connect) {
771
- const err = await test_utils_1.assert.doesThrowAsync(() => connect('mongodb://localhost:9999'));
1013
+ const { db, scheme } = await connect();
1014
+ await this.shutdown(db);
1015
+ const err = await test_utils_1.assert.doesThrowAsync(() => connect(`${scheme}localhost:9999`));
772
1016
  test_utils_2.errorAssert.assertError(err, 'UNABLE_TO_CONNECT_TO_DB');
773
1017
  },
774
1018
  async assertThrowsWithInvalidConnectionString(connect) {
@@ -776,95 +1020,101 @@ const databaseAssertUtil = {
776
1020
  test_utils_2.errorAssert.assertError(err, 'INVALID_DB_CONNECTION_STRING');
777
1021
  },
778
1022
  async assertKnowsIfConnectionClosed(connect) {
779
- const db = await connect();
780
- test_utils_1.assert.isTrue(db.isConnected());
1023
+ const db = await connectToDabatase(connect);
1024
+ test_utils_1.assert.isFunction(db.isConnected, `db.isConnected() needs to be a function!`);
1025
+ test_utils_1.assert.isTrue(db.isConnected(), 'isConnected() should return true when connected');
781
1026
  await db.close();
782
- test_utils_1.assert.isFalse(db.isConnected());
1027
+ test_utils_1.assert.isFalse(db.isConnected(), 'isConnected() should return false when disconnected (after calling db.close())');
1028
+ await this.shutdown(db);
783
1029
  },
784
1030
  async assertCanQueryPathWithDotSyntax(connect) {
785
- const db = await connect();
1031
+ const db = await connectToDabatase(connect);
786
1032
  await db.create(this.collectionName, [
787
1033
  {
788
1034
  name: 'first',
789
- subObject: {
1035
+ target: {
790
1036
  score: 1,
791
1037
  },
792
1038
  },
793
1039
  {
794
1040
  name: 'second',
795
- subObject: {
1041
+ target: {
796
1042
  score: 2,
797
1043
  },
798
1044
  },
799
1045
  {
800
1046
  name: 'third',
801
- subObject: {
1047
+ target: {
802
1048
  score: 2,
803
1049
  },
804
1050
  },
805
1051
  ]);
806
1052
  const secondMatch = await db.findOne(this.collectionName, {
807
- 'subObject.score': 2,
1053
+ 'target.score': 2,
808
1054
  });
809
- test_utils_1.assert.isTruthy(secondMatch);
1055
+ test_utils_1.assert.isTruthy(secondMatch, 'Could not match on path with dot syntax.');
810
1056
  test_utils_1.assert.isEqual(secondMatch.name, 'second');
811
1057
  await this.shutdown(db);
812
1058
  },
813
1059
  async assertCanQueryByGtLtGteLte(connect) {
814
- const db = await connect();
1060
+ const db = await connectToDabatase(connect);
815
1061
  const created = await db.create(this.collectionName, [
816
1062
  {
817
- position: 1,
1063
+ number: 1,
1064
+ name: (0, generateId_1.default)(),
818
1065
  },
819
1066
  {
820
- position: 2,
1067
+ number: 2,
1068
+ name: (0, generateId_1.default)(),
821
1069
  },
822
1070
  {
823
- position: 3,
1071
+ number: 3,
1072
+ name: (0, generateId_1.default)(),
824
1073
  },
825
1074
  {
826
- position: 4,
1075
+ number: 4,
1076
+ name: (0, generateId_1.default)(),
827
1077
  },
828
1078
  ]);
829
1079
  const gtMatches = await db.find(this.collectionName, {
830
- id: { $gt: created[2].id },
1080
+ number: { $gt: 3 },
831
1081
  });
832
- test_utils_1.assert.isLength(gtMatches, 1);
833
- test_utils_1.assert.isEqual(gtMatches[0].position, 4);
1082
+ test_utils_1.assert.isLength(gtMatches, 1, 'Expected 1 match for $gt: 3');
1083
+ test_utils_1.assert.isEqual(gtMatches[0].number, 4, 'Did not match the expected result for $gt: 3');
834
1084
  const gteMatches = await db.find(this.collectionName, {
835
- id: { $gte: created[2].id },
1085
+ number: { $gte: created[2].number },
836
1086
  });
837
1087
  test_utils_1.assert.isLength(gteMatches, 2);
838
- test_utils_1.assert.isEqual(gteMatches[0].position, 3);
839
- test_utils_1.assert.isEqual(gteMatches[1].position, 4);
1088
+ test_utils_1.assert.isEqual(gteMatches[0].number, 3);
1089
+ test_utils_1.assert.isEqual(gteMatches[1].number, 4);
840
1090
  const ltMatches = await db.find(this.collectionName, {
841
- id: { $lt: created[2].id },
1091
+ number: { $lt: created[2].number },
842
1092
  });
843
1093
  test_utils_1.assert.isLength(ltMatches, 2);
844
- test_utils_1.assert.isEqual(ltMatches[0].position, 1);
845
- test_utils_1.assert.isEqual(ltMatches[1].position, 2);
1094
+ test_utils_1.assert.isEqual(ltMatches[0].number, 1);
1095
+ test_utils_1.assert.isEqual(ltMatches[1].number, 2);
846
1096
  const lteMatches = await db.find(this.collectionName, {
847
- id: { $lte: created[2].id },
1097
+ number: { $lte: created[2].number },
848
1098
  });
849
1099
  test_utils_1.assert.isLength(lteMatches, 3);
850
- test_utils_1.assert.isEqual(lteMatches[0].position, 1);
851
- test_utils_1.assert.isEqual(lteMatches[1].position, 2);
852
- test_utils_1.assert.isEqual(lteMatches[2].position, 3);
1100
+ test_utils_1.assert.isEqual(lteMatches[0].number, 1);
1101
+ test_utils_1.assert.isEqual(lteMatches[1].number, 2);
1102
+ test_utils_1.assert.isEqual(lteMatches[2].number, 3);
853
1103
  await this.shutdown(db);
854
1104
  },
855
1105
  async assertCanFindWithNe(connect) {
856
- const db = await connect();
1106
+ const db = await connectToDabatase(connect);
857
1107
  const record1 = await db.createOne(this.collectionName, {
858
- foo: 'bar',
859
- hello: 'world',
1108
+ name: 'bar',
1109
+ someField: 'world',
860
1110
  });
861
1111
  await db.createOne(this.collectionName, {
862
- foo: 'bar2',
863
- hello: 'world',
1112
+ name: 'bar2',
1113
+ someField: 'world',
864
1114
  });
865
1115
  await db.createOne(this.collectionName, {
866
- foo: 'bar3',
867
- hello: 'planet',
1116
+ name: 'bar3',
1117
+ someField: 'planet',
868
1118
  });
869
1119
  const query = { id: { $ne: record1.id } };
870
1120
  const results = await db.find(this.collectionName, query);
@@ -872,37 +1122,57 @@ const databaseAssertUtil = {
872
1122
  await this.shutdown(db);
873
1123
  },
874
1124
  async assertCanFindWithIn(connect) {
875
- const db = await connect();
1125
+ const db = await connectToDabatase(connect);
876
1126
  const record1 = await db.createOne(this.collectionName, {
877
- foo: 'bar',
878
- hello: 'world',
1127
+ name: 'bar',
1128
+ otherField: 'world',
879
1129
  });
880
1130
  const record2 = await db.createOne(this.collectionName, {
881
- foo: 'bar2',
882
- hello: 'world',
1131
+ name: 'bar2',
1132
+ otherField: 'world',
883
1133
  });
884
1134
  const record3 = await db.createOne(this.collectionName, {
885
- foo: 'bar3',
886
- hello: 'planet',
1135
+ name: 'bar3',
1136
+ otherField: 'planet',
887
1137
  });
888
1138
  const query = { id: { $in: [record1.id, record2.id, record3.id] } };
889
1139
  const results = await db.find(this.collectionName, query);
890
1140
  test_utils_1.assert.isLength(results, 3);
891
1141
  await this.shutdown(db);
892
1142
  },
1143
+ async assertCanFindWithBooleanField(connect) {
1144
+ const db = await connectToDabatase(connect);
1145
+ const first = await db.createOne(this.collectionName, {
1146
+ name: (0, generateId_1.default)(),
1147
+ isPublic: true,
1148
+ });
1149
+ const second = await db.createOne(this.collectionName, {
1150
+ name: (0, generateId_1.default)(),
1151
+ isPublic: false,
1152
+ });
1153
+ const firstMatch = await db.findOne(this.collectionName, {
1154
+ isPublic: true,
1155
+ });
1156
+ test_utils_1.assert.isEqualDeep(firstMatch, first, `Searching where boolean field is true failed.`);
1157
+ const secondMatch = await db.findOne(this.collectionName, {
1158
+ isPublic: false,
1159
+ });
1160
+ test_utils_1.assert.isEqualDeep(secondMatch, second, `Searching where boolean field is false failed.`);
1161
+ await this.shutdown(db);
1162
+ },
893
1163
  async assertCanCountOnId(connect) {
894
- const db = await connect();
1164
+ const db = await connectToDabatase(connect);
895
1165
  const first = await db.createOne(this.collectionName, {
896
- foo: 'bar',
897
- hello: 'world',
1166
+ name: 'bar',
1167
+ otherField: 'world',
898
1168
  });
899
1169
  const second = await db.createOne(this.collectionName, {
900
- foo: 'bar2',
901
- hello: 'world',
1170
+ name: 'bar2',
1171
+ otherField: 'world',
902
1172
  });
903
1173
  const third = await db.createOne(this.collectionName, {
904
- foo: 'bar3',
905
- hello: 'planet',
1174
+ name: 'bar3',
1175
+ otherField: 'planet',
906
1176
  });
907
1177
  const countFirst = await db.count(this.collectionName, { id: first.id });
908
1178
  const countSecond = await db.count(this.collectionName, {
@@ -917,76 +1187,93 @@ const databaseAssertUtil = {
917
1187
  await this.shutdown(db);
918
1188
  },
919
1189
  async assertCanCount(connect) {
920
- const db = await connect();
921
- await db.createOne(this.collectionName, { foo: 'bar', hello: 'world' });
922
- await db.createOne(this.collectionName, { foo: 'bar2', hello: 'world' });
923
- await db.createOne(this.collectionName, { foo: 'bar3', hello: 'planet' });
1190
+ const db = await connectToDabatase(connect);
1191
+ await db.createOne(this.collectionName, {
1192
+ name: 'bar',
1193
+ otherField: 'world',
1194
+ });
1195
+ await db.createOne(this.collectionName, {
1196
+ name: 'bar2',
1197
+ otherField: 'world',
1198
+ });
1199
+ await db.createOne(this.collectionName, {
1200
+ name: 'bar3',
1201
+ otherField: 'planet',
1202
+ });
924
1203
  const countAll = await db.count(this.collectionName);
925
1204
  test_utils_1.assert.isEqual(countAll, 3);
926
1205
  await this.shutdown(db);
927
1206
  },
928
1207
  async assertCanUpsertNull(connect) {
929
- const db = await connect();
1208
+ var _a, _b;
1209
+ const db = await connectToDabatase(connect);
930
1210
  const createdUndefined = await db.upsertOne(this.collectionName, {
931
1211
  undefinedField: undefined,
932
1212
  }, {
933
1213
  undefinedField: undefined,
1214
+ name: (0, generateId_1.default)(),
934
1215
  });
935
1216
  test_utils_1.assert.isTruthy(createdUndefined);
936
- test_utils_1.assert.isNull(createdUndefined.undefinedField);
1217
+ test_utils_1.assert.isTrue(createdUndefined.undefinedField === null ||
1218
+ createdUndefined.undefinedfield === null);
937
1219
  const createdNull = await db.upsertOne(this.collectionName, {
938
1220
  nullField: null,
1221
+ name: '234',
939
1222
  }, {
940
1223
  nullField: null,
1224
+ name: (0, generateId_1.default)(),
941
1225
  });
942
1226
  test_utils_1.assert.isTruthy(createdNull);
943
1227
  let all = await db.find(this.collectionName);
944
- test_utils_1.assert.isLength(all, 2);
1228
+ test_utils_1.assert.isLength(all, 2, 'I expected to find 2 records after 2 upserts.');
945
1229
  const updatedUndefined = await db.upsertOne(this.collectionName, {
946
1230
  undefinedField: undefined,
947
1231
  }, { undefinedField: 'now defined' });
948
1232
  test_utils_1.assert.isEqual(updatedUndefined.id, createdUndefined.id);
949
- test_utils_1.assert.isEqual(updatedUndefined.undefinedField, 'now defined');
1233
+ test_utils_1.assert.isEqual((_a = updatedUndefined.undefinedField) !== null && _a !== void 0 ? _a : updatedUndefined.undefinedfield, 'now defined');
950
1234
  const updatedNull = await db.upsertOne(this.collectionName, {
951
1235
  nullField: null,
952
1236
  }, { nullField: 'now defined' });
953
1237
  test_utils_1.assert.isEqual(updatedNull.id, createdNull.id);
954
- test_utils_1.assert.isEqual(updatedNull.nullField, 'now defined');
1238
+ test_utils_1.assert.isEqual((_b = updatedNull.nullField) !== null && _b !== void 0 ? _b : updatedNull.nullfield, 'now defined', 'nullField should have upserted to "now defined"');
955
1239
  all = await db.find(this.collectionName);
956
1240
  test_utils_1.assert.isLength(all, 2);
957
1241
  await this.shutdown(db);
958
1242
  },
959
1243
  async assertCanSaveAndGetNullAndUndefined(connect) {
960
- const db = await connect();
1244
+ var _a, _b;
1245
+ const db = await connectToDabatase(connect);
961
1246
  const created = await db.createOne(this.collectionName, {
962
1247
  undefinedField: undefined,
963
1248
  nullField: null,
1249
+ name: 'hey',
964
1250
  });
965
- test_utils_1.assert.isNull(created.undefinedField);
966
- test_utils_1.assert.isNull(created.nullField);
1251
+ test_utils_1.assert.isTrue(created.undefinedField === null || created.undefinedfield === null, 'undefinedField should be null');
1252
+ test_utils_1.assert.isTrue(created.nullField === null || created.nullfield === null, 'nullField should be null');
967
1253
  const matchedUndefined = await db.findOne(this.collectionName, {
968
1254
  undefinedField: undefined,
969
1255
  });
970
1256
  test_utils_1.assert.isTruthy(matchedUndefined);
971
- test_utils_1.assert.isNull(matchedUndefined.undefinedField);
1257
+ test_utils_1.assert.isTrue(matchedUndefined.undefinedField === null ||
1258
+ matchedUndefined.undefinedfield === null, 'findOne should return null for undefinedField');
972
1259
  const matchedNull = await db.findOne(this.collectionName, {
973
1260
  nullField: null,
974
1261
  });
975
1262
  test_utils_1.assert.isTruthy(matchedNull);
976
- test_utils_1.assert.isNull(matchedNull.nullField);
1263
+ test_utils_1.assert.isTrue(matchedNull.nullField === null || matchedNull.nullfield === null, 'findOne should return null for nullField');
977
1264
  const updatedUndefined = await db.updateOne(this.collectionName, {
978
1265
  undefinedField: undefined,
979
1266
  }, { undefinedField: 'now defined' });
980
1267
  test_utils_1.assert.isEqual(updatedUndefined.id, created.id);
981
- test_utils_1.assert.isEqual(updatedUndefined.undefinedField, 'now defined');
1268
+ test_utils_1.assert.isEqual((_a = updatedUndefined.undefinedField) !== null && _a !== void 0 ? _a : updatedUndefined.undefinedfield, 'now defined');
982
1269
  const updatedNull = await db.updateOne(this.collectionName, {
983
1270
  nullField: null,
984
1271
  }, { nullField: 'now defined' });
985
- test_utils_1.assert.isEqual(updatedNull.nullField, 'now defined');
1272
+ test_utils_1.assert.isEqual((_b = updatedNull.nullField) !== null && _b !== void 0 ? _b : updatedNull.nullfield, 'now defined');
986
1273
  await this.shutdown(db);
987
1274
  },
988
1275
  async assertSyncIndexesDoesNotRemoveExisting(connect) {
989
- const db = await connect();
1276
+ const db = await connectToDabatase(connect);
990
1277
  await db.createIndex(this.collectionName, ['otherField']);
991
1278
  await db.createIndex(this.collectionName, ['someField']);
992
1279
  db.createIndex = () => {
@@ -999,27 +1286,27 @@ const databaseAssertUtil = {
999
1286
  await this.shutdown(db);
1000
1287
  },
1001
1288
  async assertSyncIndexesRemovesExtraIndexes(connect) {
1002
- const db = await connect();
1289
+ const db = await connectToDabatase(connect);
1003
1290
  await db.syncIndexes(this.collectionName, [
1004
- ['field'],
1291
+ ['name'],
1005
1292
  ['someField'],
1006
1293
  ['otherField', 'otherField2'],
1007
1294
  ]);
1008
1295
  let indexes = await this._getFilteredIndexes(db);
1009
1296
  test_utils_1.assert.isLength(indexes, 3);
1010
- await db.syncIndexes(this.collectionName, [['field']]);
1297
+ await db.syncIndexes(this.collectionName, [['name']]);
1011
1298
  indexes = await this._getFilteredIndexes(db);
1012
1299
  test_utils_1.assert.isLength(indexes, 1);
1013
- test_utils_1.assert.isEqual(indexes[0][0], 'field');
1300
+ test_utils_1.assert.isEqual(indexes[0][0], 'name');
1014
1301
  await this.shutdown(db);
1015
1302
  },
1016
1303
  async assertSyncIndexesSkipsExisting(connect) {
1017
- const db = await connect();
1018
- await db.syncIndexes(this.collectionName, [['field']]);
1304
+ const db = await connectToDabatase(connect);
1305
+ await db.syncIndexes(this.collectionName, [['name']]);
1019
1306
  let indexes = await this._getFilteredIndexes(db);
1020
1307
  test_utils_1.assert.isLength(indexes, 1);
1021
1308
  await db.syncIndexes(this.collectionName, [
1022
- ['field'],
1309
+ ['name'],
1023
1310
  ['someField'],
1024
1311
  ['otherField', 'otherField2'],
1025
1312
  ]);
@@ -1028,14 +1315,17 @@ const databaseAssertUtil = {
1028
1315
  await this.shutdown(db);
1029
1316
  },
1030
1317
  async assertCantDropCompoundIndexThatDoesNotExist(connect) {
1031
- const db = await connect();
1318
+ const db = await connectToDabatase(connect);
1032
1319
  await db.createIndex(this.collectionName, ['someField', 'someOtherField']);
1033
1320
  const err = await test_utils_1.assert.doesThrowAsync(() => db.dropIndex(this.collectionName, ['uniqueField', 'someOtherField']));
1034
- test_utils_2.errorAssert.assertError(err, 'INDEX_NOT_FOUND');
1321
+ test_utils_2.errorAssert.assertError(err, 'INDEX_NOT_FOUND', {
1322
+ collectionName: this.collectionName,
1323
+ missingIndex: ['uniqueField', 'someOtherField'],
1324
+ });
1035
1325
  await this.shutdown(db);
1036
1326
  },
1037
1327
  async assertCanDropCompoundIndex(connect) {
1038
- const db = await connect();
1328
+ const db = await connectToDabatase(connect);
1039
1329
  await db.createIndex(this.collectionName, ['someField', 'otherField']);
1040
1330
  await db.dropIndex(this.collectionName, ['someField', 'otherField']);
1041
1331
  let indexes = await this._getFilteredIndexes(db);
@@ -1048,7 +1338,7 @@ const databaseAssertUtil = {
1048
1338
  await this.shutdown(db);
1049
1339
  },
1050
1340
  async assertCanDropIndex(connect) {
1051
- const db = await connect();
1341
+ const db = await connectToDabatase(connect);
1052
1342
  await db.createIndex(this.collectionName, ['someField']);
1053
1343
  await db.dropIndex(this.collectionName, ['someField']);
1054
1344
  let indexes = await this._getFilteredIndexes(db);
@@ -1060,49 +1350,61 @@ const databaseAssertUtil = {
1060
1350
  test_utils_1.assert.isLength(indexes, 1);
1061
1351
  await this.shutdown(db);
1062
1352
  },
1063
- async assertCanCreateMultiFieldIndex(connect, fields) {
1064
- const db = await connect();
1065
- await db.createIndex(this.collectionName, fields);
1066
- let indexes = await this._getFilteredIndexes(db);
1067
- test_utils_1.assert.isLength(indexes, 1);
1068
- test_utils_1.assert.isEqualDeep(indexes, [fields]);
1069
- await this.shutdown(db);
1353
+ async assertCanCreateMultiFieldIndex(connect) {
1354
+ await this._assertCanCreateMultiFieldIndex(connect, [
1355
+ 'otherField',
1356
+ 'someField',
1357
+ ]);
1358
+ await this._assertCanCreateMultiFieldIndex(connect, [
1359
+ 'someField',
1360
+ 'otherField',
1361
+ ]);
1362
+ await this._assertCanCreateMultiFieldIndex(connect, [
1363
+ 'otherField',
1364
+ 'otherField2',
1365
+ ]);
1366
+ await this._assertCanCreateMultiFieldIndex(connect, [
1367
+ 'otherField2',
1368
+ 'otherField',
1369
+ ]);
1070
1370
  },
1071
1371
  async assertCantCreateSameIndexTwice(connect) {
1072
- const db = await connect();
1073
- await db.createIndex(this.collectionName, ['field']);
1372
+ const db = await connectToDabatase(connect);
1373
+ await db.createIndex(this.collectionName, ['name']);
1074
1374
  let indexes = await this._getFilteredIndexes(db);
1075
1375
  test_utils_1.assert.isLength(indexes, 1);
1076
- const err = await test_utils_1.assert.doesThrowAsync(() => db.createIndex(this.collectionName, ['field']));
1376
+ const err = await test_utils_1.assert.doesThrowAsync(() => db.createIndex(this.collectionName, ['name']));
1077
1377
  test_utils_2.errorAssert.assertError(err, 'INDEX_EXISTS');
1078
1378
  await this.shutdown(db);
1079
1379
  },
1080
1380
  async assertCanCreateIndex(connect) {
1081
- const db = await connect();
1082
- await db.createIndex(this.collectionName, ['field']);
1381
+ const db = await connectToDabatase(connect);
1382
+ await db.createIndex(this.collectionName, ['uniqueField']);
1083
1383
  let indexes = await this._getFilteredIndexes(db);
1084
- test_utils_1.assert.isLength(indexes, 1);
1085
- test_utils_1.assert.isLength(indexes[0], 1);
1086
- test_utils_1.assert.isEqual(indexes[0][0], 'field');
1087
- await db.createIndex(this.collectionName, ['field2']);
1384
+ test_utils_1.assert.isArray(indexes, 'getIndexes() should return an array!');
1385
+ test_utils_1.assert.isLength(indexes, 1, 'getIndexes() should return the one index that was created!');
1386
+ test_utils_1.assert.isLength(indexes[0], 1, 'getIndexes() should return an array of arrays! It should be returing the first index I created!');
1387
+ test_utils_1.assert.isEqual(indexes[0][0].toLowerCase(), 'uniqueField'.toLowerCase());
1388
+ await db.createIndex(this.collectionName, ['uniqueField2']);
1088
1389
  indexes = await this._getFilteredIndexes(db);
1089
1390
  test_utils_1.assert.isLength(indexes, 2);
1090
- test_utils_1.assert.isEqual(indexes[1][0], 'field2');
1091
- await db.createIndex(this.collectionName, ['field3', 'field4']);
1391
+ test_utils_1.assert.isEqual(indexes[1][0].toLowerCase(), 'uniqueField2'.toLowerCase());
1392
+ await db.createIndex(this.collectionName, ['uniqueField3', 'uniqueField4']);
1092
1393
  indexes = await this._getFilteredIndexes(db);
1093
1394
  test_utils_1.assert.isLength(indexes, 3);
1094
- test_utils_1.assert.isEqual(indexes[2][0], 'field3');
1095
- test_utils_1.assert.isEqual(indexes[2][1], 'field4');
1395
+ test_utils_1.assert.isEqual(indexes[2][0].toLowerCase(), 'uniqueField3'.toLowerCase());
1396
+ test_utils_1.assert.isEqual(indexes[2][1].toLowerCase(), 'uniqueField4'.toLowerCase());
1096
1397
  await this.shutdown(db);
1097
1398
  },
1098
1399
  async assertHasNoIndexToStart(connect) {
1099
- const db = await connect();
1400
+ const db = await connectToDabatase(connect);
1100
1401
  const indexes = await db.getIndexes(this.collectionName);
1101
1402
  test_utils_1.assert.isLength(indexes, 0);
1102
1403
  await this.shutdown(db);
1103
1404
  },
1104
1405
  async assertNestedFieldIndexUpdates(connect) {
1105
- const db = await connect();
1406
+ var _a;
1407
+ const db = await connectToDabatase(connect);
1106
1408
  await db.createUniqueIndex(this.collectionName, [
1107
1409
  'target.organizationId',
1108
1410
  'slug',
@@ -1112,6 +1414,7 @@ const databaseAssertUtil = {
1112
1414
  organizationId: 'go!',
1113
1415
  },
1114
1416
  aNonIndexedField: true,
1417
+ name: 'squirrel',
1115
1418
  slug: 'a slug',
1116
1419
  });
1117
1420
  const updated = await db.updateOne(this.collectionName, { id: results.id }, {
@@ -1121,11 +1424,11 @@ const databaseAssertUtil = {
1121
1424
  aNonIndexedField: false,
1122
1425
  slug: 'a slug',
1123
1426
  });
1124
- test_utils_1.assert.isEqual(updated.aNonIndexedField, false);
1427
+ test_utils_1.assert.isEqual((_a = updated.aNonIndexedField) !== null && _a !== void 0 ? _a : updated.anonindexedfield, false);
1125
1428
  await this.shutdown(db);
1126
1429
  },
1127
1430
  async assertUpsertWithUniqueIndex(connect) {
1128
- const db = await connect();
1431
+ const db = await connectToDabatase(connect);
1129
1432
  await db.syncUniqueIndexes(this.collectionName, [
1130
1433
  ['name', 'target.organizationId'],
1131
1434
  ['slug', 'target.organizationId'],
@@ -1149,7 +1452,7 @@ const databaseAssertUtil = {
1149
1452
  await this.shutdown(db);
1150
1453
  },
1151
1454
  async assertSyncIndexesHandlesRaceConditions(connect) {
1152
- const db = await connect();
1455
+ const db = await connectToDabatase(connect);
1153
1456
  const syncs = [
1154
1457
  db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1155
1458
  db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
@@ -1166,34 +1469,87 @@ const databaseAssertUtil = {
1166
1469
  await this.shutdown(db);
1167
1470
  },
1168
1471
  async assertDuplicateFieldsWithMultipleUniqueIndexesWorkAsExpected(connect) {
1169
- const db = await connect();
1170
- await db.createUniqueIndex(this.collectionName, ['uniqueField1']);
1472
+ const db = await connectToDabatase(connect);
1473
+ await db.createUniqueIndex(this.collectionName, ['uniqueField']);
1171
1474
  await db.createUniqueIndex(this.collectionName, ['uniqueField2']);
1172
1475
  await db.createOne(this.collectionName, {
1173
- uniqueField1: 'unique field 1',
1476
+ name: (0, generateId_1.default)(),
1477
+ uniqueField: 'unique field 1',
1174
1478
  uniqueField2: 'unique field 2',
1175
1479
  });
1176
1480
  let err = await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, {
1177
- uniqueField1: 'unique field 1',
1481
+ name: (0, generateId_1.default)(),
1482
+ uniqueField: 'unique field 1',
1178
1483
  uniqueField2: 'unique field 2',
1179
1484
  }));
1485
+ lowerCaseStringArray(err, 'duplicateFields');
1180
1486
  test_utils_2.errorAssert.assertError(err, 'DUPLICATE_RECORD', {
1181
1487
  collectionName: this.collectionName,
1182
- duplicateFields: ['uniqueField1'],
1488
+ duplicateFields: ['uniquefield'],
1183
1489
  duplicateValues: ['unique field 1'],
1184
1490
  action: 'create',
1185
1491
  });
1186
1492
  err = await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, {
1187
- uniqueField1: 'unique field 1.0',
1493
+ name: (0, generateId_1.default)(),
1494
+ uniqueField: 'unique field 1.0',
1188
1495
  uniqueField2: 'unique field 2',
1189
1496
  }));
1497
+ lowerCaseStringArray(err, 'duplicateFields');
1190
1498
  test_utils_2.errorAssert.assertError(err, 'DUPLICATE_RECORD', {
1191
1499
  collectionName: this.collectionName,
1192
- duplicateFields: ['uniqueField2'],
1500
+ duplicateFields: ['uniquefield2'],
1193
1501
  duplicateValues: ['unique field 2'],
1194
1502
  action: 'create',
1195
1503
  });
1196
1504
  await this.shutdown(db);
1197
1505
  },
1506
+ async _assertThrowsExpectedNotFoundOnUpdateOne(db, query) {
1507
+ const err = (await test_utils_1.assert.doesThrowAsync(() => db.updateOne(this.collectionName, query, { name: 'bar' }), undefined, `updateOne should throw a RECORD_NOT_FOUND when updating a record that cannot be found!`));
1508
+ test_utils_2.errorAssert.assertError(err, 'RECORD_NOT_FOUND', {
1509
+ query,
1510
+ });
1511
+ },
1512
+ async _assert$orReturnsExpectedTotalRecords(db, $or, expected) {
1513
+ const matches = await db.find(this.collectionName, {
1514
+ $or,
1515
+ });
1516
+ test_utils_1.assert.isLength(matches, expected);
1517
+ },
1518
+ async _assertCanCreateMultiFieldIndex(connect, fields) {
1519
+ const db = await connectToDabatase(connect);
1520
+ await db.createIndex(this.collectionName, fields);
1521
+ const indexes = await this._getFilteredIndexes(db);
1522
+ test_utils_1.assert.isLength(indexes, 1, `getIndexes() should return the index it created. I expected 1 but got ${indexes.length}. Also, make sure you clear the indexes when resetting the database!`);
1523
+ test_utils_1.assert.isEqualDeep(indexes[0].map((i) => i.toLowerCase()), fields.map((f) => f.toLowerCase()));
1524
+ await this.shutdown(db);
1525
+ },
1198
1526
  };
1199
1527
  exports.default = databaseAssertUtil;
1528
+ async function assertCantCreateUniqueIndexTwice(db, collectionName, fields, expectedTotalUniqueIndexes) {
1529
+ await db.createUniqueIndex(collectionName, fields);
1530
+ let indexes = await db.getUniqueIndexes(collectionName);
1531
+ test_utils_1.assert.isLength(indexes, expectedTotalUniqueIndexes, 'getUniqueIndexes() should return total unique indexs');
1532
+ const err = await test_utils_1.assert.doesThrowAsync(() => db.createUniqueIndex(collectionName, ['uniqueField']), undefined, 'createUniqueIndex() should throw a DataStoreError({code: "INDEX_EXISTS"}) error.');
1533
+ test_utils_2.errorAssert.assertError(err, 'INDEX_EXISTS', {
1534
+ collectionName,
1535
+ index: ['uniqueField'],
1536
+ });
1537
+ }
1538
+ function lowerCaseErrorDuplicateFields(err) {
1539
+ lowerCaseStringArray(err, 'duplicateFields');
1540
+ }
1541
+ function lowerCaseMissingIndexValues(err) {
1542
+ lowerCaseStringArray(err, 'missingIndex');
1543
+ }
1544
+ function lowerCaseStringArray(err, key) {
1545
+ var _a, _b;
1546
+ //@ts-ignore
1547
+ if ((_b = (_a = err.options) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.map) {
1548
+ //@ts-ignore
1549
+ err.options[key] = err.options[key].map((i) => i.toLowerCase());
1550
+ }
1551
+ }
1552
+ async function connectToDabatase(connect) {
1553
+ const { db } = await connect();
1554
+ return db;
1555
+ }