budibase/packages/server/src/integration-test/row.spec.ts

149 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-01-17 18:22:31 +01:00
import { faker } from "@faker-js/faker"
import {
generateMakeRequest,
MakeRequestResponse,
} from "../api/routes/public/tests/utils"
import * as setup from "../api/routes/tests/utilities"
2023-01-18 17:46:40 +01:00
import { Datasource, FieldType, SourceName, Table } from "@budibase/types"
2023-01-17 18:22:31 +01:00
const config = setup.getConfig()
2023-01-18 17:46:40 +01:00
let apiKey,
makeRequest: MakeRequestResponse,
postgresDatasource: Datasource,
postgresTable: Table
2023-01-17 18:22:31 +01:00
2023-01-18 17:06:45 +01:00
beforeEach(async () => {
2023-01-18 14:55:24 +01:00
await config.init()
2023-01-17 18:22:31 +01:00
apiKey = await config.generateApiKey()
2023-01-18 13:19:40 +01:00
postgresDatasource = await config.createDatasource({
type: "datasource",
source: SourceName.POSTGRES,
plus: true,
config: {
host: "192.168.1.98",
port: 54321,
database: "postgres",
user: "root",
password: "root",
schema: "public",
ssl: false,
rejectUnauthorized: false,
ca: false,
},
})
2023-01-18 14:55:24 +01:00
2023-01-17 18:22:31 +01:00
makeRequest = generateMakeRequest(apiKey)
2023-01-18 17:46:40 +01:00
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,
})
2023-01-17 18:22:31 +01:00
})
afterAll(async () => {
await config.end()
})
2023-01-18 17:46:40 +01:00
function createRandomRow() {
return {
name: faker.name.fullName(),
description: faker.lorem.paragraphs(),
value: +faker.random.numeric(),
}
}
2023-01-17 18:22:31 +01:00
describe("row api", () => {
describe("create a row", () => {
test("Given than no row exists, adding a new rows persists it", async () => {
2023-01-18 17:46:40 +01:00
const newRow = createRandomRow()
2023-01-17 18:22:31 +01:00
2023-01-18 17:46:40 +01:00
const res = await makeRequest(
"post",
`/tables/${postgresTable._id}/rows`,
newRow
)
2023-01-17 18:22:31 +01:00
expect(res.status).toBe(200)
2023-01-17 18:39:59 +01:00
2023-01-18 17:46:40 +01:00
const persistedRows = await config.getRows(postgresTable._id!)
2023-01-17 18:39:59 +01:00
expect(persistedRows).toHaveLength(1)
expect(persistedRows).toEqual([
expect.objectContaining({
...res.body.data,
...newRow,
}),
])
2023-01-17 18:22:31 +01:00
})
2023-01-18 13:26:26 +01:00
test("Given than no row exists, multiple rows can be persisted", async () => {
const numberOfRows = 10
2023-01-18 17:46:40 +01:00
const newRows = Array(numberOfRows).fill(createRandomRow())
2023-01-18 13:26:26 +01:00
for (const newRow of newRows) {
const res = await makeRequest(
"post",
2023-01-18 17:46:40 +01:00
`/tables/${postgresTable._id}/rows`,
2023-01-18 13:26:26 +01:00
newRow
)
expect(res.status).toBe(200)
}
2023-01-18 17:46:40 +01:00
const persistedRows = await config.getRows(postgresTable._id!)
2023-01-18 13:26:26 +01:00
expect(persistedRows).toHaveLength(numberOfRows)
2023-01-18 17:06:45 +01:00
expect(persistedRows).toEqual(
expect.arrayContaining(newRows.map(expect.objectContaining))
)
2023-01-18 13:26:26 +01:00
})
2023-01-17 18:22:31 +01:00
})
2023-01-18 17:46:40 +01:00
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))
})
2023-01-18 17:48:18 +01:00
test("Given than a table have a multiple rows, a single row can be retrieved successfully", async () => {
await Promise.all(Array(5).map(() => config.createRow(createRandomRow())))
const rowData = createRandomRow()
const row = await config.createRow(rowData)
await Promise.all(Array(2).map(() => config.createRow(createRandomRow())))
const res = await makeRequest(
"get",
`/tables/${postgresTable._id}/rows/${row._id}`
)
expect(res.status).toBe(200)
expect(res.body.data).toEqual(expect.objectContaining(rowData))
})
2023-01-18 17:46:40 +01:00
})
2023-01-17 18:22:31 +01:00
})