Add tests for create queries.

This commit is contained in:
Sam Rose 2024-02-05 16:49:21 +00:00
parent a773841518
commit 7a012f1f4b
No known key found for this signature in database
5 changed files with 154 additions and 35 deletions

View File

@ -15,6 +15,9 @@ import {
SessionCookie,
QuerySchema,
FieldType,
type ExecuteQueryRequest,
type ExecuteQueryResponse,
type Row,
} from "@budibase/types"
import { ValidQueryNameRegex } from "@budibase/shared-core"
@ -223,7 +226,7 @@ export async function preview(ctx: UserCtx) {
}
async function execute(
ctx: UserCtx,
ctx: UserCtx<ExecuteQueryRequest, ExecuteQueryResponse | Row[]>,
opts: any = { rowsOnly: false, isAutomation: false }
) {
const db = context.getAppDB()

View File

@ -1,7 +1,7 @@
import { Datasource, Query } from "@budibase/types"
import * as setup from "../utilities"
import { databaseTestProviders } from "../../../../integrations/tests/utils"
import { MongoClient } from "mongodb"
import { MongoClient, type Collection } from "mongodb"
jest.unmock("mongodb")
@ -23,6 +23,31 @@ describe("/queries", () => {
return await config.api.query.create({ ...defaultQuery, ...query })
}
async function withClient(
callback: (client: MongoClient) => Promise<void>
): Promise<void> {
const ds = await databaseTestProviders.mongodb.datasource()
const client = new MongoClient(ds.config!.connectionString)
await client.connect()
try {
await callback(client)
} finally {
await client.close()
}
}
async function withCollection(
collection: string,
callback: (collection: Collection) => Promise<void>
): Promise<void> {
await withClient(async client => {
const db = client.db(
(await databaseTestProviders.mongodb.datasource()).config!.db
)
await callback(db.collection(collection))
})
}
afterAll(async () => {
await databaseTestProviders.mongodb.stop()
setup.afterAll()
@ -36,29 +61,21 @@ describe("/queries", () => {
})
beforeEach(async () => {
const ds = await databaseTestProviders.mongodb.datasource()
const client = new MongoClient(ds.config!.connectionString)
await client.connect()
const db = client.db(ds.config!.db)
const collection = db.collection("test_table")
await collection.insertMany([
{ name: "one" },
{ name: "two" },
{ name: "three" },
{ name: "four" },
{ name: "five" },
])
await client.close()
await withCollection("test_table", async collection => {
await collection.insertMany([
{ name: "one" },
{ name: "two" },
{ name: "three" },
{ name: "four" },
{ name: "five" },
])
})
})
afterEach(async () => {
const ds = await databaseTestProviders.mongodb.datasource()
const client = new MongoClient(ds.config!.connectionString)
await client.connect()
const db = client.db(ds.config!.db)
await db.collection("test_table").drop()
await client.close()
await withCollection("test_table", async collection => {
await collection.drop()
})
})
it("should execute a query", async () => {
@ -93,4 +110,42 @@ describe("/queries", () => {
expect(result.data).toEqual([{ value: 6 }])
})
it("should execute a create query with parameters", async () => {
const query = await createQuery({
fields: {
json: '{"foo": "{{ foo }}"}',
extra: {
actionType: "insertOne",
collection: "test_table",
},
},
queryVerb: "create",
parameters: [
{
name: "foo",
default: "default",
},
],
})
const result = await config.api.query.execute(query._id!, {
parameters: { foo: "bar" },
})
expect(result.data).toEqual([
{
acknowledged: true,
insertedId: expect.anything(),
},
])
await withCollection("test_table", async collection => {
const doc = await collection.findOne({ foo: { $eq: "bar" } })
expect(doc).toEqual({
_id: expect.anything(),
foo: "bar",
})
})
})
})

View File

@ -42,6 +42,19 @@ describe("/queries", () => {
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()
@ -55,20 +68,16 @@ describe("/queries", () => {
})
beforeEach(async () => {
const ds = await databaseTestProviders.postgres.datasource()
const client = new Client(ds.config!)
await client.connect()
await client.query(createTableSQL)
await client.query(insertSQL)
await client.end()
await withClient(async client => {
await client.query(createTableSQL)
await client.query(insertSQL)
})
})
afterEach(async () => {
const ds = await databaseTestProviders.postgres.datasource()
const client = new Client(ds.config!)
await client.connect()
await client.query(dropTableSQL)
await client.end()
await withClient(async client => {
await client.query(dropTableSQL)
})
})
it("should execute a query", async () => {
@ -124,4 +133,38 @@ describe("/queries", () => {
},
])
})
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)
})
})
})

View File

@ -1,5 +1,9 @@
import TestConfiguration from "../TestConfiguration"
import { Query } from "@budibase/types"
import {
Query,
type ExecuteQueryRequest,
type ExecuteQueryResponse,
} from "@budibase/types"
import { TestAPI } from "./base"
export class QueryAPI extends TestAPI {
@ -21,10 +25,14 @@ export class QueryAPI extends TestAPI {
return res.body as Query
}
execute = async (queryId: string): Promise<{ data: any }> => {
execute = async (
queryId: string,
body?: ExecuteQueryRequest
): Promise<ExecuteQueryResponse> => {
const res = await this.request
.post(`/api/v2/queries/${queryId}`)
.set(this.config.defaultHeaders())
.send(body)
.expect("Content-Type", /json/)
if (res.status !== 200) {

View File

@ -1,4 +1,5 @@
import { Document } from "../document"
import type { Row } from "./row"
export interface QuerySchema {
name?: string
@ -54,3 +55,12 @@ export interface PreviewQueryRequest extends Omit<Query, "parameters"> {
urlName?: boolean
}
}
export interface ExecuteQueryRequest {
parameters?: { [key: string]: string }
pagination?: any
}
export interface ExecuteQueryResponse {
data: Row[]
}