From 2da1d27ba63d848af6e47ce29384cb24008c7181 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 16 Mar 2021 13:54:39 +0000 Subject: [PATCH] arangodb tests --- packages/server/__mocks__/arangojs.js | 21 +++++++ packages/server/__mocks__/aws-sdk.js | 3 +- packages/server/__mocks__/mongodb.js | 19 +++++++ packages/server/__mocks__/mssql.js | 14 +++++ .../src/integrations/tests/arangodb.spec.js | 35 ++++++++++++ .../tests/microsoftSqlServer.spec.js | 55 +++++++++++++++++++ .../src/integrations/tests/mongo.spec.js | 40 ++++++++++++++ .../src/integrations/tests/rest.spec.js | 0 .../server/src/integrations/tests/s3.spec.js | 26 +++++++++ .../middleware/tests/authenticated.spec.js | 1 + 10 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 packages/server/__mocks__/arangojs.js create mode 100644 packages/server/__mocks__/mongodb.js create mode 100644 packages/server/__mocks__/mssql.js create mode 100644 packages/server/src/integrations/tests/arangodb.spec.js create mode 100644 packages/server/src/integrations/tests/microsoftSqlServer.spec.js create mode 100644 packages/server/src/integrations/tests/mongo.spec.js create mode 100644 packages/server/src/integrations/tests/rest.spec.js create mode 100644 packages/server/src/integrations/tests/s3.spec.js diff --git a/packages/server/__mocks__/arangojs.js b/packages/server/__mocks__/arangojs.js new file mode 100644 index 0000000000..1a40529ca0 --- /dev/null +++ b/packages/server/__mocks__/arangojs.js @@ -0,0 +1,21 @@ +const arangodb = {} + +arangodb.Database = function() { + this.query = jest.fn(() => ({ + all: jest.fn(), + })) + this.collection = jest.fn(() => "collection") + this.close = jest.fn() +} + +arangodb.aql = (strings, ...args) => { + let str = strings.join("{}") + + for (let arg of args) { + str = str.replace("{}", arg) + } + + return str +} + +module.exports = arangodb diff --git a/packages/server/__mocks__/aws-sdk.js b/packages/server/__mocks__/aws-sdk.js index 49b430603b..503d098256 100644 --- a/packages/server/__mocks__/aws-sdk.js +++ b/packages/server/__mocks__/aws-sdk.js @@ -26,12 +26,13 @@ function DocumentClient() { function S3() { this.listObjects = jest.fn( response({ - foo: {}, + Contents: {}, }) ) } aws.DynamoDB = { DocumentClient } +aws.S3 = S3 aws.config = { update: jest.fn() } module.exports = aws diff --git a/packages/server/__mocks__/mongodb.js b/packages/server/__mocks__/mongodb.js new file mode 100644 index 0000000000..160ca89ebe --- /dev/null +++ b/packages/server/__mocks__/mongodb.js @@ -0,0 +1,19 @@ +const mongodb = {} + +mongodb.MongoClient = function() { + this.connect = jest.fn() + this.close = jest.fn() + this.insertOne = jest.fn() + this.find = jest.fn(() => ({ toArray: () => [] })) + + this.collection = jest.fn(() => ({ + insertOne: this.insertOne, + find: this.find, + })) + + this.db = () => ({ + collection: this.collection, + }) +} + +module.exports = mongodb diff --git a/packages/server/__mocks__/mssql.js b/packages/server/__mocks__/mssql.js new file mode 100644 index 0000000000..6119c014da --- /dev/null +++ b/packages/server/__mocks__/mssql.js @@ -0,0 +1,14 @@ +const mssql = {} + +mssql.query = jest.fn(() => ({ + recordset: [ + { + a: "string", + b: 1, + }, + ], +})) + +mssql.connect = jest.fn(() => ({ recordset: [] })) + +module.exports = mssql diff --git a/packages/server/src/integrations/tests/arangodb.spec.js b/packages/server/src/integrations/tests/arangodb.spec.js new file mode 100644 index 0000000000..437a7fd3ec --- /dev/null +++ b/packages/server/src/integrations/tests/arangodb.spec.js @@ -0,0 +1,35 @@ +const arangodb = require("arangojs") +const ArangoDBIntegration = require("../arangodb") +jest.mock("arangojs") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new ArangoDBIntegration.integration(config) + } +} + +describe("ArangoDB Integration", () => { + let config + let indexName = "Users" + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const body = { + json: "Hello" + } + + const response = await config.integration.create(body) + expect(config.integration.client.query).toHaveBeenCalledWith(`INSERT Hello INTO collection RETURN NEW`) + }) + + it("calls the read method with the correct params", async () => { + const query = { + json: `test`, + } + const response = await config.integration.read(query) + expect(config.integration.client.query).toHaveBeenCalledWith(query.sql) + }) +}) \ No newline at end of file diff --git a/packages/server/src/integrations/tests/microsoftSqlServer.spec.js b/packages/server/src/integrations/tests/microsoftSqlServer.spec.js new file mode 100644 index 0000000000..6b0fd62000 --- /dev/null +++ b/packages/server/src/integrations/tests/microsoftSqlServer.spec.js @@ -0,0 +1,55 @@ +const sqlServer = require("mssql") +const MSSQLIntegration = require("../microsoftSqlServer") +jest.mock("mssql") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new MSSQLIntegration.integration(config) + } +} + +describe("MS SQL Server 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) + }) + + 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({ + sql + }) + expect(response).toEqual([{ created: 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/mongo.spec.js b/packages/server/src/integrations/tests/mongo.spec.js new file mode 100644 index 0000000000..1e37d5dd70 --- /dev/null +++ b/packages/server/src/integrations/tests/mongo.spec.js @@ -0,0 +1,40 @@ +const mongo = require("mongodb") +const MongoDBIntegration = require("../mongodb") +jest.mock("mongodb") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new MongoDBIntegration.integration(config) + } +} + +describe("MongoDB Integration", () => { + let config + let indexName = "Users" + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the create method with the correct params", async () => { + const body = { + name: "Hello" + } + const response = await config.integration.create({ + index: indexName, + json: body + }) + expect(config.integration.client.insertOne).toHaveBeenCalledWith(body) + }) + + it("calls the read method with the correct params", async () => { + const query = { + json: { + address: "test" + } + } + const response = await config.integration.read(query) + expect(config.integration.client.find).toHaveBeenCalledWith(query.json) + expect(response).toEqual(expect.any(Array)) + }) +}) \ No newline at end of file diff --git a/packages/server/src/integrations/tests/rest.spec.js b/packages/server/src/integrations/tests/rest.spec.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/server/src/integrations/tests/s3.spec.js b/packages/server/src/integrations/tests/s3.spec.js new file mode 100644 index 0000000000..7ac403dbd4 --- /dev/null +++ b/packages/server/src/integrations/tests/s3.spec.js @@ -0,0 +1,26 @@ +const AWS = require("aws-sdk") +const S3Integration = require("../s3") +jest.mock("aws-sdk") + +class TestConfiguration { + constructor(config = {}) { + this.integration = new S3Integration.integration(config) + } +} + +describe("S3 Integration", () => { + let config + + beforeEach(() => { + config = new TestConfiguration() + }) + + it("calls the read method with the correct params", async () => { + const response = await config.integration.read({ + bucket: "test" + }) + expect(config.integration.client.listObjects).toHaveBeenCalledWith({ + Bucket: "test" + }) + }) +}) \ No newline at end of file diff --git a/packages/server/src/middleware/tests/authenticated.spec.js b/packages/server/src/middleware/tests/authenticated.spec.js index fe7e592528..26c9e3f15d 100644 --- a/packages/server/src/middleware/tests/authenticated.spec.js +++ b/packages/server/src/middleware/tests/authenticated.spec.js @@ -2,6 +2,7 @@ const { AuthTypes } = require("../../constants") const authenticatedMiddleware = require("../authenticated") const jwt = require("jsonwebtoken") jest.mock("jsonwebtoken") +jest.dontMock("pouchdb") class TestConfiguration { constructor(middleware) {