Merge pull request #1295 from Budibase/integrations-tests

Integrations tests
This commit is contained in:
Martin McKeaveney 2021-03-18 09:45:50 +00:00 committed by GitHub
commit 26ae531842
24 changed files with 890 additions and 19 deletions

View File

@ -0,0 +1,24 @@
const elastic = {}
elastic.Client = function() {
this.index = jest.fn().mockResolvedValue({ body: [] })
this.search = jest.fn().mockResolvedValue({
body: {
hits: {
hits: [
{
_source: {
name: "test",
},
},
],
},
},
})
this.update = jest.fn().mockResolvedValue({ body: [] })
this.delete = jest.fn().mockResolvedValue({ body: [] })
this.close = jest.fn()
}
module.exports = elastic

View File

@ -0,0 +1,5 @@
function Airtable() {
this.base = jest.fn()
}
module.exports = Airtable

View File

@ -0,0 +1,21 @@
const arangodb = {}
arangodb.Database = function() {
this.query = jest.fn(() => ({
all: jest.fn(),
}))
this.collection = jest.fn(() => "collection")
this.close = jest.fn()
}
arangodb.aql = (strings, ...args) => {
let str = strings.join("{}")
for (let arg of args) {
str = str.replace("{}", arg)
}
return str
}
module.exports = arangodb

View File

@ -0,0 +1,38 @@
const aws = {}
const response = body => () => ({ promise: () => body })
function DocumentClient() {
this.put = jest.fn(response({}))
this.query = jest.fn(
response({
Items: [],
})
)
this.scan = jest.fn(
response({
Items: [
{
Name: "test",
},
],
})
)
this.get = jest.fn(response({}))
this.update = jest.fn(response({}))
this.delete = jest.fn(response({}))
}
function S3() {
this.listObjects = jest.fn(
response({
Contents: {},
})
)
}
aws.DynamoDB = { DocumentClient }
aws.S3 = S3
aws.config = { update: jest.fn() }
module.exports = aws

View File

@ -0,0 +1,19 @@
const mongodb = {}
mongodb.MongoClient = function() {
this.connect = jest.fn()
this.close = jest.fn()
this.insertOne = jest.fn()
this.find = jest.fn(() => ({ toArray: () => [] }))
this.collection = jest.fn(() => ({
insertOne: this.insertOne,
find: this.find,
}))
this.db = () => ({
collection: this.collection,
})
}
module.exports = mongodb

View File

@ -0,0 +1,14 @@
const mssql = {}
mssql.query = jest.fn(() => ({
recordset: [
{
a: "string",
b: 1,
},
],
}))
mssql.connect = jest.fn(() => ({ recordset: [] }))
module.exports = mssql

View File

@ -0,0 +1,10 @@
const mysql = {}
const client = {
connect: jest.fn(),
query: jest.fn(),
}
mysql.createConnection = jest.fn(() => client)
module.exports = mysql

View File

@ -3,18 +3,16 @@ const pg = {}
// constructor // constructor
function Client() {} function Client() {}
Client.prototype.query = async function() { Client.prototype.query = jest.fn(() => ({
return {
rows: [ rows: [
{ {
a: "string", a: "string",
b: 1, b: 1,
}, },
], ],
} }))
}
Client.prototype.connect = async function() {} Client.prototype.connect = jest.fn()
pg.Client = Client pg.Client = Client

View File

@ -0,0 +1,9 @@
function CouchDB() {
this.post = jest.fn()
this.allDocs = jest.fn(() => ({ rows: [] }))
this.put = jest.fn()
this.remove = jest.fn()
this.plugin = jest.fn()
}
module.exports = CouchDB

View File

@ -166,7 +166,7 @@ class DynamoDBIntegration {
async update(query) { async update(query) {
const params = { const params = {
TableName: query.Table, TableName: query.table,
...query.json, ...query.json,
} }
return this.client.update(params).promise() return this.client.update(params).promise()

View File

@ -65,7 +65,7 @@ class SqlServerIntegration {
try { try {
await this.connect() await this.connect()
const response = await this.client.query(query.sql) const response = await this.client.query(query.sql)
return response.recordset return response.recordset || [{ created: true }]
} catch (err) { } catch (err) {
console.error("Error querying MS SQL Server", err) console.error("Error querying MS SQL Server", err)
throw err throw err

View File

@ -73,20 +73,23 @@ class MySQLIntegration {
}) })
} }
create(query) { async create(query) {
return this.query(query) const results = await this.query(query)
return results.length ? results : [{ created: true }]
} }
read(query) { read(query) {
return this.query(query) return this.query(query)
} }
update(query) { async update(query) {
return this.query(query) const results = await this.query(query)
return results.length ? results : [{ updated: true }]
} }
delete(query) { async delete(query) {
return this.query(query) const results = await this.query(query)
return results.length ? results : [{ deleted: true }]
} }
} }

View File

@ -0,0 +1,70 @@
const Airtable = require("airtable")
const AirtableIntegration = require("../airtable")
jest.mock("airtable")
class TestConfiguration {
constructor(config = {}) {
this.integration = new AirtableIntegration.integration(config)
this.client = {
create: jest.fn(),
select: jest.fn(),
update: jest.fn(),
destroy: jest.fn(),
}
this.integration.client = () => this.client
}
}
describe("Airtable Integration", () => {
let config
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const response = await config.integration.create({
table: "test",
json: {}
})
expect(config.client.create).toHaveBeenCalledWith([
{
fields: {}
}
])
})
it("calls the read method with the correct params", async () => {
const response = await config.integration.read({
table: "test",
view: "Grid view"
})
expect(config.client.select).toHaveBeenCalledWith({
maxRecords: 10, view: "Grid view"
})
})
it("calls the update method with the correct params", async () => {
const response = await config.integration.update({
table: "test",
id: "123",
json: {
name: "test"
}
})
expect(config.client.update).toHaveBeenCalledWith([
{
id: "123",
fields: { name: "test" }
}
])
})
it("calls the delete method with the correct params", async () => {
const ids = [1,2,3,4]
const response = await config.integration.delete({
ids
})
expect(config.client.destroy).toHaveBeenCalledWith(ids)
})
})

View File

@ -0,0 +1,35 @@
const arangodb = require("arangojs")
const ArangoDBIntegration = require("../arangodb")
jest.mock("arangojs")
class TestConfiguration {
constructor(config = {}) {
this.integration = new ArangoDBIntegration.integration(config)
}
}
describe("ArangoDB Integration", () => {
let config
let indexName = "Users"
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const body = {
json: "Hello"
}
const response = await config.integration.create(body)
expect(config.integration.client.query).toHaveBeenCalledWith(`INSERT Hello INTO collection RETURN NEW`)
})
it("calls the read method with the correct params", async () => {
const query = {
json: `test`,
}
const response = await config.integration.read(query)
expect(config.integration.client.query).toHaveBeenCalledWith(query.sql)
})
})

View File

@ -0,0 +1,68 @@
const PouchDB = require("pouchdb")
const CouchDBIntegration = require("../couchdb")
jest.mock("pouchdb", () => function CouchDBMock() {
this.post = jest.fn()
this.allDocs = jest.fn(() => ({ rows: [] }))
this.put = jest.fn()
this.remove = jest.fn()
this.plugin = jest.fn()
})
class TestConfiguration {
constructor(config = {}) {
this.integration = new CouchDBIntegration.integration(config)
}
}
describe("CouchDB Integration", () => {
let config
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const doc = {
test: 1
}
const response = await config.integration.create({
json: doc
})
expect(config.integration.client.post).toHaveBeenCalledWith(doc)
})
it("calls the read method with the correct params", async () => {
const doc = {
name: "search"
}
const response = await config.integration.read({
json: doc
})
expect(config.integration.client.allDocs).toHaveBeenCalledWith({
include_docs: true,
name: "search"
})
})
it("calls the update method with the correct params", async () => {
const doc = {
_id: "1234",
name: "search"
}
const response = await config.integration.update({
json: doc
})
expect(config.integration.client.put).toHaveBeenCalledWith(doc)
})
it("calls the delete method with the correct params", async () => {
const id = "1234"
const response = await config.integration.delete({ id })
expect(config.integration.client.remove).toHaveBeenCalledWith(id)
})
})

View File

@ -0,0 +1,103 @@
const AWS = require("aws-sdk")
const DynamoDBIntegration = require("../dynamodb")
jest.mock("aws-sdk")
class TestConfiguration {
constructor(config = {}) {
this.integration = new DynamoDBIntegration.integration(config)
}
}
describe("DynamoDB Integration", () => {
let config
let tableName = "Users"
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const response = await config.integration.create({
table: tableName,
json: {
Name: "John"
}
})
expect(config.integration.client.put).toHaveBeenCalledWith({
TableName: tableName,
Name: "John"
})
})
it("calls the read method with the correct params", async () => {
const indexName = "Test"
const response = await config.integration.read({
table: tableName,
index: indexName,
json: {}
})
expect(config.integration.client.query).toHaveBeenCalledWith({
TableName: tableName,
IndexName: indexName,
})
expect(response).toEqual([])
})
it("calls the scan method with the correct params", async () => {
const indexName = "Test"
const response = await config.integration.scan({
table: tableName,
index: indexName,
json: {}
})
expect(config.integration.client.scan).toHaveBeenCalledWith({
TableName: tableName,
IndexName: indexName,
})
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
}
})
expect(config.integration.client.get).toHaveBeenCalledWith({
TableName: tableName,
Id: 123
})
})
it("calls the update method with the correct params", async () => {
const response = await config.integration.update({
table: tableName,
json: {
Name: "John"
}
})
expect(config.integration.client.update).toHaveBeenCalledWith({
TableName: tableName,
Name: "John"
})
})
it("calls the delete method with the correct params", async () => {
const response = await config.integration.delete({
table: tableName,
json: {
Name: "John"
}
})
expect(config.integration.client.delete).toHaveBeenCalledWith({
TableName: tableName,
Name: "John"
})
})
})

