Add tests for create queries.
This commit is contained in:
parent
a773841518
commit
7a012f1f4b
|
@ -15,6 +15,9 @@ import {
|
||||||
SessionCookie,
|
SessionCookie,
|
||||||
QuerySchema,
|
QuerySchema,
|
||||||
FieldType,
|
FieldType,
|
||||||
|
type ExecuteQueryRequest,
|
||||||
|
type ExecuteQueryResponse,
|
||||||
|
type Row,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { ValidQueryNameRegex } from "@budibase/shared-core"
|
import { ValidQueryNameRegex } from "@budibase/shared-core"
|
||||||
|
|
||||||
|
@ -223,7 +226,7 @@ export async function preview(ctx: UserCtx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function execute(
|
async function execute(
|
||||||
ctx: UserCtx,
|
ctx: UserCtx<ExecuteQueryRequest, ExecuteQueryResponse | Row[]>,
|
||||||
opts: any = { rowsOnly: false, isAutomation: false }
|
opts: any = { rowsOnly: false, isAutomation: false }
|
||||||
) {
|
) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Datasource, Query } from "@budibase/types"
|
import { Datasource, Query } 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 { MongoClient } from "mongodb"
|
import { MongoClient, type Collection } from "mongodb"
|
||||||
|
|
||||||
jest.unmock("mongodb")
|
jest.unmock("mongodb")
|
||||||
|
|
||||||
|
@ -23,6 +23,31 @@ describe("/queries", () => {
|
||||||
return await config.api.query.create({ ...defaultQuery, ...query })
|
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 () => {
|
afterAll(async () => {
|
||||||
await databaseTestProviders.mongodb.stop()
|
await databaseTestProviders.mongodb.stop()
|
||||||
setup.afterAll()
|
setup.afterAll()
|
||||||
|
@ -36,29 +61,21 @@ describe("/queries", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const ds = await databaseTestProviders.mongodb.datasource()
|
await withCollection("test_table", async collection => {
|
||||||
const client = new MongoClient(ds.config!.connectionString)
|
await collection.insertMany([
|
||||||
await client.connect()
|
{ name: "one" },
|
||||||
|
{ name: "two" },
|
||||||
const db = client.db(ds.config!.db)
|
{ name: "three" },
|
||||||
const collection = db.collection("test_table")
|
{ name: "four" },
|
||||||
await collection.insertMany([
|
{ name: "five" },
|
||||||
{ name: "one" },
|
])
|
||||||
{ name: "two" },
|
})
|
||||||
{ name: "three" },
|
|
||||||
{ name: "four" },
|
|
||||||
{ name: "five" },
|
|
||||||
])
|
|
||||||
await client.close()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
const ds = await databaseTestProviders.mongodb.datasource()
|
await withCollection("test_table", async collection => {
|
||||||
const client = new MongoClient(ds.config!.connectionString)
|
await collection.drop()
|
||||||
await client.connect()
|
})
|
||||||
const db = client.db(ds.config!.db)
|
|
||||||
await db.collection("test_table").drop()
|
|
||||||
await client.close()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should execute a query", async () => {
|
it("should execute a query", async () => {
|
||||||
|
@ -93,4 +110,42 @@ describe("/queries", () => {
|
||||||
|
|
||||||
expect(result.data).toEqual([{ value: 6 }])
|
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",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -42,6 +42,19 @@ describe("/queries", () => {
|
||||||
return await config.api.query.create({ ...defaultQuery, ...query })
|
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 () => {
|
afterAll(async () => {
|
||||||
await databaseTestProviders.postgres.stop()
|
await databaseTestProviders.postgres.stop()
|
||||||
setup.afterAll()
|
setup.afterAll()
|
||||||
|
@ -55,20 +68,16 @@ describe("/queries", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const ds = await databaseTestProviders.postgres.datasource()
|
await withClient(async client => {
|
||||||
const client = new Client(ds.config!)
|
await client.query(createTableSQL)
|
||||||
await client.connect()
|
await client.query(insertSQL)
|
||||||
await client.query(createTableSQL)
|
})
|
||||||
await client.query(insertSQL)
|
|
||||||
await client.end()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
const ds = await databaseTestProviders.postgres.datasource()
|
await withClient(async client => {
|
||||||
const client = new Client(ds.config!)
|
await client.query(dropTableSQL)
|
||||||
await client.connect()
|
})
|
||||||
await client.query(dropTableSQL)
|
|
||||||
await client.end()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should execute a query", async () => {
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
import TestConfiguration from "../TestConfiguration"
|
import TestConfiguration from "../TestConfiguration"
|
||||||
import { Query } from "@budibase/types"
|
import {
|
||||||
|
Query,
|
||||||
|
type ExecuteQueryRequest,
|
||||||
|
type ExecuteQueryResponse,
|
||||||
|
} from "@budibase/types"
|
||||||
import { TestAPI } from "./base"
|
import { TestAPI } from "./base"
|
||||||
|
|
||||||
export class QueryAPI extends TestAPI {
|
export class QueryAPI extends TestAPI {
|
||||||
|
@ -21,10 +25,14 @@ export class QueryAPI extends TestAPI {
|
||||||
return res.body as Query
|
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
|
const res = await this.request
|
||||||
.post(`/api/v2/queries/${queryId}`)
|
.post(`/api/v2/queries/${queryId}`)
|
||||||
.set(this.config.defaultHeaders())
|
.set(this.config.defaultHeaders())
|
||||||
|
.send(body)
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
|
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { Document } from "../document"
|
import { Document } from "../document"
|
||||||
|
import type { Row } from "./row"
|
||||||
|
|
||||||
export interface QuerySchema {
|
export interface QuerySchema {
|
||||||
name?: string
|
name?: string
|
||||||
|
@ -54,3 +55,12 @@ export interface PreviewQueryRequest extends Omit<Query, "parameters"> {
|
||||||
urlName?: boolean
|
urlName?: boolean
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ExecuteQueryRequest {
|
||||||
|
parameters?: { [key: string]: string }
|
||||||
|
pagination?: any
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ExecuteQueryResponse {
|
||||||
|
data: Row[]
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue