Updating test cases.
This commit is contained in:
parent
0c7a4c1b5b
commit
6abe5c5f6d
|
@ -2,7 +2,9 @@ const mysql = {}
|
||||||
|
|
||||||
const client = {
|
const client = {
|
||||||
connect: jest.fn(),
|
connect: jest.fn(),
|
||||||
query: jest.fn(),
|
query: jest.fn((query, fn) => {
|
||||||
|
fn(null, [])
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
mysql.createConnection = jest.fn(() => client)
|
mysql.createConnection = jest.fn(() => client)
|
||||||
|
|
|
@ -6,7 +6,7 @@ const {
|
||||||
} = require("../../db/utils")
|
} = require("../../db/utils")
|
||||||
const { integrations } = require("../../integrations")
|
const { integrations } = require("../../integrations")
|
||||||
const plusIntegrations = require("../../integrations/plus")
|
const plusIntegrations = require("../../integrations/plus")
|
||||||
const PostgresConnector = require("../../integrations/plus/Postgres")
|
const PostgresConnector = require("../../integrations/plus/postgres")
|
||||||
|
|
||||||
exports.fetch = async function (ctx) {
|
exports.fetch = async function (ctx) {
|
||||||
const database = new CouchDB(ctx.appId)
|
const database = new CouchDB(ctx.appId)
|
||||||
|
@ -63,6 +63,11 @@ exports.find = async function (ctx) {
|
||||||
ctx.body = await database.get(ctx.params.datasourceId)
|
ctx.body = await database.get(ctx.params.datasourceId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dynamic query functionality
|
||||||
|
exports.query = async function (ctx) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: merge endpoint with main datasource endpoint
|
// TODO: merge endpoint with main datasource endpoint
|
||||||
exports.plus = async function (ctx) {
|
exports.plus = async function (ctx) {
|
||||||
const db = new CouchDB(ctx.appId)
|
const db = new CouchDB(ctx.appId)
|
||||||
|
|
|
@ -102,12 +102,17 @@ class SqlQueryBuilder {
|
||||||
this._limit = limit
|
this._limit = limit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_operation(json) {
|
||||||
|
if (!json || !json.endpoint) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return json.endpoint.operation
|
||||||
|
}
|
||||||
|
|
||||||
_query(json) {
|
_query(json) {
|
||||||
const { endpoint } = json
|
|
||||||
const knex = require("knex")({ client: this._client })
|
const knex = require("knex")({ client: this._client })
|
||||||
const operation = endpoint.operation
|
|
||||||
let query
|
let query
|
||||||
switch (operation) {
|
switch (this._operation(json)) {
|
||||||
case Operation.CREATE:
|
case Operation.CREATE:
|
||||||
query = buildCreate(knex, json)
|
query = buildCreate(knex, json)
|
||||||
break
|
break
|
||||||
|
|
|
@ -51,6 +51,15 @@ const SCHEMA = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function internalQuery(client, sql) {
|
||||||
|
try {
|
||||||
|
return await client.query(sql)
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class SqlServerIntegration extends Sql {
|
class SqlServerIntegration extends Sql {
|
||||||
static pool
|
static pool
|
||||||
|
|
||||||
|
@ -67,52 +76,43 @@ class SqlServerIntegration extends Sql {
|
||||||
}
|
}
|
||||||
|
|
||||||
async connect() {
|
async connect() {
|
||||||
const client = await this.pool.connect()
|
try {
|
||||||
this.client = client.request()
|
const client = await this.pool.connect()
|
||||||
|
this.client = client.request()
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async read(query) {
|
async read(query) {
|
||||||
try {
|
await this.connect()
|
||||||
await this.connect()
|
const response = await internalQuery(this.client, query.sql)
|
||||||
const response = await this.client.query(query.sql)
|
return response.recordset
|
||||||
return response.recordset
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Error querying MS SQL Server", err)
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async create(query) {
|
async create(query) {
|
||||||
try {
|
await this.connect()
|
||||||
await this.connect()
|
const response = await internalQuery(this.client, query.sql)
|
||||||
const response = await this.client.query(query.sql)
|
return response.recordset || [{ created: true }]
|
||||||
return response.recordset || [{ created: true }]
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Error querying MS SQL Server", err)
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(query) {
|
async update(query) {
|
||||||
try {
|
await this.connect()
|
||||||
await this.connect()
|
const response = await internalQuery(this.client, query.sql)
|
||||||
const response = await this.client.query(query.sql)
|
return response.recordset || [{ updated: true }]
|
||||||
return response.recordset
|
|
||||||
} catch (err) {
|
|
||||||
console.error("Error querying MS SQL Server", err)
|
|
||||||
throw err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(query) {
|
async delete(query) {
|
||||||
try {
|
await this.connect()
|
||||||
await this.connect()
|
const response = await internalQuery(this.client, query.sql)
|
||||||
const response = await this.client.query(query.sql)
|
return response.recordset || [{ deleted: true }]
|
||||||
return response.recordset
|
}
|
||||||
} catch (err) {
|
|
||||||
console.error("Error querying MS SQL Server", err)
|
async query(json) {
|
||||||
throw err
|
const operation = this._operation(json).toLowerCase()
|
||||||
}
|
const input = this._query(json)
|
||||||
|
const response = await internalQuery(this.client, input)
|
||||||
|
return response.recordset ? response.recordset : [{ [operation]: true }]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,21 @@ const SCHEMA = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function internalQuery(client, query) {
|
||||||
|
// Node MySQL is callback based, so we must wrap our call in a promise
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
client.connect()
|
||||||
|
return client.query(query, (error, results) => {
|
||||||
|
if (error) {
|
||||||
|
reject(error)
|
||||||
|
} else {
|
||||||
|
resolve(results)
|
||||||
|
client.end()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
class MySQLIntegration extends Sql {
|
class MySQLIntegration extends Sql {
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
super("mysql")
|
super("mysql")
|
||||||
|
@ -63,36 +78,31 @@ class MySQLIntegration extends Sql {
|
||||||
this.client = mysql.createConnection(config)
|
this.client = mysql.createConnection(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
query(query) {
|
|
||||||
// Node MySQL is callback based, so we must wrap our call in a promise
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
this.client.connect()
|
|
||||||
return this.client.query(query.sql, (error, results) => {
|
|
||||||
if (error) return reject(error)
|
|
||||||
resolve(results)
|
|
||||||
this.client.end()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async create(query) {
|
async create(query) {
|
||||||
const results = await this.query(query)
|
const results = await internalQuery(this.client, query.sql)
|
||||||
return results.length ? results : [{ created: true }]
|
return results.length ? results : [{ created: true }]
|
||||||
}
|
}
|
||||||
|
|
||||||
read(query) {
|
read(query) {
|
||||||
return this.query(query)
|
return internalQuery(this.client, query.sql)
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(query) {
|
async update(query) {
|
||||||
const results = await this.query(query)
|
const results = await internalQuery(this.client, query.sql)
|
||||||
return results.length ? results : [{ updated: true }]
|
return results.length ? results : [{ updated: true }]
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(query) {
|
async delete(query) {
|
||||||
const results = await this.query(query)
|
const results = await internalQuery(this.client, query.sql)
|
||||||
return results.length ? results : [{ deleted: true }]
|
return results.length ? results : [{ deleted: true }]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async query(json) {
|
||||||
|
const operation = this._operation(json).toLowerCase()
|
||||||
|
const input = this._query(json)
|
||||||
|
const results = await internalQuery(this.client, input)
|
||||||
|
return results.length ? results : [{ [operation]: true }]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -55,6 +55,15 @@ const SCHEMA = {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function internalQuery(client, sql) {
|
||||||
|
try {
|
||||||
|
return await client.query(sql)
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class PostgresIntegration extends Sql {
|
class PostgresIntegration extends Sql {
|
||||||
static pool
|
static pool
|
||||||
|
|
||||||
|
@ -74,33 +83,32 @@ class PostgresIntegration extends Sql {
|
||||||
this.client = this.pool
|
this.client = this.pool
|
||||||
}
|
}
|
||||||
|
|
||||||
async query(sql) {
|
|
||||||
try {
|
|
||||||
return await this.client.query(sql)
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async create({ sql }) {
|
async create({ sql }) {
|
||||||
const response = await this.query(sql)
|
const response = await internalQuery(this.client, sql)
|
||||||
return response.rows.length ? response.rows : [{ created: true }]
|
return response.rows.length ? response.rows : [{ created: true }]
|
||||||
}
|
}
|
||||||
|
|
||||||
async read({ sql }) {
|
async read({ sql }) {
|
||||||
const response = await this.query(sql)
|
const response = await internalQuery(this.client, sql)
|
||||||
return response.rows
|
return response.rows
|
||||||
}
|
}
|
||||||
|
|
||||||
async update({ sql }) {
|
async update({ sql }) {
|
||||||
const response = await this.query(sql)
|
const response = await internalQuery(this.client, sql)
|
||||||
return response.rows.length ? response.rows : [{ updated: true }]
|
return response.rows.length ? response.rows : [{ updated: true }]
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete({ sql }) {
|
async delete({ sql }) {
|
||||||
const response = await this.query(sql)
|
const response = await internalQuery(this.client, sql)
|
||||||
return response.rows.length ? response.rows : [{ deleted: true }]
|
return response.rows.length ? response.rows : [{ deleted: true }]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async query(json) {
|
||||||
|
const operation = this._operation(json).toLowerCase()
|
||||||
|
const input = this._query(json)
|
||||||
|
const response = await internalQuery(this.client, input)
|
||||||
|
return response.rows.length ? response.rows : [{ [operation]: true }]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
const pg = require("mysql")
|
|
||||||
const MySQLIntegration = require("../mysql")
|
const MySQLIntegration = require("../mysql")
|
||||||
jest.mock("mysql")
|
jest.mock("mysql")
|
||||||
|
|
||||||
class TestConfiguration {
|
class TestConfiguration {
|
||||||
constructor(config = { ssl: {} }) {
|
constructor(config = { ssl: {} }) {
|
||||||
this.integration = new MySQLIntegration.integration(config)
|
this.integration = new MySQLIntegration.integration(config)
|
||||||
this.query = jest.fn(() => [{ id: 1 }])
|
|
||||||
this.integration.query = this.query
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,43 +16,37 @@ describe("MySQL Integration", () => {
|
||||||
|
|
||||||
it("calls the create method with the correct params", async () => {
|
it("calls the create method with the correct params", 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({
|
await config.integration.create({
|
||||||
sql
|
sql
|
||||||
})
|
})
|
||||||
expect(config.query).toHaveBeenCalledWith({ sql })
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, expect.any(Function))
|
||||||
})
|
})
|
||||||
|
|
||||||
it("calls the read method with the correct params", async () => {
|
it("calls the read method with the correct params", async () => {
|
||||||
const sql = "select * from users;"
|
const sql = "select * from users;"
|
||||||
const response = await config.integration.read({
|
await config.integration.read({
|
||||||
sql
|
|
||||||
})
|
|
||||||
expect(config.query).toHaveBeenCalledWith({
|
|
||||||
sql
|
sql
|
||||||
})
|
})
|
||||||
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, expect.any(Function))
|
||||||
})
|
})
|
||||||
|
|
||||||
it("calls the update method with the correct params", async () => {
|
it("calls the update method with the correct params", async () => {
|
||||||
const sql = "update table users set name = 'test';"
|
const sql = "update table users set name = 'test';"
|
||||||
const response = await config.integration.update({
|
await config.integration.update({
|
||||||
sql
|
sql
|
||||||
})
|
})
|
||||||
expect(config.query).toHaveBeenCalledWith({ sql })
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, expect.any(Function))
|
||||||
})
|
})
|
||||||
|
|
||||||
it("calls the delete method with the correct params", async () => {
|
it("calls the delete method with the correct params", async () => {
|
||||||
const sql = "delete from users where name = 'todelete';"
|
const sql = "delete from users where name = 'todelete';"
|
||||||
const response = await config.integration.delete({
|
await config.integration.delete({
|
||||||
sql
|
sql
|
||||||
})
|
})
|
||||||
expect(config.query).toHaveBeenCalledWith({ sql })
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, expect.any(Function))
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("no rows returned", () => {
|
describe("no rows returned", () => {
|
||||||
beforeEach(() => {
|
|
||||||
config.query.mockImplementation(() => [])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns the correct response when the create response has no rows", async () => {
|
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({
|
const response = await config.integration.create({
|
||||||
|
|
Loading…
Reference in New Issue