Use test api
This commit is contained in:
parent
986decb103
commit
14259c82f3
|
@ -36,8 +36,8 @@ describe.each([
|
||||||
])("/rows (%s)", (_, dsProvider) => {
|
])("/rows (%s)", (_, dsProvider) => {
|
||||||
const isInternal = !dsProvider
|
const isInternal = !dsProvider
|
||||||
|
|
||||||
let request = setup.getRequest()
|
const request = setup.getRequest()
|
||||||
let config = setup.getConfig()
|
const config = setup.getConfig()
|
||||||
let table: Table
|
let table: Table
|
||||||
let tableId: string
|
let tableId: string
|
||||||
|
|
||||||
|
@ -90,12 +90,8 @@ describe.each([
|
||||||
mocks.licenses.useCloudFree()
|
mocks.licenses.useCloudFree()
|
||||||
})
|
})
|
||||||
|
|
||||||
const loadRow = async (id: string, tbl_Id: string, status = 200) =>
|
const loadRow = (id: string, tbl_Id: string, status = 200) =>
|
||||||
await request
|
config.api.row.get(tbl_Id, id, { expectStatus: status })
|
||||||
.get(`/api/${tbl_Id}/rows/${id}`)
|
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(status)
|
|
||||||
|
|
||||||
const getRowUsage = async () => {
|
const getRowUsage = async () => {
|
||||||
const { total } = await config.doInContext(null, () =>
|
const { total } = await config.doInContext(null, () =>
|
||||||
|
@ -193,20 +189,12 @@ describe.each([
|
||||||
|
|
||||||
// Performing several create row requests should increment the autoID fields accordingly
|
// Performing several create row requests should increment the autoID fields accordingly
|
||||||
const createRow = async (id: number) => {
|
const createRow = async (id: number) => {
|
||||||
const res = await request
|
const res = await config.api.row.save(newTable._id!, {
|
||||||
.post(`/api/${newTable._id}/rows`)
|
name: "row_" + id,
|
||||||
.send({
|
})
|
||||||
name: "row_" + id,
|
expect(res.name).toEqual("row_" + id)
|
||||||
})
|
expect(res._rev).toBeDefined()
|
||||||
.set(config.defaultHeaders())
|
expect(res["Row ID"]).toEqual(id)
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(200)
|
|
||||||
expect((res as any).res.statusMessage).toEqual(
|
|
||||||
`${newTable.name} saved successfully`
|
|
||||||
)
|
|
||||||
expect(res.body.name).toEqual("row_" + id)
|
|
||||||
expect(res.body._rev).toBeDefined()
|
|
||||||
expect(res.body["Row ID"]).toEqual(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < ids.length; i++) {
|
for (let i = 0; i < ids.length; i++) {
|
||||||
|
@ -563,14 +551,7 @@ describe.each([
|
||||||
const rowUsage = await getRowUsage()
|
const rowUsage = await getRowUsage()
|
||||||
const queryUsage = await getQueryUsage()
|
const queryUsage = await getQueryUsage()
|
||||||
|
|
||||||
const res = await request
|
const res = await config.api.row.delete(table._id!, [createdRow])
|
||||||
.delete(`/api/${table._id}/rows`)
|
|
||||||
.send({
|
|
||||||
rows: [createdRow],
|
|
||||||
})
|
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(200)
|
|
||||||
expect(res.body[0]._id).toEqual(createdRow._id)
|
expect(res.body[0]._id).toEqual(createdRow._id)
|
||||||
await assertRowUsage(rowUsage - 1)
|
await assertRowUsage(rowUsage - 1)
|
||||||
await assertQueryUsage(queryUsage + 1)
|
await assertQueryUsage(queryUsage + 1)
|
||||||
|
@ -587,15 +568,10 @@ describe.each([
|
||||||
const rowUsage = await getRowUsage()
|
const rowUsage = await getRowUsage()
|
||||||
const queryUsage = await getQueryUsage()
|
const queryUsage = await getQueryUsage()
|
||||||
|
|
||||||
const res = await request
|
const res = await config.api.row.validate(table._id!, { name: "ivan" })
|
||||||
.post(`/api/${table._id}/rows/validate`)
|
|
||||||
.send({ name: "ivan" })
|
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(200)
|
|
||||||
|
|
||||||
expect(res.body.valid).toBe(true)
|
expect(res.valid).toBe(true)
|
||||||
expect(Object.keys(res.body.errors)).toEqual([])
|
expect(Object.keys(res.errors)).toEqual([])
|
||||||
await assertRowUsage(rowUsage)
|
await assertRowUsage(rowUsage)
|
||||||
await assertQueryUsage(queryUsage)
|
await assertQueryUsage(queryUsage)
|
||||||
})
|
})
|
||||||
|
@ -604,20 +580,15 @@ describe.each([
|
||||||
const rowUsage = await getRowUsage()
|
const rowUsage = await getRowUsage()
|
||||||
const queryUsage = await getQueryUsage()
|
const queryUsage = await getQueryUsage()
|
||||||
|
|
||||||
const res = await request
|
const res = await config.api.row.validate(table._id!, { name: 1 })
|
||||||
.post(`/api/${table._id}/rows/validate`)
|
|
||||||
.send({ name: 1 })
|
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(200)
|
|
||||||
|
|
||||||
if (isInternal) {
|
if (isInternal) {
|
||||||
expect(res.body.valid).toBe(false)
|
expect(res.valid).toBe(false)
|
||||||
expect(Object.keys(res.body.errors)).toEqual(["name"])
|
expect(Object.keys(res.errors)).toEqual(["name"])
|
||||||
} else {
|
} else {
|
||||||
// Validation for external is not implemented, so it will always return valid
|
// Validation for external is not implemented, so it will always return valid
|
||||||
expect(res.body.valid).toBe(true)
|
expect(res.valid).toBe(true)
|
||||||
expect(Object.keys(res.body.errors)).toEqual([])
|
expect(Object.keys(res.errors)).toEqual([])
|
||||||
}
|
}
|
||||||
await assertRowUsage(rowUsage)
|
await assertRowUsage(rowUsage)
|
||||||
await assertQueryUsage(queryUsage)
|
await assertQueryUsage(queryUsage)
|
||||||
|
@ -636,14 +607,7 @@ describe.each([
|
||||||
const rowUsage = await getRowUsage()
|
const rowUsage = await getRowUsage()
|
||||||
const queryUsage = await getQueryUsage()
|
const queryUsage = await getQueryUsage()
|
||||||
|
|
||||||
const res = await request
|
const res = await config.api.row.delete(table._id!, [row1, row2])
|
||||||
.delete(`/api/${table._id}/rows`)
|
|
||||||
.send({
|
|
||||||
rows: [row1, row2],
|
|
||||||
})
|
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(200)
|
|
||||||
|
|
||||||
expect(res.body.length).toEqual(2)
|
expect(res.body.length).toEqual(2)
|
||||||
await loadRow(row1._id!, table._id!, 404)
|
await loadRow(row1._id!, table._id!, 404)
|
||||||
|
@ -660,14 +624,11 @@ describe.each([
|
||||||
const rowUsage = await getRowUsage()
|
const rowUsage = await getRowUsage()
|
||||||
const queryUsage = await getQueryUsage()
|
const queryUsage = await getQueryUsage()
|
||||||
|
|
||||||
const res = await request
|
const res = await config.api.row.delete(table._id!, [
|
||||||
.delete(`/api/${table._id}/rows`)
|
row1,
|
||||||
.send({
|
row2._id,
|
||||||
rows: [row1, row2._id, { _id: row3._id }],
|
{ _id: row3._id },
|
||||||
})
|
])
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(200)
|
|
||||||
|
|
||||||
expect(res.body.length).toEqual(3)
|
expect(res.body.length).toEqual(3)
|
||||||
await loadRow(row1._id!, table._id!, 404)
|
await loadRow(row1._id!, table._id!, 404)
|
||||||
|
@ -680,12 +641,7 @@ describe.each([
|
||||||
const rowUsage = await getRowUsage()
|
const rowUsage = await getRowUsage()
|
||||||
const queryUsage = await getQueryUsage()
|
const queryUsage = await getQueryUsage()
|
||||||
|
|
||||||
const res = await request
|
const res = await config.api.row.delete(table._id!, row1)
|
||||||
.delete(`/api/${table._id}/rows`)
|
|
||||||
.send(row1)
|
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(200)
|
|
||||||
|
|
||||||
expect(res.body.id).toEqual(row1._id)
|
expect(res.body.id).toEqual(row1._id)
|
||||||
await loadRow(row1._id!, table._id!, 404)
|
await loadRow(row1._id!, table._id!, 404)
|
||||||
|
@ -697,31 +653,23 @@ describe.each([
|
||||||
const rowUsage = await getRowUsage()
|
const rowUsage = await getRowUsage()
|
||||||
const queryUsage = await getQueryUsage()
|
const queryUsage = await getQueryUsage()
|
||||||
|
|
||||||
const res = await request
|
const res = await config.api.row.delete(
|
||||||
.delete(`/api/${table._id}/rows`)
|
table._id!,
|
||||||
.send({ not: "valid" })
|
{ not: "valid" },
|
||||||
.set(config.defaultHeaders())
|
{ expectStatus: 400 }
|
||||||
.expect("Content-Type", /json/)
|
)
|
||||||
.expect(400)
|
|
||||||
|
|
||||||
expect(res.body.message).toEqual("Invalid delete rows request")
|
expect(res.body.message).toEqual("Invalid delete rows request")
|
||||||
|
|
||||||
const res2 = await request
|
const res2 = await config.api.row.delete(
|
||||||
.delete(`/api/${table._id}/rows`)
|
table._id!,
|
||||||
.send({ rows: 123 })
|
{ rows: 123 },
|
||||||
.set(config.defaultHeaders())
|
{ expectStatus: 400 }
|
||||||
.expect("Content-Type", /json/)
|
)
|
||||||
.expect(400)
|
|
||||||
|
|
||||||
expect(res2.body.message).toEqual("Invalid delete rows request")
|
expect(res2.body.message).toEqual("Invalid delete rows request")
|
||||||
|
|
||||||
const res3 = await request
|
const res3 = await config.api.row.delete(table._id!, "invalid", {
|
||||||
.delete(`/api/${table._id}/rows`)
|
expectStatus: 400,
|
||||||
.send("invalid")
|
})
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(400)
|
|
||||||
|
|
||||||
expect(res3.body.message).toEqual("Invalid delete rows request")
|
expect(res3.body.message).toEqual("Invalid delete rows request")
|
||||||
|
|
||||||
await assertRowUsage(rowUsage)
|
await assertRowUsage(rowUsage)
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
import { PatchRowRequest, SaveRowRequest, Row } from "@budibase/types"
|
import {
|
||||||
|
PatchRowRequest,
|
||||||
|
SaveRowRequest,
|
||||||
|
Row,
|
||||||
|
ValidateResponse,
|
||||||
|
} from "@budibase/types"
|
||||||
import TestConfiguration from "../TestConfiguration"
|
import TestConfiguration from "../TestConfiguration"
|
||||||
import { TestAPI } from "./base"
|
import { TestAPI } from "./base"
|
||||||
|
|
||||||
|
@ -51,6 +56,20 @@ export class RowAPI extends TestAPI {
|
||||||
return resp.body as Row
|
return resp.body as Row
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validate = async (
|
||||||
|
sourceId: string,
|
||||||
|
row: SaveRowRequest,
|
||||||
|
{ expectStatus } = { expectStatus: 200 }
|
||||||
|
): Promise<ValidateResponse> => {
|
||||||
|
const resp = await this.request
|
||||||
|
.post(`/api/${sourceId}/rows/validate`)
|
||||||
|
.send(row)
|
||||||
|
.set(this.config.defaultHeaders())
|
||||||
|
.expect("Content-Type", /json/)
|
||||||
|
.expect(expectStatus)
|
||||||
|
return resp.body as ValidateResponse
|
||||||
|
}
|
||||||
|
|
||||||
patch = async (
|
patch = async (
|
||||||
sourceId: string,
|
sourceId: string,
|
||||||
row: PatchRowRequest,
|
row: PatchRowRequest,
|
||||||
|
@ -71,7 +90,7 @@ export class RowAPI extends TestAPI {
|
||||||
) => {
|
) => {
|
||||||
return this.request
|
return this.request
|
||||||
.delete(`/api/${sourceId}/rows`)
|
.delete(`/api/${sourceId}/rows`)
|
||||||
.send({ rows })
|
.send(Array.isArray(rows) ? { rows } : rows)
|
||||||
.set(this.config.defaultHeaders())
|
.set(this.config.defaultHeaders())
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(expectStatus)
|
.expect(expectStatus)
|
||||||
|
|
Loading…
Reference in New Issue