Skip to content

Commit cc52c50

Browse files
committed
Add test and fix issue with WOQL from (fixes #318)
1 parent 8368ce9 commit cc52c50

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//@ts-check
2+
import { describe, expect, test, beforeAll } from '@jest/globals';
3+
import { WOQLClient, WOQL, Vars } from '../index.js';
4+
import { DbDetails } from '../dist/typescript/lib/typedef.js';
5+
6+
let client: WOQLClient
7+
const db01 = 'db__test_woql_regression';
8+
9+
beforeAll(async () => {
10+
client = new WOQLClient("http://localhost:6363", { user: 'admin', organization: 'admin', key: process.env.TDB_ADMIN_PASS ?? 'root' })
11+
client.db(db01);
12+
const dbObj: DbDetails = { label: db01, comment: 'add db', schema: true }
13+
await client.createDatabase(db01, dbObj);
14+
});
15+
16+
afterAll(async () => {
17+
await client.deleteDatabase(db01);
18+
});
19+
20+
describe('Tests for woql graph addressing', () => {
21+
it('should construct correct query for: from instance', async () => {
22+
const query = WOQL.from('instance').limit(10).eval(WOQL.plus(1, 1), 'v:result');
23+
24+
const expectedJson = [{ "result": { "@type": "xsd:decimal", "@value": 2 } }];
25+
26+
const result = await client.query(query);
27+
expect(result?.bindings).toStrictEqual(expectedJson);
28+
});
29+
30+
it('should construct correct query for: from schema', async () => {
31+
// This tests corresponds to issue #2077, with incorrect AST, now fixed
32+
// https://github.com/terminusdb/terminusdb/issues/2077
33+
let v = Vars("cls");
34+
const query = WOQL.from("schema")
35+
.triple(v.cls, "rdf:type", "sys:Class")
36+
const expectedJson = [];
37+
const result = await client.query(query);
38+
expect(result?.bindings).toStrictEqual(expectedJson);
39+
});
40+
41+
test('should construct correct query for: info', async () => {
42+
const result = await client.info();
43+
expect(result["@type"]).toStrictEqual("api:InfoResponse");
44+
});
45+
});

lib/query/woqlQuery.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,14 @@ WOQLQuery.prototype.or = function (...subqueries) {
421421
*/
422422

423423
WOQLQuery.prototype.from = function (graphRef, query) {
424-
// if (graph && graph === 'args')
425-
// return ['graph', 'query']
426424
if (this.cursor['@type']) this.wrapCursorWithAnd();
427425
this.cursor['@type'] = 'From';
428-
if (!graphRef || typeof graph !== 'string') {
426+
if (!graphRef || typeof graphRef !== 'string') {
429427
return this.parameterError(
430428
'The first parameter to from must be a Graph Filter Expression (string)',
431429
);
432430
}
433-
this.cursor.graph = graphRef;
431+
this.cursor.graph = this.jlt(graphRef);
434432
return this.addSubQuery(query);
435433
};
436434

test/woql.spec.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,20 @@ describe('woql queries', () => {
123123
});
124124

125125
it('check the from method', () => {
126-
const WOQLQuery = WOQL.limit(10);
127-
128-
const woqlObjectChain = WOQL.from('http://dburl').limit(10);
129-
const woqlObject = WOQL.from('http://dburl', WOQLQuery);
126+
const woqlObjectChain = WOQL.from('instance/main').limit(10).and();
130127
const jsonObj = {
131128
'@type': 'From',
132-
graph: 'http://dburl',
129+
graph: {
130+
"@type": "xsd:string",
131+
"@value": "instance/main",
132+
},
133133
query: {
134134
'@type': 'Limit',
135135
limit: 10,
136-
query: {},
137136
},
138137
};
139138

140-
// expect(woqlObject.json()).to.eql(jsonObj);
141-
// expect(woqlObjectChain.json()).to.eql(jsonObj);
139+
expect(woqlObjectChain.json()).to.eql(jsonObj);
142140
});
143141

144142
it('check the star method', () => {
@@ -496,4 +494,11 @@ describe('woql queries', () => {
496494
expect(wq).to.deep.eql(
497495
{"@type":"And","and":[{"@type":"Eval","expression":{"@type":"Times","left":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":3}},"right":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":4}}},"result":{"@type":"ArithmeticValue","variable":"a"}},{"@type":"Eval","expression":{"@type":"Times","left":{"@type":"ArithmeticValue","variable":"a"},"right":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":3}}},"result":{"@type":"ArithmeticValue","variable":"res"}}]})
498496
});
497+
498+
it('check limit().eval()', () => {
499+
let v = Vars("result");
500+
const woqlObject = WOQL.limit(100).eval(WOQL.times(2, 3), v.result);
501+
const expectedJson = {"@type":"Limit","limit":100,"query":{"@type":"Eval","expression":{"@type":"Times","left":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":2}},"right":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":3}}},"result":{"@type":"ArithmeticValue","variable":"result"}}};
502+
expect(woqlObject.json()).to.deep.eql(expectedJson);
503+
});
499504
});

0 commit comments

Comments
 (0)