Skip to content

Commit 29cdece

Browse files
committed
test: add test case for duplicated node label ids
1 parent 08d4ab1 commit 29cdece

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

packages/graph-schema-utils/src/formatters/json/extensions.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { fromJson } from "./index.js";
55
import { strict as assert } from "node:assert";
66

77
describe("JSON formatter", () => {
8-
describe("fromJsonStruct", () => {
8+
describe("fromJson", () => {
99
const schemaWithDuplicatedNodeLabels = readFile(
1010
path.resolve(__dirname, "./test-schemas/duplicated-nodeLabel-ids.json")
1111
);
1212
test("Identifies duplicated node labels adn throws an error", () => {
13-
assert.throws(() => fromJson(schemaWithDuplicatedNodeLabels));
13+
assert.throws(() => fromJson(schemaWithDuplicatedNodeLabels), {
14+
message: "Duplicate node label IDs found in schema",
15+
});
1416
});
1517
});
1618
});

packages/graph-schema-utils/src/formatters/json/extensions.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "../../model/index.js";
2424
import {
2525
ConstraintJsonStruct,
26+
GraphSchemaJsonStruct,
2627
IndexJsonStruct,
2728
isLookupIndexJsonStruct,
2829
isNodeLabelConstraintJsonStruct,
@@ -107,8 +108,27 @@ export function fromJson(schema: string): GraphSchema {
107108
return fromJsonStruct(schemaJson);
108109
}
109110

111+
export function hasDuplicateNodeLabelIds(
112+
schema: GraphSchemaJsonStruct
113+
): boolean {
114+
const ids = new Set<string>();
115+
116+
for (const nodeLabel of schema.nodeLabels) {
117+
if (ids.has(nodeLabel.$id)) {
118+
return true;
119+
}
120+
ids.add(nodeLabel.$id);
121+
}
122+
123+
return false;
124+
}
125+
110126
export function fromJsonStruct(schemaJson: RootSchemaJsonStruct): GraphSchema {
111127
const { graphSchema } = schemaJson.graphSchemaRepresentation;
128+
console.log(graphSchema, hasDuplicateNodeLabelIds(graphSchema));
129+
if (hasDuplicateNodeLabelIds(graphSchema)) {
130+
throw new Error("Duplicate node label IDs found in schema");
131+
}
112132
const labels = graphSchema.nodeLabels.map(nodeLabel.create);
113133
const relationshipTypes = graphSchema.relationshipTypes.map(
114134
relationshipType.create

packages/graph-schema-utils/src/formatters/json/test-schemas/duplicated-nodeLabel-ids.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,43 +50,43 @@
5050
]
5151
},
5252
{
53-
"$id": "nl:1",
53+
"$id": "nl:0",
5454
"token": "DOCS_METADATA",
5555
"properties": [
5656
{
57-
"$id": "p:1_0",
57+
"$id": "p:0_0",
5858
"token": "RELATIVE_PATH",
5959
"type": {
6060
"type": "string"
6161
},
6262
"nullable": false
6363
},
6464
{
65-
"$id": "p:1_1",
65+
"$id": "p:0_1",
6666
"token": "MODEL_NAME",
6767
"type": {
6868
"type": "string"
6969
},
7070
"nullable": false
7171
},
7272
{
73-
"$id": "p:1_2",
73+
"$id": "p:0_2",
7474
"token": "SKILLS",
7575
"type": {
7676
"type": "string"
7777
},
7878
"nullable": false
7979
},
8080
{
81-
"$id": "p:1_3",
81+
"$id": "p:0_3",
8282
"token": "DOMAINS",
8383
"type": {
8484
"type": "string"
8585
},
8686
"nullable": false
8787
},
8888
{
89-
"$id": "p:1_4",
89+
"$id": "p:0_4",
9090
"token": "SUMMARY",
9191
"type": {
9292
"type": "string"
@@ -107,10 +107,10 @@
107107
]
108108
},
109109
{
110-
"$id": "n:1",
110+
"$id": "n:0",
111111
"labels": [
112112
{
113-
"$ref": "#nl:1"
113+
"$ref": "#nl:0"
114114
}
115115
]
116116
}

0 commit comments

Comments
 (0)