1
0
Fork 0
mirror of synced 2024-09-21 20:01:32 +12:00
budibase/packages/server/src/integration-test/postgres.spec.ts

598 lines
16 KiB
TypeScript
Raw Normal View History

2023-01-18 06:22:31 +13:00
import {
generateMakeRequest,
MakeRequestResponse,
} from "../api/routes/public/tests/utils"
import * as setup from "../api/routes/tests/utilities"
2023-01-20 09:09:39 +13:00
import {
Datasource,
FieldType,
RelationshipTypes,
Row,
SourceName,
Table,
} from "@budibase/types"
2023-01-19 06:07:09 +13:00
import _ from "lodash"
2023-02-02 23:43:18 +13:00
import { generator } from "@budibase/backend-core/tests"
import { utils } from "@budibase/backend-core"
2023-01-18 06:22:31 +13:00
2023-01-21 00:48:11 +13:00
const config = setup.getConfig()!
2023-01-19 02:55:24 +13:00
2023-01-20 00:00:51 +13:00
jest.unmock("node-fetch")
2023-01-21 00:48:11 +13:00
// TODO: Waiting for the test image to exist
2023-02-02 23:43:18 +13:00
describe("row api - postgres", () => {
2023-01-19 06:07:09 +13:00
let apiKey,
makeRequest: MakeRequestResponse,
postgresDatasource: Datasource,
2023-01-20 09:09:39 +13:00
postgresTable: Table,
auxPostgresTable: Table
2023-01-19 06:07:09 +13:00
2023-02-02 23:43:18 +13:00
const host = generator.ip()
const port = generator.natural()
2023-01-19 06:07:09 +13:00
beforeEach(async () => {
await config.init()
apiKey = await config.generateApiKey()
postgresDatasource = await config.createDatasource({
2023-01-21 00:48:11 +13:00
datasource: {
type: "datasource",
source: SourceName.POSTGRES,
plus: true,
config: {
2023-02-02 23:43:18 +13:00
host,
port,
2023-01-21 00:48:11 +13:00
database: "postgres",
user: "root",
password: "root",
schema: "public",
ssl: false,
rejectUnauthorized: false,
ca: false,
},
2023-01-19 06:07:09 +13:00
},
})
2023-01-19 05:46:40 +13:00
2023-01-20 05:43:39 +13:00
makeRequest = generateMakeRequest(apiKey, true)
2023-01-19 06:07:09 +13:00
postgresTable = await config.createTable({
2023-02-02 23:43:18 +13:00
name: generator.word(),
2023-01-19 06:07:09 +13:00
schema: {
name: {
name: "name",
type: FieldType.STRING,
constraints: {
presence: true,
},
},
description: {
name: "description",
type: FieldType.STRING,
},
value: {
name: "value",
type: FieldType.NUMBER,
2023-01-19 05:46:40 +13:00
},
},
2023-01-19 06:07:09 +13:00
sourceId: postgresDatasource._id,
})
2023-01-20 09:09:39 +13:00
auxPostgresTable = await config.createTable({
2023-02-02 23:43:18 +13:00
name: generator.word(),
2023-01-20 09:09:39 +13:00
schema: {
title: {
name: "title",
type: FieldType.STRING,
constraints: {
presence: true,
},
},
linkedField: {
type: FieldType.LINK,
constraints: {
type: "array",
presence: true,
},
fieldName: "foreignField",
name: "linkedField",
relationshipType: RelationshipTypes.MANY_TO_MANY,
tableId: postgresTable._id,
},
},
sourceId: postgresDatasource._id,
})
2023-01-19 05:46:40 +13:00
})
2023-01-18 06:22:31 +13:00
2023-01-19 06:07:09 +13:00
afterAll(async () => {
await config.end()
})
function createRandomRow() {
return {
2023-02-02 23:43:18 +13:00
name: generator.name(),
description: generator.paragraph(),
value: generator.integer(),
2023-01-19 06:07:09 +13:00
}
}
2023-01-18 06:22:31 +13:00
2023-01-20 00:35:00 +13:00
function createRow(
row: {
name: string
description: string
value: number
},
tableId?: string
) {
return config.createRow({
tableId: tableId || postgresTable._id,
...row,
})
}
2023-01-20 00:06:41 +13:00
async function populateRows(count: number, tableId?: string) {
2023-01-19 06:07:09 +13:00
return await Promise.all(
Array(count)
.fill({})
.map(async () => {
const rowData = createRandomRow()
return {
rowData,
2023-01-20 00:35:00 +13:00
row: await createRow(rowData, tableId || postgresTable._id),
2023-01-19 06:07:09 +13:00
}
})
)
2023-01-19 05:46:40 +13:00
}
2023-01-20 05:43:39 +13:00
test("validate table schema", async () => {
2023-01-20 05:21:37 +13:00
const res = await makeRequest(
"get",
2023-01-20 05:43:39 +13:00
`/api/datasources/${postgresDatasource._id}`
2023-01-20 05:21:37 +13:00
)
expect(res.status).toBe(200)
2023-01-20 05:43:39 +13:00
expect(res.body).toEqual({
config: {
ca: false,
database: "postgres",
2023-02-02 23:43:18 +13:00
host,
password: "--secret-value--",
port,
2023-01-20 05:43:39 +13:00
rejectUnauthorized: false,
schema: "public",
ssl: false,
user: "root",
},
plus: true,
source: "POSTGRES",
type: "datasource",
_id: expect.any(String),
_rev: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
})
2023-01-20 05:21:37 +13:00
})
2023-01-18 06:22:31 +13:00
describe("create a row", () => {
2023-01-20 06:23:48 +13:00
const createRow = (tableId: string | undefined, body: object) =>
makeRequest("post", `/api/${tableId}/rows`, body)
2023-01-19 06:07:09 +13:00
test("Given than no row exists, adding a new row persists it", async () => {
2023-01-19 05:46:40 +13:00
const newRow = createRandomRow()
2023-01-18 06:22:31 +13:00
2023-01-20 06:23:48 +13:00
const res = await createRow(postgresTable._id, newRow)
2023-01-18 06:22:31 +13:00
expect(res.status).toBe(200)
2023-01-18 06:39:59 +13:00
2023-01-19 05:46:40 +13:00
const persistedRows = await config.getRows(postgresTable._id!)
2023-01-18 06:39:59 +13:00
expect(persistedRows).toHaveLength(1)
expect(persistedRows).toEqual([
expect.objectContaining({
2023-01-20 06:23:48 +13:00
...res.body,
2023-01-18 06:39:59 +13:00
...newRow,
}),
])
2023-01-18 06:22:31 +13:00
})
2023-01-19 01:26:26 +13:00
test("Given than no row exists, multiple rows can be persisted", async () => {
const numberOfRows = 10
2023-01-19 05:46:40 +13:00
const newRows = Array(numberOfRows).fill(createRandomRow())
2023-01-19 01:26:26 +13:00
for (const newRow of newRows) {
2023-01-20 06:23:48 +13:00
const res = await createRow(postgresTable._id, newRow)
2023-01-19 01:26:26 +13:00
expect(res.status).toBe(200)
}
2023-01-19 05:46:40 +13:00
const persistedRows = await config.getRows(postgresTable._id!)
2023-01-19 01:26:26 +13:00
expect(persistedRows).toHaveLength(numberOfRows)
2023-01-19 05:06:45 +13:00
expect(persistedRows).toEqual(
expect.arrayContaining(newRows.map(expect.objectContaining))
)
2023-01-19 01:26:26 +13:00
})
2023-01-18 06:22:31 +13:00
})
2023-01-19 05:46:40 +13:00
2023-01-19 06:11:52 +13:00
describe("update a row", () => {
2023-01-20 06:23:48 +13:00
const updateRow = (tableId: string | undefined, body: Row) =>
makeRequest("patch", `/api/${tableId}/rows`, body)
2023-01-19 06:11:52 +13:00
test("Given than a row exists, updating it persists it", async () => {
2023-01-20 06:23:48 +13:00
let { row } = _.sample(await populateRows(10))!
2023-01-19 06:11:52 +13:00
2023-02-02 23:43:18 +13:00
const newName = generator.name()
const newValue = generator.integer()
2023-01-20 06:23:48 +13:00
const updatedRow = {
...row,
2023-01-19 06:11:52 +13:00
name: newName,
value: newValue,
}
2023-01-20 06:23:48 +13:00
const res = await updateRow(postgresTable._id, updatedRow)
2023-01-19 06:11:52 +13:00
expect(res.status).toBe(200)
2023-01-19 06:21:17 +13:00
const persistedRow = await config.getRow(postgresTable._id!, row._id!)
2023-01-19 06:11:52 +13:00
2023-01-19 06:21:17 +13:00
expect(persistedRow).toEqual(
2023-01-19 06:11:52 +13:00
expect.objectContaining({
2023-01-20 06:23:48 +13:00
_id: row._id,
name: newName,
value: newValue,
2023-01-19 06:11:52 +13:00
})
)
})
})
2023-01-19 06:21:17 +13:00
describe("delete a row", () => {
2023-01-20 06:23:48 +13:00
const deleteRow = (
tableId: string | undefined,
body: Row | { rows: Row[] }
) => makeRequest("delete", `/api/${tableId}/rows`, body)
2023-01-19 06:21:17 +13:00
test("Given than a row exists, delete request removes it", async () => {
const numberOfInitialRows = 5
let { row } = _.sample(await populateRows(numberOfInitialRows))!
2023-01-20 06:23:48 +13:00
const res = await deleteRow(postgresTable._id, row)
2023-01-19 06:21:17 +13:00
expect(res.status).toBe(200)
const persistedRows = await config.getRows(postgresTable._id!)
expect(persistedRows).toHaveLength(numberOfInitialRows - 1)
expect(row._id).toBeDefined()
expect(persistedRows).not.toContain(
expect.objectContaining({ _id: row._id })
)
})
2023-01-20 06:23:48 +13:00
2023-01-20 06:28:42 +13:00
test("Given than multiple rows exist, multiple rows can be removed", async () => {
const numberOfInitialRows = 5
let rows = _.sampleSize(await populateRows(numberOfInitialRows), 3)!.map(
x => x.row
)
const res = await deleteRow(postgresTable._id, { rows })
expect(res.status).toBe(200)
const persistedRows = await config.getRows(postgresTable._id!)
expect(persistedRows).toHaveLength(numberOfInitialRows - 3)
for (const row of rows) {
expect(persistedRows).not.toContain(
expect.objectContaining({ _id: row._id })
)
}
})
2023-01-19 06:21:17 +13:00
})
2023-01-19 06:07:09 +13:00
describe("retrieve a row", () => {
2023-01-20 06:23:48 +13:00
const getRow = (tableId: string | undefined, rowId?: string | undefined) =>
makeRequest("get", `/api/${tableId}/rows/${rowId}`)
2023-01-19 05:46:40 +13:00
test("Given than a table have a single row, the row can be retrieved successfully", async () => {
2023-01-19 06:07:09 +13:00
const [{ rowData, row }] = await populateRows(1)
2023-01-19 05:46:40 +13:00
2023-01-20 06:23:48 +13:00
const res = await getRow(postgresTable._id, row._id)
2023-01-19 05:46:40 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body).toEqual(expect.objectContaining(rowData))
2023-01-19 05:46:40 +13:00
})
2023-01-19 05:48:18 +13:00
test("Given than a table have a multiple rows, a single row can be retrieved successfully", async () => {
2023-01-19 06:07:09 +13:00
const rows = await populateRows(10)
const { rowData, row } = _.sample(rows)!
2023-01-19 05:48:18 +13:00
2023-01-20 06:23:48 +13:00
const res = await getRow(postgresTable._id, row._id)
2023-01-19 05:48:18 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body).toEqual(expect.objectContaining(rowData))
2023-01-19 05:48:18 +13:00
})
2023-01-20 09:09:39 +13:00
test("given having rows with relation data, only the ids are retrieved", async () => {
let [{ row }] = await populateRows(1)
const foreignRow = await config.createRow({
tableId: auxPostgresTable._id,
2023-02-02 23:43:18 +13:00
title: generator.sentence(),
2023-01-20 09:09:39 +13:00
linkedField: row._id,
})
const res = await getRow(postgresTable._id, row._id)
expect(res.status).toBe(200)
expect(res.body).toEqual({
...row,
foreignField: [
{
_id: foreignRow._id,
},
],
createdAt: expect.any(String),
updatedAt: expect.any(String),
})
})
2023-01-19 05:46:40 +13:00
})
2023-01-20 00:00:51 +13:00
describe("search for rows", () => {
2023-01-20 00:44:48 +13:00
const search = (tableId: string | undefined, body?: object) =>
2023-01-20 06:23:48 +13:00
makeRequest("post", `/api/${tableId}/search`, body)
2023-01-20 00:44:48 +13:00
2023-01-20 00:20:20 +13:00
describe("empty search", () => {
test("Given than a table has no rows, search without query returns empty", async () => {
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id)
2023-01-20 00:06:41 +13:00
2023-01-20 00:20:20 +13:00
expect(res.status).toBe(200)
2023-01-20 00:06:41 +13:00
2023-01-20 06:23:48 +13:00
expect(res.body).toEqual({ rows: [] })
2023-01-20 00:20:20 +13:00
})
2023-01-20 00:06:41 +13:00
2023-01-20 00:20:20 +13:00
test("Given than a table has multiple rows, search without query returns all of them", async () => {
const rowsCount = 6
const rows = await populateRows(rowsCount)
2023-01-20 00:00:51 +13:00
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id)
2023-01-20 00:00:51 +13:00
2023-01-20 00:20:20 +13:00
expect(res.status).toBe(200)
2023-01-20 00:00:51 +13:00
2023-01-20 06:23:48 +13:00
expect(res.body).toEqual({
rows: expect.arrayContaining(
2023-01-20 00:20:20 +13:00
rows.map(r => expect.objectContaining(r.rowData))
2023-01-20 06:23:48 +13:00
),
})
expect(res.body.rows).toHaveLength(rowsCount)
2023-01-20 00:20:20 +13:00
})
test("Given than multiple tables have multiple rows, search only return the requested ones", async () => {
await populateRows(2, (await config.createTable())._id)
const rowsCount = 6
await populateRows(rowsCount)
await populateRows(2, (await config.createTable())._id)
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id)
2023-01-20 00:20:20 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body.rows).toHaveLength(rowsCount)
2023-01-20 00:20:20 +13:00
})
2023-01-20 00:00:51 +13:00
})
2023-01-20 00:06:41 +13:00
2023-01-20 00:35:00 +13:00
test("Querying by a string field returns the rows with field containing or starting by that value", async () => {
2023-02-02 23:43:18 +13:00
const name = generator.name()
2023-01-20 00:20:20 +13:00
const rowsToFilter = [
...Array(2).fill({
name,
2023-02-02 23:43:18 +13:00
description: generator.paragraph(),
value: generator.integer(),
2023-01-20 00:20:20 +13:00
}),
...Array(2).fill({
2023-02-02 23:43:18 +13:00
name: `${name}${utils.newid()}`,
description: generator.paragraph(),
value: generator.integer(),
2023-01-20 00:20:20 +13:00
}),
]
await populateRows(3)
for (const row of rowsToFilter) {
2023-01-20 00:35:00 +13:00
await createRow(row, postgresTable._id)
2023-01-20 00:20:20 +13:00
}
await populateRows(1)
2023-01-20 00:06:41 +13:00
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id, {
query: {
string: {
name,
2023-01-20 00:20:20 +13:00
},
2023-01-20 00:44:48 +13:00
},
})
2023-01-20 00:06:41 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body).toEqual({
rows: expect.arrayContaining(rowsToFilter.map(expect.objectContaining)),
})
expect(res.body.rows).toHaveLength(4)
2023-01-20 00:06:41 +13:00
})
2023-01-20 00:35:00 +13:00
test("Querying respects the limit fields", async () => {
await populateRows(6)
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id, {
limit: 2,
})
2023-01-20 00:35:00 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body.rows).toHaveLength(2)
2023-01-20 00:35:00 +13:00
})
describe("sort", () => {
beforeEach(async () => {
const defaultValue = createRandomRow()
await createRow(
{
...defaultValue,
name: "d",
value: 3,
},
postgresTable._id
)
await createRow(
{ ...defaultValue, name: "aaa", value: 40 },
postgresTable._id
)
await createRow(
{ ...defaultValue, name: "ccccc", value: -5 },
postgresTable._id
)
await createRow(
{ ...defaultValue, name: "bb", value: 0 },
postgresTable._id
)
})
test("Querying respects the sort order when sorting ascending by a string value", async () => {
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id, {
2023-01-20 06:23:48 +13:00
sort: "name",
sortOrder: "ascending",
sortType: "string",
2023-01-20 00:44:48 +13:00
})
2023-01-20 00:35:00 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body.rows).toEqual([
2023-01-20 00:35:00 +13:00
expect.objectContaining({ name: "aaa" }),
expect.objectContaining({ name: "bb" }),
expect.objectContaining({ name: "ccccc" }),
expect.objectContaining({ name: "d" }),
])
})
test("Querying respects the sort order when sorting descending by a string value", async () => {
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id, {
2023-01-20 06:23:48 +13:00
sort: "name",
sortOrder: "descending",
sortType: "string",
2023-01-20 00:44:48 +13:00
})
2023-01-20 00:35:00 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body.rows).toEqual([
2023-01-20 00:35:00 +13:00
expect.objectContaining({ name: "d" }),
expect.objectContaining({ name: "ccccc" }),
expect.objectContaining({ name: "bb" }),
expect.objectContaining({ name: "aaa" }),
])
})
test("Querying respects the sort order when sorting ascending by a numeric value", async () => {
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id, {
2023-01-20 06:23:48 +13:00
sort: "value",
sortOrder: "ascending",
sortType: "number",
2023-01-20 00:44:48 +13:00
})
2023-01-20 00:35:00 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body.rows).toEqual([
2023-01-20 00:35:00 +13:00
expect.objectContaining({ value: -5 }),
expect.objectContaining({ value: 0 }),
expect.objectContaining({ value: 3 }),
expect.objectContaining({ value: 40 }),
])
})
test("Querying respects the sort order when sorting descending by a numeric value", async () => {
2023-01-20 00:44:48 +13:00
const res = await search(postgresTable._id, {
2023-01-20 06:23:48 +13:00
sort: "value",
sortOrder: "descending",
sortType: "number",
2023-01-20 00:44:48 +13:00
})
2023-01-20 00:35:00 +13:00
expect(res.status).toBe(200)
2023-01-20 06:23:48 +13:00
expect(res.body.rows).toEqual([
2023-01-20 00:35:00 +13:00
expect.objectContaining({ value: 40 }),
expect.objectContaining({ value: 3 }),
expect.objectContaining({ value: 0 }),
expect.objectContaining({ value: -5 }),
])
})
})
2023-01-20 00:00:51 +13:00
})
2023-01-20 00:44:48 +13:00
2023-01-20 09:09:39 +13:00
describe("enrich a row", () => {
const getAll = (tableId: string | undefined, rowId: string | undefined) =>
makeRequest("get", `/api/${tableId}/${rowId}/enrich`)
test("given having rows with relation data, enrich populates the", async () => {
let [{ row }] = await populateRows(1)
const foreignRow = await config.createRow({
tableId: auxPostgresTable._id,
2023-02-02 23:43:18 +13:00
title: generator.name(),
2023-01-20 09:09:39 +13:00
linkedField: row._id,
})
const res = await getAll(postgresTable._id, row._id)
expect(res.status).toBe(200)
expect(res.body).toEqual({
...row,
foreignField: [
{
...foreignRow,
linkedField: [{ _id: row._id }],
createdAt: expect.any(String),
updatedAt: expect.any(String),
},
],
createdAt: expect.any(String),
updatedAt: expect.any(String),
})
})
})
2023-01-20 05:46:05 +13:00
describe("get all rows", () => {
const getAll = (tableId: string | undefined) =>
makeRequest("get", `/api/${tableId}/rows`)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
test("Given than a table has no rows, get returns empty", async () => {
const res = await getAll(postgresTable._id)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
expect(res.status).toBe(200)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
expect(res.body).toHaveLength(0)
})
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
test("Given than a table has multiple rows, get returns all of them", async () => {
const rowsCount = 6
const rows = await populateRows(rowsCount)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
const res = await getAll(postgresTable._id)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
expect(res.status).toBe(200)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
expect(res.body).toHaveLength(rowsCount)
expect(res.body).toEqual(
expect.arrayContaining(
rows.map(r => expect.objectContaining(r.rowData))
)
)
})
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
test("Given than multiple tables have multiple rows, get returns the requested ones", async () => {
await populateRows(2, (await config.createTable())._id)
const rowsCount = 6
await populateRows(rowsCount, postgresTable._id)
await populateRows(2, (await config.createTable())._id)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
const res = await getAll(postgresTable._id)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
expect(res.status).toBe(200)
2023-01-20 00:44:48 +13:00
2023-01-20 05:46:05 +13:00
expect(res.body).toHaveLength(rowsCount)
})
})
2023-01-18 06:22:31 +13:00
})