Remove mssql mock, unify SQL-based query tests.
This commit is contained in:
parent
6adcf3ec70
commit
545c67eac6
|
@ -1,17 +0,0 @@
|
|||
module.exports = {
|
||||
ConnectionPool: jest.fn(() => ({
|
||||
connect: jest.fn(() => ({
|
||||
request: jest.fn(() => ({
|
||||
query: jest.fn(sql => ({ recordset: [sql] })),
|
||||
})),
|
||||
})),
|
||||
})),
|
||||
query: jest.fn(() => ({
|
||||
recordset: [
|
||||
{
|
||||
a: "string",
|
||||
b: 1,
|
||||
},
|
||||
],
|
||||
})),
|
||||
}
|
|
@ -1,26 +1,43 @@
|
|||
import { Datasource, Query } from "@budibase/types"
|
||||
import { Datasource, Query, SourceName } from "@budibase/types"
|
||||
import * as setup from "../utilities"
|
||||
import { databaseTestProviders } from "../../../../integrations/tests/utils"
|
||||
import pg from "pg"
|
||||
import mysql from "mysql2/promise"
|
||||
import { generator } from "@budibase/backend-core/tests"
|
||||
import mssql from "mssql"
|
||||
|
||||
const createTableSQL = `
|
||||
jest.unmock("pg")
|
||||
|
||||
const createTableSQL: Record<string, string> = {
|
||||
[SourceName.POSTGRES]: `
|
||||
CREATE TABLE test_table (
|
||||
id serial PRIMARY KEY,
|
||||
name VARCHAR ( 50 ) NOT NULL,
|
||||
birthday TIMESTAMP
|
||||
);`,
|
||||
[SourceName.MYSQL]: `
|
||||
CREATE TABLE test_table (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(50) NOT NULL
|
||||
)
|
||||
`
|
||||
name VARCHAR(50) NOT NULL,
|
||||
birthday TIMESTAMP
|
||||
);`,
|
||||
[SourceName.SQL_SERVER]: `
|
||||
CREATE TABLE test_table (
|
||||
id INT IDENTITY(1,1) PRIMARY KEY,
|
||||
name NVARCHAR(50) NOT NULL,
|
||||
birthday DATETIME
|
||||
);`,
|
||||
}
|
||||
|
||||
const insertSQL = `
|
||||
INSERT INTO test_table (name) VALUES ('one'), ('two'), ('three'), ('four'), ('five')
|
||||
`
|
||||
const insertSQL = `INSERT INTO test_table (name) VALUES ('one'), ('two'), ('three'), ('four'), ('five')`
|
||||
const dropTableSQL = `DROP TABLE test_table;`
|
||||
|
||||
const dropTableSQL = `
|
||||
DROP TABLE test_table
|
||||
`
|
||||
|
||||
describe("/queries", () => {
|
||||
let config = setup.getConfig()
|
||||
describe.each([
|
||||
["postgres", databaseTestProviders.postgres],
|
||||
["mysql", databaseTestProviders.mysql],
|
||||
["mssql", databaseTestProviders.mssql],
|
||||
["mariadb", databaseTestProviders.mariadb],
|
||||
])("queries (%s)", (__, dsProvider) => {
|
||||
const config = setup.getConfig()
|
||||
let datasource: Datasource
|
||||
|
||||
async function createQuery(query: Partial<Query>): Promise<Query> {
|
||||
|
@ -37,124 +54,63 @@ describe("/queries", () => {
|
|||
return await config.api.query.create({ ...defaultQuery, ...query })
|
||||
}
|
||||
|
||||
async function withConnection(
|
||||
callback: (client: mysql.Connection) => Promise<void>
|
||||
): Promise<void> {
|
||||
const ds = await databaseTestProviders.mysql.datasource()
|
||||
async function rawQuery(sql: string): Promise<any> {
|
||||
// We re-fetch the datasource here because the one returned by
|
||||
// config.api.datasource.create has the password field blanked out, and we
|
||||
// need the password to connect to the database.
|
||||
const ds = await dsProvider.datasource()
|
||||
switch (ds.source) {
|
||||
case SourceName.POSTGRES: {
|
||||
const client = new pg.Client(ds.config!)
|
||||
await client.connect()
|
||||
try {
|
||||
const { rows } = await client.query(sql)
|
||||
return rows
|
||||
} finally {
|
||||
await client.end()
|
||||
}
|
||||
}
|
||||
case SourceName.MYSQL: {
|
||||
const con = await mysql.createConnection(ds.config!)
|
||||
try {
|
||||
await callback(con)
|
||||
const [rows] = await con.query(sql)
|
||||
return rows
|
||||
} finally {
|
||||
con.end()
|
||||
}
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
await databaseTestProviders.mysql.stop()
|
||||
setup.afterAll()
|
||||
})
|
||||
case SourceName.SQL_SERVER: {
|
||||
const pool = new mssql.ConnectionPool(ds.config! as mssql.config)
|
||||
const client = await pool.connect()
|
||||
try {
|
||||
const { recordset } = await client.query(sql)
|
||||
return recordset
|
||||
} finally {
|
||||
await pool.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
await config.init()
|
||||
datasource = await config.api.datasource.create(
|
||||
await databaseTestProviders.mysql.datasource()
|
||||
await dsProvider.datasource()
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await withConnection(async connection => {
|
||||
await connection.query(createTableSQL)
|
||||
await connection.query(insertSQL)
|
||||
})
|
||||
await rawQuery(createTableSQL[datasource.source])
|
||||
await rawQuery(insertSQL)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await withConnection(async connection => {
|
||||
await connection.query(dropTableSQL)
|
||||
})
|
||||
await rawQuery(dropTableSQL)
|
||||
})
|
||||
|
||||
describe("read", () => {
|
||||
it("should execute a query", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "SELECT * FROM test_table ORDER BY id",
|
||||
},
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!)
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: "one",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "two",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "three",
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: "four",
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
name: "five",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should be able to transform a query", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "SELECT * FROM test_table WHERE id = 1",
|
||||
},
|
||||
transformer: `
|
||||
data[0].id = data[0].id + 1;
|
||||
return data;
|
||||
`,
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!)
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
id: 2,
|
||||
name: "one",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should coerce numeric bindings", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "SELECT * FROM test_table WHERE id = {{ id }}",
|
||||
},
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
default: "",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!, {
|
||||
parameters: {
|
||||
id: "1",
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: "one",
|
||||
},
|
||||
])
|
||||
})
|
||||
afterAll(async () => {
|
||||
await dsProvider.stop()
|
||||
setup.afterAll()
|
||||
})
|
||||
|
||||
describe("create", () => {
|
||||
|
@ -184,33 +140,21 @@ describe("/queries", () => {
|
|||
},
|
||||
])
|
||||
|
||||
await withConnection(async connection => {
|
||||
const [rows] = await connection.query(
|
||||
"SELECT * FROM test_table WHERE name = 'baz'"
|
||||
)
|
||||
const rows = await rawQuery("SELECT * FROM test_table WHERE name = 'baz'")
|
||||
expect(rows).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
it.each(["2021-02-05T12:01:00.000Z", "2021-02-05"])(
|
||||
"should coerce %s into a date",
|
||||
async dateStr => {
|
||||
const date = new Date(dateStr)
|
||||
const tableName = `\`${generator.guid()}\``
|
||||
await withConnection(async connection => {
|
||||
await connection.query(`CREATE TABLE ${tableName} (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
date DATETIME NOT NULL
|
||||
)`)
|
||||
})
|
||||
|
||||
async datetimeStr => {
|
||||
const date = new Date(datetimeStr)
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: `INSERT INTO ${tableName} (date) VALUES ({{ date }})`,
|
||||
sql: `INSERT INTO test_table (name, birthday) VALUES ('foo', {{ birthday }})`,
|
||||
},
|
||||
parameters: [
|
||||
{
|
||||
name: "date",
|
||||
name: "birthday",
|
||||
default: "",
|
||||
},
|
||||
],
|
||||
|
@ -218,23 +162,21 @@ describe("/queries", () => {
|
|||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!, {
|
||||
parameters: { date: dateStr },
|
||||
parameters: { birthday: datetimeStr },
|
||||
})
|
||||
|
||||
expect(result.data).toEqual([{ created: true }])
|
||||
|
||||
await withConnection(async connection => {
|
||||
const [rows] = await connection.query(
|
||||
`SELECT * FROM ${tableName} WHERE date = '${date.toISOString()}'`
|
||||
const rows = await rawQuery(
|
||||
`SELECT * FROM test_table WHERE birthday = '${date.toISOString()}'`
|
||||
)
|
||||
expect(rows).toHaveLength(1)
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
it.each(["2021,02,05", "202205-1500"])(
|
||||
"should not coerce %s as a date",
|
||||
async date => {
|
||||
async notDateStr => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "INSERT INTO test_table (name) VALUES ({{ name }})",
|
||||
|
@ -250,22 +192,110 @@ describe("/queries", () => {
|
|||
|
||||
const result = await config.api.query.execute(query._id!, {
|
||||
parameters: {
|
||||
name: date,
|
||||
name: notDateStr,
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.data).toEqual([{ created: true }])
|
||||
|
||||
await withConnection(async connection => {
|
||||
const [rows] = await connection.query(
|
||||
`SELECT * FROM test_table WHERE name = '${date}'`
|
||||
const rows = await rawQuery(
|
||||
`SELECT * FROM test_table WHERE name = '${notDateStr}'`
|
||||
)
|
||||
expect(rows).toHaveLength(1)
|
||||
})
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
describe("read", () => {
|
||||
it("should execute a query", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "SELECT * FROM test_table ORDER BY id",
|
||||
},
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!)
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: "one",
|
||||
birthday: null,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "two",
|
||||
birthday: null,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "three",
|
||||
birthday: null,
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: "four",
|
||||
birthday: null,
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
name: "five",
|
||||
birthday: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should be able to transform a query", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "SELECT * FROM test_table WHERE id = 1",
|
||||
},
|
||||
transformer: `
|
||||
data[0].id = data[0].id + 1;
|
||||
return data;
|
||||
`,
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!)
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
id: 2,
|
||||
name: "one",
|
||||
birthday: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should coerce numeric bindings", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "SELECT * FROM test_table WHERE id = {{ id }}",
|
||||
},
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
default: "",
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!, {
|
||||
parameters: {
|
||||
id: "1",
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: "one",
|
||||
birthday: null,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
it("should be able to update rows", async () => {
|
||||
const query = await createQuery({
|
||||
|
@ -298,12 +328,8 @@ describe("/queries", () => {
|
|||
},
|
||||
])
|
||||
|
||||
await withConnection(async connection => {
|
||||
const [rows] = await connection.query(
|
||||
"SELECT * FROM test_table WHERE id = 1"
|
||||
)
|
||||
expect(rows).toEqual([{ id: 1, name: "foo" }])
|
||||
})
|
||||
const rows = await rawQuery("SELECT * FROM test_table WHERE id = 1")
|
||||
expect(rows).toEqual([{ id: 1, name: "foo", birthday: null }])
|
||||
})
|
||||
|
||||
it("should be able to execute an update that updates no rows", async () => {
|
||||
|
@ -322,6 +348,23 @@ describe("/queries", () => {
|
|||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should be able to execute a delete that deletes no rows", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "DELETE FROM test_table WHERE id = 100",
|
||||
},
|
||||
queryVerb: "delete",
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!)
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
deleted: true,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", () => {
|
||||
|
@ -351,29 +394,8 @@ describe("/queries", () => {
|
|||
},
|
||||
])
|
||||
|
||||
await withConnection(async connection => {
|
||||
const [rows] = await connection.query(
|
||||
"SELECT * FROM test_table WHERE id = 1"
|
||||
)
|
||||
const rows = await rawQuery("SELECT * FROM test_table WHERE id = 1")
|
||||
expect(rows).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
it("should be able to execute a delete that deletes no rows", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "DELETE FROM test_table WHERE id = 100",
|
||||
},
|
||||
queryVerb: "delete",
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!)
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
deleted: true,
|
||||
},
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
|
@ -1,243 +0,0 @@
|
|||
import { Datasource, Query } from "@budibase/types"
|
||||
import * as setup from "../utilities"
|
||||
import { databaseTestProviders } from "../../../../integrations/tests/utils"
|
||||
import { Client } from "pg"
|
||||
|
||||
jest.unmock("pg")
|
||||
|
||||
const createTableSQL = `
|
||||
CREATE TABLE test_table (
|
||||
id serial PRIMARY KEY,
|
||||
name VARCHAR ( 50 ) NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
const insertSQL = `
|
||||
INSERT INTO test_table (name) VALUES ('one');
|
||||
INSERT INTO test_table (name) VALUES ('two');
|
||||
INSERT INTO test_table (name) VALUES ('three');
|
||||
INSERT INTO test_table (name) VALUES ('four');
|
||||
INSERT INTO test_table (name) VALUES ('five');
|
||||
`
|
||||
|
||||
const dropTableSQL = `
|
||||
DROP TABLE test_table;
|
||||
`
|
||||
|
||||
describe("/queries", () => {
|
||||
let config = setup.getConfig()
|
||||
let datasource: Datasource
|
||||
|
||||
async function createQuery(query: Partial<Query>): Promise<Query> {
|
||||
const defaultQuery: Query = {
|
||||
datasourceId: datasource._id!,
|
||||
name: "New Query",
|
||||
parameters: [],
|
||||
fields: {},
|
||||
schema: {},
|
||||
queryVerb: "read",
|
||||
transformer: "return data",
|
||||
readable: true,
|
||||
}
|
||||
return await config.api.query.create({ ...defaultQuery, ...query })
|
||||
}
|
||||
|
||||
async function withClient(
|
||||
callback: (client: Client) => Promise<void>
|
||||
): Promise<void> {
|
||||
const ds = await databaseTestProviders.postgres.datasource()
|
||||
const client = new Client(ds.config!)
|
||||
await client.connect()
|
||||
try {
|
||||
await callback(client)
|
||||
} finally {
|
||||
await client.end()
|
||||
}
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
await databaseTestProviders.postgres.stop()
|
||||
setup.afterAll()
|
||||
})
|
||||
|
||||
beforeAll(async () => {
|
||||
await config.init()
|
||||
datasource = await config.api.datasource.create(
|
||||
await databaseTestProviders.postgres.datasource()
|
||||
)
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await withClient(async client => {
|
||||
await client.query(createTableSQL)
|
||||
await client.query(insertSQL)
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await withClient(async client => {
|
||||
await client.query(dropTableSQL)
|
||||
})
|
||||
})
|
||||
|
||||
it("should execute a query", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "SELECT * FROM test_table ORDER BY id",
|
||||
},
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!)
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: "one",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "two",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "three",
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: "four",
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
name: "five",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should be able to transform a query", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "SELECT * FROM test_table WHERE id = 1",
|
||||
},
|
||||
transformer: `
|
||||
data[0].id = data[0].id + 1;
|
||||
return data;
|
||||
`,
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!)
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
id: 2,
|
||||
name: "one",
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("should be able to insert with bindings", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "INSERT INTO test_table (name) VALUES ({{ foo }})",
|
||||
},
|
||||
parameters: [
|
||||
{
|
||||
name: "foo",
|
||||
default: "bar",
|
||||
},
|
||||
],
|
||||
queryVerb: "create",
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!, {
|
||||
parameters: {
|
||||
foo: "baz",
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
created: true,
|
||||
},
|
||||
])
|
||||
|
||||
await withClient(async client => {
|
||||
const { rows } = await client.query(
|
||||
"SELECT * FROM test_table WHERE name = 'baz'"
|
||||
)
|
||||
expect(rows).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
it("should be able to update rows", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "UPDATE test_table SET name = {{ name }} WHERE id = {{ id }}",
|
||||
},
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
default: "",
|
||||
},
|
||||
{
|
||||
name: "name",
|
||||
default: "updated",
|
||||
},
|
||||
],
|
||||
queryVerb: "update",
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!, {
|
||||
parameters: {
|
||||
id: "1",
|
||||
name: "foo",
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
updated: true,
|
||||
},
|
||||
])
|
||||
|
||||
await withClient(async client => {
|
||||
const { rows } = await client.query(
|
||||
"SELECT * FROM test_table WHERE id = 1"
|
||||
)
|
||||
expect(rows).toEqual([{ id: 1, name: "foo" }])
|
||||
})
|
||||
})
|
||||
|
||||
it("should be able to delete rows", async () => {
|
||||
const query = await createQuery({
|
||||
fields: {
|
||||
sql: "DELETE FROM test_table WHERE id = {{ id }}",
|
||||
},
|
||||
parameters: [
|
||||
{
|
||||
name: "id",
|
||||
default: "",
|
||||
},
|
||||
],
|
||||
queryVerb: "delete",
|
||||
})
|
||||
|
||||
const result = await config.api.query.execute(query._id!, {
|
||||
parameters: {
|
||||
id: "1",
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.data).toEqual([
|
||||
{
|
||||
deleted: true,
|
||||
},
|
||||
])
|
||||
|
||||
await withClient(async client => {
|
||||
const { rows } = await client.query(
|
||||
"SELECT * FROM test_table WHERE id = 1"
|
||||
)
|
||||
expect(rows).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
})
|
|
@ -1,57 +0,0 @@
|
|||
import { default as MSSQLIntegration } from "../microsoftSqlServer"
|
||||
|
||||
jest.mock("mssql")
|
||||
|
||||
class TestConfiguration {
|
||||
integration: any
|
||||
|
||||
constructor(config: any = {}) {
|
||||
this.integration = new MSSQLIntegration.integration(config)
|
||||
}
|
||||
}
|
||||
|
||||
describe("MS SQL Server Integration", () => {
|
||||
let config: any
|
||||
|
||||
beforeEach(async () => {
|
||||
config = new TestConfiguration()
|
||||
})
|
||||
|
||||
describe("check sql used", () => {
|
||||
beforeEach(async () => {
|
||||
await config.integration.connect()
|
||||
})
|
||||
|
||||
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.request).toHaveBeenCalledWith()
|
||||
expect(response[0]).toEqual(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.request).toHaveBeenCalledWith()
|
||||
expect(response[0]).toEqual(sql)
|
||||
})
|
||||
})
|
||||
|
||||
describe("no rows returned", () => {
|
||||
beforeEach(async () => {
|
||||
await config.integration.connect()
|
||||
})
|
||||
|
||||
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[0]).toEqual(sql)
|
||||
})
|
||||
})
|
||||
})
|
|
@ -1,83 +0,0 @@
|
|||
const pg = require("pg")
|
||||
|
||||
import { default as PostgresIntegration } from "../postgres"
|
||||
|
||||
jest.mock("pg")
|
||||
|
||||
class TestConfiguration {
|
||||
integration: any
|
||||
|
||||
constructor(config: any = {}) {
|
||||
this.integration = new PostgresIntegration.integration(config)
|
||||
}
|
||||
}
|
||||
|
||||
describe("Postgres Integration", () => {
|
||||
let config: any
|
||||
|
||||
beforeEach(() => {
|
||||
config = new TestConfiguration()
|
||||
})
|
||||
|
||||
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,
|
||||
})
|
||||
expect(pg.queryMock).toHaveBeenCalledWith(sql, [])
|
||||
})
|
||||
|
||||
it("calls the read method with the correct params", async () => {
|
||||
const sql = "select * from users;"
|
||||
await config.integration.read({
|
||||
sql,
|
||||
})
|
||||
expect(pg.queryMock).toHaveBeenCalledWith(sql, [])
|
||||
})
|
||||
|
||||
it("calls the update method with the correct params", async () => {
|
||||
const sql = "update table users set name = 'test';"
|
||||
await config.integration.update({
|
||||
sql,
|
||||
})
|
||||
expect(pg.queryMock).toHaveBeenCalledWith(sql, [])
|
||||
})
|
||||
|
||||
it("calls the delete method with the correct params", async () => {
|
||||
const sql = "delete from users where name = 'todelete';"
|
||||
await config.integration.delete({
|
||||
sql,
|
||||
})
|
||||
expect(pg.queryMock).toHaveBeenCalledWith(sql, [])
|
||||
})
|
||||
|
||||
describe("no rows returned", () => {
|
||||
beforeEach(() => {
|
||||
pg.queryMock.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 }])
|
||||
})
|
||||
})
|
||||
})
|
|
@ -41,6 +41,9 @@ export async function datasource(): Promise<Datasource> {
|
|||
port,
|
||||
user: "sa",
|
||||
password: "Password_123",
|
||||
options: {
|
||||
encrypt: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue