Fix tests
This commit is contained in:
parent
83649f1959
commit
70689c0369
|
@ -23,19 +23,25 @@ jest.setTimeout(30000)
|
||||||
|
|
||||||
jest.unmock("pg")
|
jest.unmock("pg")
|
||||||
|
|
||||||
interface RandomForeignKeyConfig {
|
interface ForeignTableInfo {
|
||||||
createOne2Many?: boolean
|
table: Table
|
||||||
createMany2One?: number
|
fieldName: string
|
||||||
createMany2Many?: number
|
relationshipType: RelationshipTypes
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ForeignRowsInfo {
|
||||||
|
row: Row
|
||||||
|
foreignKey: string
|
||||||
|
relationshipType: RelationshipTypes
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("row api - postgres", () => {
|
describe("row api - postgres", () => {
|
||||||
let makeRequest: MakeRequestResponse,
|
let makeRequest: MakeRequestResponse,
|
||||||
postgresDatasource: Datasource,
|
postgresDatasource: Datasource,
|
||||||
primaryPostgresTable: Table,
|
primaryPostgresTable: Table,
|
||||||
o2mInfo: { table: Table; fieldName: string },
|
o2mInfo: ForeignTableInfo,
|
||||||
m2oInfo: { table: Table; fieldName: string },
|
m2oInfo: ForeignTableInfo,
|
||||||
m2mInfo: { table: Table; fieldName: string }
|
m2mInfo: ForeignTableInfo
|
||||||
|
|
||||||
let host: string
|
let host: string
|
||||||
let port: number
|
let port: number
|
||||||
|
@ -103,14 +109,17 @@ describe("row api - postgres", () => {
|
||||||
o2mInfo = {
|
o2mInfo = {
|
||||||
table: await createAuxTable("o2m"),
|
table: await createAuxTable("o2m"),
|
||||||
fieldName: "oneToManyRelation",
|
fieldName: "oneToManyRelation",
|
||||||
|
relationshipType: RelationshipTypes.ONE_TO_MANY,
|
||||||
}
|
}
|
||||||
m2oInfo = {
|
m2oInfo = {
|
||||||
table: await createAuxTable("m2o"),
|
table: await createAuxTable("m2o"),
|
||||||
fieldName: "manyToOneRelation",
|
fieldName: "manyToOneRelation",
|
||||||
|
relationshipType: RelationshipTypes.MANY_TO_ONE,
|
||||||
}
|
}
|
||||||
m2mInfo = {
|
m2mInfo = {
|
||||||
table: await createAuxTable("m2m"),
|
table: await createAuxTable("m2m"),
|
||||||
fieldName: "manyToManyRelation",
|
fieldName: "manyToManyRelation",
|
||||||
|
relationshipType: RelationshipTypes.MANY_TO_MANY,
|
||||||
}
|
}
|
||||||
|
|
||||||
primaryPostgresTable = await config.createTable({
|
primaryPostgresTable = await config.createTable({
|
||||||
|
@ -196,21 +205,43 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
async function createPrimaryRow(opts: {
|
async function createPrimaryRow(opts: {
|
||||||
rowData: PrimaryRowData
|
rowData: PrimaryRowData
|
||||||
createForeignRows?: RandomForeignKeyConfig
|
createForeignRows?: {
|
||||||
|
createOne2Many?: boolean
|
||||||
|
createMany2One?: number
|
||||||
|
createMany2Many?: number
|
||||||
|
}
|
||||||
}) {
|
}) {
|
||||||
let { rowData } = opts
|
let { rowData } = opts
|
||||||
let foreignRow: Row | undefined
|
let foreignRows: ForeignRowsInfo[] = []
|
||||||
|
|
||||||
if (opts?.createForeignRows?.createOne2Many) {
|
async function createForeignRow(tableInfo: ForeignTableInfo) {
|
||||||
foreignRow = await config.createRow({
|
const foreignRow = await config.createRow({
|
||||||
tableId: o2mInfo.table._id,
|
tableId: tableInfo.table._id,
|
||||||
title: generator.name(),
|
title: generator.name(),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const foreignKey = `fk_${tableInfo.table.name}_${tableInfo.fieldName}`
|
||||||
rowData = {
|
rowData = {
|
||||||
...rowData,
|
...rowData,
|
||||||
[`fk_${o2mInfo.table.name}_${o2mInfo.fieldName}`]: foreignRow.id,
|
[foreignKey]: foreignRow.id,
|
||||||
}
|
}
|
||||||
|
foreignRows.push({
|
||||||
|
row: foreignRow,
|
||||||
|
foreignKey,
|
||||||
|
relationshipType: tableInfo.relationshipType,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts?.createForeignRows?.createOne2Many) {
|
||||||
|
await createForeignRow(o2mInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < (opts?.createForeignRows?.createMany2One || 0); i++) {
|
||||||
|
await createForeignRow(m2oInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < (opts?.createForeignRows?.createMany2Many || 0); i++) {
|
||||||
|
await createForeignRow(m2mInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
const row = await config.createRow({
|
const row = await config.createRow({
|
||||||
|
@ -218,7 +249,7 @@ describe("row api - postgres", () => {
|
||||||
...rowData,
|
...rowData,
|
||||||
})
|
})
|
||||||
|
|
||||||
return { row, foreignRow }
|
return { row, foreignRows }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createDefaultPgTable() {
|
async function createDefaultPgTable() {
|
||||||
|
@ -241,7 +272,11 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
async function populatePrimaryRows(
|
async function populatePrimaryRows(
|
||||||
count: number,
|
count: number,
|
||||||
opts?: RandomForeignKeyConfig
|
opts?: {
|
||||||
|
createOne2Many?: boolean
|
||||||
|
createMany2One?: number
|
||||||
|
createMany2Many?: number
|
||||||
|
}
|
||||||
) {
|
) {
|
||||||
return await Promise.all(
|
return await Promise.all(
|
||||||
Array(count)
|
Array(count)
|
||||||
|
@ -461,30 +496,136 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
describe("given a row with relation data", () => {
|
describe("given a row with relation data", () => {
|
||||||
let row: Row
|
let row: Row
|
||||||
let foreignRow: Row
|
let rowData: {
|
||||||
|
name: string
|
||||||
|
description: string
|
||||||
|
value: number
|
||||||
|
}
|
||||||
|
let foreignRows: ForeignRowsInfo[]
|
||||||
|
|
||||||
|
describe("with all relationship types", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
let [createdRow] = await populatePrimaryRows(1, {
|
||||||
|
createOne2Many: true,
|
||||||
|
createMany2One: 3,
|
||||||
|
createMany2Many: 2,
|
||||||
|
})
|
||||||
|
row = createdRow.row
|
||||||
|
rowData = createdRow.rowData
|
||||||
|
foreignRows = createdRow.foreignRows
|
||||||
|
})
|
||||||
|
|
||||||
|
it("only one to many foreign keys are retrieved", async () => {
|
||||||
|
const res = await getRow(primaryPostgresTable._id, row.id)
|
||||||
|
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
|
const one2ManyForeignRows = foreignRows.filter(
|
||||||
|
x => x.relationshipType === RelationshipTypes.ONE_TO_MANY
|
||||||
|
)
|
||||||
|
expect(one2ManyForeignRows).toHaveLength(1)
|
||||||
|
|
||||||
|
expect(res.body).toEqual({
|
||||||
|
...rowData,
|
||||||
|
id: row.id,
|
||||||
|
tableId: row.tableId,
|
||||||
|
_id: expect.any(String),
|
||||||
|
_rev: expect.any(String),
|
||||||
|
[one2ManyForeignRows[0].foreignKey]: one2ManyForeignRows[0].row.id,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.body[o2mInfo.fieldName]).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("with only one to many", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
let [createdRow] = await populatePrimaryRows(1, {
|
let [createdRow] = await populatePrimaryRows(1, {
|
||||||
createOne2Many: true,
|
createOne2Many: true,
|
||||||
})
|
})
|
||||||
row = createdRow.row
|
row = createdRow.row
|
||||||
foreignRow = createdRow.foreignRow!
|
rowData = createdRow.rowData
|
||||||
|
foreignRows = createdRow.foreignRows
|
||||||
})
|
})
|
||||||
|
|
||||||
it("only foreign keys are retrieved", async () => {
|
it("only one to many foreign keys are 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)
|
||||||
|
|
||||||
|
expect(foreignRows).toHaveLength(1)
|
||||||
|
|
||||||
expect(res.body).toEqual({
|
expect(res.body).toEqual({
|
||||||
...row,
|
...rowData,
|
||||||
|
id: row.id,
|
||||||
|
tableId: row.tableId,
|
||||||
_id: expect.any(String),
|
_id: expect.any(String),
|
||||||
_rev: expect.any(String),
|
_rev: expect.any(String),
|
||||||
[`fk_${o2mInfo.table.name}_${o2mInfo.fieldName}`]: foreignRow.id,
|
[foreignRows[0].foreignKey]: foreignRows[0].row.id,
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(res.body[o2mInfo.fieldName]).toBeUndefined()
|
expect(res.body[o2mInfo.fieldName]).toBeUndefined()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("with only many to one", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
let [createdRow] = await populatePrimaryRows(1, {
|
||||||
|
createMany2One: 3,
|
||||||
|
})
|
||||||
|
row = createdRow.row
|
||||||
|
rowData = createdRow.rowData
|
||||||
|
foreignRows = createdRow.foreignRows
|
||||||
|
})
|
||||||
|
|
||||||
|
it("only one to many foreign keys are retrieved", async () => {
|
||||||
|
const res = await getRow(primaryPostgresTable._id, row.id)
|
||||||
|
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
|
expect(foreignRows).toHaveLength(3)
|
||||||
|
|
||||||
|
expect(res.body).toEqual({
|
||||||
|
...rowData,
|
||||||
|
id: row.id,
|
||||||
|
tableId: row.tableId,
|
||||||
|
_id: expect.any(String),
|
||||||
|
_rev: expect.any(String),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.body[o2mInfo.fieldName]).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("with only many to many", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
let [createdRow] = await populatePrimaryRows(1, {
|
||||||
|
createMany2Many: 2,
|
||||||
|
})
|
||||||
|
row = createdRow.row
|
||||||
|
rowData = createdRow.rowData
|
||||||
|
foreignRows = createdRow.foreignRows
|
||||||
|
})
|
||||||
|
|
||||||
|
it("only one to many foreign keys are retrieved", async () => {
|
||||||
|
const res = await getRow(primaryPostgresTable._id, row.id)
|
||||||
|
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
|
expect(foreignRows).toHaveLength(2)
|
||||||
|
|
||||||
|
expect(res.body).toEqual({
|
||||||
|
...rowData,
|
||||||
|
id: row.id,
|
||||||
|
tableId: row.tableId,
|
||||||
|
_id: expect.any(String),
|
||||||
|
_rev: expect.any(String),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.body[o2mInfo.fieldName]).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("POST /api/:tableId/search", () => {
|
describe("POST /api/:tableId/search", () => {
|
||||||
|
@ -703,16 +844,18 @@ describe("row api - postgres", () => {
|
||||||
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", () => {
|
describe("given a row with relation data", () => {
|
||||||
let row: Row, foreignRow: Row | undefined
|
let row: Row, rowData: PrimaryRowData, foreignRows: ForeignRowsInfo[]
|
||||||
|
|
||||||
|
describe("only with one to many data", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
rowData = generateRandomPrimaryRowData()
|
||||||
const rowsInfo = await createPrimaryRow({
|
const rowsInfo = await createPrimaryRow({
|
||||||
rowData: generateRandomPrimaryRowData(),
|
rowData,
|
||||||
createForeignRows: { createOne2Many: true },
|
createForeignRows: { createOne2Many: true },
|
||||||
})
|
})
|
||||||
|
|
||||||
row = rowsInfo.row
|
row = rowsInfo.row
|
||||||
foreignRow = rowsInfo.foreignRow
|
foreignRows = rowsInfo.foreignRows
|
||||||
})
|
})
|
||||||
|
|
||||||
it("enrich populates the foreign field", async () => {
|
it("enrich populates the foreign field", async () => {
|
||||||
|
@ -720,14 +863,22 @@ describe("row api - postgres", () => {
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
|
||||||
expect(foreignRow).toBeDefined()
|
expect(foreignRows).toHaveLength(1)
|
||||||
expect(res.body).toEqual({
|
expect(res.body).toEqual({
|
||||||
...row,
|
...rowData,
|
||||||
|
[foreignRows[0].foreignKey]: foreignRows[0].row.id,
|
||||||
[o2mInfo.fieldName]: [
|
[o2mInfo.fieldName]: [
|
||||||
{
|
{
|
||||||
...foreignRow,
|
...foreignRows[0].row,
|
||||||
|
_id: expect.any(String),
|
||||||
|
_rev: expect.any(String),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
id: row.id,
|
||||||
|
tableId: row.tableId,
|
||||||
|
_id: expect.any(String),
|
||||||
|
_rev: expect.any(String),
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -750,7 +901,7 @@ describe("row api - postgres", () => {
|
||||||
const rowsCount = 6
|
const rowsCount = 6
|
||||||
let rows: {
|
let rows: {
|
||||||
row: Row
|
row: Row
|
||||||
foreignRow: Row | undefined
|
foreignRows: ForeignRowsInfo[]
|
||||||
rowData: PrimaryRowData
|
rowData: PrimaryRowData
|
||||||
}[]
|
}[]
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
|
Loading…
Reference in New Issue