-
Notifications
You must be signed in to change notification settings - Fork 18
/
execute-query-test.js
149 lines (132 loc) · 5.3 KB
/
execute-query-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import PathProxy from '../../src/PathProxy';
import ExecuteQueryHandler from '../../src/ExecuteQueryHandler';
import SparqlHandler from '../../src/SparqlHandler';
import PathExpressionHandler from '../../src/PathExpressionHandler';
import InsertFunctionHandler from '../../src/InsertFunctionHandler';
import DeleteFunctionHandler from '../../src/DeleteFunctionHandler';
import ReplaceFunctionHandler from '../../src/ReplaceFunctionHandler';
import SubjectsHandler from '../../src/SubjectsHandler';
import SetFunctionHandler from '../../src/SetFunctionHandler';
import DataHandler from '../../src/DataHandler';
import JSONLDResolver from '../../src/JSONLDResolver';
import MutationExpressionsHandler from '../../src/MutationExpressionsHandler';
import { createQueryEngine, deindent } from '../util';
import { namedNode, literal } from '@rdfjs/data-model';
import { iterableToArray } from '../../src/iterableUtils';
import context from '../context';
import ThenHandler from '../../src/ThenHandler';
const subject = namedNode('https://example.org/#me');
const queryEngine = createQueryEngine([
literal('Alice'),
literal('Bob'),
literal('Carol'),
]);
const resolvers = [
new JSONLDResolver(context),
];
const handlersPath = {
then: new ThenHandler(),
mutationExpressions: new MutationExpressionsHandler(),
sparql: new SparqlHandler(),
pathExpression: new PathExpressionHandler(),
results: new ExecuteQueryHandler(),
[Symbol.asyncIterator]: {
handle(pathData, path) {
return () => path.results[Symbol.asyncIterator]();
},
},
toString: DataHandler.syncFunction('subject', 'value'),
};
const handlersMutation = {
then: new ThenHandler(),
mutationExpressions: new MutationExpressionsHandler(),
sparql: new SparqlHandler(),
pathExpression: new PathExpressionHandler(),
results: new ExecuteQueryHandler(),
[Symbol.asyncIterator]: {
handle(pathData, path) {
return () => path.results[Symbol.asyncIterator]();
},
},
add: new InsertFunctionHandler(),
delete: new DeleteFunctionHandler(),
replace: new ReplaceFunctionHandler(),
set: new SetFunctionHandler(),
toString: DataHandler.syncFunction('subject', 'value'),
};
describe('when the query engine throws an error', () => {
let person;
beforeAll(() => {
const failingQueryEngine = {
async *execute() {
throw new Error('Query engine error');
},
};
const pathProxy = new PathProxy({ handlers: handlersPath, resolvers });
person = pathProxy.createPath({ queryEngine: failingQueryEngine }, { subject });
});
it('rejects with an error to a calling iterator', async () => {
await expect(iterableToArray(person.firstName)).rejects
.toThrow('Query engine error');
});
});
describe('a query path with a path expression handler', () => {
let person;
beforeAll(() => {
const pathProxy = new PathProxy({ handlers: handlersPath, resolvers });
person = pathProxy.createPath({ queryEngine }, { subject });
});
it('returns results for a path with 3 links', async () => {
const names = await iterableToArray(person.friends.firstName);
expect(names.map(n => `${n}`)).toEqual(['Alice', 'Bob', 'Carol']);
});
it('returns results for nested expression calls', async () => {
const names = [];
for await (const friend of person.friends) {
for await (const firstName of friend.friends.firstName)
names.push(firstName);
}
expect(names.map(n => `${n}`)).toEqual(['Alice', 'Bob', 'Carol', 'Alice', 'Bob', 'Carol', 'Alice', 'Bob', 'Carol']);
});
});
describe('a query path with a subjects handler', () => {
let person;
beforeAll(() => {
const pathProxy = new PathProxy({ handlers: { ...handlersPath, subjects: new SubjectsHandler() }, resolvers });
person = pathProxy.createPath({ queryEngine }, { subject });
});
it('returns results, with executeQuery being called with the right query', async () => {
const subjects = await iterableToArray(person.subjects);
expect(queryEngine.execute).toBeCalledWith(deindent(`
SELECT DISTINCT ?subject WHERE {
?subject ?predicate ?object.
}`));
expect(subjects.map(s => `${s}`)).toEqual(['Alice', 'Bob', 'Carol']);
});
});
describe('a query path with a path and mutation expression handler', () => {
let person;
beforeAll(() => {
const pathProxy = new PathProxy({ handlers: handlersMutation, resolvers });
person = pathProxy.createPath({ queryEngine }, { subject });
});
it('returns results for a path with 3 links', async () => {
const names = await iterableToArray(person.friends.firstName);
expect(names.map(n => `${n}`)).toEqual(['Alice', 'Bob', 'Carol']);
});
it('returns results for a path with 4 links and fixed values', async () => {
const names = [];
for await (const firstName of person.friends.friends(namedNode('http://ex.org/John')).firstName)
names.push(firstName);
expect(names.map(n => `${n}`)).toEqual(['Alice', 'Bob', 'Carol']);
});
it('returns true for an addition with 3 links', async () => {
expect(await person.friends.firstName.add('Ruben')).toBeTruthy();
});
it('returns true for an set with 3 links', async () => {
expect(await person.friends.firstName.set('Ruben')).toBeTruthy();
});
it('returns true for an replace with 3 links', async () => {
expect(await person.friends.firstName.set('ruben', 'Ruben')).toBeTruthy();
});
});