Adding test case and fixing issue that it revealed with external tables as well.
This commit is contained in:
parent
b86640772b
commit
160fbf2125
|
@ -517,9 +517,22 @@ describe.each([
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("patch", () => {
|
describe("patch", () => {
|
||||||
|
let otherTable: Table
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const tableConfig = generateTableConfig()
|
const tableConfig = generateTableConfig()
|
||||||
table = await createTable(tableConfig)
|
table = await createTable(tableConfig)
|
||||||
|
const otherTableConfig = generateTableConfig()
|
||||||
|
// need a short name of table here - for relationship tests
|
||||||
|
otherTableConfig.name = "a"
|
||||||
|
otherTableConfig.schema.relationship = {
|
||||||
|
name: "relationship",
|
||||||
|
type: FieldType.LINK,
|
||||||
|
fieldName: "relationship",
|
||||||
|
tableId: table._id!,
|
||||||
|
relationshipType: RelationshipType.ONE_TO_MANY,
|
||||||
|
}
|
||||||
|
otherTable = await createTable(otherTableConfig)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should update only the fields that are supplied", async () => {
|
it("should update only the fields that are supplied", async () => {
|
||||||
|
@ -615,6 +628,28 @@ describe.each([
|
||||||
expect(getResp.body.user1[0]._id).toEqual(user2._id)
|
expect(getResp.body.user1[0]._id).toEqual(user2._id)
|
||||||
expect(getResp.body.user2[0]._id).toEqual(user2._id)
|
expect(getResp.body.user2[0]._id).toEqual(user2._id)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should be able to update relationships when both columns are same name", async () => {
|
||||||
|
let row = await config.api.row.save(table._id!, {
|
||||||
|
name: "test",
|
||||||
|
description: "test",
|
||||||
|
})
|
||||||
|
let row2 = await config.api.row.save(otherTable._id!, {
|
||||||
|
name: "test",
|
||||||
|
description: "test",
|
||||||
|
relationship: [row._id],
|
||||||
|
})
|
||||||
|
row = (await config.api.row.get(table._id!, row._id!)).body
|
||||||
|
expect(row.relationship.length).toBe(1)
|
||||||
|
const resp = await config.api.row.patch(table._id!, {
|
||||||
|
_id: row._id!,
|
||||||
|
_rev: row._rev!,
|
||||||
|
tableId: row.tableId!,
|
||||||
|
name: "test2",
|
||||||
|
relationship: [row2._id],
|
||||||
|
})
|
||||||
|
expect(resp.relationship.length).toBe(1)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("destroy", () => {
|
describe("destroy", () => {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import {
|
import {
|
||||||
Datasource,
|
Datasource,
|
||||||
|
FieldType,
|
||||||
ManyToManyRelationshipFieldMetadata,
|
ManyToManyRelationshipFieldMetadata,
|
||||||
ManyToOneRelationshipFieldMetadata,
|
ManyToOneRelationshipFieldMetadata,
|
||||||
OneToManyRelationshipFieldMetadata,
|
OneToManyRelationshipFieldMetadata,
|
||||||
|
@ -42,10 +43,13 @@ export function cleanupRelationships(
|
||||||
for (let [relatedKey, relatedSchema] of Object.entries(
|
for (let [relatedKey, relatedSchema] of Object.entries(
|
||||||
relatedTable.schema
|
relatedTable.schema
|
||||||
)) {
|
)) {
|
||||||
if (
|
if (relatedSchema.type !== FieldType.LINK) {
|
||||||
relatedSchema.type === FieldTypes.LINK &&
|
continue
|
||||||
relatedSchema.fieldName === foreignKey
|
}
|
||||||
) {
|
// if they both have the same field name it will appear as if it needs to be removed,
|
||||||
|
// don't cleanup in this scenario
|
||||||
|
const sameFieldNameForBoth = relatedSchema.name === schema.name
|
||||||
|
if (relatedSchema.fieldName === foreignKey && !sameFieldNameForBoth) {
|
||||||
delete relatedTable.schema[relatedKey]
|
delete relatedTable.schema[relatedKey]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue