1
0
Fork 0
mirror of synced 2024-10-04 03:54:37 +13:00

Fix many-to-one tests

This commit is contained in:
adrinr 2023-02-23 10:39:16 +01:00
parent 9e0d003038
commit 7868fc657d

View file

@ -23,18 +23,6 @@ jest.setTimeout(30000)
jest.unmock("pg") jest.unmock("pg")
interface ForeignTableInfo {
table: Table
fieldName: string
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,
@ -86,10 +74,12 @@ describe("row api - postgres", () => {
name: `${prefix}_${generator.word({ length: 6 })}`, name: `${prefix}_${generator.word({ length: 6 })}`,
type: "external", type: "external",
primary: ["id"], primary: ["id"],
primaryDisplay: "title",
schema: { schema: {
id: { id: {
name: "id", name: "id",
type: FieldType.AUTO, type: FieldType.AUTO,
autocolumn: true,
constraints: { constraints: {
presence: true, presence: true,
}, },
@ -130,6 +120,7 @@ describe("row api - postgres", () => {
id: { id: {
name: "id", name: "id",
type: FieldType.AUTO, type: FieldType.AUTO,
autocolumn: true,
constraints: { constraints: {
presence: true, presence: true,
}, },
@ -159,6 +150,7 @@ describe("row api - postgres", () => {
name: "oneToManyRelation", name: "oneToManyRelation",
relationshipType: RelationshipTypes.ONE_TO_MANY, relationshipType: RelationshipTypes.ONE_TO_MANY,
tableId: o2mInfo.table._id, tableId: o2mInfo.table._id,
main: true,
}, },
manyToOneRelation: { manyToOneRelation: {
type: FieldType.LINK, type: FieldType.LINK,
@ -170,6 +162,7 @@ describe("row api - postgres", () => {
name: "manyToOneRelation", name: "manyToOneRelation",
relationshipType: RelationshipTypes.MANY_TO_ONE, relationshipType: RelationshipTypes.MANY_TO_ONE,
tableId: m2oInfo.table._id, tableId: m2oInfo.table._id,
main: true,
}, },
manyToManyRelation: { manyToManyRelation: {
type: FieldType.LINK, type: FieldType.LINK,
@ -181,6 +174,7 @@ describe("row api - postgres", () => {
name: "manyToManyRelation", name: "manyToManyRelation",
relationshipType: RelationshipTypes.MANY_TO_MANY, relationshipType: RelationshipTypes.MANY_TO_MANY,
tableId: m2mInfo.table._id, tableId: m2mInfo.table._id,
main: true,
}, },
}, },
sourceId: postgresDatasource._id, sourceId: postgresDatasource._id,
@ -203,6 +197,17 @@ describe("row api - postgres", () => {
value: number value: number
} }
type ForeignTableInfo = {
table: Table
fieldName: string
relationshipType: RelationshipTypes
}
type ForeignRowsInfo = {
row: Row
relationshipType: RelationshipTypes
}
async function createPrimaryRow(opts: { async function createPrimaryRow(opts: {
rowData: PrimaryRowData rowData: PrimaryRowData
createForeignRows?: { createForeignRows?: {
@ -211,33 +216,61 @@ describe("row api - postgres", () => {
createMany2Many?: number createMany2Many?: number
} }
}) { }) {
let { rowData } = opts let { rowData } = opts as any
let foreignRows: ForeignRowsInfo[] = [] let foreignRows: ForeignRowsInfo[] = []
async function createForeignRow(tableInfo: ForeignTableInfo) { async function createForeignRow(tableInfo: ForeignTableInfo) {
const foreignKey = `fk_${tableInfo.table.name}_${tableInfo.fieldName}`
const foreignRow = await config.createRow({ const foreignRow = await config.createRow({
tableId: tableInfo.table._id, tableId: tableInfo.table._id,
title: generator.name(), title: generator.name(),
}) })
const foreignKey = `fk_${tableInfo.table.name}_${tableInfo.fieldName}`
rowData = { rowData = {
...rowData, ...rowData,
[foreignKey]: foreignRow.id, [foreignKey]: foreignRow.id,
} }
foreignRows.push({ foreignRows.push({
row: foreignRow, row: foreignRow,
foreignKey,
relationshipType: tableInfo.relationshipType, relationshipType: tableInfo.relationshipType,
}) })
} }
if (opts?.createForeignRows?.createOne2Many) { if (opts?.createForeignRows?.createOne2Many) {
await createForeignRow(o2mInfo) const foreignKey = `fk_${o2mInfo.table.name}_${o2mInfo.fieldName}`
const foreignRow = await config.createRow({
tableId: o2mInfo.table._id,
title: generator.name(),
})
rowData = {
...rowData,
[foreignKey]: foreignRow.id,
}
foreignRows.push({
row: foreignRow,
relationshipType: o2mInfo.relationshipType,
})
} }
for (let i = 0; i < (opts?.createForeignRows?.createMany2One || 0); i++) { for (let i = 0; i < (opts?.createForeignRows?.createMany2One || 0); i++) {
await createForeignRow(m2oInfo) const foreignRow = await config.createRow({
tableId: m2oInfo.table._id,
title: generator.name(),
})
rowData = {
...rowData,
[m2oInfo.fieldName]: rowData[m2oInfo.fieldName] || [],
}
rowData[m2oInfo.fieldName].push(foreignRow._id)
foreignRows.push({
row: foreignRow,
relationshipType: RelationshipTypes.MANY_TO_ONE,
})
} }
for (let i = 0; i < (opts?.createForeignRows?.createMany2Many || 0); i++) { for (let i = 0; i < (opts?.createForeignRows?.createMany2Many || 0); i++) {
@ -531,7 +564,8 @@ describe("row api - postgres", () => {
tableId: row.tableId, tableId: row.tableId,
_id: expect.any(String), _id: expect.any(String),
_rev: expect.any(String), _rev: expect.any(String),
[one2ManyForeignRows[0].foreignKey]: one2ManyForeignRows[0].row.id, [`fk_${o2mInfo.table.name}_${o2mInfo.fieldName}`]:
one2ManyForeignRows[0].row.id,
}) })
expect(res.body[o2mInfo.fieldName]).toBeUndefined() expect(res.body[o2mInfo.fieldName]).toBeUndefined()
@ -561,7 +595,8 @@ describe("row api - postgres", () => {
tableId: row.tableId, tableId: row.tableId,
_id: expect.any(String), _id: expect.any(String),
_rev: expect.any(String), _rev: expect.any(String),
[foreignRows[0].foreignKey]: foreignRows[0].row.id, [`fk_${o2mInfo.table.name}_${o2mInfo.fieldName}`]:
foreignRows[0].row.id,
}) })
expect(res.body[o2mInfo.fieldName]).toBeUndefined() expect(res.body[o2mInfo.fieldName]).toBeUndefined()
@ -866,7 +901,8 @@ describe("row api - postgres", () => {
expect(foreignRows).toHaveLength(1) expect(foreignRows).toHaveLength(1)
expect(res.body).toEqual({ expect(res.body).toEqual({
...rowData, ...rowData,
[foreignRows[0].foreignKey]: foreignRows[0].row.id, [`fk_${o2mInfo.table.name}_${o2mInfo.fieldName}`]:
foreignRows[0].row.id,
[o2mInfo.fieldName]: [ [o2mInfo.fieldName]: [
{ {
...foreignRows[0].row, ...foreignRows[0].row,
@ -881,6 +917,45 @@ describe("row api - postgres", () => {
}) })
}) })
}) })
describe("only with many to one data", () => {
beforeEach(async () => {
rowData = generateRandomPrimaryRowData()
const rowsInfo = await createPrimaryRow({
rowData,
createForeignRows: {
createMany2One: 2,
},
})
row = rowsInfo.row
foreignRows = rowsInfo.foreignRows
})
it("enrich populates the foreign field", async () => {
const res = await getAll(primaryPostgresTable._id, row.id)
expect(res.status).toBe(200)
expect(res.body).toEqual({
...rowData,
[m2oInfo.fieldName]: [
{
...foreignRows[0].row,
[`fk_${m2oInfo.table.name}_${m2oInfo.fieldName}`]: row.id,
},
{
...foreignRows[1].row,
[`fk_${m2oInfo.table.name}_${m2oInfo.fieldName}`]: row.id,
},
],
id: row.id,
tableId: row.tableId,
_id: expect.any(String),
_rev: expect.any(String),
})
})
})
}) })
}) })