|
| 1 | +//@ts-check |
| 2 | +import { describe, expect, test, beforeAll, afterAll } from '@jest/globals'; |
| 3 | +import { WOQLClient, WOQL, Doc } from '../index.js'; |
| 4 | +import WOQLQuery from '../lib/query/woqlQuery.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Integration test for update_document with a list of subdocuments. |
| 8 | + * |
| 9 | + * This test verifies that the WOQL builder correctly handles nested Doc() |
| 10 | + * objects when updating documents containing lists of subdocuments. |
| 11 | + * |
| 12 | + * Related issue: Nested Doc() in update_document was producing incorrect |
| 13 | + * WOQL JSON structure due to double-conversion. |
| 14 | + */ |
| 15 | + |
| 16 | +let client: WOQLClient; |
| 17 | +const testDbName = `update_list_test_${Date.now()}`; |
| 18 | + |
| 19 | +// Schema with a subdocument list |
| 20 | +const schema = [ |
| 21 | + { |
| 22 | + '@base': 'terminusdb:///data/', |
| 23 | + '@schema': 'terminusdb:///schema#', |
| 24 | + '@type': '@context', |
| 25 | + }, |
| 26 | + { |
| 27 | + '@type': 'Class', |
| 28 | + '@id': 'UpdateList', |
| 29 | + '@key': { '@type': 'Random' }, |
| 30 | + list: { '@class': 'Structure', '@type': 'List' }, |
| 31 | + }, |
| 32 | + { |
| 33 | + '@type': 'Class', |
| 34 | + '@id': 'Structure', |
| 35 | + '@key': { '@type': 'Random' }, |
| 36 | + '@subdocument': [], |
| 37 | + string: 'xsd:string', |
| 38 | + }, |
| 39 | +]; |
| 40 | + |
| 41 | +beforeAll(async () => { |
| 42 | + client = new WOQLClient('http://127.0.0.1:6363', { |
| 43 | + user: 'admin', |
| 44 | + organization: 'admin', |
| 45 | + key: process.env.TDB_ADMIN_PASS ?? 'root', |
| 46 | + }); |
| 47 | + |
| 48 | + // Create test database |
| 49 | + await client.createDatabase(testDbName, { |
| 50 | + label: 'Update List Test', |
| 51 | + comment: 'Test database for update_document with subdocument lists', |
| 52 | + schema: true, |
| 53 | + }); |
| 54 | + client.db(testDbName); |
| 55 | + |
| 56 | + // Add schema |
| 57 | + await client.addDocument(schema, { graph_type: 'schema', full_replace: true }); |
| 58 | +}); |
| 59 | + |
| 60 | +afterAll(async () => { |
| 61 | + try { |
| 62 | + await client.deleteDatabase(testDbName); |
| 63 | + } catch (e) { |
| 64 | + // Database might not exist |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +describe('update_document with list of subdocuments', () => { |
| 69 | + const docId = 'UpdateList/test-doc'; |
| 70 | + |
| 71 | + test('should insert initial document with subdocument list', async () => { |
| 72 | + const initialDoc = { |
| 73 | + '@type': 'UpdateList', |
| 74 | + '@id': docId, |
| 75 | + list: [ |
| 76 | + { '@type': 'Structure', string: 'initial-1' }, |
| 77 | + { '@type': 'Structure', string: 'initial-2' }, |
| 78 | + ], |
| 79 | + }; |
| 80 | + |
| 81 | + const result = await client.addDocument(initialDoc); |
| 82 | + // Result contains full IRI with prefix |
| 83 | + expect(result[0]).toContain('UpdateList/test-doc'); |
| 84 | + }); |
| 85 | + |
| 86 | + test('should update document list using WOQL.update_document with nested Doc()', async () => { |
| 87 | + // This is the pattern that was failing before the fix |
| 88 | + const query = WOQL.update_document( |
| 89 | + new (Doc as any)({ |
| 90 | + '@type': 'UpdateList', |
| 91 | + '@id': docId, |
| 92 | + list: [ |
| 93 | + new (Doc as any)({ '@type': 'Structure', string: 'updated-1' }), |
| 94 | + new (Doc as any)({ '@type': 'Structure', string: 'updated-2' }), |
| 95 | + new (Doc as any)({ '@type': 'Structure', string: 'updated-3' }), |
| 96 | + ], |
| 97 | + }), |
| 98 | + ) as WOQLQuery; |
| 99 | + |
| 100 | + const result = await client.query(query); |
| 101 | + expect(result).toBeDefined(); |
| 102 | + expect(result?.inserts).toBeGreaterThan(0); |
| 103 | + expect(result?.deletes).toBeGreaterThan(0); |
| 104 | + |
| 105 | + // Verify the document was updated correctly |
| 106 | + const doc = await client.getDocument({ id: docId }); |
| 107 | + expect(doc['@type']).toEqual('UpdateList'); |
| 108 | + expect(doc.list).toHaveLength(3); |
| 109 | + expect(doc.list[0].string).toEqual('updated-1'); |
| 110 | + expect(doc.list[1].string).toEqual('updated-2'); |
| 111 | + expect(doc.list[2].string).toEqual('updated-3'); |
| 112 | + }); |
| 113 | + |
| 114 | + test('should update document list using plain objects (alternative syntax)', async () => { |
| 115 | + // Alternative approach without nested Doc() - should also work |
| 116 | + const query = WOQL.update_document( |
| 117 | + new (Doc as any)({ |
| 118 | + '@type': 'UpdateList', |
| 119 | + '@id': docId, |
| 120 | + list: [ |
| 121 | + { '@type': 'Structure', string: 'plain-1' }, |
| 122 | + { '@type': 'Structure', string: 'plain-2' }, |
| 123 | + ], |
| 124 | + }), |
| 125 | + ) as WOQLQuery; |
| 126 | + |
| 127 | + const result = await client.query(query); |
| 128 | + expect(result).toBeDefined(); |
| 129 | + |
| 130 | + // Verify the document was updated correctly |
| 131 | + const doc = await client.getDocument({ id: docId }); |
| 132 | + expect(doc.list).toHaveLength(2); |
| 133 | + expect(doc.list[0].string).toEqual('plain-1'); |
| 134 | + expect(doc.list[1].string).toEqual('plain-2'); |
| 135 | + }); |
| 136 | + |
| 137 | + test('should update to empty list', async () => { |
| 138 | + const query = WOQL.update_document( |
| 139 | + new (Doc as any)({ |
| 140 | + '@type': 'UpdateList', |
| 141 | + '@id': docId, |
| 142 | + list: [], |
| 143 | + }), |
| 144 | + ) as WOQLQuery; |
| 145 | + |
| 146 | + const result = await client.query(query); |
| 147 | + expect(result).toBeDefined(); |
| 148 | + |
| 149 | + // Verify the list is now empty |
| 150 | + const doc = await client.getDocument({ id: docId }); |
| 151 | + expect(doc.list).toEqual([]); |
| 152 | + }); |
| 153 | +}); |
0 commit comments