Retrieve row test
This commit is contained in:
parent
5313c51e0f
commit
4ae43f1a96
2 changed files with 63 additions and 66 deletions
|
@ -5,10 +5,13 @@ import {
|
|||
} from "../api/routes/public/tests/utils"
|
||||
|
||||
import * as setup from "../api/routes/tests/utilities"
|
||||
import { Datasource, FieldType, SourceName } from "@budibase/types"
|
||||
import { Datasource, FieldType, SourceName, Table } from "@budibase/types"
|
||||
|
||||
const config = setup.getConfig()
|
||||
let apiKey, makeRequest: MakeRequestResponse, postgresDatasource: Datasource
|
||||
let apiKey,
|
||||
makeRequest: MakeRequestResponse,
|
||||
postgresDatasource: Datasource,
|
||||
postgresTable: Table
|
||||
|
||||
beforeEach(async () => {
|
||||
await config.init()
|
||||
|
@ -31,50 +34,56 @@ beforeEach(async () => {
|
|||
})
|
||||
|
||||
makeRequest = generateMakeRequest(apiKey)
|
||||
|
||||
postgresTable = await config.createTable({
|
||||
name: faker.lorem.word(),
|
||||
schema: {
|
||||
name: {
|
||||
name: "name",
|
||||
type: FieldType.STRING,
|
||||
constraints: {
|
||||
presence: true,
|
||||
},
|
||||
},
|
||||
description: {
|
||||
name: "description",
|
||||
type: FieldType.STRING,
|
||||
},
|
||||
value: {
|
||||
name: "value",
|
||||
type: FieldType.NUMBER,
|
||||
},
|
||||
},
|
||||
sourceId: postgresDatasource._id,
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await config.end()
|
||||
})
|
||||
|
||||
function createRandomRow() {
|
||||
return {
|
||||
name: faker.name.fullName(),
|
||||
description: faker.lorem.paragraphs(),
|
||||
value: +faker.random.numeric(),
|
||||
}
|
||||
}
|
||||
|
||||
describe("row api", () => {
|
||||
describe("create a row", () => {
|
||||
test("Given than no row exists, adding a new rows persists it", async () => {
|
||||
const tableName = faker.lorem.word()
|
||||
const newRow = createRandomRow()
|
||||
|
||||
const table = await config.createTable({
|
||||
name: tableName,
|
||||
schema: {
|
||||
name: {
|
||||
name: "name",
|
||||
type: FieldType.STRING,
|
||||
constraints: {
|
||||
presence: true,
|
||||
},
|
||||
},
|
||||
description: {
|
||||
name: "description",
|
||||
type: FieldType.STRING,
|
||||
},
|
||||
value: {
|
||||
name: "value",
|
||||
type: FieldType.NUMBER,
|
||||
},
|
||||
},
|
||||
sourceId: postgresDatasource._id,
|
||||
})
|
||||
|
||||
const newRow = {
|
||||
name: faker.name.fullName(),
|
||||
description: faker.lorem.paragraphs(),
|
||||
value: +faker.random.numeric(),
|
||||
}
|
||||
|
||||
const res = await makeRequest("post", `/tables/${table._id}/rows`, newRow)
|
||||
const res = await makeRequest(
|
||||
"post",
|
||||
`/tables/${postgresTable._id}/rows`,
|
||||
newRow
|
||||
)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
const persistedRows = await config.getRows(table._id!)
|
||||
const persistedRows = await config.getRows(postgresTable._id!)
|
||||
expect(persistedRows).toHaveLength(1)
|
||||
expect(persistedRows).toEqual([
|
||||
expect.objectContaining({
|
||||
|
@ -85,51 +94,39 @@ describe("row api", () => {
|
|||
})
|
||||
|
||||
test("Given than no row exists, multiple rows can be persisted", async () => {
|
||||
const tableName = faker.lorem.word()
|
||||
|
||||
const table = await config.createTable({
|
||||
name: tableName,
|
||||
schema: {
|
||||
name: {
|
||||
name: "name",
|
||||
type: FieldType.STRING,
|
||||
constraints: {
|
||||
presence: true,
|
||||
},
|
||||
},
|
||||
description: {
|
||||
name: "description",
|
||||
type: FieldType.STRING,
|
||||
},
|
||||
value: {
|
||||
name: "value",
|
||||
type: FieldType.NUMBER,
|
||||
},
|
||||
},
|
||||
sourceId: postgresDatasource._id,
|
||||
})
|
||||
|
||||
const numberOfRows = 10
|
||||
const newRows = Array(numberOfRows).fill({
|
||||
name: faker.name.fullName(),
|
||||
description: faker.lorem.paragraphs(),
|
||||
value: +faker.random.numeric(),
|
||||
})
|
||||
const newRows = Array(numberOfRows).fill(createRandomRow())
|
||||
|
||||
for (const newRow of newRows) {
|
||||
const res = await makeRequest(
|
||||
"post",
|
||||
`/tables/${table._id}/rows`,
|
||||
`/tables/${postgresTable._id}/rows`,
|
||||
newRow
|
||||
)
|
||||
expect(res.status).toBe(200)
|
||||
}
|
||||
|
||||
const persistedRows = await config.getRows(table._id!)
|
||||
const persistedRows = await config.getRows(postgresTable._id!)
|
||||
expect(persistedRows).toHaveLength(numberOfRows)
|
||||
expect(persistedRows).toEqual(
|
||||
expect.arrayContaining(newRows.map(expect.objectContaining))
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Retrieve a row", () => {
|
||||
test("Given than a table have a single row, the row can be retrieved successfully", async () => {
|
||||
const rowData = createRandomRow()
|
||||
const row = await config.createRow(rowData)
|
||||
|
||||
const res = await makeRequest(
|
||||
"get",
|
||||
`/tables/${postgresTable._id}/rows/${row._id}`
|
||||
)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
expect(res.body.data).toEqual(expect.objectContaining(rowData))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -39,7 +39,7 @@ import newid from "../../db/newid"
|
|||
import { generateUserMetadataID } from "../../db/utils"
|
||||
import { startup } from "../../startup"
|
||||
import supertest from "supertest"
|
||||
import { Datasource, SourceName, Table } from "@budibase/types"
|
||||
import { Datasource, Row, SourceName, Table } from "@budibase/types"
|
||||
|
||||
const GLOBAL_USER_ID = "us_uuid1"
|
||||
const EMAIL = "babs@babs.com"
|
||||
|
@ -463,7 +463,7 @@ class TestConfiguration {
|
|||
|
||||
// ROW
|
||||
|
||||
async createRow(config: any = null) {
|
||||
async createRow(config: any = null): Promise<Row> {
|
||||
if (!this.table) {
|
||||
throw "Test requires table to be configured."
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue