starting into relationship testing.
This commit is contained in:
parent
7ec90faf1c
commit
fbbb3d12a1
|
@ -133,12 +133,12 @@ class LinkController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the two schemas are equal (in the important parts, not a pure equality check)
|
* Returns whether the two link schemas are equal (in the important parts, not a pure equality check)
|
||||||
*/
|
*/
|
||||||
areSchemasEqual(schema1, schema2) {
|
areLinkSchemasEqual(linkSchema1, linkSchema2) {
|
||||||
const compareFields = ["name", "type", "tableId", "fieldName", "autocolumn"]
|
const compareFields = ["name", "type", "tableId", "fieldName", "autocolumn"]
|
||||||
for (let field of compareFields) {
|
for (let field of compareFields) {
|
||||||
if (schema1[field] !== schema2[field]) {
|
if (linkSchema1[field] !== linkSchema2[field]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,24 +146,24 @@ class LinkController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given two the field of this table, and the field of the linked table, this makes sure
|
* Given the link field of this table, and the link field of the linked table, this makes sure
|
||||||
* the state of relationship type is accurate on both.
|
* the state of relationship type is accurate on both.
|
||||||
*/
|
*/
|
||||||
handleRelationshipType(field, linkedField) {
|
handleRelationshipType(linkerField, linkedField) {
|
||||||
if (
|
if (
|
||||||
!field.relationshipType ||
|
!linkerField.relationshipType ||
|
||||||
field.relationshipType === RelationshipTypes.MANY_TO_MANY
|
linkerField.relationshipType === RelationshipTypes.MANY_TO_MANY
|
||||||
) {
|
) {
|
||||||
linkedField.relationshipType = RelationshipTypes.MANY_TO_MANY
|
linkedField.relationshipType = RelationshipTypes.MANY_TO_MANY
|
||||||
// make sure by default all are many to many (if not specified)
|
// make sure by default all are many to many (if not specified)
|
||||||
field.relationshipType = RelationshipTypes.MANY_TO_MANY
|
linkerField.relationshipType = RelationshipTypes.MANY_TO_MANY
|
||||||
} else if (field.relationshipType === RelationshipTypes.MANY_TO_ONE) {
|
} else if (linkerField.relationshipType === RelationshipTypes.MANY_TO_ONE) {
|
||||||
// Ensure that the other side of the relationship is locked to one record
|
// Ensure that the other side of the relationship is locked to one record
|
||||||
linkedField.relationshipType = RelationshipTypes.ONE_TO_MANY
|
linkedField.relationshipType = RelationshipTypes.ONE_TO_MANY
|
||||||
} else if (field.relationshipType === RelationshipTypes.ONE_TO_MANY) {
|
} else if (linkerField.relationshipType === RelationshipTypes.ONE_TO_MANY) {
|
||||||
linkedField.relationshipType = RelationshipTypes.MANY_TO_ONE
|
linkedField.relationshipType = RelationshipTypes.MANY_TO_ONE
|
||||||
}
|
}
|
||||||
return { field, linkedField }
|
return { linkerField, linkedField }
|
||||||
}
|
}
|
||||||
|
|
||||||
// all operations here will assume that the table
|
// all operations here will assume that the table
|
||||||
|
@ -347,7 +347,7 @@ class LinkController {
|
||||||
})
|
})
|
||||||
|
|
||||||
// update table schema after checking relationship types
|
// update table schema after checking relationship types
|
||||||
schema[fieldName] = fields.field
|
schema[fieldName] = fields.linkerField
|
||||||
const linkedField = fields.linkedField
|
const linkedField = fields.linkedField
|
||||||
|
|
||||||
if (field.autocolumn) {
|
if (field.autocolumn) {
|
||||||
|
@ -358,7 +358,7 @@ class LinkController {
|
||||||
const existingSchema = linkedTable.schema[field.fieldName]
|
const existingSchema = linkedTable.schema[field.fieldName]
|
||||||
if (
|
if (
|
||||||
existingSchema != null &&
|
existingSchema != null &&
|
||||||
!this.areSchemasEqual(existingSchema, linkedField)
|
!this.areLinkSchemasEqual(existingSchema, linkedField)
|
||||||
) {
|
) {
|
||||||
throw new Error("Cannot overwrite existing column.")
|
throw new Error("Cannot overwrite existing column.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
const TestConfig = require("../../tests/utilities/TestConfiguration")
|
||||||
|
const { basicTable } = require("../../tests/utilities/structures")
|
||||||
|
const LinkController = require("../linkedRows/LinkController")
|
||||||
|
const { RelationshipTypes } = require("../../constants")
|
||||||
|
|
||||||
|
describe("test the link controller", () => {
|
||||||
|
let config = new TestConfig(false)
|
||||||
|
let table1, table2
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await config.init()
|
||||||
|
const { _id } = await config.createTable()
|
||||||
|
table2 = await config.createLinkedTable()
|
||||||
|
// update table after creating link
|
||||||
|
table1 = await config.getTable(_id)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(config.end)
|
||||||
|
|
||||||
|
function createLinkController(table, row = null, oldTable = null) {
|
||||||
|
const linkConfig = {
|
||||||
|
appId: config.getAppId(),
|
||||||
|
tableId: table._id,
|
||||||
|
table,
|
||||||
|
}
|
||||||
|
if (row) {
|
||||||
|
linkConfig.row = row
|
||||||
|
}
|
||||||
|
if (oldTable) {
|
||||||
|
linkConfig.oldTable = oldTable
|
||||||
|
}
|
||||||
|
return new LinkController(linkConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
it("should be able to confirm if two table schemas are equal", () => {
|
||||||
|
const controller = createLinkController(table1)
|
||||||
|
let equal = controller.areLinkSchemasEqual(table2.schema.link, table2.schema.link)
|
||||||
|
expect(equal).toEqual(true)
|
||||||
|
equal = controller.areLinkSchemasEqual(table1.schema.link, table2.schema.link)
|
||||||
|
expect(equal).toEqual(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should be able to check the relationship types across two fields", () => {
|
||||||
|
const controller = createLinkController(table1)
|
||||||
|
// empty case
|
||||||
|
let output = controller.handleRelationshipType({}, {})
|
||||||
|
expect(output.linkedField.relationshipType).toEqual(RelationshipTypes.MANY_TO_MANY)
|
||||||
|
expect(output.linkerField.relationshipType).toEqual(RelationshipTypes.MANY_TO_MANY)
|
||||||
|
output = controller.handleRelationshipType({ relationshipType: RelationshipTypes.MANY_TO_MANY }, {})
|
||||||
|
expect(output.linkedField.relationshipType).toEqual(RelationshipTypes.MANY_TO_MANY)
|
||||||
|
expect(output.linkerField.relationshipType).toEqual(RelationshipTypes.MANY_TO_MANY)
|
||||||
|
output = controller.handleRelationshipType({ relationshipType: RelationshipTypes.MANY_TO_ONE }, {})
|
||||||
|
expect(output.linkedField.relationshipType).toEqual(RelationshipTypes.ONE_TO_MANY)
|
||||||
|
expect(output.linkerField.relationshipType).toEqual(RelationshipTypes.MANY_TO_ONE)
|
||||||
|
output = controller.handleRelationshipType({ relationshipType: RelationshipTypes.ONE_TO_MANY }, {})
|
||||||
|
expect(output.linkedField.relationshipType).toEqual(RelationshipTypes.MANY_TO_ONE)
|
||||||
|
expect(output.linkerField.relationshipType).toEqual(RelationshipTypes.ONE_TO_MANY)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue