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