|
|
|
@ -102,4 +102,222 @@ describe("MongoDB Integration", () => { |
|
|
|
expect(error).toBeDefined() |
|
|
|
restore() |
|
|
|
}) |
|
|
|
|
|
|
|
it("creates ObjectIds if the _id fields contains a match on ObjectId", async () => { |
|
|
|
const query = { |
|
|
|
json: { |
|
|
|
filter: { |
|
|
|
_id: "ObjectId('ACBD12345678ABCD12345678')", |
|
|
|
name: "ObjectId('name')" |
|
|
|
}, |
|
|
|
update: { |
|
|
|
_id: "ObjectId('FFFF12345678ABCD12345678')", |
|
|
|
name: "ObjectId('updatedName')", |
|
|
|
}, |
|
|
|
options: { |
|
|
|
upsert: false, |
|
|
|
}, |
|
|
|
}, |
|
|
|
extra: { collection: "testCollection", actionTypes: "updateOne" }, |
|
|
|
} |
|
|
|
await config.integration.update(query) |
|
|
|
expect(config.integration.client.updateOne).toHaveBeenCalled() |
|
|
|
|
|
|
|
const args = config.integration.client.updateOne.mock.calls[0] |
|
|
|
expect(args[0]).toEqual({ |
|
|
|
_id: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), |
|
|
|
name: "ObjectId('name')", |
|
|
|
}) |
|
|
|
expect(args[1]).toEqual({ |
|
|
|
_id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"), |
|
|
|
name: "ObjectId('updatedName')", |
|
|
|
}) |
|
|
|
expect(args[2]).toEqual({ |
|
|
|
upsert: false |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
it("creates ObjectIds if the $ operator fields contains a match on ObjectId", async () => { |
|
|
|
const query = { |
|
|
|
json: { |
|
|
|
filter: { |
|
|
|
_id: { |
|
|
|
$eq: "ObjectId('ACBD12345678ABCD12345678')", |
|
|
|
} |
|
|
|
}, |
|
|
|
update: { |
|
|
|
$set: { |
|
|
|
_id: "ObjectId('FFFF12345678ABCD12345678')", |
|
|
|
}, |
|
|
|
}, |
|
|
|
options: { |
|
|
|
upsert: true, |
|
|
|
}, |
|
|
|
}, |
|
|
|
extra: { collection: "testCollection", actionTypes: "updateOne" }, |
|
|
|
} |
|
|
|
await config.integration.update(query) |
|
|
|
expect(config.integration.client.updateOne).toHaveBeenCalled() |
|
|
|
|
|
|
|
const args = config.integration.client.updateOne.mock.calls[0] |
|
|
|
expect(args[0]).toEqual({ |
|
|
|
_id: { |
|
|
|
$eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), |
|
|
|
} |
|
|
|
}) |
|
|
|
expect(args[1]).toEqual({ |
|
|
|
$set: { |
|
|
|
_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 |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
it("can parse nested objects with arrays", async () => { |
|
|
|
const query = { |
|
|
|
json: `{
|
|
|
|
"_id": { |
|
|
|
"$eq": "ObjectId('ACBD12345678ABCD12345678')" |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
"$set": { |
|
|
|
"value": { |
|
|
|
"data": [ |
|
|
|
{ "cid": 1 }, |
|
|
|
{ "cid": 2 }, |
|
|
|
{ "nested": { |
|
|
|
"name": "test" |
|
|
|
}} |
|
|
|
] |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
"upsert": true |
|
|
|
}`,
|
|
|
|
extra: { collection: "testCollection", actionTypes: "updateOne" }, |
|
|
|
} |
|
|
|
await config.integration.update(query) |
|
|
|
expect(config.integration.client.updateOne).toHaveBeenCalled() |
|
|
|
|
|
|
|
const args = config.integration.client.updateOne.mock.calls[0] |
|
|
|
expect(args[0]).toEqual({ |
|
|
|
_id: { |
|
|
|
$eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), |
|
|
|
} |
|
|
|
}) |
|
|
|
expect(args[1]).toEqual({ |
|
|
|
$set: { |
|
|
|
value: { |
|
|
|
data: [ |
|
|
|
{ cid: 1 }, |
|
|
|
{ cid: 2 }, |
|
|
|
{ nested: { |
|
|
|
name: "test" |
|
|
|
}} |
|
|
|
] |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
expect(args[2]).toEqual({ |
|
|
|
upsert: true |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
it("ignores braces within strings when parsing nested objects", async () => { |
|
|
|
const query = { |
|
|
|
json: `{
|
|
|
|
"_id": { |
|
|
|
"$eq": "ObjectId('ACBD12345678ABCD12345678')" |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
"$set": { |
|
|
|
"value": { |
|
|
|
"data": [ |
|
|
|
{ "cid": 1 }, |
|
|
|
{ "cid": 2 }, |
|
|
|
{ "nested": { |
|
|
|
"name": "te}st" |
|
|
|
}} |
|
|
|
] |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
"upsert": true, |
|
|
|
"extra": "ad\\"{\\"d" |
|
|
|
}`,
|
|
|
|
extra: { collection: "testCollection", actionTypes: "updateOne" }, |
|
|
|
} |
|
|
|
await config.integration.update(query) |
|
|
|
expect(config.integration.client.updateOne).toHaveBeenCalled() |
|
|
|
|
|
|
|
const args = config.integration.client.updateOne.mock.calls[0] |
|
|
|
expect(args[0]).toEqual({ |
|
|
|
_id: { |
|
|
|
$eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), |
|
|
|
} |
|
|
|
}) |
|
|
|
expect(args[1]).toEqual({ |
|
|
|
$set: { |
|
|
|
value: { |
|
|
|
data: [ |
|
|
|
{ cid: 1 }, |
|
|
|
{ cid: 2 }, |
|
|
|
{ nested: { |
|
|
|
name: "te}st" |
|
|
|
}} |
|
|
|
] |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
expect(args[2]).toEqual({ |
|
|
|
upsert: true, |
|
|
|
extra: "ad\"{\"d" |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|