2020-02-03 10:24:25 +01:00
|
|
|
import { getMemoryTemplateApi } from "./specHelpers"
|
|
|
|
import { errors } from "../src/templateApi"
|
2019-07-15 08:12:52 +02:00
|
|
|
|
|
|
|
describe("hierarchy node creation", () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
it("> getNewRootLevel > should be initialised with correct members", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
|
|
|
expect(root.name).toBe("root")
|
|
|
|
expect(root.type).toBe("root")
|
|
|
|
expect(root.children).toEqual([])
|
|
|
|
expect(root.pathRegx).toBeDefined()
|
|
|
|
expect(root.pathRegx()).toBe("/")
|
|
|
|
expect(root.parent).toBeDefined()
|
|
|
|
expect(root.isRoot()).toBeTruthy()
|
|
|
|
expect(root.indexes).toEqual([])
|
2020-04-02 07:55:11 +02:00
|
|
|
expect(root.nodeName()).toBe("/")
|
2020-02-03 10:24:25 +01:00
|
|
|
})
|
|
|
|
|
2020-04-14 10:49:10 +02:00
|
|
|
it("> getNewModelTemplate > should be initialise with correct members", async () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const record = templateApi.getNewModelTemplate(root)
|
2020-02-03 10:24:25 +01:00
|
|
|
record.name = "child"
|
|
|
|
expect(record.type).toBe("record")
|
|
|
|
expect(record.children).toEqual([])
|
|
|
|
expect(record.validationRules).toEqual([])
|
|
|
|
expect(record.indexes).toEqual([])
|
|
|
|
expect(record.parent()).toBe(root)
|
2020-03-25 11:45:11 +01:00
|
|
|
expect(record.collectionName).toBe(record.nodeId.toString())
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(record.estimatedRecordCount).toBe(1000000)
|
|
|
|
expect(record.isSingle).toBe(false)
|
|
|
|
|
|
|
|
record.collectionName = "records"
|
|
|
|
expect(record.collectionNodeKey()).toBe("/records")
|
|
|
|
expect(record.collectionPathRegx()).toBe("/records")
|
|
|
|
expect(record.nodeKey()).toBe(`/records/${record.nodeId}-{id}`)
|
2020-04-02 07:55:11 +02:00
|
|
|
expect(record.nodeName()).toBe(`/${record.name}`)
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(record.pathRegx()).toBe(`/records/${record.nodeId}-[a-zA-Z0-9_\-]+`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("> getNewSingleRecordTemplate > should set isSingle = true", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
|
|
|
const record = templateApi.getNewSingleRecordTemplate(root)
|
|
|
|
expect(record.isSingle).toBe(true)
|
|
|
|
})
|
|
|
|
|
2020-04-14 10:49:10 +02:00
|
|
|
it("> getNewModelTemplate > should have static pathRegx if is singlerecord", async () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
|
|
|
const record = templateApi.getNewSingleRecordTemplate(root)
|
|
|
|
record.name = "child"
|
|
|
|
expect(record.pathRegx()).toBe("/child")
|
|
|
|
})
|
|
|
|
|
2020-04-14 10:49:10 +02:00
|
|
|
it("> getNewModelTemplate > should add itself to parent records's children", async () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const parentRecord = templateApi.getNewModelTemplate(root)
|
|
|
|
const record = templateApi.getNewModelTemplate(parentRecord)
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(parentRecord.children.length).toBe(1)
|
|
|
|
expect(parentRecord.children[0]).toBe(record)
|
|
|
|
})
|
|
|
|
|
2020-04-14 10:49:10 +02:00
|
|
|
it("> getNewModelTemplate > child should get correct nodeName ", async () => {
|
2020-04-02 07:55:11 +02:00
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const parentRecord = templateApi.getNewModelTemplate(root)
|
2020-04-02 07:55:11 +02:00
|
|
|
parentRecord.name = "parent"
|
2020-04-14 10:49:10 +02:00
|
|
|
const record = templateApi.getNewModelTemplate(parentRecord)
|
2020-04-02 07:55:11 +02:00
|
|
|
record.name = "child"
|
|
|
|
expect(record.nodeName()).toBe(`/${parentRecord.name}/${record.name}`)
|
|
|
|
})
|
|
|
|
|
2020-04-14 10:49:10 +02:00
|
|
|
it("> getNewModelTemplate > should add itself to parents's default index allowedNodeIds", async () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const parentRecord = templateApi.getNewModelTemplate(root)
|
|
|
|
const record = templateApi.getNewModelTemplate(parentRecord)
|
2020-04-14 11:19:14 +02:00
|
|
|
expect(root.indexes[0].allowedModelNodeIds).toEqual([parentRecord.nodeId])
|
|
|
|
expect(parentRecord.indexes[0].allowedModelNodeIds).toEqual([
|
2020-02-03 10:24:25 +01:00
|
|
|
record.nodeId,
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
2020-04-14 10:49:10 +02:00
|
|
|
it("> getNewModelTemplate > should add itself to root's children", async () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const record = templateApi.getNewModelTemplate(root)
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(root.children.length).toBe(1)
|
|
|
|
expect(root.children[0]).toBe(record)
|
|
|
|
})
|
|
|
|
|
2020-04-14 10:49:10 +02:00
|
|
|
it("> getNewModelTemplate > should have dynamic pathRegx if parent is record", async () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const parent = templateApi.getNewModelTemplate(root)
|
2020-02-03 10:24:25 +01:00
|
|
|
parent.collectionName = "customers"
|
2020-04-14 10:49:10 +02:00
|
|
|
const record = templateApi.getNewModelTemplate(parent)
|
2020-02-03 10:24:25 +01:00
|
|
|
record.name = "child"
|
|
|
|
expect(record.pathRegx().startsWith("/customers")).toBe(true)
|
|
|
|
expect(record.pathRegx().includes("[")).toBe(true)
|
|
|
|
})
|
|
|
|
|
2020-04-14 10:49:10 +02:00
|
|
|
it("> getNewModelTemplate > should add default index", async () => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const record = templateApi.getNewModelTemplate(root, "rec")
|
2020-02-03 10:24:25 +01:00
|
|
|
expect(root.indexes.length).toBe(1)
|
|
|
|
expect(root.indexes[0].name).toBe("rec_index")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("> getNewIndexTemplate > should initialise with correct members", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
|
|
|
const index = templateApi.getNewIndexTemplate(root)
|
|
|
|
expect(index.type).toBe("index")
|
|
|
|
expect(index.name).toBeDefined()
|
|
|
|
expect(index.map).toBeDefined()
|
|
|
|
expect(index.filter).toBeDefined()
|
|
|
|
expect(index.children).toBeUndefined()
|
|
|
|
expect(index.indexType).toBe("ancestor")
|
|
|
|
expect(index.getShardName).toBeDefined()
|
|
|
|
index.name = "naughty-customers"
|
|
|
|
expect(index.pathRegx()).toBe("/naughty-customers")
|
|
|
|
expect(index.parent()).toBe(root)
|
|
|
|
expect(index.aggregateGroups).toEqual([])
|
|
|
|
})
|
|
|
|
|
|
|
|
it("> getNewIndexTemplate > should add itself to roots indexes", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
|
|
|
const index = templateApi.getNewIndexTemplate(root)
|
|
|
|
expect(root.indexes.length).toBe(1)
|
|
|
|
expect(root.indexes[0]).toBe(index)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("> getNewIndexTemplate > should add itself to record indexes", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const record = templateApi.getNewModelTemplate(root)
|
2020-02-03 10:24:25 +01:00
|
|
|
const index = templateApi.getNewIndexTemplate(record)
|
|
|
|
expect(record.indexes.length).toBe(1)
|
|
|
|
expect(record.indexes[0]).toBe(index)
|
|
|
|
expect(index.parent()).toBe(record)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should throw exception when no parent supplied, for non root node", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
expect(() => templateApi.getNewIndexTemplate()).toThrow(
|
|
|
|
errors.allNonRootNodesMustHaveParent
|
|
|
|
)
|
2020-04-14 10:49:10 +02:00
|
|
|
expect(() => templateApi.getNewModelTemplate()).toThrow(
|
2020-02-03 10:24:25 +01:00
|
|
|
errors.allNonRootNodesMustHaveParent
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("> adding node > should just add one (bugfix)", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const parent = templateApi.getNewModelTemplate(root)
|
|
|
|
templateApi.getNewModelTemplate(parent)
|
2020-02-03 10:24:25 +01:00
|
|
|
|
|
|
|
expect(root.children.length).toBe(1)
|
|
|
|
expect(parent.children.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("> getNewAggregateGroupTemplate > should throw exception when non index supplied as parent", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
|
|
|
expect(() => templateApi.getNewAggregateGroupTemplate(root)).toThrow()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("> getNewAggregateGroupTemplate > should add itself to index aggregateGroups", async () => {
|
|
|
|
const { templateApi } = await getMemoryTemplateApi()
|
|
|
|
const root = templateApi.getNewRootLevel()
|
2020-04-14 10:49:10 +02:00
|
|
|
const record = templateApi.getNewModelTemplate(root)
|
2020-02-03 10:24:25 +01:00
|
|
|
const index = templateApi.getNewIndexTemplate(record)
|
|
|
|
const aggregateGroup = templateApi.getNewAggregateGroupTemplate(index)
|
|
|
|
expect(index.aggregateGroups.length).toBe(1)
|
|
|
|
expect(index.aggregateGroups[0]).toBe(aggregateGroup)
|
|
|
|
expect(aggregateGroup.parent()).toBe(index)
|
|
|
|
})
|
|
|
|
})
|