1
0
Fork 0
mirror of synced 2024-09-30 09:07:25 +13:00
budibase/packages/server/src/integrations/tests/mongo.spec.js

172 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-03-17 02:54:39 +13:00
const mongo = require("mongodb")
const MongoDBIntegration = require("../mongodb")
jest.mock("mongodb")
class TestConfiguration {
constructor(config = {}) {
2022-05-14 02:56:02 +12:00
this.integration = new MongoDBIntegration.integration(config)
2021-03-17 02:54:39 +13:00
}
}
function disableConsole() {
2022-05-14 02:56:02 +12:00
jest.spyOn(console, "error")
console.error.mockImplementation(() => {})
2022-05-14 02:56:02 +12:00
return console.error.mockRestore
}
2021-03-17 02:54:39 +13:00
describe("MongoDB Integration", () => {
2022-05-14 02:56:02 +12:00
let config
2021-03-17 02:54:39 +13:00
let indexName = "Users"
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const body = {
2022-05-14 02:56:02 +12:00
name: "Hello",
2021-03-17 02:54:39 +13:00
}
await config.integration.create({
2021-03-17 02:54:39 +13:00
index: indexName,
json: body,
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "insertOne" },
2021-03-17 02:54:39 +13:00
})
expect(config.integration.client.insertOne).toHaveBeenCalledWith(body)
})
it("calls the read method with the correct params", async () => {
const query = {
json: {
2022-05-14 02:56:02 +12:00
address: "test",
},
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "find" },
2021-03-17 02:54:39 +13:00
}
const response = await config.integration.read(query)
expect(config.integration.client.find).toHaveBeenCalledWith(query.json)
expect(response).toEqual(expect.any(Array))
})
it("calls the delete method with the correct params", async () => {
const query = {
json: {
2022-05-16 20:50:47 +12:00
filter: {
id: "test",
},
options: {
opt: "option"
}
},
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "deleteOne" },
}
await config.integration.delete(query)
2022-05-16 20:50:47 +12:00
expect(config.integration.client.deleteOne).toHaveBeenCalledWith(query.json.filter, query.json.options)
})
it("calls the update method with the correct params", async () => {
const query = {
json: {
2022-05-14 02:56:02 +12:00
filter: {
id: "test",
},
update: {
name: "TestName",
},
options: {
upsert: false,
},
},
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "updateOne" },
}
await config.integration.update(query)
2022-05-14 02:56:02 +12:00
expect(config.integration.client.updateOne).toHaveBeenCalledWith(
query.json.filter,
query.json.update,
query.json.options
)
})
it("throws an error when an invalid query.extra.actionType is passed for each method", async () => {
const restore = disableConsole()
const query = {
2022-05-14 02:56:02 +12:00
extra: { collection: "testCollection", actionTypes: "deleteOne" },
}
let error = null
try {
await config.integration.read(query)
} catch (err) {
error = err
}
expect(error).toBeDefined()
restore()
})
2022-08-09 04:01:56 +12:00
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')",
})
})
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: 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: {
$eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"),
}
})
expect(args[1]).toEqual({
$set: {
_id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"),
}
})
})
2022-05-14 02:56:02 +12:00
})