From c9c1dd2e9deae743ed4bf1df8ccce531884467f1 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 19 Mar 2024 15:06:52 -0400 Subject: [PATCH 1/7] added unit test testing fix testing fix 2 --- src/mongo_types.ts | 2 +- test/integration/crud/find_and_modify.test.ts | 37 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index 1924b2afe2..b754a8d443 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -283,7 +283,7 @@ export type PullAllOperator = ({ export type UpdateFilter = { $currentDate?: OnlyFieldsOfType< TSchema, - Date | Timestamp, + Date | Timestamp | true, true | { $type: 'date' | 'timestamp' } >; $inc?: OnlyFieldsOfType; diff --git a/test/integration/crud/find_and_modify.test.ts b/test/integration/crud/find_and_modify.test.ts index 16764f020e..9b444c2d63 100644 --- a/test/integration/crud/find_and_modify.test.ts +++ b/test/integration/crud/find_and_modify.test.ts @@ -1,6 +1,12 @@ import { expect } from 'chai'; -import { type CommandStartedEvent, MongoServerError, ObjectId } from '../../mongodb'; +import { + type Collection, + type CommandStartedEvent, + type Db, + MongoServerError, + ObjectId +} from '../../mongodb'; import { setupDatabase } from '../shared'; describe('Collection (#findOneAnd...)', function () { @@ -324,6 +330,35 @@ describe('Collection (#findOneAnd...)', function () { }); }); }); + + describe('update filter', function () { + context('when $currentDate is provided', function () { + let client; + let db: Db; + let collection: Collection; + + beforeEach(async function () { + client = this.configuration.newClient({ w: 1 }); + await client.connect(); + db = client.db(this.configuration.db); + collection = db.collection('test_coll'); + await collection.insertOne({ a: 'c' }); + }); + + afterEach(async function () { + await collection.drop(); + await client.close(); + }); + + it(`should support fields with value 'true'`, async function () { + await collection.findOneAndUpdate( + {}, + { $set: { a: 1 }, $currentDate: { lastModified: true } }, + { writeConcern: { w: 1 } } + ); + }); + }); + }); }); describe('#findOneAndReplace', function () { From 839d1dc446b1e436dc96a78e4dee5a0d7edc0ed1 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 19 Mar 2024 15:38:14 -0400 Subject: [PATCH 2/7] added expanded types to strict update filter too --- src/mongo_types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index b754a8d443..0093cc1151 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -283,7 +283,7 @@ export type PullAllOperator = ({ export type UpdateFilter = { $currentDate?: OnlyFieldsOfType< TSchema, - Date | Timestamp | true, + true | Date | Timestamp, true | { $type: 'date' | 'timestamp' } >; $inc?: OnlyFieldsOfType; @@ -588,7 +588,7 @@ export type StrictFilter = export type StrictUpdateFilter = { $currentDate?: OnlyFieldsOfType< TSchema, - Date | Timestamp, + true | Date | Timestamp, true | { $type: 'date' | 'timestamp' } >; $inc?: OnlyFieldsOfType; From 1037d9e56a13420d7bbacf45d7b277729377a633 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 19 Mar 2024 16:00:41 -0400 Subject: [PATCH 3/7] update unit type tests --- src/mongo_types.ts | 4 +-- test/integration/crud/find_and_modify.test.ts | 29 ------------------- .../community/collection/updateX.test-d.ts | 6 ++++ test/types/helper_types.test-d.ts | 1 + 4 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index 0093cc1151..5da86d2785 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -219,7 +219,7 @@ export type NotAcceptedFields = { /** @public */ export type OnlyFieldsOfType = IsAny< TSchema[keyof TSchema], - Record, + AssignableType extends FieldType ? Record : Record, AcceptedFields & NotAcceptedFields & Record @@ -283,7 +283,7 @@ export type PullAllOperator = ({ export type UpdateFilter = { $currentDate?: OnlyFieldsOfType< TSchema, - true | Date | Timestamp, + Date | Timestamp, true | { $type: 'date' | 'timestamp' } >; $inc?: OnlyFieldsOfType; diff --git a/test/integration/crud/find_and_modify.test.ts b/test/integration/crud/find_and_modify.test.ts index 9b444c2d63..0c051117b5 100644 --- a/test/integration/crud/find_and_modify.test.ts +++ b/test/integration/crud/find_and_modify.test.ts @@ -330,35 +330,6 @@ describe('Collection (#findOneAnd...)', function () { }); }); }); - - describe('update filter', function () { - context('when $currentDate is provided', function () { - let client; - let db: Db; - let collection: Collection; - - beforeEach(async function () { - client = this.configuration.newClient({ w: 1 }); - await client.connect(); - db = client.db(this.configuration.db); - collection = db.collection('test_coll'); - await collection.insertOne({ a: 'c' }); - }); - - afterEach(async function () { - await collection.drop(); - await client.close(); - }); - - it(`should support fields with value 'true'`, async function () { - await collection.findOneAndUpdate( - {}, - { $set: { a: 1 }, $currentDate: { lastModified: true } }, - { writeConcern: { w: 1 } } - ); - }); - }); - }); }); describe('#findOneAndReplace', function () { diff --git a/test/types/community/collection/updateX.test-d.ts b/test/types/community/collection/updateX.test-d.ts index 5ea59cd525..ca41193b12 100644 --- a/test/types/community/collection/updateX.test-d.ts +++ b/test/types/community/collection/updateX.test-d.ts @@ -60,6 +60,12 @@ expectAssignable>({ $inc: { anyKeyWhatsoever: 2 } }); // But this no longer asserts anything about what the original keys map to expectNotType>({ $inc: { numberField: '2' } }); + +expectAssignable>({ $currentDate: { anyKeyWhatsoever: { $type: 'timestamp' }} }); +expectAssignable>({ $currentDate: { anyKeyWhatsoever: { $type: 'date' }} }); +expectAssignable>({ $currentDate: { anyKeyWhatsoever: true } }); +expectNotType>({ $currentDate: { anyKeyWhatsoever: 'true' } }); +expectNotType>({ $currentDate: { anyKeyWhatsoever: { key2: true }} }); // collection.updateX tests const client = new MongoClient(''); const db = client.db('test'); diff --git a/test/types/helper_types.test-d.ts b/test/types/helper_types.test-d.ts index f5df10de56..9606dfeab0 100644 --- a/test/types/helper_types.test-d.ts +++ b/test/types/helper_types.test-d.ts @@ -68,6 +68,7 @@ expectType>(notAc expectAssignable>({ a: 2 }); expectAssignable>({ b: 'hello' }); expectAssignable>({ b: true }); +expectAssignable>({ b: true }); // test generic schema, essentially we expect nearly no safety here expectAssignable>({ someKey: 2 }); From 36cfdb46b03c6740f21807fb74f74ba4808f7263 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 19 Mar 2024 16:06:05 -0400 Subject: [PATCH 4/7] removed extraneous test --- test/integration/crud/find_and_modify.test.ts | 8 +------- test/types/helper_types.test-d.ts | 1 - 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/test/integration/crud/find_and_modify.test.ts b/test/integration/crud/find_and_modify.test.ts index 0c051117b5..16764f020e 100644 --- a/test/integration/crud/find_and_modify.test.ts +++ b/test/integration/crud/find_and_modify.test.ts @@ -1,12 +1,6 @@ import { expect } from 'chai'; -import { - type Collection, - type CommandStartedEvent, - type Db, - MongoServerError, - ObjectId -} from '../../mongodb'; +import { type CommandStartedEvent, MongoServerError, ObjectId } from '../../mongodb'; import { setupDatabase } from '../shared'; describe('Collection (#findOneAnd...)', function () { diff --git a/test/types/helper_types.test-d.ts b/test/types/helper_types.test-d.ts index 9606dfeab0..f5df10de56 100644 --- a/test/types/helper_types.test-d.ts +++ b/test/types/helper_types.test-d.ts @@ -68,7 +68,6 @@ expectType>(notAc expectAssignable>({ a: 2 }); expectAssignable>({ b: 'hello' }); expectAssignable>({ b: true }); -expectAssignable>({ b: true }); // test generic schema, essentially we expect nearly no safety here expectAssignable>({ someKey: 2 }); From 90dbcc8752fa72550e34cf4f6002b8c675aa3d30 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 19 Mar 2024 16:08:13 -0400 Subject: [PATCH 5/7] removed extraneous change --- src/mongo_types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index 5da86d2785..0f878041e0 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -588,7 +588,7 @@ export type StrictFilter = export type StrictUpdateFilter = { $currentDate?: OnlyFieldsOfType< TSchema, - true | Date | Timestamp, + Date | Timestamp, true | { $type: 'date' | 'timestamp' } >; $inc?: OnlyFieldsOfType; From 366eee978d1b716fd44ff8396e4c20f4a6428554 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 19 Mar 2024 16:13:53 -0400 Subject: [PATCH 6/7] lint fix --- test/types/community/collection/updateX.test-d.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/types/community/collection/updateX.test-d.ts b/test/types/community/collection/updateX.test-d.ts index ca41193b12..c2fb170b74 100644 --- a/test/types/community/collection/updateX.test-d.ts +++ b/test/types/community/collection/updateX.test-d.ts @@ -60,12 +60,13 @@ expectAssignable>({ $inc: { anyKeyWhatsoever: 2 } }); // But this no longer asserts anything about what the original keys map to expectNotType>({ $inc: { numberField: '2' } }); - -expectAssignable>({ $currentDate: { anyKeyWhatsoever: { $type: 'timestamp' }} }); -expectAssignable>({ $currentDate: { anyKeyWhatsoever: { $type: 'date' }} }); -expectAssignable>({ $currentDate: { anyKeyWhatsoever: true } }); -expectNotType>({ $currentDate: { anyKeyWhatsoever: 'true' } }); -expectNotType>({ $currentDate: { anyKeyWhatsoever: { key2: true }} }); +expectAssignable>({ + $currentDate: { anyKeyWhatsoever: { $type: 'timestamp' } } +}); +expectAssignable>({ $currentDate: { anyKeyWhatsoever: { $type: 'date' } } }); +expectAssignable>({ $currentDate: { anyKeyWhatsoever: true } }); +expectNotType>({ $currentDate: { anyKeyWhatsoever: 'true' } }); +expectNotType>({ $currentDate: { anyKeyWhatsoever: { key2: true } } }); // collection.updateX tests const client = new MongoClient(''); const db = client.db('test'); From 1fb2009d48566ecdde21d4bf44bb70395eb5e444 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Wed, 20 Mar 2024 11:38:42 -0400 Subject: [PATCH 7/7] added in final test case --- test/types/helper_types.test-d.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/types/helper_types.test-d.ts b/test/types/helper_types.test-d.ts index f5df10de56..ce8fbfce0f 100644 --- a/test/types/helper_types.test-d.ts +++ b/test/types/helper_types.test-d.ts @@ -69,6 +69,9 @@ expectAssignable>({ a: 2 }); expectAssignable>({ b: 'hello' }); expectAssignable>({ b: true }); +// test the case in which AssignableType does not inherit from FieldType, and AssignableType is provided +expectAssignable>({ b: false }); + // test generic schema, essentially we expect nearly no safety here expectAssignable>({ someKey: 2 }); // We can still at least enforce the type that the keys map to