View File

@ -0,0 +1,81 @@
const elasticsearch = require("@elastic/elasticsearch")
const ElasticSearchIntegration = require("../elasticsearch")
jest.mock("@elastic/elasticsearch")
class TestConfiguration {
constructor(config = {}) {
this.integration = new ElasticSearchIntegration.integration(config)
}
}
describe("Elasticsearch Integration", () => {
let config
let indexName = "Users"
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const body = {
name: "Hello"
}
const response = await config.integration.create({
index: indexName,
json: body
})
expect(config.integration.client.index).toHaveBeenCalledWith({
index: indexName,
body
})
})
it("calls the read method with the correct params", async () => {
const body = {
query: {
term: {
name: "kimchy"
}
}
}
const response = await config.integration.read({
index: indexName,
json: body
})
expect(config.integration.client.search).toHaveBeenCalledWith({
index: indexName,
body
})
expect(response).toEqual(expect.any(Array))
})
it("calls the update method with the correct params", async () => {
const body = {
name: "updated"
}
const response = await config.integration.update({
id: "1234",
index: indexName,
json: body
})
expect(config.integration.client.update).toHaveBeenCalledWith({
id: "1234",
index: indexName,
body
})
expect(response).toEqual(expect.any(Array))
})
it("calls the delete method with the correct params", async () => {
const body = {
id: "1234"
}
const response = await config.integration.delete(body)
expect(config.integration.client.delete).toHaveBeenCalledWith(body)
expect(response).toEqual(expect.any(Array))
})
})

View File

@ -0,0 +1,47 @@
const sqlServer = require("mssql")
const MSSQLIntegration = require("../microsoftSqlServer")
jest.mock("mssql")
class TestConfiguration {
constructor(config = {}) {
this.integration = new MSSQLIntegration.integration(config)
}
}
describe("MS SQL Server Integration", () => {
let config
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
})
expect(config.integration.client.query).toHaveBeenCalledWith(sql)
})
it("calls the read method with the correct params", async () => {
const sql = "select * from users;"
const response = await config.integration.read({
sql
})
expect(config.integration.client.query).toHaveBeenCalledWith(sql)
})
describe("no rows returned", () => {
beforeEach(() => {
config.integration.client.query.mockImplementation(() => ({ rows: [] }))
})
it("returns the correct response when the create response has no rows", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
})
expect(response).toEqual([{ created: true }])
})
})
})

View File

@ -0,0 +1,40 @@
const mongo = require("mongodb")
const MongoDBIntegration = require("../mongodb")
jest.mock("mongodb")
class TestConfiguration {
constructor(config = {}) {
this.integration = new MongoDBIntegration.integration(config)
}
}
describe("MongoDB Integration", () => {
let config
let indexName = "Users"
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const body = {
name: "Hello"
}
const response = await config.integration.create({
index: indexName,
json: body
})
expect(config.integration.client.insertOne).toHaveBeenCalledWith(body)
})
it("calls the read method with the correct params", async () => {
const query = {
json: {
address: "test"
}
}
const response = await config.integration.read(query)
expect(config.integration.client.find).toHaveBeenCalledWith(query.json)
expect(response).toEqual(expect.any(Array))
})
})

View File

