Remove mssql mock, unify SQL-based query tests.

This commit is contained in:
Sam Rose 2024-03-20 17:59:35 +00:00
parent 6adcf3ec70
commit 545c67eac6
No known key found for this signature in database
6 changed files with 206 additions and 581 deletions

View File

@ -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,
},
],
})),
}

View File

@ -1,26 +1,43 @@
import { Datasource, Query } from "@budibase/types" import { Datasource, Query, SourceName } from "@budibase/types"
import * as setup from "../utilities" import * as setup from "../utilities"
import { databaseTestProviders } from "../../../../integrations/tests/utils" import { databaseTestProviders } from "../../../../integrations/tests/utils"
import pg from "pg"
import mysql from "mysql2/promise" 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 ( CREATE TABLE test_table (
id INT AUTO_INCREMENT PRIMARY KEY, 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 = ` const insertSQL = `INSERT INTO test_table (name) VALUES ('one'), ('two'), ('three'), ('four'), ('five')`
INSERT INTO test_table (name) VALUES ('one'), ('two'), ('three'), ('four'), ('five') const dropTableSQL = `DROP TABLE test_table;`
`
const dropTableSQL = ` describe.each([
DROP TABLE test_table ["postgres", databaseTestProviders.postgres],
` ["mysql", databaseTestProviders.mysql],
["mssql", databaseTestProviders.mssql],
describe("/queries", () => { ["mariadb", databaseTestProviders.mariadb],
let config = setup.getConfig() ])("queries (%s)", (__, dsProvider) => {
const config = setup.getConfig()
let datasource: Datasource let datasource: Datasource
async function createQuery(query: Partial<Query>): Promise<Query> { async function createQuery(query: Partial<Query>): Promise<Query> {
@ -37,124 +54,63 @@ describe("/queries", () => {
return await config.api.query.create({ ...defaultQuery, ...query }) return await config.api.query.create({ ...defaultQuery, ...query })
} }
async function withConnection( async function rawQuery(sql: string): Promise<any> {
callback: (client: mysql.Connection) => Promise<void> // We re-fetch the datasource here because the one returned by
): Promise<void> { // config.api.datasource.create has the password field blanked out, and we
const ds = await databaseTestProviders.mysql.datasource() // 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!) const con = await mysql.createConnection(ds.config!)
try { try {
await callback(con) const [rows] = await con.query(sql)
return rows
} finally { } finally {
con.end() con.end()
} }
} }
case SourceName.SQL_SERVER: {
afterAll(async () => { const pool = new mssql.ConnectionPool(ds.config! as mssql.config)
await databaseTestProviders.mysql.stop() const client = await pool.connect()
setup.afterAll() try {
}) const { recordset } = await client.query(sql)
return recordset
} finally {
await pool.close()
}
}
}
}
beforeAll(async () => { beforeAll(async () => {
await config.init() await config.init()
datasource = await config.api.datasource.create( datasource = await config.api.datasource.create(
await databaseTestProviders.mysql.datasource() await dsProvider.datasource()
) )
}) })
beforeEach(async () => { beforeEach(async () => {
await withConnection(async connection => { await rawQuery(createTableSQL[datasource.source])
await connection.query(createTableSQL) await rawQuery(insertSQL)
await connection.query(insertSQL)
})
}) })
afterEach(async () => { afterEach(async () => {
await withConnection(async connection => { await rawQuery(dropTableSQL)
await connection.query(dropTableSQL)
})
}) })
describe("read", () => { afterAll(async () => {
it("should execute a query", async () => { await dsProvider.stop()
const query = await createQuery({ setup.afterAll()
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",
},
])
})
}) })
describe("create", () => { describe("create", () => {
@ -184,33 +140,21 @@ describe("/queries", () => {
}, },
]) ])
await withConnection(async connection => { const rows = await rawQuery("SELECT * FROM test_table WHERE name = 'baz'")
const [rows] = await connection.query(
"SELECT * FROM test_table WHERE name = 'baz'"
)
expect(rows).toHaveLength(1) expect(rows).toHaveLength(1)
}) })
})
it.each(["2021-02-05T12:01:00.000Z", "2021-02-05"])( it.each(["2021-02-05T12:01:00.000Z", "2021-02-05"])(
"should coerce %s into a date", "should coerce %s into a date",
async dateStr => { async datetimeStr => {
const date = new Date(dateStr) const date = new Date(datetimeStr)
const tableName = `\`${generator.guid()}\``
await withConnection(async connection => {
await connection.query(`CREATE TABLE ${tableName} (
id INT AUTO_INCREMENT PRIMARY KEY,
date DATETIME NOT NULL
)`)
})
const query = await createQuery({ const query = await createQuery({
fields: { fields: {
sql: `INSERT INTO ${tableName} (date) VALUES ({{ date }})`, sql: `INSERT INTO test_table (name, birthday) VALUES ('foo', {{ birthday }})`,
}, },
parameters: [ parameters: [
{ {
name: "date", name: "birthday",
default: "", default: "",
}, },
], ],
@ -218,23 +162,21 @@ describe("/queries", () => {
}) })
const result = await config.api.query.execute(query._id!, { const result = await config.api.query.execute(query._id!, {
parameters: { date: dateStr }, parameters: { birthday: datetimeStr },
}) })
expect(result.data).toEqual([{ created: true }]) expect(result.data).toEqual([{ created: true }])
await withConnection(async connection => { const rows = await rawQuery(
const [rows] = await connection.query( `SELECT * FROM test_table WHERE birthday = '${date.toISOString()}'`
`SELECT * FROM ${tableName} WHERE date = '${date.toISOString()}'`
) )
expect(rows).toHaveLength(1) expect(rows).toHaveLength(1)
})
} }
) )
it.each(["2021,02,05", "202205-1500"])( it.each(["2021,02,05", "202205-1500"])(
"should not coerce %s as a date", "should not coerce %s as a date",
async date => { async notDateStr => {
const query = await createQuery({ const query = await createQuery({
fields: { fields: {
sql: "INSERT INTO test_table (name) VALUES ({{ name }})", sql: "INSERT INTO test_table (name) VALUES ({{ name }})",
@ -250,22 +192,110 @@ describe("/queries", () => {
const result = await config.api.query.execute(query._id!, { const result = await config.api.query.execute(query._id!, {
parameters: { parameters: {
name: date, name: notDateStr,
}, },
}) })
expect(result.data).toEqual([{ created: true }]) expect(result.data).toEqual([{ created: true }])
await withConnection(async connection => { const rows = await rawQuery(
const [rows] = await connection.query( `SELECT * FROM test_table WHERE name = '${notDateStr}'`
`SELECT * FROM test_table WHERE name = '${date}'`
) )
expect(rows).toHaveLength(1) 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", () => { describe("update", () => {
it("should be able to update rows", async () => { it("should be able to update rows", async () => {
const query = await createQuery({ const query = await createQuery({
@ -298,12 +328,8 @@ describe("/queries", () => {
}, },
]) ])
await withConnection(async connection => { const rows = await rawQuery("SELECT * FROM test_table WHERE id = 1")
const [rows] = await connection.query( expect(rows).toEqual([{ id: 1, name: "foo", birthday: null }])
"SELECT * FROM test_table WHERE id = 1"
)
expect(rows).toEqual([{ id: 1, name: "foo" }])
})
}) })
it("should be able to execute an update that updates no rows", async () => { 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", () => { describe("delete", () => {
@ -351,29 +394,8 @@ describe("/queries", () => {
}, },
]) ])
await withConnection(async connection => { const rows = await rawQuery("SELECT * FROM test_table WHERE id = 1")
const [rows] = await connection.query(
"SELECT * FROM test_table WHERE id = 1"
)
expect(rows).toHaveLength(0) 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,
},
])
})
})
}) })

View File

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

View File

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

View File

@ -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 }])
})
})
})

View File

@ -41,6 +41,9 @@ export async function datasource(): Promise<Datasource> {
port, port,
user: "sa", user: "sa",
password: "Password_123", password: "Password_123",
options: {
encrypt: false,
},
}, },
} }
} }