Skip to content

Commit 286882f

Browse files
committed
test: add some additional tests with comments
1 parent 4c7c54a commit 286882f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

test/types/community/collection/filterQuery.test-d.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BSONRegExp, Decimal128, ObjectId } from 'bson';
1+
import { BSONRegExp, Decimal128, Long, ObjectId } from 'bson';
22
import { expectAssignable, expectNotType, expectType } from 'tsd';
33
import { Filter, MongoClient } from '../../../../src';
44

@@ -30,6 +30,7 @@ interface PetModel {
3030
isCute: boolean; // boolean field
3131
bestFriend?: HumanModel; // object field (Embedded/Nested Documents)
3232
createdAt: Date; // date field
33+
numOfPats: Long; // long field
3334
treats: string[]; // array of string
3435
playTimePercent: Decimal128; // bson Decimal128 type
3536
readonly friends?: ReadonlyArray<HumanModel>; // readonly array of objects
@@ -38,6 +39,7 @@ interface PetModel {
3839
meta?: {
3940
updatedAt?: Date;
4041
deep?: {
42+
nestedArray: number[];
4143
nested?: {
4244
level?: number;
4345
};
@@ -58,6 +60,7 @@ const spot = {
5860
type: 'dog' as const,
5961
isCute: true,
6062
createdAt: new Date(),
63+
numOfPats: Long.fromBigInt(100000000n),
6164
treats: ['kibble', 'bone'],
6265
playTimePercent: new Decimal128('0.999999')
6366
};
@@ -109,8 +112,20 @@ expectNotType<Filter<PetModel>>({ bestFriend: [{ name: 'Andersons' }] });
109112
/// it should query __nested document__ fields using dot-notation
110113
collectionT.find({ 'meta.updatedAt': new Date() });
111114
collectionT.find({ 'meta.deep.nested.level': 123 });
115+
collectionT.find({ meta: { deep: { nested: { level: 123 } } } }); // no impact on actual nesting
112116
collectionT.find({ 'friends.0.name': 'John' });
113117
collectionT.find({ 'playmates.0.name': 'John' });
118+
119+
// There's an issue with the special BSON types
120+
collectionT.find({ 'numOfPats.__isLong__': true });
121+
collectionT.find({ numOfPats: Long.fromBigInt(2n) });
122+
collectionT.find({ 'playTimePercent.bytes.BYTES_PER_ELEMENT': 1 });
123+
collectionT.find({ playTimePercent: new Decimal128('123.2') });
124+
125+
// works with some extreme indexes
126+
collectionT.find({ 'friends.4294967295.name': 'John' });
127+
collectionT.find({ 'friends.999999999999999999999999999999999999.name': 'John' });
128+
114129
/// it should not accept wrong types for nested document fields
115130
expectNotType<Filter<PetModel>>({ 'meta.updatedAt': 123 });
116131
expectNotType<Filter<PetModel>>({ 'meta.updatedAt': true });
@@ -121,6 +136,10 @@ expectNotType<Filter<PetModel>>({ 'meta.deep.nested.level': new Date() });
121136
expectNotType<Filter<PetModel>>({ 'friends.0.name': 123 });
122137
expectNotType<Filter<PetModel>>({ 'playmates.0.name': 123 });
123138

139+
// Nested arrays aren't checked
140+
expectType<Filter<PetModel>>({ 'meta.deep.nestedArray.0': 'not a number' });
141+
expectNotType<Filter<PetModel>>({ 'meta.deep.nestedArray.23': 'not a number' });
142+
124143
/// it should query __array__ fields by exact match
125144
await collectionT.find({ treats: ['kibble', 'bone'] }).toArray();
126145
/// it should query __array__ fields by element type

0 commit comments

Comments
 (0)