Skip to content

Commit f68f71b

Browse files
nbbeekenavaly
authored andcommitted
test: add some additional tests with comments
1 parent b0e17e8 commit f68f71b

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
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

44
import { Filter, MongoClient, WithId } from '../../../../src';
@@ -31,6 +31,7 @@ interface PetModel {
3131
isCute: boolean; // boolean field
3232
bestFriend?: HumanModel; // object field (Embedded/Nested Documents)
3333
createdAt: Date; // date field
34+
numOfPats: Long; // long field
3435
treats: string[]; // array of string
3536
playTimePercent: Decimal128; // bson Decimal128 type
3637
readonly friends?: ReadonlyArray<HumanModel>; // readonly array of objects
@@ -39,6 +40,7 @@ interface PetModel {
3940
meta?: {
4041
updatedAt?: Date;
4142
deep?: {
43+
nestedArray: number[];
4244
nested?: {
4345
level?: number;
4446
};
@@ -59,6 +61,7 @@ const spot = {
5961
type: 'dog' as const,
6062
isCute: true,
6163
createdAt: new Date(),
64+
numOfPats: Long.fromBigInt(100000000n),
6265
treats: ['kibble', 'bone'],
6366
playTimePercent: new Decimal128('0.999999')
6467
};
@@ -114,8 +117,20 @@ expectNotType<Filter<PetModel>>({ bestFriend: [{ name: 'Andersons' }] });
114117
/// it should query __nested document__ fields using dot-notation
115118
collectionT.find({ 'meta.updatedAt': new Date() });
116119
collectionT.find({ 'meta.deep.nested.level': 123 });
120+
collectionT.find({ meta: { deep: { nested: { level: 123 } } } }); // no impact on actual nesting
117121
collectionT.find({ 'friends.0.name': 'John' });
118122
collectionT.find({ 'playmates.0.name': 'John' });
123+
124+
// There's an issue with the special BSON types
125+
collectionT.find({ 'numOfPats.__isLong__': true });
126+
collectionT.find({ numOfPats: Long.fromBigInt(2n) });
127+
collectionT.find({ 'playTimePercent.bytes.BYTES_PER_ELEMENT': 1 });
128+
collectionT.find({ playTimePercent: new Decimal128('123.2') });
129+
130+
// works with some extreme indexes
131+
collectionT.find({ 'friends.4294967295.name': 'John' });
132+
collectionT.find({ 'friends.999999999999999999999999999999999999.name': 'John' });
133+
119134
/// it should not accept wrong types for nested document fields
120135
expectNotType<Filter<PetModel>>({ 'meta.updatedAt': 123 });
121136
expectNotType<Filter<PetModel>>({ 'meta.updatedAt': true });
@@ -126,6 +141,10 @@ expectNotType<Filter<PetModel>>({ 'meta.deep.nested.level': new Date() });
126141
expectNotType<Filter<PetModel>>({ 'friends.0.name': 123 });
127142
expectNotType<Filter<PetModel>>({ 'playmates.0.name': 123 });
128143

144+
// Nested arrays aren't checked
145+
expectType<Filter<PetModel>>({ 'meta.deep.nestedArray.0': 'not a number' });
146+
expectNotType<Filter<PetModel>>({ 'meta.deep.nestedArray.23': 'not a number' });
147+
129148
/// it should query __array__ fields by exact match
130149
await collectionT.find({ treats: ['kibble', 'bone'] }).toArray();
131150
/// it should query __array__ fields by element type

0 commit comments

Comments
 (0)