Bug fix findOneAndUpdate

This commit is contained in:
Mel O'Hagan 2022-08-08 17:53:17 +01:00
parent c289d72816
commit fe0163ac29
3 changed files with 53 additions and 2 deletions

View File

@ -8,6 +8,7 @@ module MongoMock {
this.insertMany = jest.fn(() => ({ toArray: () => [] })) this.insertMany = jest.fn(() => ({ toArray: () => [] }))
this.find = jest.fn(() => ({ toArray: () => [] })) this.find = jest.fn(() => ({ toArray: () => [] }))
this.findOne = jest.fn() this.findOne = jest.fn()
this.findOneAndUpdate = jest.fn()
this.count = jest.fn() this.count = jest.fn()
this.deleteOne = jest.fn() this.deleteOne = jest.fn()
this.deleteMany = jest.fn(() => ({ toArray: () => [] })) this.deleteMany = jest.fn(() => ({ toArray: () => [] }))
@ -19,6 +20,7 @@ module MongoMock {
find: this.find, find: this.find,
insertMany: this.insertMany, insertMany: this.insertMany,
findOne: this.findOne, findOne: this.findOne,
findOneAndUpdate: this.findOneAndUpdate,
count: this.count, count: this.count,
deleteOne: this.deleteOne, deleteOne: this.deleteOne,
deleteMany: this.deleteMany, deleteMany: this.deleteMany,

View File

@ -179,7 +179,10 @@ module MongoDBModule {
return await collection.findOne(json) return await collection.findOne(json)
} }
case "findOneAndUpdate": { case "findOneAndUpdate": {
let findAndUpdateJson = json as { if (typeof query.json === "string") {
json = this.parseQueryParams(query.json, "update")
}
let findAndUpdateJson = this.createObjectIds(json) as {
filter: FilterQuery<any> filter: FilterQuery<any>
update: UpdateQuery<any> update: UpdateQuery<any>
options: FindOneAndUpdateOption<any> options: FindOneAndUpdateOption<any>

View File

@ -132,6 +132,9 @@ describe("MongoDB Integration", () => {
_id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"), _id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"),
name: "ObjectId('updatedName')", name: "ObjectId('updatedName')",
}) })
expect(args[2]).toEqual({
upsert: false
})
}) })
it("creates ObjectIds if the $ operator fields contains a match on ObjectId", async () => { it("creates ObjectIds if the $ operator fields contains a match on ObjectId", async () => {
@ -148,7 +151,7 @@ describe("MongoDB Integration", () => {
}, },
}, },
options: { options: {
upsert: false, upsert: true,
}, },
}, },
extra: { collection: "testCollection", actionTypes: "updateOne" }, extra: { collection: "testCollection", actionTypes: "updateOne" },
@ -167,5 +170,48 @@ describe("MongoDB Integration", () => {
_id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"), _id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"),
} }
}) })
expect(args[2]).toEqual({
upsert: true
})
})
it("supports findOneAndUpdate", async () => {
const query = {
json: {
filter: {
_id: {
$eq: "ObjectId('ACBD12345678ABCD12345678')",
}
},
update: {
$set: {
name: "UPDATED",
age: 99
},
},
options: {
upsert: false,
},
},
extra: { collection: "testCollection", actionTypes: "findOneAndUpdate" },
}
await config.integration.read(query)
expect(config.integration.client.findOneAndUpdate).toHaveBeenCalled()
const args = config.integration.client.findOneAndUpdate.mock.calls[0]
expect(args[0]).toEqual({
_id: {
$eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"),
}
})
expect(args[1]).toEqual({
$set: {
name: "UPDATED",
age: 99
}
})
expect(args[2]).toEqual({
upsert: false
})
}) })
}) })