1
0
Fork 0
mirror of synced 2024-06-30 12:00:31 +12:00
budibase/packages/server/src/integrations/tests/postgres.spec.ts

82 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-03-16 05:07:04 +13:00
const pg = require("pg")
import { default as PostgresIntegration } from "../postgres"
2021-03-16 05:07:04 +13:00
jest.mock("pg")
2021-03-12 22:29:27 +13:00
class TestConfiguration {
integration: any
constructor(config: any = {}) {
this.integration = new PostgresIntegration.integration(config)
2021-03-12 22:29:27 +13:00
}
}
2021-03-16 05:07:04 +13:00
describe("Postgres Integration", () => {
let config: any
2021-03-12 22:29:27 +13:00
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
2021-03-16 05:07:04 +13:00
const sql = "insert into users (name, age) values ('Joe', 123);"
await config.integration.create({
sql,
2021-03-12 22:29:27 +13:00
})
2021-07-14 04:11:11 +12:00
expect(pg.queryMock).toHaveBeenCalledWith(sql, [])
2021-03-12 22:29:27 +13:00
})
2021-03-16 05:07:04 +13:00
it("calls the read method with the correct params", async () => {
const sql = "select * from users;"
await config.integration.read({
sql,
2021-03-16 05:07:04 +13:00
})
2021-07-14 04:11:11 +12:00
expect(pg.queryMock).toHaveBeenCalledWith(sql, [])
2021-03-12 22:29:27 +13:00
})
2021-03-16 05:07:04 +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-16 05:07:04 +13:00
})
2021-07-14 04:11:11 +12:00
expect(pg.queryMock).toHaveBeenCalledWith(sql, [])
2021-03-16 05:07:04 +13:00
})
2021-03-12 22:29:27 +13:00
2021-03-16 05:07:04 +13:00
it("calls the delete method with the correct params", async () => {
const sql = "delete from users where name = 'todelete';"
2021-05-22 03:02:21 +12:00
await config.integration.delete({
sql,
2021-03-16 05:07:04 +13:00
})
2021-07-14 04:11:11 +12:00
expect(pg.queryMock).toHaveBeenCalledWith(sql, [])
2021-03-12 22:29:27 +13:00
})
2021-03-16 05:07:04 +13:00
describe("no rows returned", () => {
2021-03-16 05:26:46 +13:00
beforeEach(() => {
2021-05-22 03:02:21 +12:00
pg.queryMock.mockImplementation(() => ({ rows: [] }))
2021-03-16 05:26:46 +13:00
})
2021-03-16 05:07:04 +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,
2021-03-16 05:07:04 +13:00
})
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,
2021-03-16 05:07:04 +13:00
})
expect(response).toEqual([{ updated: true }])
})
2021-03-12 22:29:27 +13:00
2021-03-16 05:07:04 +13:00
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,
2021-03-16 05:07:04 +13:00
})
expect(response).toEqual([{ deleted: true }])
})
2021-03-12 22:29:27 +13:00
})
})