From e7b0564ca379444b7d634451a33c8224ece06c44 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Mon, 15 Mar 2021 16:26:46 +0000 Subject: [PATCH] couchDB tests --- packages/server/__mocks__/mysql.js | 10 +++ packages/server/__mocks__/pg.js | 4 +- packages/server/__mocks__/pouchdb.js | 8 ++ .../src/integrations/tests/couchdb.spec.js | 61 +++++++++++++++ .../src/integrations/tests/mysql.spec.js | 75 +++++++++++++++++++ .../src/integrations/tests/postgres.spec.js | 5 +- 6 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 packages/server/__mocks__/mysql.js create mode 100644 packages/server/__mocks__/pouchdb.js create mode 100644 packages/server/src/integrations/tests/couchdb.spec.js create mode 100644 packages/server/src/integrations/tests/mysql.spec.js diff --git a/packages/server/__mocks__/mysql.js b/packages/server/__mocks__/mysql.js new file mode 100644 index 0000000000..03036e8ce8 --- /dev/null +++ b/packages/server/__mocks__/mysql.js @@ -0,0 +1,10 @@ +const mysql = {} + +const client = { + connect: jest.fn(), + query: jest.fn((sql, cb) => cb), +} + +mysql.createConnection = jest.fn(() => client) + +module.exports = mysql diff --git a/packages/server/__mocks__/pg.js b/packages/server/__mocks__/pg.js index 39da49aae0..0d8b8cc26a 100644 --- a/packages/server/__mocks__/pg.js +++ b/packages/server/__mocks__/pg.js @@ -1,9 +1,7 @@ const pg = {} // constructor -function Client() { - this.query = jest.fn(() => ({ rows: [] })) -} +function Client() {} Client.prototype.query = jest.fn(() => ({ rows: [ diff --git a/packages/server/__mocks__/pouchdb.js b/packages/server/__mocks__/pouchdb.js new file mode 100644 index 0000000000..bb63ca6446 --- /dev/null +++ b/packages/server/__mocks__/pouchdb.js @@ -0,0 +1,8 @@ +function CouchDB() { + this.post = jest.fn() + this.allDocs = jest.fn(() => ({ rows: [] })) + this.put = jest.fn() + this.remove = jest.fn() +} + +module.exports = CouchDB diff --git a/packages/server/src/integrations/tests/couchdb.spec.js b/packages/server/src/integrations/tests/couchdb.spec.js new file mode 100644 index 0000000000..fc84f4ace9 --- /dev/null +++ b/packages/server/src/integrations/tests/couchdb.spec.js @@ -0,0 +1,61 @@ +const PouchDB = require("pouchdb") +const CouchDBIntegration = require("../couchdb") +jest.mock("pouchdb") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new CouchDBIntegration.integration(config) + } +} + +describe("CouchDB Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const doc = { + test: 1 + } + const response = await config.integration.create({ + json: doc + }) + expect(config.integration.client.post).toHaveBeenCalledWith(doc) + }) + + it("calls the read method with the correct params", async () => { + const doc = { + name: "search" + } + + const response = await config.integration.read({ + json: doc + }) + + expect(config.integration.client.allDocs).toHaveBeenCalledWith({ + include_docs: true, + name: "search" + }) + }) + + it("calls the update method with the correct params", async () => { + const doc = { + _id: "1234", + name: "search" + } + + const response = await config.integration.update({ + json: doc + }) + + expect(config.integration.client.put).toHaveBeenCalledWith(doc) + }) + + it("calls the delete method with the correct params", async () => { + const id = "1234" + const response = await config.integration.delete({ id }) + expect(config.integration.client.remove).toHaveBeenCalledWith(id) + }) +}) \ No newline at end of file diff --git a/packages/server/src/integrations/tests/mysql.spec.js b/packages/server/src/integrations/tests/mysql.spec.js new file mode 100644 index 0000000000..e275dbccfa --- /dev/null +++ b/packages/server/src/integrations/tests/mysql.spec.js @@ -0,0 +1,75 @@ +const pg = require("mysql") +const MySQLIntegration = require("../mysql") +jest.mock("mysql") + +class TestConfiguration { + constructor(config = { ssl: {} }) { + this.integration = new MySQLIntegration.integration(config) + } +} + +describe("MySQL Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const sql = "insert into users (name, age) values ('Joe', 123);" + const response = await config.integration.create({ + sql + }) + expect(config.integration.client.query).toHaveBeenCalledWith(sql) + }) + + 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", 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", 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 diff --git a/packages/server/src/integrations/tests/postgres.spec.js b/packages/server/src/integrations/tests/postgres.spec.js index 572af2a740..8a8876a556 100644 --- a/packages/server/src/integrations/tests/postgres.spec.js +++ b/packages/server/src/integrations/tests/postgres.spec.js @@ -1,4 +1,3 @@ - const pg = require("pg") const PostgresIntegration = require("../postgres") jest.mock("pg") @@ -49,6 +48,10 @@ describe("Postgres Integration", () => { }) describe("no rows returned", () => { + beforeEach(() => { + config.integration.client.query.mockImplementation(() => ({ rows: [] })) + }) + 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({