|
| 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 | +const db01 = 'db__test_woql_set_operations'; |
| 8 | + |
| 9 | +beforeAll(() => { |
| 10 | + client = new WOQLClient("http://127.0.0.1:6363", { user: 'admin', organization: 'admin', key: process.env.TDB_ADMIN_PASS ?? 'root' }); |
| 11 | + client.db(db01); |
| 12 | +}); |
| 13 | + |
| 14 | +describe('Tests for WOQL set operations', () => { |
| 15 | + test('Create a database', async () => { |
| 16 | + const dbObj: DbDetails = { label: db01, comment: 'test woql set operations', schema: true }; |
| 17 | + const result = await client.createDatabase(db01, dbObj); |
| 18 | + expect(result["@type"]).toEqual("api:DbCreateResponse"); |
| 19 | + expect(result["api:status"]).toEqual("api:success"); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('set_difference', () => { |
| 23 | + test('computes difference between two lists', async () => { |
| 24 | + const query = WOQL.and( |
| 25 | + WOQL.eq("v:ListA", [1, 2, 3, 4]), |
| 26 | + WOQL.eq("v:ListB", [2, 4]), |
| 27 | + WOQL.set_difference("v:ListA", "v:ListB", "v:Diff") |
| 28 | + ); |
| 29 | + |
| 30 | + const result = await client.query(query); |
| 31 | + expect(result?.bindings).toHaveLength(1); |
| 32 | + const diff = result?.bindings[0].Diff.map((v: any) => v['@value']); |
| 33 | + expect(diff).toEqual([1, 3]); |
| 34 | + }); |
| 35 | + |
| 36 | + test('returns empty list when first list is subset of second', async () => { |
| 37 | + const query = WOQL.and( |
| 38 | + WOQL.eq("v:ListA", [1, 2]), |
| 39 | + WOQL.eq("v:ListB", [1, 2, 3]), |
| 40 | + WOQL.set_difference("v:ListA", "v:ListB", "v:Diff") |
| 41 | + ); |
| 42 | + |
| 43 | + const result = await client.query(query); |
| 44 | + expect(result?.bindings).toHaveLength(1); |
| 45 | + expect(result?.bindings[0].Diff).toEqual([]); |
| 46 | + }); |
| 47 | + |
| 48 | + test('handles empty lists', async () => { |
| 49 | + const query = WOQL.and( |
| 50 | + WOQL.eq("v:ListA", []), |
| 51 | + WOQL.eq("v:ListB", [1]), |
| 52 | + WOQL.set_difference("v:ListA", "v:ListB", "v:Diff") |
| 53 | + ); |
| 54 | + |
| 55 | + const result = await client.query(query); |
| 56 | + expect(result?.bindings).toHaveLength(1); |
| 57 | + expect(result?.bindings[0].Diff).toEqual([]); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('set_intersection', () => { |
| 62 | + test('computes intersection of two lists', async () => { |
| 63 | + const query = WOQL.and( |
| 64 | + WOQL.eq("v:ListA", [1, 2, 3]), |
| 65 | + WOQL.eq("v:ListB", [2, 3, 4]), |
| 66 | + WOQL.set_intersection("v:ListA", "v:ListB", "v:Common") |
| 67 | + ); |
| 68 | + |
| 69 | + const result = await client.query(query); |
| 70 | + expect(result?.bindings).toHaveLength(1); |
| 71 | + const common = result?.bindings[0].Common.map((v: any) => v['@value']); |
| 72 | + expect(common).toEqual([2, 3]); |
| 73 | + }); |
| 74 | + |
| 75 | + test('returns empty list when no common elements', async () => { |
| 76 | + const query = WOQL.and( |
| 77 | + WOQL.eq("v:ListA", [1, 2]), |
| 78 | + WOQL.eq("v:ListB", [3, 4]), |
| 79 | + WOQL.set_intersection("v:ListA", "v:ListB", "v:Common") |
| 80 | + ); |
| 81 | + |
| 82 | + const result = await client.query(query); |
| 83 | + expect(result?.bindings).toHaveLength(1); |
| 84 | + expect(result?.bindings[0].Common).toEqual([]); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('set_union', () => { |
| 89 | + test('computes union of two lists', async () => { |
| 90 | + const query = WOQL.and( |
| 91 | + WOQL.eq("v:ListA", [1, 2]), |
| 92 | + WOQL.eq("v:ListB", [2, 3]), |
| 93 | + WOQL.set_union("v:ListA", "v:ListB", "v:All") |
| 94 | + ); |
| 95 | + |
| 96 | + const result = await client.query(query); |
| 97 | + expect(result?.bindings).toHaveLength(1); |
| 98 | + const all = result?.bindings[0].All.map((v: any) => v['@value']); |
| 99 | + expect(all).toEqual([1, 2, 3]); |
| 100 | + }); |
| 101 | + |
| 102 | + test('removes duplicates', async () => { |
| 103 | + const query = WOQL.and( |
| 104 | + WOQL.eq("v:ListA", [1, 1, 2]), |
| 105 | + WOQL.eq("v:ListB", [2, 2]), |
| 106 | + WOQL.set_union("v:ListA", "v:ListB", "v:All") |
| 107 | + ); |
| 108 | + |
| 109 | + const result = await client.query(query); |
| 110 | + expect(result?.bindings).toHaveLength(1); |
| 111 | + const all = result?.bindings[0].All.map((v: any) => v['@value']); |
| 112 | + expect(all).toEqual([1, 2]); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('set_member', () => { |
| 117 | + test('checks membership in a set', async () => { |
| 118 | + const query = WOQL.and( |
| 119 | + WOQL.eq("v:MySet", [1, 2, 3]), |
| 120 | + WOQL.set_member(2, "v:MySet") |
| 121 | + ); |
| 122 | + |
| 123 | + const result = await client.query(query); |
| 124 | + expect(result?.bindings).toHaveLength(1); |
| 125 | + }); |
| 126 | + |
| 127 | + test('fails for non-member', async () => { |
| 128 | + const query = WOQL.and( |
| 129 | + WOQL.eq("v:MySet", [1, 2, 3]), |
| 130 | + WOQL.set_member(5, "v:MySet") |
| 131 | + ); |
| 132 | + |
| 133 | + const result = await client.query(query); |
| 134 | + expect(result?.bindings).toHaveLength(0); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('list_to_set', () => { |
| 139 | + test('converts list to set removing duplicates and sorting', async () => { |
| 140 | + const query = WOQL.and( |
| 141 | + WOQL.eq("v:MyList", [3, 1, 2, 1]), |
| 142 | + WOQL.list_to_set("v:MyList", "v:MySet") |
| 143 | + ); |
| 144 | + |
| 145 | + const result = await client.query(query); |
| 146 | + expect(result?.bindings).toHaveLength(1); |
| 147 | + const mySet = result?.bindings[0].MySet.map((v: any) => v['@value']); |
| 148 | + expect(mySet).toEqual([1, 2, 3]); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + describe('performance test', () => { |
| 153 | + test('handles large set operations efficiently', async () => { |
| 154 | + // Create two large arrays with some overlap |
| 155 | + const listA = Array.from({ length: 1000 }, (_, i) => i); |
| 156 | + const listB = Array.from({ length: 1000 }, (_, i) => i + 500); |
| 157 | + |
| 158 | + const query = WOQL.and( |
| 159 | + WOQL.eq("v:ListA", listA), |
| 160 | + WOQL.eq("v:ListB", listB), |
| 161 | + WOQL.set_difference("v:ListA", "v:ListB", "v:Diff") |
| 162 | + ); |
| 163 | + |
| 164 | + const startTime = Date.now(); |
| 165 | + const result = await client.query(query); |
| 166 | + const elapsed = Date.now() - startTime; |
| 167 | + |
| 168 | + expect(result?.bindings).toHaveLength(1); |
| 169 | + expect(result?.bindings[0].Diff.length).toEqual(500); |
| 170 | + |
| 171 | + // Should complete in under 1 second with O(n log n) algorithm |
| 172 | + expect(elapsed).toBeLessThan(1000); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + test('Delete a database', async () => { |
| 177 | + const result = await client.deleteDatabase(db01); |
| 178 | + expect(result).toStrictEqual({ '@type': 'api:DbDeleteResponse', 'api:status': 'api:success' }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments