From de2f82606fff51338e6b21e1feb4b3580556a3cb Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Thu, 29 Aug 2024 12:55:27 -0400 Subject: [PATCH] Test treeFromSchemes() --- .../arches_lingo/fixtures/test_scheme.json | 144 ++++++++++++++++++ arches_lingo/src/arches_lingo/types.ts | 2 - arches_lingo/src/arches_lingo/utils.spec.ts | 29 ++++ arches_lingo/src/arches_lingo/utils.ts | 2 +- tests/tests.py | 2 +- tsconfig.json | 1 + 6 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 arches_lingo/src/arches_lingo/fixtures/test_scheme.json create mode 100644 arches_lingo/src/arches_lingo/utils.spec.ts diff --git a/arches_lingo/src/arches_lingo/fixtures/test_scheme.json b/arches_lingo/src/arches_lingo/fixtures/test_scheme.json new file mode 100644 index 00000000..e31b6fd3 --- /dev/null +++ b/arches_lingo/src/arches_lingo/fixtures/test_scheme.json @@ -0,0 +1,144 @@ +{ + "schemes": [ + { + "id": "c9c8548f-6ecc-40d5-ad67-42e9a7e7242e", + "labels": [ + { + "language": "en", + "value": "Test Scheme", + "valuetype": "prefLabel" + } + ], + "top_concepts": [ + { + "id": "943a70f1-236e-4c28-b5ab-69a4b83a63e9", + "labels": [ + { + "language": "en", + "value": "Concept 1", + "valuetype": "prefLabel" + } + ], + "narrower": [ + { + "id": "8f7a7ca8-8548-464b-8fb7-01b3cec6487d", + "labels": [ + { + "language": "en", + "value": "Concept 3", + "valuetype": "prefLabel" + } + ], + "narrower": [ + { + "id": "15bb81ff-194e-4fea-b495-b222f431caee", + "labels": [ + { + "language": "en", + "value": "Concept 4", + "valuetype": "prefLabel" + } + ], + "narrower": [ + { + "id": "edf8dd01-a3fe-445e-a330-dc3bd04c7302", + "labels": [ + { + "language": "en", + "value": "Concept 5", + "valuetype": "prefLabel" + } + ], + "narrower": [] + } + ] + } + ] + }, + { + "id": "53ee9df5-3fa8-4686-a25b-ea8034cc4a5c", + "labels": [ + { + "language": "en", + "value": "Concept 2", + "valuetype": "prefLabel" + } + ], + "narrower": [ + { + "id": "8f7a7ca8-8548-464b-8fb7-01b3cec6487d", + "labels": [ + { + "language": "en", + "value": "Concept 3", + "valuetype": "prefLabel" + } + ], + "narrower": [ + { + "id": "15bb81ff-194e-4fea-b495-b222f431caee", + "labels": [ + { + "language": "en", + "value": "Concept 4", + "valuetype": "prefLabel" + } + ], + "narrower": [ + { + "id": "edf8dd01-a3fe-445e-a330-dc3bd04c7302", + "labels": [ + { + "language": "en", + "value": "Concept 5", + "valuetype": "prefLabel" + } + ], + "narrower": [] + } + ] + } + ] + } + ] + }, + { + "id": "15bb81ff-194e-4fea-b495-b222f431caee", + "labels": [ + { + "language": "en", + "value": "Concept 4", + "valuetype": "prefLabel" + } + ], + "narrower": [ + { + "id": "edf8dd01-a3fe-445e-a330-dc3bd04c7302", + "labels": [ + { + "language": "en", + "value": "Concept 5", + "valuetype": "prefLabel" + } + ], + "narrower": [] + } + ] + }, + { + "id": "edf8dd01-a3fe-445e-a330-dc3bd04c7302", + "labels": [ + { + "language": "en", + "value": "Concept 5", + "valuetype": "prefLabel" + } + ], + "narrower": [] + } + ] + } + ] + } + ] +} diff --git a/arches_lingo/src/arches_lingo/types.ts b/arches_lingo/src/arches_lingo/types.ts index 8c3420f9..cab45560 100644 --- a/arches_lingo/src/arches_lingo/types.ts +++ b/arches_lingo/src/arches_lingo/types.ts @@ -15,14 +15,12 @@ export interface UserRefAndSetter { export interface Concept { id: string; - identifier: string | null; labels: Label[]; narrower: Concept[]; } export interface Scheme { id: string; - identifier: string | null; labels: Label[]; top_concepts: Concept[]; } diff --git a/arches_lingo/src/arches_lingo/utils.spec.ts b/arches_lingo/src/arches_lingo/utils.spec.ts new file mode 100644 index 00000000..36c3ddf4 --- /dev/null +++ b/arches_lingo/src/arches_lingo/utils.spec.ts @@ -0,0 +1,29 @@ +import { ENGLISH } from "@/arches_lingo/constants.ts"; +import { treeFromSchemes } from "@/arches_lingo/utils.ts"; +import schemesFixture from "./fixtures/test_scheme.json"; + +import type { IconLabels, Scheme } from "@/arches_lingo/types"; + +const iconLabels: IconLabels = { + concept: "Concept", + scheme: "Scheme", +}; + +describe("Build scheme hierarchy", () => { + it("Should shape schemes into TreeNodes", () => { + const nodes = treeFromSchemes( + schemesFixture["schemes"] as Scheme[], + ENGLISH, + iconLabels, + null, + ); + const schemeNode = nodes[0]; + expect(schemeNode.label).toEqual("Test Scheme"); + expect(schemeNode.iconLabel).toEqual("Scheme"); + expect(schemeNode.data.top_concepts.length).toEqual(1); + + const topConcept = schemeNode.data.top_concepts[0]; + expect(topConcept.labels[0].value).toEqual("Concept 1"); + expect(topConcept.narrower.length).toEqual(4); + }); +}); diff --git a/arches_lingo/src/arches_lingo/utils.ts b/arches_lingo/src/arches_lingo/utils.ts index 988a3aa7..6a667758 100644 --- a/arches_lingo/src/arches_lingo/utils.ts +++ b/arches_lingo/src/arches_lingo/utils.ts @@ -42,7 +42,7 @@ export const treeFromSchemes = ( selectedLanguage: Language, iconLabels: IconLabels, focusedNode: TreeNode | null, -) => { +): TreeNode[] => { // Use a closure to avoid passing around params (selectedLanguage, etc). const conceptAsNodeAndInstruction = ( concept: Concept, diff --git a/tests/tests.py b/tests/tests.py index 3d5ee9cc..78e087a7 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -135,7 +135,7 @@ def setUpTestData(cls): MAX_DEPTH = 5 CONCEPT_COUNT = 5 cls.concepts = [ - ResourceInstance(graph_id=SCHEMES_GRAPH_ID) for _ in range(CONCEPT_COUNT) + ResourceInstance(graph_id=CONCEPTS_GRAPH_ID) for _ in range(CONCEPT_COUNT) ] for concept in cls.concepts: concept.save() diff --git a/tsconfig.json b/tsconfig.json index 34902ec7..c223f2c6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,7 @@ "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "skipLibCheck": true, + "types": ["vitest/globals"], "allowImportingTsExtensions": true } }