|
| 1 | +//@ts-check |
| 2 | +import { describe, expect, test, beforeAll, afterAll } from "@jest/globals"; |
| 3 | +import { WOQLClient, WOQL } from "../index.js"; |
| 4 | +import { DbDetails } from "../dist/typescript/lib/typedef.js"; |
| 5 | + |
| 6 | +let client: WOQLClient; |
| 7 | + |
| 8 | +beforeAll(() => { |
| 9 | + client = new WOQLClient("http://127.0.0.1:6363", { |
| 10 | + user: "admin", |
| 11 | + organization: "admin", |
| 12 | + key: process.env.TDB_ADMIN_PASS ?? "root" |
| 13 | + }); |
| 14 | +}); |
| 15 | + |
| 16 | +const testDb = "db__test_woql_random_idgen"; |
| 17 | + |
| 18 | +describe("WOQL Random ID Generation", () => { |
| 19 | + test("Setup: Create database and schema", async () => { |
| 20 | + const dbObj: DbDetails = { |
| 21 | + label: testDb, |
| 22 | + comment: "Test database for random ID generation", |
| 23 | + schema: true |
| 24 | + }; |
| 25 | + const result = await client.createDatabase(testDb, dbObj); |
| 26 | + expect(result["@type"]).toEqual("api:DbCreateResponse"); |
| 27 | + |
| 28 | + const schema = [ |
| 29 | + { |
| 30 | + "@type": "Class", |
| 31 | + "@id": "Person", |
| 32 | + "@key": { "@type": "Random" }, |
| 33 | + name: "xsd:string" |
| 34 | + } |
| 35 | + ]; |
| 36 | + |
| 37 | + await client.addDocument(schema, { graph_type: "schema" }); |
| 38 | + }); |
| 39 | + |
| 40 | + test("Generate random ID using WOQL", async () => { |
| 41 | + const query = WOQL.random_idgen("Person/", "v:person_id"); |
| 42 | + |
| 43 | + const result = await client.query(query); |
| 44 | + expect(result?.bindings).toBeDefined(); |
| 45 | + expect(result?.bindings?.length).toBeGreaterThan(0); |
| 46 | + |
| 47 | + // Server returns bindings without the 'v:' prefix |
| 48 | + const binding = result?.bindings?.[0]; |
| 49 | + const personId = binding["person_id"] || binding["v:person_id"]; |
| 50 | + expect(personId).toBeDefined(); |
| 51 | + expect(personId).toContain("Person/"); |
| 52 | + |
| 53 | + // Should have a 16-character random suffix |
| 54 | + const suffix = personId.split("Person/")[1]; |
| 55 | + expect(suffix.length).toBe(16); |
| 56 | + }); |
| 57 | + |
| 58 | + test("Generate multiple unique IDs", async () => { |
| 59 | + const query = WOQL.and( |
| 60 | + WOQL.random_idgen("Person/", "v:id1"), |
| 61 | + WOQL.random_idgen("Person/", "v:id2"), |
| 62 | + WOQL.random_idgen("Person/", "v:id3") |
| 63 | + ); |
| 64 | + |
| 65 | + const result = await client.query(query); |
| 66 | + expect(result?.bindings).toBeDefined(); |
| 67 | + expect(result?.bindings?.length).toBe(1); |
| 68 | + |
| 69 | + const binding = result?.bindings?.[0]; |
| 70 | + const id1 = binding["id1"] || binding["v:id1"]; |
| 71 | + const id2 = binding["id2"] || binding["v:id2"]; |
| 72 | + const id3 = binding["id3"] || binding["v:id3"]; |
| 73 | + |
| 74 | + // All IDs should be different |
| 75 | + expect(id1).not.toEqual(id2); |
| 76 | + expect(id1).not.toEqual(id3); |
| 77 | + expect(id2).not.toEqual(id3); |
| 78 | + |
| 79 | + // All should start with prefix |
| 80 | + expect(id1).toContain("Person/"); |
| 81 | + expect(id2).toContain("Person/"); |
| 82 | + expect(id3).toContain("Person/"); |
| 83 | + }); |
| 84 | + |
| 85 | + test("Use random ID to create document", async () => { |
| 86 | + //Generate random ID |
| 87 | + const query = WOQL.random_idgen("Person/", "v:new_person"); |
| 88 | + |
| 89 | + const result = await client.query(query); |
| 90 | + expect(result?.bindings).toBeDefined(); |
| 91 | + |
| 92 | + const binding = result?.bindings?.[0]; |
| 93 | + const personId = binding["new_person"] || binding["v:new_person"]; |
| 94 | + expect(personId).toBeDefined(); |
| 95 | + expect(personId).toContain("Person/"); |
| 96 | + |
| 97 | + // Create the document using the generated ID |
| 98 | + const doc = { |
| 99 | + "@type": "Person", |
| 100 | + "@id": personId, |
| 101 | + name: "Alice" |
| 102 | + }; |
| 103 | + |
| 104 | + await client.addDocument(doc); |
| 105 | + |
| 106 | + // Verify the document was created |
| 107 | + const retrieved: any = await client.getDocument({ id: personId }); |
| 108 | + expect(retrieved["@id"]).toEqual(personId); |
| 109 | + expect(retrieved.name).toEqual("Alice"); |
| 110 | + }); |
| 111 | + |
| 112 | + test("Generate different IDs on repeated query execution", async () => { |
| 113 | + const query = WOQL.random_idgen("Data/", "v:id"); |
| 114 | + const ids = new Set<string>(); |
| 115 | + |
| 116 | + for (let i = 0; i < 10; i++) { |
| 117 | + const result = await client.query(query); |
| 118 | + const binding = result?.bindings?.[0]; |
| 119 | + const id = binding["id"] || binding["v:id"]; |
| 120 | + ids.add(id); |
| 121 | + } |
| 122 | + |
| 123 | + // All 10 executions should produce unique IDs |
| 124 | + expect(ids.size).toBe(10); |
| 125 | + }); |
| 126 | + |
| 127 | + test("Mix WOQL builder with raw JSON-LD", async () => { |
| 128 | + // Test mixing WOQL builder syntax with raw JSON-LD |
| 129 | + // This verifies the pattern documented in woql-json-ld-queries guide |
| 130 | + const query1 = WOQL.and( |
| 131 | + WOQL.random_idgen("Test/", "v:test_id"), |
| 132 | + { |
| 133 | + "@type": "LexicalKey", |
| 134 | + base: { |
| 135 | + "@type": "DataValue", |
| 136 | + data: { |
| 137 | + "@type": "xsd:string", |
| 138 | + "@value": "Display/" |
| 139 | + } |
| 140 | + }, |
| 141 | + key_list: [], |
| 142 | + uri: { |
| 143 | + "@type": "NodeValue", |
| 144 | + variable: "out" |
| 145 | + } |
| 146 | + } as any |
| 147 | + ); |
| 148 | + |
| 149 | + const result1 = await client.query(query1); |
| 150 | + const binding1 = result1?.bindings?.[0]; |
| 151 | + |
| 152 | + // Verify random_idgen result |
| 153 | + const id1 = binding1["test_id"] || binding1["v:test_id"]; |
| 154 | + expect(id1).toBeDefined(); |
| 155 | + expect(id1).toContain("Test/"); |
| 156 | + |
| 157 | + // Verify the ID has correct 16-character suffix |
| 158 | + const suffix = id1.split("Test/")[1]; |
| 159 | + expect(suffix.length).toBe(16); |
| 160 | + |
| 161 | + // Verify LexicalKey result (empty key_list generates just the base) |
| 162 | + const out = binding1["out"] || binding1["v:out"]; |
| 163 | + expect(out).toBeDefined(); |
| 164 | + expect(out).toContain("Display/"); |
| 165 | + }); |
| 166 | + |
| 167 | + test("Both random_idgen and idgen_random aliases work", async () => { |
| 168 | + // Test random_idgen alias |
| 169 | + const query1 = WOQL.random_idgen("Test/", "v:test_id"); |
| 170 | + const result1 = await client.query(query1); |
| 171 | + const binding1 = result1?.bindings?.[0]; |
| 172 | + const id1 = binding1["test_id"] || binding1["v:test_id"]; |
| 173 | + expect(id1).toBeDefined(); |
| 174 | + expect(id1).toContain("Test/"); |
| 175 | + |
| 176 | + // Test idgen_random alias (should produce same structure) |
| 177 | + const query2 = WOQL.idgen_random("Test/", "v:test_id"); |
| 178 | + const result2 = await client.query(query2); |
| 179 | + const binding2 = result2?.bindings?.[0]; |
| 180 | + const id2 = binding2["test_id"] || binding2["v:test_id"]; |
| 181 | + expect(id2).toBeDefined(); |
| 182 | + expect(id2).toContain("Test/"); |
| 183 | + |
| 184 | + // Both queries should produce the same JSON structure (same variable name) |
| 185 | + expect(query1.json()).toEqual(query2.json()); |
| 186 | + }); |
| 187 | + |
| 188 | + afterAll(async () => { |
| 189 | + try { |
| 190 | + await client.deleteDatabase(testDb); |
| 191 | + } catch (e) { |
| 192 | + // Ignore errors |
| 193 | + } |
| 194 | + }); |
| 195 | +}); |
0 commit comments