@ -0,0 +1,83 @@
const pg = require("mysql")
const MySQLIntegration = require("../mysql")
jest.mock("mysql")
class TestConfiguration {
constructor(config = { ssl: {} }) {
this.integration = new MySQLIntegration.integration(config)
this.query = jest.fn(() => [{ id: 1 }])
this.integration.query = this.query
}
}
describe("MySQL Integration", () => {
let config
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
})
expect(config.query).toHaveBeenCalledWith({ sql })
})
it("calls the read method with the correct params", async () => {
const sql = "select * from users;"
const response = await config.integration.read({
sql
})
expect(config.query).toHaveBeenCalledWith({
sql
})
})
it("calls the update method with the correct params", async () => {
const sql = "update table users set name = 'test';"
const response = await config.integration.update({
sql
})
expect(config.query).toHaveBeenCalledWith({ sql })
})
it("calls the delete method with the correct params", async () => {
const sql = "delete from users where name = 'todelete';"
const response = await config.integration.delete({
sql
})
expect(config.query).toHaveBeenCalledWith({ sql })
})
describe("no rows returned", () => {
beforeEach(() => {
config.query.mockImplementation(() => [])
})
it("returns the correct response when the create response has no rows", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
})
expect(response).toEqual([{ created: true }])
})
it("returns the correct response when the update response has no rows", async () => {
const sql = "update table users set name = 'test';"
const response = await config.integration.update({
sql
})
expect(response).toEqual([{ updated: true }])
})
it("returns the correct response when the delete response has no rows", async () => {
const sql = "delete from users where name = 'todelete';"
const response = await config.integration.delete({
sql
})
expect(response).toEqual([{ deleted: true }])
})
})
})

View File

@ -0,0 +1,79 @@
const pg = require("pg")
const PostgresIntegration = require("../postgres")
jest.mock("pg")
class TestConfiguration {
constructor(config = {}) {
this.integration = new PostgresIntegration.integration(config)
}
}
describe("Postgres Integration", () => {
let config
beforeEach(() => {
config = new TestConfiguration()
})
it("calls the create method with the correct params", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
})
expect(config.integration.client.query).toHaveBeenCalledWith(sql)
})
it("calls the read method with the correct params", async () => {
const sql = "select * from users;"
const response = await config.integration.read({
sql
})
expect(config.integration.client.query).toHaveBeenCalledWith(sql)
})
it("calls the update method with the correct params", async () => {
const sql = "update table users set name = 'test';"
const response = await config.integration.update({
sql
})
expect(config.integration.client.query).toHaveBeenCalledWith(sql)
})
it("calls the delete method with the correct params", async () => {
const sql = "delete from users where name = 'todelete';"
const response = await config.integration.delete({
sql
})
expect(config.integration.client.query).toHaveBeenCalledWith(sql)
})
describe("no rows returned", () => {
beforeEach(() => {
config.integration.client.query.mockImplementation(() => ({ rows: [] }))
})
it("returns the correct response when the create response has no rows", async () => {
const sql = "insert into users (name, age) values ('Joe', 123);"
const response = await config.integration.create({
sql
})
expect(response).toEqual([{ created: true }])
})
it("returns the correct response when the update response has no rows", async () => {
const sql = "update table users set name = 'test';"
const response = await config.integration.update({
sql
})
expect(response).toEqual([{ updated: true }])
})
it("returns the correct response when the delete response has no rows", async () => {
const sql = "delete from users where name = 'todelete';"
const response = await config.integration.delete({
sql
})
expect(response).toEqual([{ deleted: true }])
})
})
})

View File

@ -0,0 +1,98 @@
const fetch = require("node-fetch")
const RestIntegration = require("../rest")
jest.mock("node-fetch", () => jest.fn(() => ({ json: jest.fn(), text: jest.fn() })))
class TestConfiguration {
constructor(config = {}) {
this.integration = new RestIntegration.integration(config)
}
}
describe("REST Integration", () => {
const BASE_URL = "https://myapi.com"
let config
beforeEach(() => {
config = new TestConfiguration({
url: BASE_URL
})
})
it("calls the create method with the correct params", async () => {
const query = {
path: "/api",
queryString: "?test=1",
headers: {
Accept: "application/json"
},
json: {
name: "test"
}
}
const response = await config.integration.create(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "POST",
body: "{\"name\":\"test\"}",
headers: {
Accept: "application/json"
}
})
})
it("calls the read method with the correct params", async () => {
const query = {
path: "/api",
queryString: "?test=1",
headers: {
Accept: "text/html"
}
}
const response = await config.integration.read(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
headers: {
Accept: "text/html"
}
})
})
it("calls the update method with the correct params", async () => {
const query = {
path: "/api",
queryString: "?test=1",
headers: {
Accept: "application/json"
},
json: {
name: "test"
}
}
const response = await config.integration.update(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "POST",
body: "{\"name\":\"test\"}",
headers: {
Accept: "application/json"
}
})
})
it("calls the delete method with the correct params", async () => {
const query = {
path: "/api",
queryString: "?test=1",
headers: {
Accept: "application/json"
},
json: {
name: "test"
}
}
const response = await config.integration.delete(query)
expect(fetch).toHaveBeenCalledWith(`${BASE_URL}/api?test=1`, {
method: "DELETE",
headers: {
Accept: "application/json"
}
})
})
})

View File

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