Fix tests

This commit is contained in:
adrinr 2023-02-06 14:54:49 +00:00
parent 7d3c24d257
commit 3d9a208bfb
1 changed files with 148 additions and 83 deletions

View File

@ -13,27 +13,31 @@ import {
Table, Table,
} from "@budibase/types" } from "@budibase/types"
import _ from "lodash" import _ from "lodash"
import { generator } from "@budibase/backend-core/tests" import { generator, structures } from "@budibase/backend-core/tests"
import { utils } from "@budibase/backend-core" import { utils } from "@budibase/backend-core"
const config = setup.getConfig()! const config = setup.getConfig()!
jest.unmock("node-fetch") jest.unmock("node-fetch")
jest.unmock("pg")
// TODO: Waiting for the test image to exist
describe("row api - postgres", () => { describe("row api - postgres", () => {
let apiKey, let makeRequest: MakeRequestResponse,
makeRequest: MakeRequestResponse,
postgresDatasource: Datasource, postgresDatasource: Datasource,
postgresTable: Table, postgresTable: Table,
auxPostgresTable: Table auxPostgresTable: Table
const host = generator.ip() const host = process.env.POSTGRES_HOST!
const port = generator.natural() const port = process.env.POSTGRES_PORT!
beforeAll(async () => {
await config.init()
const apiKey = await config.generateApiKey()
makeRequest = generateMakeRequest(apiKey, true)
})
beforeEach(async () => { beforeEach(async () => {
await config.init()
apiKey = await config.generateApiKey()
postgresDatasource = await config.createDatasource({ postgresDatasource = await config.createDatasource({
datasource: { datasource: {
type: "datasource", type: "datasource",
@ -43,8 +47,8 @@ describe("row api - postgres", () => {
host, host,
port, port,
database: "postgres", database: "postgres",
user: "root", user: "postgres",
password: "root", password: process.env.POSTGRES_PASSWORD!,
schema: "public", schema: "public",
ssl: false, ssl: false,
rejectUnauthorized: false, rejectUnauthorized: false,
@ -53,11 +57,41 @@ describe("row api - postgres", () => {
}, },
}) })
makeRequest = generateMakeRequest(apiKey, true) auxPostgresTable = await config.createTable({
name: generator.word({ length: 10 }),
type: "external",
primary: ["id"],
schema: {
id: {
name: "id",
type: FieldType.AUTO,
constraints: {
presence: true,
},
},
title: {
name: "title",
type: FieldType.STRING,
constraints: {
presence: true,
},
},
},
sourceId: postgresDatasource._id,
})
postgresTable = await config.createTable({ postgresTable = await config.createTable({
name: generator.word(), name: generator.word({ length: 10 }),
type: "external",
primary: ["id"],
schema: { schema: {
id: {
name: "id",
type: FieldType.AUTO,
constraints: {
presence: true,
},
},
name: { name: {
name: "name", name: "name",
type: FieldType.STRING, type: FieldType.STRING,
@ -73,30 +107,16 @@ describe("row api - postgres", () => {
name: "value", name: "value",
type: FieldType.NUMBER, type: FieldType.NUMBER,
}, },
},
sourceId: postgresDatasource._id,
})
auxPostgresTable = await config.createTable({
name: generator.word(),
schema: {
title: {
name: "title",
type: FieldType.STRING,
constraints: {
presence: true,
},
},
linkedField: { linkedField: {
type: FieldType.LINK, type: FieldType.LINK,
constraints: { constraints: {
type: "array", type: "array",
presence: true, presence: false,
}, },
fieldName: "foreignField", fieldName: "foreignField",
name: "linkedField", name: "linkedField",
relationshipType: RelationshipTypes.MANY_TO_MANY, relationshipType: RelationshipTypes.ONE_TO_MANY,
tableId: postgresTable._id, tableId: auxPostgresTable._id,
}, },
}, },
sourceId: postgresDatasource._id, sourceId: postgresDatasource._id,
@ -107,34 +127,67 @@ describe("row api - postgres", () => {
await config.end() await config.end()
}) })
function createRandomRow() { const randomInteger = () => generator.integer({ min: 0, max: 10000 })
function makeRandomRow() {
return { return {
name: generator.name(), name: generator.name(),
description: generator.paragraph(), description: generator.paragraph(),
value: generator.integer(), value: randomInteger(),
} }
} }
function createRow( async function createRow(
row: { row: {
name: string name: string
description: string description: string
value: number value: number
}, },
tableId?: string tableId?: string,
createForeignRow?: boolean
) { ) {
return config.createRow({ if (createForeignRow) {
const foreignRow = await config.createRow({
tableId: auxPostgresTable._id,
title: generator.name(),
})
row = {
...row,
[`fk_${auxPostgresTable.name}_foreignField`]: foreignRow.id,
}
}
return await config.createRow({
tableId: tableId || postgresTable._id, tableId: tableId || postgresTable._id,
...row, ...row,
}) })
} }
async function createDefaultPgTable() {
return await config.createTable({
name: generator.word({ length: 10 }),
type: "external",
primary: ["id"],
schema: {
id: {
name: "id",
type: FieldType.AUTO,
constraints: {
presence: true,
},
},
},
sourceId: postgresDatasource._id,
})
}
async function populateRows(count: number, tableId?: string) { async function populateRows(count: number, tableId?: string) {
return await Promise.all( return await Promise.all(
Array(count) Array(count)
.fill({}) .fill({})
.map(async () => { .map(async () => {
const rowData = createRandomRow() const rowData = makeRandomRow()
return { return {
rowData, rowData,
row: await createRow(rowData, tableId || postgresTable._id), row: await createRow(rowData, tableId || postgresTable._id),
@ -160,7 +213,7 @@ describe("row api - postgres", () => {
rejectUnauthorized: false, rejectUnauthorized: false,
schema: "public", schema: "public",
ssl: false, ssl: false,
user: "root", user: "postgres",
}, },
plus: true, plus: true,
source: "POSTGRES", source: "POSTGRES",
@ -169,6 +222,7 @@ describe("row api - postgres", () => {
_rev: expect.any(String), _rev: expect.any(String),
createdAt: expect.any(String), createdAt: expect.any(String),
updatedAt: expect.any(String), updatedAt: expect.any(String),
entities: expect.any(Object),
}) })
}) })
@ -177,7 +231,7 @@ describe("row api - postgres", () => {
makeRequest("post", `/api/${tableId}/rows`, body) makeRequest("post", `/api/${tableId}/rows`, body)
test("Given than no row exists, adding a new row persists it", async () => { test("Given than no row exists, adding a new row persists it", async () => {
const newRow = createRandomRow() const newRow = makeRandomRow()
const res = await createRow(postgresTable._id, newRow) const res = await createRow(postgresTable._id, newRow)
@ -185,17 +239,20 @@ describe("row api - postgres", () => {
const persistedRows = await config.getRows(postgresTable._id!) const persistedRows = await config.getRows(postgresTable._id!)
expect(persistedRows).toHaveLength(1) expect(persistedRows).toHaveLength(1)
expect(persistedRows).toEqual([
expect.objectContaining({ const expected = {
...res.body, ...res.body,
...newRow, ...newRow,
}), }
])
// TODO: check why this is being returned from the creation
delete expected.linkedField
expect(persistedRows).toEqual([expect.objectContaining(expected)])
}) })
test("Given than no row exists, multiple rows can be persisted", async () => { test("Given than no row exists, multiple rows can be persisted", async () => {
const numberOfRows = 10 const numberOfRows = 10
const newRows = Array(numberOfRows).fill(createRandomRow()) const newRows = Array(numberOfRows).fill(makeRandomRow())
for (const newRow of newRows) { for (const newRow of newRows) {
const res = await createRow(postgresTable._id, newRow) const res = await createRow(postgresTable._id, newRow)
@ -218,22 +275,25 @@ describe("row api - postgres", () => {
let { row } = _.sample(await populateRows(10))! let { row } = _.sample(await populateRows(10))!
const newName = generator.name() const newName = generator.name()
const newValue = generator.integer() const newValue = randomInteger()
const updatedRow = { const updatedRow = {
...row, ...row,
name: newName, name: newName,
value: newValue, value: newValue,
} }
// TODO: check why this is being returned from the creation
delete (updatedRow as any).linkedField
const res = await updateRow(postgresTable._id, updatedRow) const res = await updateRow(postgresTable._id, updatedRow)
expect(res.status).toBe(200) expect(res.status).toBe(200)
const persistedRow = await config.getRow(postgresTable._id!, row._id!) const persistedRow = await config.getRow(postgresTable._id!, row.id)
expect(persistedRow).toEqual( expect(persistedRow).toEqual(
expect.objectContaining({ expect.objectContaining({
_id: row._id, id: row.id,
name: newName, name: newName,
value: newValue, value: newValue,
}) })
@ -258,9 +318,9 @@ describe("row api - postgres", () => {
const persistedRows = await config.getRows(postgresTable._id!) const persistedRows = await config.getRows(postgresTable._id!)
expect(persistedRows).toHaveLength(numberOfInitialRows - 1) expect(persistedRows).toHaveLength(numberOfInitialRows - 1)
expect(row._id).toBeDefined() expect(row.id).toBeDefined()
expect(persistedRows).not.toContain( expect(persistedRows).not.toContain(
expect.objectContaining({ _id: row._id }) expect.objectContaining({ _id: row.id })
) )
}) })
@ -279,7 +339,7 @@ describe("row api - postgres", () => {
for (const row of rows) { for (const row of rows) {
expect(persistedRows).not.toContain( expect(persistedRows).not.toContain(
expect.objectContaining({ _id: row._id }) expect.objectContaining({ _id: row.id })
) )
} }
}) })
@ -292,7 +352,7 @@ describe("row api - postgres", () => {
test("Given than a table have a single row, the row can be retrieved successfully", async () => { test("Given than a table have a single row, the row can be retrieved successfully", async () => {
const [{ rowData, row }] = await populateRows(1) const [{ rowData, row }] = await populateRows(1)
const res = await getRow(postgresTable._id, row._id) const res = await getRow(postgresTable._id, row.id)
expect(res.status).toBe(200) expect(res.status).toBe(200)
@ -303,7 +363,7 @@ describe("row api - postgres", () => {
const rows = await populateRows(10) const rows = await populateRows(10)
const { rowData, row } = _.sample(rows)! const { rowData, row } = _.sample(rows)!
const res = await getRow(postgresTable._id, row._id) const res = await getRow(postgresTable._id, row.id)
expect(res.status).toBe(200) expect(res.status).toBe(200)
@ -313,26 +373,25 @@ describe("row api - postgres", () => {
test("given having rows with relation data, only the ids are retrieved", async () => { test("given having rows with relation data, only the ids are retrieved", async () => {
let [{ row }] = await populateRows(1) let [{ row }] = await populateRows(1)
const foreignRow = await config.createRow({ await config.createRow({
tableId: auxPostgresTable._id, tableId: auxPostgresTable._id,
title: generator.sentence(), title: generator.sentence(),
linkedField: row._id, linkedField: row.id,
}) })
const res = await getRow(postgresTable._id, row._id) const res = await getRow(postgresTable._id, row.id)
expect(res.status).toBe(200) expect(res.status).toBe(200)
const expected = { ...row }
// TODO: check why this is being returned from the creation
delete expected.linkedField
expect(res.body).toEqual({ expect(res.body).toEqual({
...row, ...expected,
foreignField: [ _id: expect.any(String),
{ _rev: expect.any(String),
_id: foreignRow._id,
},
],
createdAt: expect.any(String),
updatedAt: expect.any(String),
}) })
expect(res.body.foreignField).toBeUndefined()
}) })
}) })
@ -346,7 +405,11 @@ describe("row api - postgres", () => {
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.body).toEqual({ rows: [] }) expect(res.body).toEqual({
rows: [],
bookmark: null,
hasNextPage: false,
})
}) })
test("Given than a table has multiple rows, search without query returns all of them", async () => { test("Given than a table has multiple rows, search without query returns all of them", async () => {
@ -361,15 +424,17 @@ describe("row api - postgres", () => {
rows: expect.arrayContaining( rows: expect.arrayContaining(
rows.map(r => expect.objectContaining(r.rowData)) rows.map(r => expect.objectContaining(r.rowData))
), ),
bookmark: null,
hasNextPage: false,
}) })
expect(res.body.rows).toHaveLength(rowsCount) expect(res.body.rows).toHaveLength(rowsCount)
}) })
test("Given than multiple tables have multiple rows, search only return the requested ones", async () => { test("Given than multiple tables have multiple rows, search only return the requested ones", async () => {
await populateRows(2, (await config.createTable())._id) await populateRows(2, (await createDefaultPgTable())._id)
const rowsCount = 6 const rowsCount = 6
await populateRows(rowsCount) await populateRows(rowsCount)
await populateRows(2, (await config.createTable())._id) await populateRows(2, (await createDefaultPgTable())._id)
const res = await search(postgresTable._id) const res = await search(postgresTable._id)
@ -385,12 +450,12 @@ describe("row api - postgres", () => {
...Array(2).fill({ ...Array(2).fill({
name, name,
description: generator.paragraph(), description: generator.paragraph(),
value: generator.integer(), value: randomInteger(),
}), }),
...Array(2).fill({ ...Array(2).fill({
name: `${name}${utils.newid()}`, name: `${name}${utils.newid()}`,
description: generator.paragraph(), description: generator.paragraph(),
value: generator.integer(), value: randomInteger(),
}), }),
] ]
@ -412,6 +477,8 @@ describe("row api - postgres", () => {
expect(res.body).toEqual({ expect(res.body).toEqual({
rows: expect.arrayContaining(rowsToFilter.map(expect.objectContaining)), rows: expect.arrayContaining(rowsToFilter.map(expect.objectContaining)),
bookmark: null,
hasNextPage: false,
}) })
expect(res.body.rows).toHaveLength(4) expect(res.body.rows).toHaveLength(4)
}) })
@ -430,7 +497,7 @@ describe("row api - postgres", () => {
describe("sort", () => { describe("sort", () => {
beforeEach(async () => { beforeEach(async () => {
const defaultValue = createRandomRow() const defaultValue = makeRandomRow()
await createRow( await createRow(
{ {
@ -520,35 +587,33 @@ describe("row api - postgres", () => {
}) })
}) })
describe("enrich a row", () => { describe("get enriched row", () => {
const getAll = (tableId: string | undefined, rowId: string | undefined) => const getAll = (tableId: string | undefined, rowId: string | undefined) =>
makeRequest("get", `/api/${tableId}/${rowId}/enrich`) makeRequest("get", `/api/${tableId}/${rowId}/enrich`)
test("given having rows with relation data, enrich populates the", async () => { test("given having rows with relation data, enrich populates the foreign field", async () => {
let [{ row }] = await populateRows(1)
const foreignRow = await config.createRow({ const foreignRow = await config.createRow({
tableId: auxPostgresTable._id, tableId: auxPostgresTable._id,
title: generator.name(), title: generator.name(),
linkedField: row._id,
}) })
const res = await getAll(postgresTable._id, row._id) const rowData = {
...makeRandomRow(),
[`fk_${auxPostgresTable.name}_foreignField`]: foreignRow.id,
}
const row = await createRow(rowData)
const res = await getAll(postgresTable._id, row.id)
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.body).toEqual({ expect(res.body).toEqual({
...row, ...row,
foreignField: [ linkedField: [
{ {
...foreignRow, ...foreignRow,
linkedField: [{ _id: row._id }],
createdAt: expect.any(String),
updatedAt: expect.any(String),
}, },
], ],
createdAt: expect.any(String),
updatedAt: expect.any(String),
}) })
}) })
}) })
@ -582,10 +647,10 @@ describe("row api - postgres", () => {
}) })
test("Given than multiple tables have multiple rows, get returns the requested ones", async () => { test("Given than multiple tables have multiple rows, get returns the requested ones", async () => {
await populateRows(2, (await config.createTable())._id) await populateRows(2, (await createDefaultPgTable())._id)
const rowsCount = 6 const rowsCount = 6
await populateRows(rowsCount, postgresTable._id) await populateRows(rowsCount, postgresTable._id)
await populateRows(2, (await config.createTable())._id) await populateRows(2, (await createDefaultPgTable())._id)
const res = await getAll(postgresTable._id) const res = await getAll(postgresTable._id)