1
0
Fork 0
mirror of synced 2024-07-02 13:01:09 +12:00
budibase/packages/server/src/integrations/tests/mysql.spec.js

83 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-03-16 05:26:46 +13:00
const pg = require("mysql")
const MySQLIntegration = require("../mysql")
jest.mock("mysql")
class TestConfiguration {
constructor(config = { ssl: {} }) {
this.integration = new MySQLIntegration.integration(config)
2021-03-17 08:01:51 +13:00
this.query = jest.fn(() => [{ id: 1 }])
this.integration.query = this.query
2021-03-16 05:26:46 +13:00
}
}
2021-03-17 07:43:56 +13:00
describe("MySQL Integration", () => {
2021-03-16 05:26:46 +13:00
let config
beforeEach(() => {
config = new TestConfiguration()
})
2021-03-17 08:01:51 +13:00
it("calls the create method with the correct params", async () => {
2021-03-16 05:26:46 +13:00
const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
})
2021-03-17 08:01:51 +13:00
expect(config.query).toHaveBeenCalledWith({ sql })
2021-03-16 05:26:46 +13:00
})
it("calls the read method with the correct params", async () => {
const sql = "select * from users;"
const response = await config.integration.read({
sql
})
2021-03-17 08:01:51 +13:00
expect(config.query).toHaveBeenCalledWith({
sql
})
2021-03-16 05:26:46 +13:00
})
it("calls the update method with the correct params", async () => {
const sql = "update table users set name = 'test';"
const response = await config.integration.update({
sql
})
2021-03-17 08:01:51 +13:00
expect(config.query).toHaveBeenCalledWith({ sql })
2021-03-16 05:26:46 +13:00
})
it("calls the delete method with the correct params", async () => {
const sql = "delete from users where name = 'todelete';"
const response = await config.integration.delete({
sql
})
2021-03-17 08:01:51 +13:00
expect(config.query).toHaveBeenCalledWith({ sql })
2021-03-16 05:26:46 +13:00
})
describe("no rows returned", () => {
2021-03-17 08:01:51 +13:00
beforeEach(() => {
config.query.mockImplementation(() => [])
})
2021-03-16 05:26:46 +13:00
it("returns the correct response when the create response has no rows", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
})
expect(response).toEqual([{ created: true }])
})
it("returns the correct response when the update response has no rows", async () => {
const sql = "update table users set name = 'test';"
const response = await config.integration.update({
sql
})
expect(response).toEqual([{ updated: true }])
})
it("returns the correct response when the delete response has no rows", async () => {
const sql = "delete from users where name = 'todelete';"
const response = await config.integration.delete({
sql
})
expect(response).toEqual([{ deleted: true }])
})
})
})