Skip to content

Commit d643407

Browse files
committed
test(NODE-7198): fix TS errors, add TODOs
1 parent 65e7f0f commit d643407

2 files changed

Lines changed: 103 additions & 94 deletions

File tree

test/integration/crud/insert.test.ts

Lines changed: 65 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { format as f } from 'node:util';
21
import * as Script from 'node:vm';
32

43
import { expect } from 'chai';
@@ -10,6 +9,7 @@ import {
109
Code,
1110
type Collection,
1211
DBRef,
12+
type Document,
1313
Double,
1414
Long,
1515
MaxKey,
@@ -22,47 +22,13 @@ import {
2222
ReturnDocument,
2323
Timestamp
2424
} from '../../../src';
25+
import { toISODate } from '../../tools/utils';
2526
import { assert as test, ignoreNsNotFound, setupDatabase } from '../shared';
2627

27-
/**
28-
* Module for parsing an ISO 8601 formatted string into a Date object.
29-
*/
30-
const ISODate = function (str: string | Date): Date {
31-
let match: RegExpMatchArray | null;
32-
33-
if (str instanceof Date) return str;
34-
else if (
35-
(match = str.match(
36-
/^(\d{4})(-(\d{2})(-(\d{2})(T(\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|((\+|-)(\d{2}):(\d{2}))))?)?)?$/
37-
))
38-
) {
39-
const date = new Date();
40-
date.setUTCFullYear(Number(match[1]));
41-
date.setUTCMonth(Number(match[3]) - 1 || 0);
42-
date.setUTCDate(Number(match[5]) || 0);
43-
date.setUTCHours(Number(match[7]) || 0);
44-
date.setUTCMinutes(Number(match[8]) || 0);
45-
date.setUTCSeconds(Number(match[10]) || 0);
46-
date.setUTCMilliseconds(Number('.' + match[12]) * 1000 || 0);
47-
48-
if (match[13] && match[13] !== 'Z') {
49-
let h = Number(match[16]) || 0,
50-
m = Number(match[17]) || 0;
51-
52-
h *= 3600000;
53-
m *= 60000;
54-
55-
let offset = h + m;
56-
if (match[15] === '+') offset = -offset;
57-
58-
new Date(date.valueOf() + offset);
59-
}
60-
61-
return date;
62-
} else throw new Error('Invalid ISO 8601 date given.', { cause: __filename });
63-
};
28+
// eslint-disable-next-line @typescript-eslint/no-empty-function
29+
const noop = () => {};
6430

65-
describe.only('crud - insert', function () {
31+
describe('crud - insert', function () {
6632
let client: MongoClient;
6733

6834
before(async function () {
@@ -102,22 +68,23 @@ describe.only('crud - insert', function () {
10268
});
10369
});
10470

105-
it('should correctly execute Collection.prototype.insertOne', async function () {
106-
const configuration = this.configuration;
107-
let url = configuration.url();
108-
url =
109-
url.indexOf('?') !== -1
110-
? f('%s&%s', url, 'maxPoolSize=100')
111-
: f('%s?%s', url, 'maxPoolSize=100');
112-
113-
const client = configuration.newClient(url);
114-
await client.connect();
115-
const db = client.db(configuration.db);
116-
117-
const r = await db.collection('insertOne').insertOne({ a: 1 });
118-
expect(r).property('insertedId').to.exist;
119-
await client.close();
120-
});
71+
// TODO(NODE-7219): remove as it duplicates "should correctly perform single insert"
72+
// it('should correctly execute Collection.prototype.insertOne', async function () {
73+
// const configuration = this.configuration;
74+
// let url = configuration.url();
75+
// url =
76+
// url.indexOf('?') !== -1
77+
// ? f('%s&%s', url, 'maxPoolSize=100')
78+
// : f('%s?%s', url, 'maxPoolSize=100');
79+
//
80+
// const client = configuration.newClient(url);
81+
// await client.connect();
82+
// const db = client.db(configuration.db);
83+
//
84+
// const r = await db.collection('insertOne').insertOne({ a: 1 });
85+
// expect(r).property('insertedId').to.exist;
86+
// await client.close();
87+
// });
12188

12289
it('rejects when insertMany is passed a non array object', async function () {
12390
const db = client.db();
@@ -135,10 +102,11 @@ describe.only('crud - insert', function () {
135102
const configuration = this.configuration;
136103
const db = client.db(configuration.db);
137104
const collection = db.collection('shouldCorrectlyPerformSingleInsert');
138-
await collection.insertOne({ a: 1 }, configuration.writeConcernMax());
105+
const r = await collection.insertOne({ a: 1 }, configuration.writeConcernMax());
106+
expect(r).to.have.property('insertedId');
139107

140-
const item = await collection.findOne();
141-
test.equal(1, item.a);
108+
const item = await collection.findOne({ _id: r.insertedId });
109+
expect(item.a).to.equal(1);
142110
});
143111

144112
it('insertMany returns the insertedIds and we can look up the documents', async function () {
@@ -232,10 +200,7 @@ describe.only('crud - insert', function () {
232200
test.equal(date.toString(), doc.date.toString());
233201
test.equal(date.getTime(), doc.date.getTime());
234202
test.equal(motherOfAllDocuments.oid.toHexString(), doc.oid.toHexString());
235-
test.equal(
236-
motherOfAllDocuments.binary.value().toString('hex'),
237-
doc.binary.value().toString('hex')
238-
);
203+
test.equal(motherOfAllDocuments.binary.toString('hex'), doc.binary.value().toString('hex'));
239204

240205
test.equal(motherOfAllDocuments.int, doc.int);
241206
test.equal(motherOfAllDocuments.long, doc.long);
@@ -245,7 +210,7 @@ describe.only('crud - insert', function () {
245210
test.equal(motherOfAllDocuments.where.code, doc.where.code);
246211
test.equal(motherOfAllDocuments.where.scope['i'], doc.where.scope.i);
247212

248-
test.equal(motherOfAllDocuments.dbref.namespace, doc.dbref.namespace);
213+
test.equal(motherOfAllDocuments.dbref.collection, doc.dbref.collection);
249214
test.equal(motherOfAllDocuments.dbref.oid.toHexString(), doc.dbref.oid.toHexString());
250215
test.equal(motherOfAllDocuments.dbref.db, doc.dbref.db);
251216
});
@@ -403,7 +368,9 @@ describe.only('crud - insert', function () {
403368
it('should correctly insert document with UUID', async function () {
404369
const configuration = this.configuration;
405370
const db = client.db(configuration.db);
406-
const collection = db.collection<{ _id: string; field: string }>('insert_doc_with_uuid');
371+
const collection = db.collection<{ _id: string | Binary; field: string }>(
372+
'insert_doc_with_uuid'
373+
);
407374

408375
await collection.insertOne(
409376
{ _id: '12345678123456781234567812345678', field: '1' },
@@ -428,7 +395,7 @@ describe.only('crud - insert', function () {
428395
test.equal(docs[0].field, '2');
429396
});
430397

431-
// REMOVE
398+
// TODO(NODE-7219): remove as it's outdated (no callbacks)
432399
// it('shouldCorrectlyCallCallbackWithDbDriverInStrictMode', {
433400
// // Add a tag that our runner can trigger on
434401
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -472,15 +439,17 @@ describe.only('crud - insert', function () {
472439
const collection = db.collection('shouldCorrectlyInsertDBRefWithDbNotDefined');
473440

474441
const doc = { _id: new ObjectId() };
475-
const doc2 = { _id: new ObjectId() };
476-
const doc3 = { _id: new ObjectId() };
442+
const doc2 = {
443+
_id: new ObjectId(),
444+
ref: new DBRef('shouldCorrectlyInsertDBRefWithDbNotDefined', doc._id)
445+
};
446+
const doc3 = {
447+
_id: new ObjectId(),
448+
ref: new DBRef('shouldCorrectlyInsertDBRefWithDbNotDefined', doc._id, undefined)
449+
};
477450

478451
await collection.insertOne(doc, configuration.writeConcernMax());
479452

480-
// Create object with dbref
481-
doc2.ref = new DBRef('shouldCorrectlyInsertDBRefWithDbNotDefined', doc._id);
482-
doc3.ref = new DBRef('shouldCorrectlyInsertDBRefWithDbNotDefined', doc._id, undefined);
483-
484453
await collection.insertMany([doc2, doc3], configuration.writeConcernMax());
485454

486455
// Get all items
@@ -494,7 +463,7 @@ describe.only('crud - insert', function () {
494463
expect(items[2].ref.db).to.not.exist;
495464
});
496465

497-
// REMOVE
466+
// TODO(NODE-7219): remove as it's redundant and combines many methods into one test
498467
// it('shouldCorrectlyInsertUpdateRemoveWithNoOptions', {
499468
// // Add a tag that our runner can trigger on
500469
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -541,7 +510,7 @@ describe.only('crud - insert', function () {
541510
// }
542511
// });
543512

544-
// REMOVE
513+
// TODO(NODE-7219): remove as it duplicates "simple insert"
545514
// it('shouldCorrectlyExecuteMultipleFetches', {
546515
// // Add a tag that our runner can trigger on
547516
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -577,7 +546,7 @@ describe.only('crud - insert', function () {
577546
// }
578547
// });
579548

580-
// REMOVE
549+
// TODO(NODE-7219): remove as it duplicates "should not fail when update returning 0 results"
581550
// it('shouldCorrectlyFailWhenNoObjectToUpdate', {
582551
// // Add a tag that our runner can trigger on
583552
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -612,7 +581,7 @@ describe.only('crud - insert', function () {
612581
_id: new ObjectId('4e886e687ff7ef5e00000162'),
613582
str: 'foreign',
614583
type: 2,
615-
timestamp: ISODate('2011-10-02T14:00:08.383Z'),
584+
timestamp: toISODate('2011-10-02T14:00:08.383Z'),
616585
links: [
617586
'http://www.reddit.com/r/worldnews/comments/kybm0/uk_home_secretary_calls_for_the_scrapping_of_the/'
618587
]
@@ -656,7 +625,7 @@ describe.only('crud - insert', function () {
656625

657626
const doc = {
658627
str: 'String',
659-
func: function () {}
628+
func: noop
660629
};
661630

662631
const db = client.db(configuration.db);
@@ -667,7 +636,7 @@ describe.only('crud - insert', function () {
667636

668637
const result = await collection.updateOne(
669638
{ str: 'String' },
670-
{ $set: { c: 1, d: function () {} } },
639+
{ $set: { c: 1, d: noop } },
671640
{ writeConcern: { w: 1 }, serializeFunctions: false }
672641
);
673642
expect(result).property('matchedCount').to.equal(1);
@@ -678,7 +647,7 @@ describe.only('crud - insert', function () {
678647
// Execute a safe insert with replication to two servers
679648
const updateResult = await collection.findOneAndUpdate(
680649
{ str: 'String' },
681-
{ $set: { f: function () {} } },
650+
{ $set: { f: noop } },
682651
{
683652
returnDocument: ReturnDocument.AFTER,
684653
serializeFunctions: true,
@@ -693,7 +662,7 @@ describe.only('crud - insert', function () {
693662

694663
const doc = {
695664
str: 'String',
696-
func: function () {}
665+
func: noop
697666
};
698667

699668
const db = client.db(configuration.db);
@@ -782,7 +751,7 @@ describe.only('crud - insert', function () {
782751
expect(err).to.be.instanceOf(MongoServerError);
783752
});
784753

785-
// REMOVE
754+
// TODO(7219): remove as it's redundant (custom ids are used in multiple other tests: "with UUID", "Date object as _id")
786755
// it('shouldCorrectlyInsertDocWithCustomId', {
787756
// // Add a tag that our runner can trigger on
788757
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -863,7 +832,7 @@ describe.only('crud - insert', function () {
863832
const collection = db.collection('shouldCorrectlyPerformInsertOfObjectsUsingToBSON');
864833

865834
// Create document with toBSON method
866-
const doc = { a: 1, b: 1 };
835+
const doc: Document = { a: 1, b: 1 };
867836
doc.toBSON = function () {
868837
return { c: this.a };
869838
};
@@ -874,7 +843,7 @@ describe.only('crud - insert', function () {
874843
test.deepEqual(1, result.c);
875844
});
876845

877-
// REMOVE
846+
// TODO(NODE-7219): remove as it duplicates "should correctly perform large text insert"
878847
// it('shouldAttempToForceBsonSize', {
879848
// // Add a tag that our runner can trigger on
880849
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -918,11 +887,9 @@ describe.only('crud - insert', function () {
918887

919888
await collection.insertOne({ a: { b: { c: 1 } } }, configuration.writeConcernMax());
920889

921-
// Dynamically build query
922-
const query = {};
923-
query['a'] = {};
924-
query.a['b'] = {};
925-
query.a.b['c'] = 1;
890+
const query = {
891+
a: { b: { c: 1 } }
892+
};
926893

927894
// Update document
928895
const r = await collection.updateOne(
@@ -933,7 +900,7 @@ describe.only('crud - insert', function () {
933900
expect(r).property('matchedCount').to.equal(1);
934901
});
935902

936-
// REMOVE
903+
// TODO(NODE-7219): remove as it's outdated (no callbacks)
937904
// it('shouldExecuteInsertWithNoCallbackAndWriteConcern', {
938905
// // Add a tag that our runner can trigger on
939906
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -959,7 +926,7 @@ describe.only('crud - insert', function () {
959926
// }
960927
// });
961928

962-
// REMOVE
929+
// TODO(NODE-7219): remove as it's outdated (no callbacks)
963930
// it('executesCallbackOnceWithOveriddenDefaultDbWriteConcern', {
964931
// // Add a tag that our runner can trigger on
965932
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -983,7 +950,7 @@ describe.only('crud - insert', function () {
983950
// }
984951
// });
985952

986-
// REMOVE
953+
// TODO(NODE-7219): remove as it's outdated (no callbacks)
987954
// it('executesCallbackOnceWithOveriddenDefaultDbWriteConcernWithUpdate', {
988955
// // Add a tag that our runner can trigger on
989956
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -1012,7 +979,7 @@ describe.only('crud - insert', function () {
1012979
// }
1013980
// });
1014981

1015-
// REMOVE
982+
// TODO(NODE-7219): remove as it's outdated (no callbacks)
1016983
// it('executesCallbackOnceWithOveriddenDefaultDbWriteConcernWithRemove', {
1017984
// // Add a tag that our runner can trigger on
1018985
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -1036,7 +1003,7 @@ describe.only('crud - insert', function () {
10361003
// }
10371004
// });
10381005

1039-
// REMOVE
1006+
// TODO(NODE-7219): remove as it duplicates "handles BSON type inserts"
10401007
// it('handleBSONTypeInsertsCorrectly', {
10411008
// // Add a tag that our runner can trigger on
10421009
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -1118,6 +1085,7 @@ describe.only('crud - insert', function () {
11181085
const configuration = this.configuration;
11191086

11201087
const document = {
1088+
symbol: new BSONSymbol('abcdefghijkl'),
11211089
string: 'abcdefghijkl',
11221090
objid: new ObjectId(Buffer.alloc(12, 1)),
11231091
double: new Double(1),
@@ -1132,6 +1100,9 @@ describe.only('crud - insert', function () {
11321100

11331101
await collection.insertOne(document, configuration.writeConcernMax());
11341102

1103+
const doc = await collection.findOne({ symbol: new BSONSymbol('abcdefghijkl') });
1104+
test.equal('abcdefghijkl', doc.symbol.toString());
1105+
11351106
const doc1 = await collection.findOne({ string: 'abcdefghijkl' });
11361107
test.equal('abcdefghijkl', doc1.string);
11371108

@@ -1356,7 +1327,7 @@ describe.only('crud - insert', function () {
13561327
expect(doc.array[0][0]).to.be.a('number');
13571328
});
13581329

1359-
// REMOVE
1330+
// TODO(NODE-7219): remove as it duplicates "should correctly honor promoteLong:[true|false]"
13601331
// it('shouldCorrectlyHonorPromoteLongFalseJSBSON', {
13611332
// // Add a tag that our runner can trigger on
13621333
// // in this case we are setting that node needs to be higher than 0.10.X to run
@@ -1384,7 +1355,7 @@ describe.only('crud - insert', function () {
13841355
// }
13851356
// });
13861357

1387-
// REMOVE
1358+
// TODO(NODE-7219): remove as it duplicates "should correctly honor promoteLong:true native BSON"
13881359
// it('shouldCorrectlyHonorPromoteLongTrueJSBSON', {
13891360
// // Add a tag that our runner can trigger on
13901361
// // in this case we are setting that node needs to be higher than 0.10.X to run

0 commit comments

Comments
 (0)