Clean describes
This commit is contained in:
parent
79e42abc1e
commit
bc0208bc83
2 changed files with 281 additions and 207 deletions
|
@ -145,12 +145,14 @@ describe("row api - postgres", () => {
|
|||
}
|
||||
}
|
||||
|
||||
async function createPrimaryRow(opts: {
|
||||
rowData: {
|
||||
type PrimaryRowData = {
|
||||
name: string
|
||||
description: string
|
||||
value: number
|
||||
}
|
||||
|
||||
async function createPrimaryRow(opts: {
|
||||
rowData: PrimaryRowData
|
||||
createForeignRow?: boolean
|
||||
}) {
|
||||
let { rowData } = opts
|
||||
|
@ -193,7 +195,12 @@ describe("row api - postgres", () => {
|
|||
})
|
||||
}
|
||||
|
||||
async function populatePrimaryRows(count: number) {
|
||||
async function populatePrimaryRows(
|
||||
count: number,
|
||||
opts?: {
|
||||
createForeignRow?: boolean
|
||||
}
|
||||
) {
|
||||
return await Promise.all(
|
||||
Array(count)
|
||||
.fill({})
|
||||
|
@ -203,6 +210,7 @@ describe("row api - postgres", () => {
|
|||
rowData,
|
||||
...(await createPrimaryRow({
|
||||
rowData,
|
||||
createForeignRow: opts?.createForeignRow,
|
||||
})),
|
||||
}
|
||||
})
|
||||
|
@ -243,7 +251,8 @@ describe("row api - postgres", () => {
|
|||
const createRow = (tableId: string | undefined, body: object) =>
|
||||
makeRequest("post", `/api/${tableId}/rows`, body)
|
||||
|
||||
it("Given than no row exists, adding a new one persists it", async () => {
|
||||
describe("given than no row exists", () => {
|
||||
it("adding a new one persists it", async () => {
|
||||
const newRow = generateRandomPrimaryRowData()
|
||||
|
||||
const res = await createRow(primaryPostgresTable._id, newRow)
|
||||
|
@ -261,7 +270,7 @@ describe("row api - postgres", () => {
|
|||
expect(persistedRows).toEqual([expect.objectContaining(expected)])
|
||||
})
|
||||
|
||||
it("Given than no row exists, multiple rows can be persisted", async () => {
|
||||
it("multiple rows can be persisted", async () => {
|
||||
const numberOfRows = 10
|
||||
const newRows = Array(numberOfRows).fill(generateRandomPrimaryRowData())
|
||||
|
||||
|
@ -277,14 +286,20 @@ describe("row api - postgres", () => {
|
|||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("PATCH /api/:tableId/rows", () => {
|
||||
const updateRow = (tableId: string | undefined, body: Row) =>
|
||||
makeRequest("patch", `/api/${tableId}/rows`, body)
|
||||
|
||||
it("Given than a row exists, updating it persists it", async () => {
|
||||
let { row } = _.sample(await populatePrimaryRows(10))!
|
||||
describe("given than a row exists", () => {
|
||||
let row: Row
|
||||
beforeEach(async () => {
|
||||
let rowResponse = _.sample(await populatePrimaryRows(10))!
|
||||
row = rowResponse.row
|
||||
})
|
||||
|
||||
it("updating it persists it", async () => {
|
||||
const newName = generator.name()
|
||||
const newValue = generator.age()
|
||||
const updatedRow = {
|
||||
|
@ -312,6 +327,7 @@ describe("row api - postgres", () => {
|
|||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /api/:tableId/rows", () => {
|
||||
const deleteRow = (
|
||||
|
@ -319,10 +335,15 @@ describe("row api - postgres", () => {
|
|||
body: Row | { rows: Row[] }
|
||||
) => makeRequest("delete", `/api/${tableId}/rows`, body)
|
||||
|
||||
it("Given than a row exists, delete request removes it", async () => {
|
||||
describe("given than multiple row exist", () => {
|
||||
const numberOfInitialRows = 5
|
||||
let { row } = _.sample(await populatePrimaryRows(numberOfInitialRows))!
|
||||
let rows: Row[]
|
||||
beforeEach(async () => {
|
||||
rows = (await populatePrimaryRows(numberOfInitialRows)).map(x => x.row)
|
||||
})
|
||||
|
||||
it("delete request removes it", async () => {
|
||||
const row = _.sample(rows)!
|
||||
const res = await deleteRow(primaryPostgresTable._id, row)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -336,44 +357,56 @@ describe("row api - postgres", () => {
|
|||
)
|
||||
})
|
||||
|
||||
it("Given than multiple rows exist, multiple rows can be removed at once", async () => {
|
||||
const numberOfInitialRows = 5
|
||||
let rows = _.sampleSize(
|
||||
await populatePrimaryRows(numberOfInitialRows),
|
||||
3
|
||||
)!.map(x => x.row)
|
||||
it("multiple rows can be removed at once", async () => {
|
||||
let rowsToDelete = _.sampleSize(rows, 3)!
|
||||
|
||||
const res = await deleteRow(primaryPostgresTable._id, { rows })
|
||||
const res = await deleteRow(primaryPostgresTable._id, {
|
||||
rows: rowsToDelete,
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
const persistedRows = await config.getRows(primaryPostgresTable._id!)
|
||||
expect(persistedRows).toHaveLength(numberOfInitialRows - 3)
|
||||
|
||||
for (const row of rows) {
|
||||
for (const row of rowsToDelete) {
|
||||
expect(persistedRows).not.toContain(
|
||||
expect.objectContaining({ _id: row.id })
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /api/:tableId/rows/:rowId", () => {
|
||||
const getRow = (tableId: string | undefined, rowId?: string | undefined) =>
|
||||
makeRequest("get", `/api/${tableId}/rows/${rowId}`)
|
||||
|
||||
it("Given than a table have a single row, that row can be retrieved successfully", async () => {
|
||||
const [{ rowData, row }] = await populatePrimaryRows(1)
|
||||
describe("given than a table have a single row", () => {
|
||||
let rowData: PrimaryRowData, row: Row
|
||||
beforeEach(async () => {
|
||||
const [createdRow] = await populatePrimaryRows(1)
|
||||
rowData = createdRow.rowData
|
||||
row = createdRow.row
|
||||
})
|
||||
|
||||
it("the row can be retrieved successfully", async () => {
|
||||
const res = await getRow(primaryPostgresTable._id, row.id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
expect(res.body).toEqual(expect.objectContaining(rowData))
|
||||
})
|
||||
})
|
||||
|
||||
it("Given than a table have a multiple rows, a single row can be retrieved successfully", async () => {
|
||||
const rows = await populatePrimaryRows(10)
|
||||
describe("given than a table have a multiple rows", () => {
|
||||
let rows: { row: Row; rowData: PrimaryRowData }[]
|
||||
|
||||
beforeEach(async () => {
|
||||
rows = await populatePrimaryRows(10)
|
||||
})
|
||||
|
||||
it("a single row can be retrieved successfully", async () => {
|
||||
const { rowData, row } = _.sample(rows)!
|
||||
|
||||
const res = await getRow(primaryPostgresTable._id, row.id)
|
||||
|
@ -382,16 +415,18 @@ describe("row api - postgres", () => {
|
|||
|
||||
expect(res.body).toEqual(expect.objectContaining(rowData))
|
||||
})
|
||||
|
||||
it("Given a rows with relation data, foreign key fields are not retrieved", async () => {
|
||||
let [{ row }] = await populatePrimaryRows(1)
|
||||
|
||||
await config.createRow({
|
||||
tableId: auxPostgresTable._id,
|
||||
title: generator.sentence(),
|
||||
linkedField: row.id,
|
||||
})
|
||||
|
||||
describe("given a row with relation data", () => {
|
||||
let row: Row
|
||||
beforeEach(async () => {
|
||||
let [createdRow] = await populatePrimaryRows(1, {
|
||||
createForeignRow: true,
|
||||
})
|
||||
row = createdRow.row
|
||||
})
|
||||
|
||||
it("foreign key fields are not retrieved", async () => {
|
||||
const res = await getRow(primaryPostgresTable._id, row.id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -404,13 +439,15 @@ describe("row api - postgres", () => {
|
|||
expect(res.body.foreignField).toBeUndefined()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /api/:tableId/search", () => {
|
||||
const search = (tableId: string | undefined, body?: object) =>
|
||||
makeRequest("post", `/api/${tableId}/search`, body)
|
||||
|
||||
describe("empty search", () => {
|
||||
it("Given than a table has no rows, search without query returns empty", async () => {
|
||||
describe("search without parameters", () => {
|
||||
describe("given than a table has no rows", () => {
|
||||
it("search without query returns empty", async () => {
|
||||
const res = await search(primaryPostgresTable._id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -421,11 +458,19 @@ describe("row api - postgres", () => {
|
|||
hasNextPage: false,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("Given than a table has multiple rows, search without query returns all of them", async () => {
|
||||
describe("given than a table has multiple rows", () => {
|
||||
const rowsCount = 6
|
||||
const rows = await populatePrimaryRows(rowsCount)
|
||||
let rows: {
|
||||
row: Row
|
||||
rowData: PrimaryRowData
|
||||
}[]
|
||||
beforeEach(async () => {
|
||||
rows = await populatePrimaryRows(rowsCount)
|
||||
})
|
||||
|
||||
it("search without query returns all of them", async () => {
|
||||
const res = await search(primaryPostgresTable._id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -439,8 +484,11 @@ describe("row api - postgres", () => {
|
|||
})
|
||||
expect(res.body.rows).toHaveLength(rowsCount)
|
||||
})
|
||||
})
|
||||
|
||||
it("Given than multiple tables have multiple rows, search only return the requested ones", async () => {
|
||||
describe("given than multiple tables have multiple rows", () => {
|
||||
const rowsCount = 6
|
||||
beforeEach(async () => {
|
||||
const createRandomTableWithRows = async () =>
|
||||
await config.createRow({
|
||||
tableId: (await createDefaultPgTable())._id,
|
||||
|
@ -450,11 +498,11 @@ describe("row api - postgres", () => {
|
|||
await createRandomTableWithRows()
|
||||
await createRandomTableWithRows()
|
||||
|
||||
const rowsCount = 6
|
||||
await populatePrimaryRows(rowsCount)
|
||||
|
||||
await createRandomTableWithRows()
|
||||
|
||||
})
|
||||
it("search only return the requested ones", async () => {
|
||||
const res = await search(primaryPostgresTable._id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -462,6 +510,7 @@ describe("row api - postgres", () => {
|
|||
expect(res.body.rows).toHaveLength(rowsCount)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("Querying by a string field returns the rows with field containing or starting by that value", async () => {
|
||||
const name = generator.name()
|
||||
|
@ -607,13 +656,20 @@ describe("row api - postgres", () => {
|
|||
describe("GET /api/:tableId/:rowId/enrich", () => {
|
||||
const getAll = (tableId: string | undefined, rowId: string | undefined) =>
|
||||
makeRequest("get", `/api/${tableId}/${rowId}/enrich`)
|
||||
describe("given a row with relation data", () => {
|
||||
let row: Row, foreignRow: Row | undefined
|
||||
|
||||
it("Given a row with relation data, enrich populates the foreign field", async () => {
|
||||
const { row, foreignRow } = await createPrimaryRow({
|
||||
beforeEach(async () => {
|
||||
const rowsInfo = await createPrimaryRow({
|
||||
rowData: generateRandomPrimaryRowData(),
|
||||
createForeignRow: true,
|
||||
})
|
||||
|
||||
row = rowsInfo.row
|
||||
foreignRow = rowsInfo.foreignRow
|
||||
})
|
||||
|
||||
it("enrich populates the foreign field", async () => {
|
||||
const res = await getAll(primaryPostgresTable._id, row.id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -629,23 +685,33 @@ describe("row api - postgres", () => {
|
|||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /api/:tableId/rows", () => {
|
||||
const getAll = (tableId: string | undefined) =>
|
||||
makeRequest("get", `/api/${tableId}/rows`)
|
||||
|
||||
it("Given than a table has no rows, get returns empty", async () => {
|
||||
describe("given a table with no rows", () => {
|
||||
it("get request returns empty", async () => {
|
||||
const res = await getAll(primaryPostgresTable._id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
expect(res.body).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("Given than a table has multiple rows, get returns all of them", async () => {
|
||||
})
|
||||
describe("given a table with multiple rows", () => {
|
||||
const rowsCount = 6
|
||||
const rows = await populatePrimaryRows(rowsCount)
|
||||
let rows: {
|
||||
row: Row
|
||||
foreignRow: Row | undefined
|
||||
rowData: PrimaryRowData
|
||||
}[]
|
||||
beforeEach(async () => {
|
||||
rows = await populatePrimaryRows(rowsCount)
|
||||
})
|
||||
|
||||
it("get request returns all of them", async () => {
|
||||
const res = await getAll(primaryPostgresTable._id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -657,8 +723,12 @@ describe("row api - postgres", () => {
|
|||
)
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it("Given than multiple tables have multiple rows, get returns the requested ones", async () => {
|
||||
describe("given multiple tables with multiple rows", () => {
|
||||
const rowsCount = 6
|
||||
|
||||
beforeEach(async () => {
|
||||
const createRandomTableWithRows = async () =>
|
||||
await config.createRow({
|
||||
tableId: (await createDefaultPgTable())._id,
|
||||
|
@ -666,10 +736,11 @@ describe("row api - postgres", () => {
|
|||
})
|
||||
|
||||
await createRandomTableWithRows()
|
||||
const rowsCount = 6
|
||||
await populatePrimaryRows(rowsCount)
|
||||
await createRandomTableWithRows()
|
||||
})
|
||||
|
||||
it("get returns the requested ones", async () => {
|
||||
const res = await getAll(primaryPostgresTable._id)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
|
@ -678,3 +749,4 @@ describe("row api - postgres", () => {
|
|||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -448,7 +448,9 @@ class InternalBuilder {
|
|||
if (resource.fields && resource.fields.length > 0) {
|
||||
// select the resources as the format "table.columnName" - this is what is provided
|
||||
// by the resource builder further up
|
||||
selectStatement = generateSelectStatement(json, knex)
|
||||
selectStatement = generateSelectStatement(json, knex, {
|
||||
excludeJoinColumns: false,
|
||||
})
|
||||
}
|
||||
let foundLimit = limit || BASE_LIMIT
|
||||
// handle pagination
|
||||
|
|
Loading…
Reference in a new issue