From de68ba23dd5c4063feee3b39edf59756f1b43b47 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 15 Mar 2021 16:07:04 +0000 Subject: [PATCH] postgres integration tests --- packages/server/__mocks__/airtable.js | 10 ++- packages/server/__mocks__/pg.js | 24 +++---- packages/server/src/integrations/airtable.js | 1 - .../src/integrations/tests/airtable.spec.js | 10 +-- .../src/integrations/tests/postgres.spec.js | 67 ++++++++++++++----- 5 files changed, 73 insertions(+), 39 deletions(-) diff --git a/packages/server/__mocks__/airtable.js b/packages/server/__mocks__/airtable.js index 4a4d413978..34a248ac7f 100644 --- a/packages/server/__mocks__/airtable.js +++ b/packages/server/__mocks__/airtable.js @@ -1,13 +1,11 @@ class Airtable { - constructor() {} + constructor() { + this.create = jest.fn() + } base() { return () => ({ - query: jest.fn(), - create: jest.fn(), - select: jest.fn(), - update: jest.fn(), - destroy: jest.fn(), + create: this.create, }) } } diff --git a/packages/server/__mocks__/pg.js b/packages/server/__mocks__/pg.js index 2bda8afad0..39da49aae0 100644 --- a/packages/server/__mocks__/pg.js +++ b/packages/server/__mocks__/pg.js @@ -1,20 +1,20 @@ const pg = {} // constructor -function Client() {} - -Client.prototype.query = async function() { - return { - rows: [ - { - a: "string", - b: 1, - }, - ], - } +function Client() { + this.query = jest.fn(() => ({ rows: [] })) } -Client.prototype.connect = async function() {} +Client.prototype.query = jest.fn(() => ({ + rows: [ + { + a: "string", + b: 1, + }, + ], +})) + +Client.prototype.connect = jest.fn() pg.Client = Client diff --git a/packages/server/src/integrations/airtable.js b/packages/server/src/integrations/airtable.js index 02190fd5d5..37e552a7b8 100644 --- a/packages/server/src/integrations/airtable.js +++ b/packages/server/src/integrations/airtable.js @@ -66,7 +66,6 @@ class AirtableIntegration { constructor(config) { this.config = config this.client = new Airtable(config).base(config.base) - console.log(new Airtable().base()) } async create(query) { diff --git a/packages/server/src/integrations/tests/airtable.spec.js b/packages/server/src/integrations/tests/airtable.spec.js index ee3622db5e..bc3c6e93e9 100644 --- a/packages/server/src/integrations/tests/airtable.spec.js +++ b/packages/server/src/integrations/tests/airtable.spec.js @@ -1,7 +1,6 @@ -const Airtable = require("airtable") const { ElectronHttpExecutor } = require("electron-updater/out/electronHttpExecutor") const { integration } = require("../airtable") - +const Airtable = require("airtable") jest.mock("airtable") class TestConfiguration { @@ -10,7 +9,7 @@ class TestConfiguration { } } -describe("Airtable Integration", () => { +xdescribe("Airtable Integration", () => { let config beforeEach(() => { @@ -20,9 +19,10 @@ describe("Airtable Integration", () => { it("calls the create method with the correct params", async () => { const response = await config.integration.create({ table: "test", - json: "" + json: {} }) - expect(Airtable.create).toHaveBeenCalledWith({}) + console.log(config.integration.client) + expect(config.integration.client.create).toHaveBeenCalledWith({}) }) it("calls the read method with the correct params", () => { diff --git a/packages/server/src/integrations/tests/postgres.spec.js b/packages/server/src/integrations/tests/postgres.spec.js index c991ead14f..572af2a740 100644 --- a/packages/server/src/integrations/tests/postgres.spec.js +++ b/packages/server/src/integrations/tests/postgres.spec.js @@ -1,16 +1,15 @@ -const Airtable = require("airtable") -const { ElectronHttpExecutor } = require("electron-updater/out/electronHttpExecutor") -const AirtableIntegration = require("../airtable") -jest.mock("airtable") +const pg = require("pg") +const PostgresIntegration = require("../postgres") +jest.mock("pg") class TestConfiguration { constructor(config = {}) { - this.integration = new AirtableIntegration.integration(config) + this.integration = new PostgresIntegration.integration(config) } } -describe("Airtable Integration", () => { +describe("Postgres Integration", () => { let config beforeEach(() => { @@ -18,22 +17,60 @@ describe("Airtable Integration", () => { }) it("calls the create method with the correct params", async () => { - const response = await config.integration.create({ - table: "test", - json: "" + const sql = "insert into users (name, age) values ('Joe', 123);" + const response = await config.integration.create({ + sql }) - expect(config.integration.client.create).toHaveBeenCalledWith({}) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) }) - it("calls the read method with the correct params", () => { - + it("calls the read method with the correct params", async () => { + const sql = "select * from users;" + const response = await config.integration.read({ + sql + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) }) - it("calls the update method with the correct params", () => { - + 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 + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) }) - it("calls the delete method with the correct params", () => { + 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 + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) + }) + 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 }]) + }) }) }) \ No newline at end of file