Updating test cases to TS to get them working properly with the full TS implementations.

This commit is contained in:
mike12345567 2022-08-31 19:21:45 +01:00
parent e149b3d807
commit f49d61bc5c
17 changed files with 558 additions and 450 deletions

View File

@ -33,7 +33,7 @@ module MongoMock {
})
}
mongodb.ObjectID = require("mongodb").ObjectID
mongodb.ObjectID = jest.requireActual("mongodb").ObjectID
module.exports = mongodb
}

View File

@ -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()

View File

@ -1,9 +1,11 @@
const Airtable = require("airtable")
const AirtableIntegration = require("../airtable")
import { default as AirtableIntegration } from "../airtable"
jest.mock("airtable")
class TestConfiguration {
constructor(config = {}) {
integration: any
client: any
constructor(config: any = {}) {
this.integration = new AirtableIntegration.integration(config)
this.client = {
create: jest.fn(),
@ -18,7 +20,7 @@ class TestConfiguration {
}
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,21 +54,21 @@ 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)
})

View File

@ -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 = {}) {
integration: any
constructor(config: any = {}) {
this.integration = new ArangoDBIntegration.integration(config)
}
}
describe("ArangoDB Integration", () => {
let config
let config: any
let indexName = "Users"
beforeEach(() => {
@ -18,16 +19,18 @@ 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)

View File

@ -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 = {}) {
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
test: 1,
}
const response = await config.integration.create({
json: doc
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
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
json: doc,
})
expect(config.integration.client.put).toHaveBeenCalledWith(doc)

View File

@ -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 = {}) {
integration: any
constructor(config: any = {}) {
this.integration = new DynamoDBIntegration.integration(config)
}
}
describe("DynamoDB Integration", () => {
let config
let config: any
let tableName = "Users"
beforeEach(() => {
@ -20,12 +21,12 @@ describe("DynamoDB Integration", () => {
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",
})
})
@ -35,7 +36,7 @@ describe("DynamoDB Integration", () => {
const response = await config.integration.read({
table: tableName,
index: indexName,
json: {}
json: {},
})
expect(config.integration.client.query).toHaveBeenCalledWith({
TableName: tableName,
@ -50,28 +51,30 @@ describe("DynamoDB Integration", () => {
const response = await config.integration.scan({
table: tableName,
index: indexName,
json: {}
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({
table: tableName,
json: {
Id: 123
}
Id: 123,
},
})
expect(config.integration.client.get).toHaveBeenCalledWith({
TableName: tableName,
Id: 123
Id: 123,
})
})
@ -79,12 +82,12 @@ describe("DynamoDB Integration", () => {
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",
})
})
@ -92,12 +95,12 @@ describe("DynamoDB Integration", () => {
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,
})
})
})

View File

@ -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 = {}) {
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({
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({
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({
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)

View File

@ -1,20 +1,21 @@
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 = {}) {
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: "{}",
})
})
@ -22,16 +23,16 @@ describe("Firebase Integration", () => {
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({
Name: "Test Name",
id: "test_id"
id: "test_id",
})
})
@ -39,18 +40,22 @@ describe("Firebase Integration", () => {
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 () => {
@ -58,19 +63,19 @@ describe("Firebase Integration", () => {
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",
})
})
@ -79,11 +84,11 @@ describe("Firebase Integration", () => {
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")

View File

@ -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 = {}) {
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,9 +46,9 @@ 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 sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
sql,
})
expect(response[0]).toEqual(sql)
})

View File

@ -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')",
@ -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: {
@ -163,15 +170,15 @@ describe("MongoDB Integration", () => {
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: {
@ -202,16 +209,16 @@ describe("MongoDB Integration", () => {
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,
})
})
@ -247,7 +254,7 @@ describe("MongoDB Integration", () => {
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,
})
})
@ -300,7 +309,7 @@ describe("MongoDB Integration", () => {
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',
})
})
})

View File

@ -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,16 +43,16 @@ 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 sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
sql,
})
expect(response).toEqual([{ created: true }])
})
@ -58,7 +60,7 @@ describe("MySQL 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
sql,
})
expect(response).toEqual([{ updated: true }])
})
@ -66,7 +68,7 @@ describe("MySQL 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
sql,
})
expect(response).toEqual([{ deleted: true }])
})

View File

@ -1,9 +1,11 @@
const oracledb = require("oracledb")
const OracleIntegration = require("../oracle")
import { default as OracleIntegration } from "../oracle"
jest.mock("oracledb")
class TestConfiguration {
constructor(config = {}) {
integration: any
constructor(config: any = {}) {
this.integration = new OracleIntegration.integration(config)
}
}
@ -11,7 +13,7 @@ class TestConfiguration {
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)
@ -44,7 +46,7 @@ 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
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 sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
sql,
})
expect(response).toEqual([{ created: true }])
expect(oracledb.executeMock).toHaveBeenCalledTimes(1)
@ -76,7 +78,7 @@ 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
sql,
})
expect(response).toEqual([{ updated: true }])
expect(oracledb.executeMock).toHaveBeenCalledTimes(1)
@ -85,7 +87,7 @@ 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
sql,
})
expect(response).toEqual([{ deleted: true }])
expect(oracledb.executeMock).toHaveBeenCalledTimes(1)

View File

@ -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 = {}) {
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,7 +28,7 @@ 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, [])
})
@ -34,7 +36,7 @@ describe("Postgres 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
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,9 +55,9 @@ 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 sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
sql,
})
expect(response).toEqual([{ created: true }])
})
@ -63,7 +65,7 @@ describe("Postgres 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
sql,
})
expect(response).toEqual([{ updated: true }])
})
@ -71,7 +73,7 @@ describe("Postgres 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
sql,
})
expect(response).toEqual([{ deleted: true }])
})

View File

@ -1,13 +1,16 @@
const Redis = require("ioredis-mock")
const RedisIntegration = require("../redis")
import { default as RedisIntegration } from "../redis"
class TestConfiguration {
constructor(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", "*"],
])
})
})

View File

@ -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("<a>1</a>")).toEqual(true)
expect(output.body.includes("<b>2</b>")).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())

View File

@ -1,15 +1,17 @@
const AWS = require("aws-sdk")
const S3Integration = require("../s3")
import { default as S3Integration } from "../s3"
jest.mock("aws-sdk")
class TestConfiguration {
constructor(config = {}) {
integration: any
constructor(config: any = {}) {
this.integration = new S3Integration.integration(config)
}
}
describe("S3 Integration", () => {
let config
let config: any
beforeEach(() => {
config = new TestConfiguration()
@ -17,10 +19,10 @@ describe("S3 Integration", () => {
it("calls the read method with the correct params", async () => {
const response = await config.integration.read({
bucket: "test"
bucket: "test",
})
expect(config.integration.client.listObjects).toHaveBeenCalledWith({
Bucket: "test"
Bucket: "test",
})
})
})

View File

@ -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}\"`,
})
})
})