From f49d61bc5cdad85431c2e26ab6274219bb8a366f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 31 Aug 2022 19:21:45 +0100 Subject: [PATCH] Updating test cases to TS to get them working properly with the full TS implementations. --- packages/server/__mocks__/mongodb.ts | 2 +- packages/server/src/api/controllers/screen.js | 2 +- .../{airtable.spec.js => airtable.spec.ts} | 39 +- .../{arangodb.spec.js => arangodb.spec.ts} | 21 +- .../{couchdb.spec.js => couchdb.spec.ts} | 60 +-- .../{dynamodb.spec.js => dynamodb.spec.ts} | 86 ++-- ...icsearch.spec.js => elasticsearch.spec.ts} | 43 +- .../{firebase.spec.js => firebase.spec.ts} | 61 +-- ...ver.spec.js => microsoftSqlServer.spec.ts} | 23 +- .../tests/{mongo.spec.js => mongo.spec.ts} | 81 ++-- .../tests/{mysql.spec.js => mysql.spec.ts} | 32 +- .../tests/{oracle.spec.js => oracle.spec.ts} | 36 +- .../{postgres.spec.js => postgres.spec.ts} | 36 +- .../tests/{redis.spec.js => redis.spec.ts} | 33 +- .../tests/{rest.spec.js => rest.spec.ts} | 30 +- .../tests/{s3.spec.js => s3.spec.ts} | 18 +- .../tests/{sql.spec.js => sql.spec.ts} | 405 ++++++++++-------- 17 files changed, 558 insertions(+), 450 deletions(-) rename packages/server/src/integrations/tests/{airtable.spec.js => airtable.spec.ts} (75%) rename packages/server/src/integrations/tests/{arangodb.spec.js => arangodb.spec.ts} (75%) rename packages/server/src/integrations/tests/{couchdb.spec.js => couchdb.spec.ts} (58%) rename packages/server/src/integrations/tests/{dynamodb.spec.js => dynamodb.spec.ts} (68%) rename packages/server/src/integrations/tests/{elasticsearch.spec.js => elasticsearch.spec.ts} (72%) rename packages/server/src/integrations/tests/{firebase.spec.js => firebase.spec.ts} (70%) rename packages/server/src/integrations/tests/{microsoftSqlServer.spec.js => microsoftSqlServer.spec.ts} (75%) rename packages/server/src/integrations/tests/{mongo.spec.js => mongo.spec.ts} (90%) rename packages/server/src/integrations/tests/{mysql.spec.js => mysql.spec.ts} (80%) rename packages/server/src/integrations/tests/{oracle.spec.js => oracle.spec.ts} (83%) rename packages/server/src/integrations/tests/{postgres.spec.js => postgres.spec.ts} (78%) rename packages/server/src/integrations/tests/{redis.spec.js => redis.spec.ts} (71%) rename packages/server/src/integrations/tests/{rest.spec.js => rest.spec.ts} (96%) rename packages/server/src/integrations/tests/{s3.spec.js => s3.spec.ts} (55%) rename packages/server/src/integrations/tests/{sql.spec.js => sql.spec.ts} (60%) diff --git a/packages/server/__mocks__/mongodb.ts b/packages/server/__mocks__/mongodb.ts index 92ec89227f..4a1867f6f9 100644 --- a/packages/server/__mocks__/mongodb.ts +++ b/packages/server/__mocks__/mongodb.ts @@ -33,7 +33,7 @@ module MongoMock { }) } - mongodb.ObjectID = require("mongodb").ObjectID + mongodb.ObjectID = jest.requireActual("mongodb").ObjectID module.exports = mongodb } diff --git a/packages/server/src/api/controllers/screen.js b/packages/server/src/api/controllers/screen.js index 99d12d064c..07b15dd8a1 100644 --- a/packages/server/src/api/controllers/screen.js +++ b/packages/server/src/api/controllers/screen.js @@ -8,7 +8,7 @@ const { AccessController } = require("@budibase/backend-core/roles") const { getAppDB } = require("@budibase/backend-core/context") const { events } = require("@budibase/backend-core") const { getGlobalDB } = require("@budibase/backend-core/tenancy") -import { updateAppPackage } from "./application" +const { updateAppPackage } = require("./application") exports.fetch = async ctx => { const db = getAppDB() diff --git a/packages/server/src/integrations/tests/airtable.spec.js b/packages/server/src/integrations/tests/airtable.spec.ts similarity index 75% rename from packages/server/src/integrations/tests/airtable.spec.js rename to packages/server/src/integrations/tests/airtable.spec.ts index df676a1397..26a389456f 100644 --- a/packages/server/src/integrations/tests/airtable.spec.js +++ b/packages/server/src/integrations/tests/airtable.spec.ts @@ -1,10 +1,12 @@ -const Airtable = require("airtable") -const AirtableIntegration = require("../airtable") +import { default as AirtableIntegration } from "../airtable" jest.mock("airtable") class TestConfiguration { - constructor(config = {}) { - this.integration = new AirtableIntegration.integration(config) + integration: any + client: any + + constructor(config: any = {}) { + this.integration = new AirtableIntegration.integration(config) this.client = { create: jest.fn(), select: jest.fn(() => ({ @@ -13,12 +15,12 @@ class TestConfiguration { update: jest.fn(), destroy: jest.fn(), } - this.integration.client = () => this.client + this.integration.client = () => this.client } } describe("Airtable Integration", () => { - let config + let config: any beforeEach(() => { config = new TestConfiguration() @@ -27,22 +29,23 @@ describe("Airtable Integration", () => { it("calls the create method with the correct params", async () => { const response = await config.integration.create({ table: "test", - json: {} + json: {}, }) expect(config.client.create).toHaveBeenCalledWith([ { - fields: {} - } + fields: {}, + }, ]) }) it("calls the read method with the correct params", async () => { const response = await config.integration.read({ table: "test", - view: "Grid view" + view: "Grid view", }) expect(config.client.select).toHaveBeenCalledWith({ - maxRecords: 10, view: "Grid view" + maxRecords: 10, + view: "Grid view", }) }) @@ -51,22 +54,22 @@ describe("Airtable Integration", () => { table: "table", id: "123", json: { - name: "test" - } + name: "test", + }, }) expect(config.client.update).toHaveBeenCalledWith([ { id: "123", - fields: { name: "test" } - } + fields: { name: "test" }, + }, ]) }) it("calls the delete method with the correct params", async () => { - const ids = [1,2,3,4] + const ids = [1, 2, 3, 4] const response = await config.integration.delete({ - ids + ids, }) expect(config.client.destroy).toHaveBeenCalledWith(ids) }) -}) \ No newline at end of file +}) diff --git a/packages/server/src/integrations/tests/arangodb.spec.js b/packages/server/src/integrations/tests/arangodb.spec.ts similarity index 75% rename from packages/server/src/integrations/tests/arangodb.spec.js rename to packages/server/src/integrations/tests/arangodb.spec.ts index 437a7fd3ec..401b2ec707 100644 --- a/packages/server/src/integrations/tests/arangodb.spec.js +++ b/packages/server/src/integrations/tests/arangodb.spec.ts @@ -1,15 +1,16 @@ -const arangodb = require("arangojs") -const ArangoDBIntegration = require("../arangodb") +import { default as ArangoDBIntegration } from "../arangodb" jest.mock("arangojs") class TestConfiguration { - constructor(config = {}) { - this.integration = new ArangoDBIntegration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new ArangoDBIntegration.integration(config) } } describe("ArangoDB Integration", () => { - let config + let config: any let indexName = "Users" beforeEach(() => { @@ -18,18 +19,20 @@ describe("ArangoDB Integration", () => { it("calls the create method with the correct params", async () => { const body = { - json: "Hello" + json: "Hello", } const response = await config.integration.create(body) - expect(config.integration.client.query).toHaveBeenCalledWith(`INSERT Hello INTO collection RETURN NEW`) + 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`, + sql: `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/couchdb.spec.js b/packages/server/src/integrations/tests/couchdb.spec.ts similarity index 58% rename from packages/server/src/integrations/tests/couchdb.spec.js rename to packages/server/src/integrations/tests/couchdb.spec.ts index 65c9c83ad2..e05a2bd247 100644 --- a/packages/server/src/integrations/tests/couchdb.spec.js +++ b/packages/server/src/integrations/tests/couchdb.spec.ts @@ -1,23 +1,29 @@ -jest.mock("pouchdb", () => function CouchDBMock() { - this.post = jest.fn() - this.allDocs = jest.fn(() => ({ rows: [] })) - this.put = jest.fn() - this.get = jest.fn() - this.remove = jest.fn() - this.plugin = jest.fn() - this.close = jest.fn() -}) +jest.mock( + "pouchdb", + () => + function CouchDBMock(this: any) { + this.post = jest.fn() + this.allDocs = jest.fn(() => ({ rows: [] })) + this.put = jest.fn() + this.get = jest.fn() + this.remove = jest.fn() + this.plugin = jest.fn() + this.close = jest.fn() + } +) -const CouchDBIntegration = require("../couchdb") +import { default as CouchDBIntegration } from "../couchdb" class TestConfiguration { - constructor(config = {}) { - this.integration = new CouchDBIntegration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new CouchDBIntegration.integration(config) } } describe("CouchDB Integration", () => { - let config + let config: any beforeEach(() => { config = new TestConfiguration() @@ -25,37 +31,37 @@ describe("CouchDB Integration", () => { it("calls the create method with the correct params", async () => { const doc = { - test: 1 - } - const response = await config.integration.create({ - json: 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" - } + name: "search", + } - const response = await config.integration.read({ - json: doc + const response = await config.integration.read({ + json: doc, }) expect(config.integration.client.allDocs).toHaveBeenCalledWith({ include_docs: true, - name: "search" + name: "search", }) }) it("calls the update method with the correct params", async () => { const doc = { _id: "1234", - name: "search" - } + name: "search", + } - const response = await config.integration.update({ - json: doc + const response = await config.integration.update({ + json: doc, }) expect(config.integration.client.put).toHaveBeenCalledWith(doc) @@ -67,4 +73,4 @@ describe("CouchDB Integration", () => { expect(config.integration.client.get).toHaveBeenCalledWith(id) expect(config.integration.client.remove).toHaveBeenCalled() }) -}) \ No newline at end of file +}) diff --git a/packages/server/src/integrations/tests/dynamodb.spec.js b/packages/server/src/integrations/tests/dynamodb.spec.ts similarity index 68% rename from packages/server/src/integrations/tests/dynamodb.spec.js rename to packages/server/src/integrations/tests/dynamodb.spec.ts index 198ed6a4b4..d245875173 100644 --- a/packages/server/src/integrations/tests/dynamodb.spec.js +++ b/packages/server/src/integrations/tests/dynamodb.spec.ts @@ -1,15 +1,16 @@ -const AWS = require("aws-sdk") -const DynamoDBIntegration = require("../dynamodb") +import { default as DynamoDBIntegration } from "../dynamodb" jest.mock("aws-sdk") class TestConfiguration { - constructor(config = {}) { - this.integration = new DynamoDBIntegration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new DynamoDBIntegration.integration(config) } } describe("DynamoDB Integration", () => { - let config + let config: any let tableName = "Users" beforeEach(() => { @@ -17,25 +18,25 @@ describe("DynamoDB Integration", () => { }) it("calls the create method with the correct params", async () => { - const response = await config.integration.create({ + const response = await config.integration.create({ table: tableName, json: { - Name: "John" - } + Name: "John", + }, }) expect(config.integration.client.put).toHaveBeenCalledWith({ TableName: tableName, - Name: "John" + Name: "John", }) }) it("calls the read method with the correct params", async () => { const indexName = "Test" - const response = await config.integration.read({ + const response = await config.integration.read({ table: tableName, - index: indexName, - json: {} + index: indexName, + json: {}, }) expect(config.integration.client.query).toHaveBeenCalledWith({ TableName: tableName, @@ -47,57 +48,59 @@ describe("DynamoDB Integration", () => { it("calls the scan method with the correct params", async () => { const indexName = "Test" - const response = await config.integration.scan({ + const response = await config.integration.scan({ table: tableName, - index: indexName, - json: {} + index: indexName, + json: {}, }) expect(config.integration.client.scan).toHaveBeenCalledWith({ TableName: tableName, IndexName: indexName, }) - expect(response).toEqual([{ - Name: "test" - }]) + expect(response).toEqual([ + { + Name: "test", + }, + ]) }) it("calls the get method with the correct params", async () => { - const response = await config.integration.get({ + const response = await config.integration.get({ table: tableName, json: { - Id: 123 - } + Id: 123, + }, }) expect(config.integration.client.get).toHaveBeenCalledWith({ TableName: tableName, - Id: 123 + Id: 123, }) }) it("calls the update method with the correct params", async () => { - const response = await config.integration.update({ + const response = await config.integration.update({ table: tableName, json: { - Name: "John" - } + Name: "John", + }, }) expect(config.integration.client.update).toHaveBeenCalledWith({ TableName: tableName, - Name: "John" + Name: "John", }) }) it("calls the delete method with the correct params", async () => { - const response = await config.integration.delete({ + const response = await config.integration.delete({ table: tableName, json: { - Name: "John" - } + Name: "John", + }, }) expect(config.integration.client.delete).toHaveBeenCalledWith({ TableName: tableName, - Name: "John" + Name: "John", }) }) @@ -105,14 +108,14 @@ describe("DynamoDB Integration", () => { const config = { region: "us-east-1", accessKeyId: "test", - secretAccessKeyId: "test" + secretAccessKey: "test", } - const integration = new DynamoDBIntegration.integration(config) + const integration: any = new DynamoDBIntegration.integration(config) expect(integration.config).toEqual({ currentClockSkew: true, - ...config + ...config, }) }) @@ -120,16 +123,16 @@ describe("DynamoDB Integration", () => { const config = { region: "us-east-1", accessKeyId: "test", - secretAccessKeyId: "test", - endpoint: "localhost:8080" + secretAccessKey: "test", + endpoint: "localhost:8080", } - const integration = new DynamoDBIntegration.integration(config) + const integration: any = new DynamoDBIntegration.integration(config) expect(integration.config).toEqual({ region: "us-east-1", currentClockSkew: true, - endpoint: "localhost:8080" + endpoint: "localhost:8080", }) }) @@ -137,15 +140,16 @@ describe("DynamoDB Integration", () => { const config = { region: "us-east-1", accessKeyId: "test", - secretAccessKeyId: "test", - endpoint: "dynamodb.aws.foo.net" + secretAccessKey: "test", + endpoint: "dynamodb.aws.foo.net", } const integration = new DynamoDBIntegration.integration(config) + // @ts-ignore expect(integration.config).toEqual({ currentClockSkew: true, - ...config + ...config, }) }) -}) \ No newline at end of file +}) diff --git a/packages/server/src/integrations/tests/elasticsearch.spec.js b/packages/server/src/integrations/tests/elasticsearch.spec.ts similarity index 72% rename from packages/server/src/integrations/tests/elasticsearch.spec.js rename to packages/server/src/integrations/tests/elasticsearch.spec.ts index fc97e04bcc..c839b9b2d4 100644 --- a/packages/server/src/integrations/tests/elasticsearch.spec.js +++ b/packages/server/src/integrations/tests/elasticsearch.spec.ts @@ -1,15 +1,16 @@ -const elasticsearch = require("@elastic/elasticsearch") -const ElasticSearchIntegration = require("../elasticsearch") +import { default as ElasticSearchIntegration } from "../elasticsearch" jest.mock("@elastic/elasticsearch") class TestConfiguration { - constructor(config = {}) { - this.integration = new ElasticSearchIntegration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new ElasticSearchIntegration.integration(config) } } describe("Elasticsearch Integration", () => { - let config + let config: any let indexName = "Users" beforeEach(() => { @@ -18,15 +19,15 @@ describe("Elasticsearch Integration", () => { it("calls the create method with the correct params", async () => { const body = { - name: "Hello" + name: "Hello", } - const response = await config.integration.create({ + const response = await config.integration.create({ index: indexName, - json: body + json: body, }) expect(config.integration.client.index).toHaveBeenCalledWith({ index: indexName, - body + body, }) }) @@ -34,43 +35,43 @@ describe("Elasticsearch Integration", () => { const body = { query: { term: { - name: "kimchy" - } - } + name: "kimchy", + }, + }, } - const response = await config.integration.read({ + const response = await config.integration.read({ index: indexName, - json: body + json: body, }) expect(config.integration.client.search).toHaveBeenCalledWith({ index: indexName, - body + body, }) expect(response).toEqual(expect.any(Array)) }) it("calls the update method with the correct params", async () => { const body = { - name: "updated" + name: "updated", } - const response = await config.integration.update({ + const response = await config.integration.update({ id: "1234", index: indexName, - json: body + json: body, }) expect(config.integration.client.update).toHaveBeenCalledWith({ id: "1234", index: indexName, - body + body, }) expect(response).toEqual(expect.any(Array)) }) it("calls the delete method with the correct params", async () => { const body = { - id: "1234" + id: "1234", } const response = await config.integration.delete(body) @@ -78,4 +79,4 @@ describe("Elasticsearch Integration", () => { expect(config.integration.client.delete).toHaveBeenCalledWith(body) expect(response).toEqual(expect.any(Array)) }) -}) \ No newline at end of file +}) diff --git a/packages/server/src/integrations/tests/firebase.spec.js b/packages/server/src/integrations/tests/firebase.spec.ts similarity index 70% rename from packages/server/src/integrations/tests/firebase.spec.js rename to packages/server/src/integrations/tests/firebase.spec.ts index 97d3b2c0d7..befbb80ccb 100644 --- a/packages/server/src/integrations/tests/firebase.spec.js +++ b/packages/server/src/integrations/tests/firebase.spec.ts @@ -1,92 +1,97 @@ -const firebase = require("@google-cloud/firestore") -const FirebaseIntegration = require("../firebase") +import { default as FirebaseIntegration } from "../firebase" jest.mock("@google-cloud/firestore") class TestConfiguration { - constructor(config = {}) { - this.integration = new FirebaseIntegration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new FirebaseIntegration.integration(config) } } describe("Firebase Integration", () => { - let config + let config: any let tableName = "Users" beforeEach(() => { config = new TestConfiguration({ - serviceAccount: "{}" + serviceAccount: "{}", }) }) it("calls the create method with the correct params", async () => { - await config.integration.create({ + await config.integration.create({ table: tableName, json: { - Name: "Test Name" + Name: "Test Name", }, extra: { - collection: "test" - } + collection: "test", + }, }) expect(config.integration.client.collection).toHaveBeenCalledWith("test") - expect(config.integration.client.set).toHaveBeenCalledWith({ + expect(config.integration.client.set).toHaveBeenCalledWith({ Name: "Test Name", - id: "test_id" + id: "test_id", }) }) it("calls the read method with the correct params", async () => { - const response = await config.integration.read({ + const response = await config.integration.read({ table: tableName, json: { - Name: "Test" + Name: "Test", }, extra: { collection: "test", filterField: "field", filter: "==", filterValue: "value", - } + }, }) expect(config.integration.client.collection).toHaveBeenCalledWith("test") - expect(config.integration.client.where).toHaveBeenCalledWith("field", "==", "value") - expect(response).toEqual([{ result: "test"}]) + expect(config.integration.client.where).toHaveBeenCalledWith( + "field", + "==", + "value" + ) + expect(response).toEqual([{ result: "test" }]) }) it("calls the update method with the correct params", async () => { - const response = await config.integration.update({ + const response = await config.integration.update({ table: tableName, json: { id: "test", - Name: "Test" + Name: "Test", }, extra: { - collection: "test" - } + collection: "test", + }, }) expect(config.integration.client.collection).toHaveBeenCalledWith("test") expect(config.integration.client.update).toHaveBeenCalledWith({ Name: "Test", - id: "test" + id: "test", }) expect(response).toEqual({ - result: "test" + result: "test", }) }) it("calls the delete method with the correct params", async () => { - const response = await config.integration.delete({ + const response = await config.integration.delete({ table: tableName, json: { id: "test", - Name: "Test" + Name: "Test", }, extra: { - collection: "test" - } + collection: "test", + }, }) expect(config.integration.client.collection).toHaveBeenCalledWith("test") expect(config.integration.client.doc).toHaveBeenCalledWith("test") expect(config.integration.client.delete).toHaveBeenCalled() }) -}) \ 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.ts similarity index 75% rename from packages/server/src/integrations/tests/microsoftSqlServer.spec.js rename to packages/server/src/integrations/tests/microsoftSqlServer.spec.ts index 19a99ad54b..8484e759a9 100644 --- a/packages/server/src/integrations/tests/microsoftSqlServer.spec.js +++ b/packages/server/src/integrations/tests/microsoftSqlServer.spec.ts @@ -1,15 +1,16 @@ -const sqlServer = require("mssql") -const MSSQLIntegration = require("../microsoftSqlServer") +import { default as MSSQLIntegration } from "../microsoftSqlServer" jest.mock("mssql") class TestConfiguration { - constructor(config = {}) { - this.integration = new MSSQLIntegration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new MSSQLIntegration.integration(config) } } describe("MS SQL Server Integration", () => { - let config + let config: any beforeEach(async () => { config = new TestConfiguration() @@ -23,7 +24,7 @@ describe("MS SQL Server Integration", () => { 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 + sql, }) expect(config.integration.client.request).toHaveBeenCalledWith() expect(response[0]).toEqual(sql) @@ -32,7 +33,7 @@ describe("MS SQL Server Integration", () => { it("calls the read method with the correct params", async () => { const sql = "select * from users;" const response = await config.integration.read({ - sql + sql, }) expect(config.integration.client.request).toHaveBeenCalledWith() expect(response[0]).toEqual(sql) @@ -45,11 +46,11 @@ describe("MS SQL Server Integration", () => { }) 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 + const sql = "insert into users (name, age) values ('Joe', 123);" + const response = await config.integration.create({ + sql, }) expect(response[0]).toEqual(sql) }) }) -}) \ 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.ts similarity index 90% rename from packages/server/src/integrations/tests/mongo.spec.js rename to packages/server/src/integrations/tests/mongo.spec.ts index 40aa6dbb58..a326d3ac2a 100644 --- a/packages/server/src/integrations/tests/mongo.spec.js +++ b/packages/server/src/integrations/tests/mongo.spec.ts @@ -1,22 +1,26 @@ const mongo = require("mongodb") -const MongoDBIntegration = require("../mongodb") +import { default as MongoDBIntegration } from "../mongodb" jest.mock("mongodb") class TestConfiguration { - constructor(config = {}) { + integration: any + + constructor(config: any = {}) { this.integration = new MongoDBIntegration.integration(config) } } function disableConsole() { jest.spyOn(console, "error") + // @ts-ignore console.error.mockImplementation(() => {}) + // @ts-ignore return console.error.mockRestore } describe("MongoDB Integration", () => { - let config + let config: any let indexName = "Users" beforeEach(() => { @@ -54,13 +58,16 @@ describe("MongoDB Integration", () => { id: "test", }, options: { - opt: "option" - } + opt: "option", + }, }, extra: { collection: "testCollection", actionTypes: "deleteOne" }, } await config.integration.delete(query) - expect(config.integration.client.deleteOne).toHaveBeenCalledWith(query.json.filter, query.json.options) + expect(config.integration.client.deleteOne).toHaveBeenCalledWith( + query.json.filter, + query.json.options + ) }) it("calls the update method with the correct params", async () => { @@ -108,7 +115,7 @@ describe("MongoDB Integration", () => { json: { filter: { _id: "ObjectId('ACBD12345678ABCD12345678')", - name: "ObjectId('BBBB12345678ABCD12345678')" + name: "ObjectId('BBBB12345678ABCD12345678')", }, update: { _id: "ObjectId('FFFF12345678ABCD12345678')", @@ -122,7 +129,7 @@ describe("MongoDB Integration", () => { } await config.integration.update(query) expect(config.integration.client.updateOne).toHaveBeenCalled() - + const args = config.integration.client.updateOne.mock.calls[0] expect(args[0]).toEqual({ _id: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), @@ -133,7 +140,7 @@ describe("MongoDB Integration", () => { name: mongo.ObjectID.createFromHexString("CCCC12345678ABCD12345678"), }) expect(args[2]).toEqual({ - upsert: false + upsert: false, }) }) @@ -143,7 +150,7 @@ describe("MongoDB Integration", () => { filter: { _id: { $eq: "ObjectId('ACBD12345678ABCD12345678')", - } + }, }, update: { $set: { @@ -158,20 +165,20 @@ describe("MongoDB Integration", () => { } await config.integration.update(query) expect(config.integration.client.updateOne).toHaveBeenCalled() - + const args = config.integration.client.updateOne.mock.calls[0] expect(args[0]).toEqual({ _id: { $eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), - } + }, }) expect(args[1]).toEqual({ $set: { _id: mongo.ObjectID.createFromHexString("FFFF12345678ABCD12345678"), - } + }, }) expect(args[2]).toEqual({ - upsert: true + upsert: true, }) }) @@ -181,12 +188,12 @@ describe("MongoDB Integration", () => { filter: { _id: { $eq: "ObjectId('ACBD12345678ABCD12345678')", - } + }, }, update: { $set: { name: "UPDATED", - age: 99 + age: 99, }, }, options: { @@ -197,21 +204,21 @@ describe("MongoDB Integration", () => { } await config.integration.read(query) expect(config.integration.client.findOneAndUpdate).toHaveBeenCalled() - + const args = config.integration.client.findOneAndUpdate.mock.calls[0] expect(args[0]).toEqual({ _id: { $eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), - } + }, }) expect(args[1]).toEqual({ $set: { name: "UPDATED", - age: 99 - } + age: 99, + }, }) expect(args[2]).toEqual({ - upsert: false + upsert: false, }) }) @@ -242,12 +249,12 @@ describe("MongoDB Integration", () => { } await config.integration.update(query) expect(config.integration.client.updateOne).toHaveBeenCalled() - + const args = config.integration.client.updateOne.mock.calls[0] expect(args[0]).toEqual({ _id: { $eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), - } + }, }) expect(args[1]).toEqual({ $set: { @@ -255,15 +262,17 @@ describe("MongoDB Integration", () => { data: [ { cid: 1 }, { cid: 2 }, - { nested: { - name: "test" - }} - ] + { + nested: { + name: "test", + }, + }, + ], }, }, }) expect(args[2]).toEqual({ - upsert: true + upsert: true, }) }) @@ -295,12 +304,12 @@ describe("MongoDB Integration", () => { } await config.integration.update(query) expect(config.integration.client.updateOne).toHaveBeenCalled() - + const args = config.integration.client.updateOne.mock.calls[0] expect(args[0]).toEqual({ _id: { $eq: mongo.ObjectID.createFromHexString("ACBD12345678ABCD12345678"), - } + }, }) expect(args[1]).toEqual({ $set: { @@ -308,16 +317,18 @@ describe("MongoDB Integration", () => { data: [ { cid: 1 }, { cid: 2 }, - { nested: { - name: "te}st" - }} - ] + { + nested: { + name: "te}st", + }, + }, + ], }, }, }) expect(args[2]).toEqual({ upsert: true, - extra: "ad\"{\"d" + extra: 'ad"{"d', }) }) }) diff --git a/packages/server/src/integrations/tests/mysql.spec.js b/packages/server/src/integrations/tests/mysql.spec.ts similarity index 80% rename from packages/server/src/integrations/tests/mysql.spec.js rename to packages/server/src/integrations/tests/mysql.spec.ts index 8304771d5d..a70eb73857 100644 --- a/packages/server/src/integrations/tests/mysql.spec.js +++ b/packages/server/src/integrations/tests/mysql.spec.ts @@ -1,14 +1,16 @@ -const MySQLIntegration = require("../mysql") +import { default as MySQLIntegration } from "../mysql" jest.mock("mysql2") class TestConfiguration { - constructor(config = { ssl: {} }) { + integration: any + + constructor(config: any = { ssl: {} }) { this.integration = new MySQLIntegration.integration(config) } } describe("MySQL Integration", () => { - let config + let config: any beforeEach(() => { config = new TestConfiguration() @@ -17,7 +19,7 @@ describe("MySQL Integration", () => { it("calls the create method with the correct params", async () => { const sql = "insert into users (name, age) values ('Joe', 123);" await config.integration.create({ - sql + sql, }) expect(config.integration.client.query).toHaveBeenCalledWith(sql, []) }) @@ -25,7 +27,7 @@ describe("MySQL Integration", () => { it("calls the read method with the correct params", async () => { const sql = "select * from users;" await config.integration.read({ - sql + sql, }) expect(config.integration.client.query).toHaveBeenCalledWith(sql, []) }) @@ -33,7 +35,7 @@ describe("MySQL Integration", () => { it("calls the update method with the correct params", async () => { const sql = "update table users set name = 'test';" await config.integration.update({ - sql + sql, }) expect(config.integration.client.query).toHaveBeenCalledWith(sql, []) }) @@ -41,34 +43,34 @@ describe("MySQL Integration", () => { it("calls the delete method with the correct params", async () => { const sql = "delete from users where name = 'todelete';" await config.integration.delete({ - sql + 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 + 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 + 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 + 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/oracle.spec.js b/packages/server/src/integrations/tests/oracle.spec.ts similarity index 83% rename from packages/server/src/integrations/tests/oracle.spec.js rename to packages/server/src/integrations/tests/oracle.spec.ts index 77f0525090..57dad48820 100644 --- a/packages/server/src/integrations/tests/oracle.spec.js +++ b/packages/server/src/integrations/tests/oracle.spec.ts @@ -1,17 +1,19 @@ const oracledb = require("oracledb") -const OracleIntegration = require("../oracle") +import { default as OracleIntegration } from "../oracle" jest.mock("oracledb") class TestConfiguration { - constructor(config = {}) { - this.integration = new OracleIntegration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new OracleIntegration.integration(config) } } const options = { autoCommit: true } describe("Oracle Integration", () => { - let config + let config: any beforeEach(() => { jest.clearAllMocks() @@ -26,7 +28,7 @@ describe("Oracle Integration", () => { it("calls the create method with the correct params", async () => { const sql = "insert into users (name, age) values ('Joe', 123);" await config.integration.create({ - sql + sql, }) expect(oracledb.executeMock).toHaveBeenCalledWith(sql, [], options) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) @@ -35,7 +37,7 @@ describe("Oracle Integration", () => { it("calls the read method with the correct params", async () => { const sql = "select * from users;" await config.integration.read({ - sql + sql, }) expect(oracledb.executeMock).toHaveBeenCalledWith(sql, [], options) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) @@ -43,8 +45,8 @@ describe("Oracle Integration", () => { 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 + const response = await config.integration.update({ + sql, }) expect(oracledb.executeMock).toHaveBeenCalledWith(sql, [], options) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) @@ -53,7 +55,7 @@ describe("Oracle Integration", () => { it("calls the delete method with the correct params", async () => { const sql = "delete from users where name = 'todelete';" await config.integration.delete({ - sql + sql, }) expect(oracledb.executeMock).toHaveBeenCalledWith(sql, [], options) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) @@ -65,9 +67,9 @@ describe("Oracle Integration", () => { }) 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 + const sql = "insert into users (name, age) values ('Joe', 123);" + const response = await config.integration.create({ + sql, }) expect(response).toEqual([{ created: true }]) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) @@ -75,8 +77,8 @@ describe("Oracle Integration", () => { 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 + const response = await config.integration.update({ + sql, }) expect(response).toEqual([{ updated: true }]) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) @@ -84,11 +86,11 @@ describe("Oracle Integration", () => { 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 + const response = await config.integration.delete({ + sql, }) expect(response).toEqual([{ deleted: true }]) expect(oracledb.executeMock).toHaveBeenCalledTimes(1) }) }) -}) \ 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.ts similarity index 78% rename from packages/server/src/integrations/tests/postgres.spec.js rename to packages/server/src/integrations/tests/postgres.spec.ts index 4ce5f12e96..a6be3aa623 100644 --- a/packages/server/src/integrations/tests/postgres.spec.js +++ b/packages/server/src/integrations/tests/postgres.spec.ts @@ -1,15 +1,17 @@ const pg = require("pg") -const PostgresIntegration = require("../postgres") +import { default as PostgresIntegration } from "../postgres" jest.mock("pg") class TestConfiguration { - constructor(config = {}) { - this.integration = new PostgresIntegration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new PostgresIntegration.integration(config) } } describe("Postgres Integration", () => { - let config + let config: any beforeEach(() => { config = new TestConfiguration() @@ -18,7 +20,7 @@ describe("Postgres Integration", () => { it("calls the create method with the correct params", async () => { const sql = "insert into users (name, age) values ('Joe', 123);" await config.integration.create({ - sql + sql, }) expect(pg.queryMock).toHaveBeenCalledWith(sql, []) }) @@ -26,15 +28,15 @@ describe("Postgres Integration", () => { it("calls the read method with the correct params", async () => { const sql = "select * from users;" await config.integration.read({ - sql + sql, }) expect(pg.queryMock).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 + const response = await config.integration.update({ + sql, }) expect(pg.queryMock).toHaveBeenCalledWith(sql, []) }) @@ -42,7 +44,7 @@ describe("Postgres Integration", () => { it("calls the delete method with the correct params", async () => { const sql = "delete from users where name = 'todelete';" await config.integration.delete({ - sql + sql, }) expect(pg.queryMock).toHaveBeenCalledWith(sql, []) }) @@ -53,27 +55,27 @@ describe("Postgres Integration", () => { }) 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 + 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 + 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 + 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/redis.spec.js b/packages/server/src/integrations/tests/redis.spec.ts similarity index 71% rename from packages/server/src/integrations/tests/redis.spec.js rename to packages/server/src/integrations/tests/redis.spec.ts index 219584bdb2..d2e5ac819d 100644 --- a/packages/server/src/integrations/tests/redis.spec.js +++ b/packages/server/src/integrations/tests/redis.spec.ts @@ -1,13 +1,16 @@ const Redis = require("ioredis-mock") -const RedisIntegration = require("../redis") +import { default as RedisIntegration } from "../redis" class TestConfiguration { - constructor(config = {}) { - this.integration = new RedisIntegration.integration(config) + integration: any + redis: any + + constructor(config: any = {}) { + this.integration = new RedisIntegration.integration(config) this.redis = new Redis({ data: { - test: 'test', - result: "1" + test: "test", + result: "1", }, }) this.integration.client = this.redis @@ -15,7 +18,7 @@ class TestConfiguration { } describe("Redis Integration", () => { - let config + let config: any beforeEach(() => { config = new TestConfiguration() @@ -24,7 +27,7 @@ describe("Redis Integration", () => { it("calls the create method with the correct params", async () => { const body = { key: "key", - value: "value" + value: "value", } const response = await config.integration.create(body) expect(await config.redis.get("key")).toEqual("value") @@ -32,7 +35,7 @@ describe("Redis Integration", () => { it("calls the read method with the correct params", async () => { const body = { - key: "test" + key: "test", } const response = await config.integration.read(body) expect(response).toEqual("test") @@ -40,7 +43,7 @@ describe("Redis Integration", () => { it("calls the delete method with the correct params", async () => { const body = { - key: "test" + key: "test", } await config.integration.delete(body) expect(await config.redis.get(body.key)).toEqual(null) @@ -48,13 +51,17 @@ describe("Redis Integration", () => { it("calls the command method with the correct params", async () => { const body = { - json: "KEYS *" + json: "KEYS *", } // ioredis-mock doesn't support pipelines - config.integration.client.pipeline = jest.fn(() => ({ exec: jest.fn(() => [[]]) })) + config.integration.client.pipeline = jest.fn(() => ({ + exec: jest.fn(() => [[]]), + })) await config.integration.command(body) - expect(config.integration.client.pipeline).toHaveBeenCalledWith([["KEYS", "*"]]) + expect(config.integration.client.pipeline).toHaveBeenCalledWith([ + ["KEYS", "*"], + ]) }) -}) \ 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.ts similarity index 96% rename from packages/server/src/integrations/tests/rest.spec.js rename to packages/server/src/integrations/tests/rest.spec.ts index 0bb1e3a75d..96f87c2355 100644 --- a/packages/server/src/integrations/tests/rest.spec.js +++ b/packages/server/src/integrations/tests/rest.spec.ts @@ -12,9 +12,8 @@ jest.mock("node-fetch", () => text: jest.fn(), })) ) -const fetch = require("node-fetch") -const RestIntegration = require("../rest") -const { AuthType } = require("../rest") +import fetch from "node-fetch" +import { default as RestIntegration } from "../rest" const FormData = require("form-data") const { URLSearchParams } = require("url") @@ -24,14 +23,16 @@ const HEADERS = { } class TestConfiguration { - constructor(config = {}) { + integration: any + + constructor(config: any = {}) { this.integration = new RestIntegration.integration(config) } } describe("REST Integration", () => { const BASE_URL = "https://myapi.com" - let config + let config: any beforeEach(() => { config = new TestConfiguration({ @@ -170,22 +171,25 @@ describe("REST Integration", () => { }) it("should allow a valid json string and parse the contents to xml", () => { - const output = config.integration.addBody("xml", JSON.stringify(input), {}) + const output = config.integration.addBody( + "xml", + JSON.stringify(input), + {} + ) expect(output.body.includes("1")).toEqual(true) expect(output.body.includes("2")).toEqual(true) expect(output.headers["Content-Type"]).toEqual("application/xml") }) - }) describe("response", () => { - function buildInput(json, text, header) { + function buildInput(json: any, text: any, header: any) { return { status: 200, json: json ? async () => json : undefined, text: text ? async () => text : undefined, headers: { - get: key => (key === "content-length" ? 100 : header), + get: (key: any) => (key === "content-length" ? 100 : header), raw: () => ({ "content-type": header }), }, } @@ -224,7 +228,7 @@ describe("REST Integration", () => { const basicAuth = { _id: "c59c14bd1898a43baa08da68959b24686", name: "basic-1", - type: AuthType.BASIC, + type: RestIntegration.AuthType.BASIC, config: { username: "user", password: "password", @@ -234,7 +238,7 @@ describe("REST Integration", () => { const bearerAuth = { _id: "0d91d732f34e4befabeff50b392a8ff3", name: "bearer-1", - type: AuthType.BEARER, + type: RestIntegration.AuthType.BEARER, config: { token: "mytoken", }, @@ -360,6 +364,7 @@ describe("REST Integration", () => { headers: {}, method: "POST", }) + // @ts-ignore const sentData = JSON.stringify(fetch.mock.calls[0][1].body) expect(sentData).toContain(pageParam) expect(sentData).toContain(sizeParam) @@ -390,6 +395,7 @@ describe("REST Integration", () => { headers: {}, method: "POST", }) + // @ts-ignore const sentData = fetch.mock.calls[0][1].body expect(sentData.has(pageParam)) expect(sentData.get(pageParam)).toEqual(pageValue.toString()) @@ -489,6 +495,7 @@ describe("REST Integration", () => { headers: {}, method: "POST", }) + // @ts-ignore const sentData = JSON.stringify(fetch.mock.calls[0][1].body) expect(sentData).toContain(pageParam) expect(sentData).toContain(sizeParam) @@ -521,6 +528,7 @@ describe("REST Integration", () => { headers: {}, method: "POST", }) + // @ts-ignore const sentData = fetch.mock.calls[0][1].body expect(sentData.has(pageParam)) expect(sentData.get(pageParam)).toEqual(pageValue.toString()) diff --git a/packages/server/src/integrations/tests/s3.spec.js b/packages/server/src/integrations/tests/s3.spec.ts similarity index 55% rename from packages/server/src/integrations/tests/s3.spec.js rename to packages/server/src/integrations/tests/s3.spec.ts index 7ac403dbd4..503702c042 100644 --- a/packages/server/src/integrations/tests/s3.spec.js +++ b/packages/server/src/integrations/tests/s3.spec.ts @@ -1,26 +1,28 @@ const AWS = require("aws-sdk") -const S3Integration = require("../s3") +import { default as S3Integration } from "../s3" jest.mock("aws-sdk") class TestConfiguration { - constructor(config = {}) { - this.integration = new S3Integration.integration(config) + integration: any + + constructor(config: any = {}) { + this.integration = new S3Integration.integration(config) } } describe("S3 Integration", () => { - let config + let config: any beforeEach(() => { config = new TestConfiguration() }) it("calls the read method with the correct params", async () => { - const response = await config.integration.read({ - bucket: "test" + const response = await config.integration.read({ + bucket: "test", }) expect(config.integration.client.listObjects).toHaveBeenCalledWith({ - Bucket: "test" + Bucket: "test", }) }) -}) \ No newline at end of file +}) diff --git a/packages/server/src/integrations/tests/sql.spec.js b/packages/server/src/integrations/tests/sql.spec.ts similarity index 60% rename from packages/server/src/integrations/tests/sql.spec.js rename to packages/server/src/integrations/tests/sql.spec.ts index 3cc9f0fb3e..421b8535d4 100644 --- a/packages/server/src/integrations/tests/sql.spec.js +++ b/packages/server/src/integrations/tests/sql.spec.ts @@ -3,7 +3,7 @@ const { SqlClient } = require("../utils") const TABLE_NAME = "test" -function endpoint(table, operation) { +function endpoint(table: any, operation: any) { return { datasourceId: "Postgres", operation: operation, @@ -11,7 +11,13 @@ function endpoint(table, operation) { } } -function generateReadJson({ table, fields, filters, sort, paginate} = {}) { +function generateReadJson({ + table, + fields, + filters, + sort, + paginate, +}: any = {}) { return { endpoint: endpoint(table || TABLE_NAME, "READ"), resource: { @@ -48,7 +54,7 @@ function generateDeleteJson(table = TABLE_NAME, filters = {}) { describe("SQL query builder", () => { const limit = 500 const client = SqlClient.POSTGRES - let sql + let sql: any beforeEach(() => { sql = new Sql(client, limit) @@ -58,118 +64,139 @@ describe("SQL query builder", () => { const query = sql._query(generateReadJson()) expect(query).toEqual({ bindings: [limit], - sql: `select * from (select * from "${TABLE_NAME}" limit $1) as "${TABLE_NAME}"` + sql: `select * from (select * from "${TABLE_NAME}" limit $1) as "${TABLE_NAME}"`, }) }) it("should test a read with specific columns", () => { - const nameProp = `${TABLE_NAME}.name`, ageProp = `${TABLE_NAME}.age` - const query = sql._query(generateReadJson({ - fields: [nameProp, ageProp] - })) + const nameProp = `${TABLE_NAME}.name`, + ageProp = `${TABLE_NAME}.age` + const query = sql._query( + generateReadJson({ + fields: [nameProp, ageProp], + }) + ) expect(query).toEqual({ bindings: [limit], - sql: `select "${TABLE_NAME}"."name" as "${nameProp}", "${TABLE_NAME}"."age" as "${ageProp}" from (select * from "${TABLE_NAME}" limit $1) as "${TABLE_NAME}"` + sql: `select "${TABLE_NAME}"."name" as "${nameProp}", "${TABLE_NAME}"."age" as "${ageProp}" from (select * from "${TABLE_NAME}" limit $1) as "${TABLE_NAME}"`, }) }) it("should test a where string starts with read", () => { - const query = sql._query(generateReadJson({ - filters: { - string: { - name: "John", - } - } - })) + const query = sql._query( + generateReadJson({ + filters: { + string: { + name: "John", + }, + }, + }) + ) expect(query).toEqual({ bindings: ["John%", limit], - sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."name" ilike $1 limit $2) as "${TABLE_NAME}"` + sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."name" ilike $1 limit $2) as "${TABLE_NAME}"`, }) }) it("should test a where range read", () => { - const query = sql._query(generateReadJson({ - filters: { - range: { - age: { - low: 2, - high: 10, - } - } - } - })) + const query = sql._query( + generateReadJson({ + filters: { + range: { + age: { + low: 2, + high: 10, + }, + }, + }, + }) + ) expect(query).toEqual({ bindings: [2, 10, limit], - sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."age" between $1 and $2 limit $3) as "${TABLE_NAME}"` + sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."age" between $1 and $2 limit $3) as "${TABLE_NAME}"`, }) }) it("should test for multiple IDs with OR", () => { - const query = sql._query(generateReadJson({ - filters: { - equal: { - age: 10, - name: "John", + const query = sql._query( + generateReadJson({ + filters: { + equal: { + age: 10, + name: "John", + }, + allOr: true, }, - allOr: true, - } - })) + }) + ) expect(query).toEqual({ bindings: [10, "John", limit], - sql: `select * from (select * from "${TABLE_NAME}" where ("${TABLE_NAME}"."age" = $1) or ("${TABLE_NAME}"."name" = $2) limit $3) as "${TABLE_NAME}"` + sql: `select * from (select * from "${TABLE_NAME}" where ("${TABLE_NAME}"."age" = $1) or ("${TABLE_NAME}"."name" = $2) limit $3) as "${TABLE_NAME}"`, }) }) it("should allow filtering on a related field", () => { - const query = sql._query(generateReadJson({ - filters: { - equal: { - age: 10, - "task.name": "task 1", + const query = sql._query( + generateReadJson({ + filters: { + equal: { + age: 10, + "task.name": "task 1", + }, }, - }, - })) + }) + ) // order of bindings changes because relationship filters occur outside inner query expect(query).toEqual({ bindings: [10, limit, "task 1"], - sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."age" = $1 limit $2) as "${TABLE_NAME}" where "task"."name" = $3` + sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."age" = $1 limit $2) as "${TABLE_NAME}" where "task"."name" = $3`, }) }) it("should test an create statement", () => { - const query = sql._query(generateCreateJson(TABLE_NAME, { - name: "Michael", - age: 45, - })) + const query = sql._query( + generateCreateJson(TABLE_NAME, { + name: "Michael", + age: 45, + }) + ) expect(query).toEqual({ bindings: [45, "Michael"], - sql: `insert into "${TABLE_NAME}" ("age", "name") values ($1, $2) returning *` + sql: `insert into "${TABLE_NAME}" ("age", "name") values ($1, $2) returning *`, }) }) it("should test an update statement", () => { - const query = sql._query(generateUpdateJson(TABLE_NAME, { - name: "John" - }, { - equal: { - id: 1001, - } - })) + const query = sql._query( + generateUpdateJson( + TABLE_NAME, + { + name: "John", + }, + { + equal: { + id: 1001, + }, + } + ) + ) expect(query).toEqual({ bindings: ["John", 1001], - sql: `update "${TABLE_NAME}" set "name" = $1 where "${TABLE_NAME}"."id" = $2 returning *` + sql: `update "${TABLE_NAME}" set "name" = $1 where "${TABLE_NAME}"."id" = $2 returning *`, }) }) it("should test a delete statement", () => { - const query = sql._query(generateDeleteJson(TABLE_NAME, { - equal: { - id: 1001, - } - })) + const query = sql._query( + generateDeleteJson(TABLE_NAME, { + equal: { + id: 1001, + }, + }) + ) expect(query).toEqual({ bindings: [1001], - sql: `delete from "${TABLE_NAME}" where "${TABLE_NAME}"."id" = $1 returning *` + sql: `delete from "${TABLE_NAME}" where "${TABLE_NAME}"."id" = $1 returning *`, }) }) @@ -177,7 +204,7 @@ describe("SQL query builder", () => { const query = new Sql(SqlClient.MS_SQL, 10)._query(generateReadJson()) expect(query).toEqual({ bindings: [10], - sql: `select * from (select top (@p0) * from [${TABLE_NAME}]) as [${TABLE_NAME}]` + sql: `select * from (select top (@p0) * from [${TABLE_NAME}]) as [${TABLE_NAME}]`, }) }) @@ -185,193 +212,217 @@ describe("SQL query builder", () => { const query = new Sql(SqlClient.MY_SQL, 10)._query(generateReadJson()) expect(query).toEqual({ bindings: [10], - sql: `select * from (select * from \`${TABLE_NAME}\` limit ?) as \`${TABLE_NAME}\`` + sql: `select * from (select * from \`${TABLE_NAME}\` limit ?) as \`${TABLE_NAME}\``, }) }) it("should use greater than when only low range specified", () => { const date = new Date() - const query = sql._query(generateReadJson({ - filters: { - range: { - property: { - low: date, - } - } - } - })) + const query = sql._query( + generateReadJson({ + filters: { + range: { + property: { + low: date, + }, + }, + }, + }) + ) expect(query).toEqual({ bindings: [date, limit], - sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" > $1 limit $2) as "${TABLE_NAME}"` + sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" > $1 limit $2) as "${TABLE_NAME}"`, }) }) it("should use less than when only high range specified", () => { const date = new Date() - const query = sql._query(generateReadJson({ - filters: { - range: { - property: { - high: date, - } - } - } - })) + const query = sql._query( + generateReadJson({ + filters: { + range: { + property: { + high: date, + }, + }, + }, + }) + ) expect(query).toEqual({ bindings: [date, limit], - sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" < $1 limit $2) as "${TABLE_NAME}"` + sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" < $1 limit $2) as "${TABLE_NAME}"`, }) }) it("should use greater than when only low range specified", () => { const date = new Date() - const query = sql._query(generateReadJson({ - filters: { - range: { - property: { - low: date, - } - } - } - })) + const query = sql._query( + generateReadJson({ + filters: { + range: { + property: { + low: date, + }, + }, + }, + }) + ) expect(query).toEqual({ bindings: [date, limit], - sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" > $1 limit $2) as "${TABLE_NAME}"` + sql: `select * from (select * from "${TABLE_NAME}" where "${TABLE_NAME}"."property" > $1 limit $2) as "${TABLE_NAME}"`, }) }) it("should use AND like expression for MS-SQL when filter is contains", () => { - const query = new Sql(SqlClient.MS_SQL, 10)._query(generateReadJson({ - filters: { - contains: { - age: [20, 25], - name: ["John", "Mary"] - } - } - })) + const query = new Sql(SqlClient.MS_SQL, 10)._query( + generateReadJson({ + filters: { + contains: { + age: [20, 25], + name: ["John", "Mary"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10, "%20%", "%25%", `%"John"%`, `%"Mary"%`], - sql: `select * from (select top (@p0) * from [${TABLE_NAME}] where (LOWER(${TABLE_NAME}.age) LIKE @p1 AND LOWER(${TABLE_NAME}.age) LIKE @p2) and (LOWER(${TABLE_NAME}.name) LIKE @p3 AND LOWER(${TABLE_NAME}.name) LIKE @p4)) as [${TABLE_NAME}]` + sql: `select * from (select top (@p0) * from [${TABLE_NAME}] where (LOWER(${TABLE_NAME}.age) LIKE @p1 AND LOWER(${TABLE_NAME}.age) LIKE @p2) and (LOWER(${TABLE_NAME}.name) LIKE @p3 AND LOWER(${TABLE_NAME}.name) LIKE @p4)) as [${TABLE_NAME}]`, }) }) it("should use JSON_CONTAINS expression for MySQL when filter is contains", () => { - const query = new Sql(SqlClient.MY_SQL, 10)._query(generateReadJson({ - filters: { - contains: { - age: [20], - name: ["John"] - } - } - })) + const query = new Sql(SqlClient.MY_SQL, 10)._query( + generateReadJson({ + filters: { + contains: { + age: [20], + name: ["John"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10], - sql: `select * from (select * from \`${TABLE_NAME}\` where JSON_CONTAINS(${TABLE_NAME}.age, '[20]') and JSON_CONTAINS(${TABLE_NAME}.name, '["John"]') limit ?) as \`${TABLE_NAME}\`` + sql: `select * from (select * from \`${TABLE_NAME}\` where JSON_CONTAINS(${TABLE_NAME}.age, '[20]') and JSON_CONTAINS(${TABLE_NAME}.name, '["John"]') limit ?) as \`${TABLE_NAME}\``, }) }) it("should use jsonb operator expression for PostgreSQL when filter is contains", () => { - const query = new Sql(SqlClient.POSTGRES, 10)._query(generateReadJson({ - filters: { - contains: { - age: [20], - name: ["John"] - } - } - })) + const query = new Sql(SqlClient.POSTGRES, 10)._query( + generateReadJson({ + filters: { + contains: { + age: [20], + name: ["John"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10], - sql: `select * from (select * from \"${TABLE_NAME}\" where \"${TABLE_NAME}\".\"age\"::jsonb @> '[20]' and \"${TABLE_NAME}\".\"name\"::jsonb @> '["John"]' limit $1) as \"${TABLE_NAME}\"` + sql: `select * from (select * from \"${TABLE_NAME}\" where \"${TABLE_NAME}\".\"age\"::jsonb @> '[20]' and \"${TABLE_NAME}\".\"name\"::jsonb @> '["John"]' limit $1) as \"${TABLE_NAME}\"`, }) }) it("should use NOT like expression for MS-SQL when filter is notContains", () => { - const query = new Sql(SqlClient.MS_SQL, 10)._query(generateReadJson({ - filters: { - notContains: { - age: [20], - name: ["John"] - } - } - })) + const query = new Sql(SqlClient.MS_SQL, 10)._query( + generateReadJson({ + filters: { + notContains: { + age: [20], + name: ["John"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10, "%20%", `%"John"%`], - sql: `select * from (select top (@p0) * from [${TABLE_NAME}] where NOT (LOWER(${TABLE_NAME}.age) LIKE @p1) and NOT (LOWER(${TABLE_NAME}.name) LIKE @p2)) as [${TABLE_NAME}]` + sql: `select * from (select top (@p0) * from [${TABLE_NAME}] where NOT (LOWER(${TABLE_NAME}.age) LIKE @p1) and NOT (LOWER(${TABLE_NAME}.name) LIKE @p2)) as [${TABLE_NAME}]`, }) }) it("should use NOT JSON_CONTAINS expression for MySQL when filter is notContains", () => { - const query = new Sql(SqlClient.MY_SQL, 10)._query(generateReadJson({ - filters: { - notContains: { - age: [20], - name: ["John"] - } - } - })) + const query = new Sql(SqlClient.MY_SQL, 10)._query( + generateReadJson({ + filters: { + notContains: { + age: [20], + name: ["John"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10], - sql: `select * from (select * from \`${TABLE_NAME}\` where NOT JSON_CONTAINS(${TABLE_NAME}.age, '[20]') and NOT JSON_CONTAINS(${TABLE_NAME}.name, '["John"]') limit ?) as \`${TABLE_NAME}\`` + sql: `select * from (select * from \`${TABLE_NAME}\` where NOT JSON_CONTAINS(${TABLE_NAME}.age, '[20]') and NOT JSON_CONTAINS(${TABLE_NAME}.name, '["John"]') limit ?) as \`${TABLE_NAME}\``, }) }) it("should use jsonb operator NOT expression for PostgreSQL when filter is notContains", () => { - const query = new Sql(SqlClient.POSTGRES, 10)._query(generateReadJson({ - filters: { - notContains: { - age: [20], - name: ["John"] - } - } - })) + const query = new Sql(SqlClient.POSTGRES, 10)._query( + generateReadJson({ + filters: { + notContains: { + age: [20], + name: ["John"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10], - sql: `select * from (select * from \"${TABLE_NAME}\" where NOT \"${TABLE_NAME}\".\"age\"::jsonb @> '[20]' and NOT \"${TABLE_NAME}\".\"name\"::jsonb @> '["John"]' limit $1) as \"${TABLE_NAME}\"` + sql: `select * from (select * from \"${TABLE_NAME}\" where NOT \"${TABLE_NAME}\".\"age\"::jsonb @> '[20]' and NOT \"${TABLE_NAME}\".\"name\"::jsonb @> '["John"]' limit $1) as \"${TABLE_NAME}\"`, }) }) it("should use OR like expression for MS-SQL when filter is containsAny", () => { - const query = new Sql(SqlClient.MS_SQL, 10)._query(generateReadJson({ - filters: { - containsAny: { - age: [20, 25], - name: ["John", "Mary"] - } - } - })) + const query = new Sql(SqlClient.MS_SQL, 10)._query( + generateReadJson({ + filters: { + containsAny: { + age: [20, 25], + name: ["John", "Mary"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10, "%20%", "%25%", `%"John"%`, `%"Mary"%`], - sql: `select * from (select top (@p0) * from [${TABLE_NAME}] where (LOWER(${TABLE_NAME}.age) LIKE @p1 OR LOWER(${TABLE_NAME}.age) LIKE @p2) and (LOWER(${TABLE_NAME}.name) LIKE @p3 OR LOWER(${TABLE_NAME}.name) LIKE @p4)) as [${TABLE_NAME}]` + sql: `select * from (select top (@p0) * from [${TABLE_NAME}] where (LOWER(${TABLE_NAME}.age) LIKE @p1 OR LOWER(${TABLE_NAME}.age) LIKE @p2) and (LOWER(${TABLE_NAME}.name) LIKE @p3 OR LOWER(${TABLE_NAME}.name) LIKE @p4)) as [${TABLE_NAME}]`, }) }) it("should use JSON_OVERLAPS expression for MySQL when filter is containsAny", () => { - const query = new Sql(SqlClient.MY_SQL, 10)._query(generateReadJson({ - filters: { - containsAny: { - age: [20, 25], - name: ["John", "Mary"] - } - } - })) + const query = new Sql(SqlClient.MY_SQL, 10)._query( + generateReadJson({ + filters: { + containsAny: { + age: [20, 25], + name: ["John", "Mary"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10], - sql: `select * from (select * from \`${TABLE_NAME}\` where JSON_OVERLAPS(${TABLE_NAME}.age, '[20,25]') and JSON_OVERLAPS(${TABLE_NAME}.name, '["John","Mary"]') limit ?) as \`${TABLE_NAME}\`` + sql: `select * from (select * from \`${TABLE_NAME}\` where JSON_OVERLAPS(${TABLE_NAME}.age, '[20,25]') and JSON_OVERLAPS(${TABLE_NAME}.name, '["John","Mary"]') limit ?) as \`${TABLE_NAME}\``, }) }) it("should use ?| operator expression for PostgreSQL when filter is containsAny", () => { - const query = new Sql(SqlClient.POSTGRES, 10)._query(generateReadJson({ - filters: { - containsAny: { - age: [20, 25], - name: ["John", "Mary"] - } - } - })) + const query = new Sql(SqlClient.POSTGRES, 10)._query( + generateReadJson({ + filters: { + containsAny: { + age: [20, 25], + name: ["John", "Mary"], + }, + }, + }) + ) expect(query).toEqual({ bindings: [10], - sql: `select * from (select * from \"${TABLE_NAME}\" where \"${TABLE_NAME}\".\"age\"::jsonb ?| array [20,25] and \"${TABLE_NAME}\".\"name\"::jsonb ?| array ['John','Mary'] limit $1) as \"${TABLE_NAME}\"` + sql: `select * from (select * from \"${TABLE_NAME}\" where \"${TABLE_NAME}\".\"age\"::jsonb ?| array [20,25] and \"${TABLE_NAME}\".\"name\"::jsonb ?| array ['John','Mary'] limit $1) as \"${TABLE_NAME}\"`, }) }) })