2021-03-15 17:26:46 +01:00
|
|
|
const MySQLIntegration = require("../mysql")
|
2021-10-21 15:47:35 +02:00
|
|
|
jest.mock("mysql2")
|
2021-03-15 17:26:46 +01:00
|
|
|
|
|
|
|
class TestConfiguration {
|
|
|
|
constructor(config = { ssl: {} }) {
|
2021-06-03 19:48:04 +02:00
|
|
|
this.integration = new MySQLIntegration.integration(config)
|
2021-03-15 17:26:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 19:43:56 +01:00
|
|
|
describe("MySQL Integration", () => {
|
2021-03-15 17:26:46 +01:00
|
|
|
let config
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
config = new TestConfiguration()
|
|
|
|
})
|
|
|
|
|
2021-03-16 20:01:51 +01:00
|
|
|
it("calls the create method with the correct params", async () => {
|
2021-03-15 17:26:46 +01:00
|
|
|
const sql = "insert into users (name, age) values ('Joe', 123);"
|
2021-06-03 19:48:04 +02:00
|
|
|
await config.integration.create({
|
2021-03-15 17:26:46 +01:00
|
|
|
sql
|
|
|
|
})
|
2021-06-25 14:46:02 +02:00
|
|
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, {}, expect.any(Function))
|
2021-03-15 17:26:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("calls the read method with the correct params", async () => {
|
|
|
|
const sql = "select * from users;"
|
2021-06-03 19:48:04 +02:00
|
|
|
await config.integration.read({
|
2021-03-16 20:01:51 +01:00
|
|
|
sql
|
|
|
|
})
|
2021-06-25 14:46:02 +02:00
|
|
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, {}, expect.any(Function))
|
2021-03-15 17:26:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("calls the update method with the correct params", async () => {
|
|
|
|
const sql = "update table users set name = 'test';"
|
2021-06-03 19:48:04 +02:00
|
|
|
await config.integration.update({
|
2021-03-15 17:26:46 +01:00
|
|
|
sql
|
|
|
|
})
|
2021-06-25 14:46:02 +02:00
|
|
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, {}, expect.any(Function))
|
2021-03-15 17:26:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("calls the delete method with the correct params", async () => {
|
|
|
|
const sql = "delete from users where name = 'todelete';"
|
2021-06-03 19:48:04 +02:00
|
|
|
await config.integration.delete({
|
2021-03-15 17:26:46 +01:00
|
|
|
sql
|
|
|
|
})
|
2021-06-25 14:46:02 +02:00
|
|
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, {}, expect.any(Function))
|
2021-03-15 17:26:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe("no rows returned", () => {
|
|
|
|
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 }])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|