-
Notifications
You must be signed in to change notification settings - Fork 21
/
connection.test.ts
333 lines (289 loc) · 12.2 KB
/
connection.test.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { Dictionary, each } from 'lodash';
import * as neo4j from 'neo4j-driver';
import { Driver, Session } from 'neo4j-driver/types';
import { AuthToken, Config } from 'neo4j-driver/types/driver';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { SinonSpy, SinonStub, spy, stub } from 'sinon';
import { Connection, Node, Query } from '../src';
import { NodePattern } from '../src/clauses';
import { expect } from '../test-setup';
import { neo4jCredentials, neo4jUrl, waitForNeo } from './utils';
type ArgumentTypes<T extends (...args: any) => any>
= T extends (...a: infer Args) => any ? Args : never;
type SinonSpyFor<T extends (...args: any) => any>
= SinonSpy<ArgumentTypes<T>, ReturnType<T>>;
type SinonStubFor<T extends (...args: any) => any>
= SinonStub<ArgumentTypes<T>, ReturnType<T>>;
describe('Connection', () => {
let connection: Connection;
let driver: Driver;
let driverCloseSpy: SinonSpyFor<Driver['close']>;
let driverSessionStub: SinonStubFor<Driver['session']>;
let sessionRunSpy: SinonSpyFor<Session['run']>;
let sessionCloseSpy: SinonSpyFor<Session['close']>;
const stubSession = stub<[Session], void>();
function attachSessionSpies(session: Session): void {
sessionRunSpy = spy(session, 'run');
sessionCloseSpy = spy(session, 'close');
}
function makeSessionMock(createSession: Driver['session']): Driver['session'] {
return (...args) => {
const session = createSession(...args);
stubSession(session);
return session;
};
}
function driverConstructor(url: string, authToken?: AuthToken, config?: Config) {
driver = neo4j.driver(url, authToken, config);
const mock = makeSessionMock(driver.session.bind(driver));
driverSessionStub = stub(driver, 'session').callsFake(mock);
driverCloseSpy = spy(driver, 'close');
return driver;
}
// Wait for neo4j to be ready before testing
before(waitForNeo);
beforeEach(() => {
stubSession.callsFake(attachSessionSpies);
connection = new Connection(neo4jUrl, neo4jCredentials, driverConstructor);
});
afterEach(() => connection.close());
describe('#constructor', () => {
it('should default to neo4j driver', async () => {
const driverSpy = spy(neo4j, 'driver');
const connection = new Connection(neo4jUrl, neo4jCredentials);
expect(driverSpy.calledOnce);
await connection.close();
driverSpy.restore();
});
it('should accept a custom driver constructor function', async () => {
const constructorSpy = spy(driverConstructor);
const connection = new Connection(neo4jUrl, neo4jCredentials, constructorSpy);
expect(constructorSpy.calledOnce).to.equal(true);
expect(constructorSpy.firstCall.args[0]).to.equal(neo4jUrl);
await connection.close();
});
it('should pass driver options to the driver constructor', async () => {
const constructorSpy = spy(driverConstructor);
const driverConfig = { maxConnectionPoolSize: 5 };
const connection = new Connection(neo4jUrl, neo4jCredentials, {
driverConfig,
driverConstructor: constructorSpy,
});
expect(constructorSpy.calledOnce).to.equal(true);
expect(constructorSpy.firstCall.args[0]).to.equal(neo4jUrl);
expect(constructorSpy.firstCall.args[2]).to.deep.equal(driverConfig);
await connection.close();
});
});
describe('#close', () => {
it('should close the driver', async () => {
await connection.close();
expect(driverCloseSpy.calledOnce);
});
it('should only close the driver once', async () => {
await connection.close();
await connection.close();
expect(driverCloseSpy.calledOnce);
});
});
describe('#session', () => {
it('should use the driver to create a session', () => {
connection.session();
expect(driverSessionStub.calledOnce);
});
it('should return null if the connection has been closed', async () => {
await connection.close();
const result = connection.session();
expect(driverSessionStub.notCalled);
expect(result).to.equal(null);
});
});
describe('#run', () => {
it('should reject if there are no clauses in the query', async () => {
const promise = connection.run(connection.query());
await expect(promise).to.be.rejectedWith(Error, 'no clauses');
});
it('should reject if the connection has been closed', async () => {
await connection.close();
const promise = connection.run(connection.query().return('1'));
await expect(promise).to.be.rejectedWith(Error, 'connection is not open');
});
it('should reject if a session cannot be opened', async () => {
const connectionSessionStub = stub(connection, 'session').returns(null);
const promise = connection.run(connection.query().return('1'));
await expect(promise).to.be.rejectedWith(Error, 'connection is not open');
connectionSessionStub.restore();
});
it('should run the query through a session', async () => {
const params = {};
const query = new Query().raw('RETURN 1', params);
const promise = connection.run(query);
await expect(promise).to.be.fulfilled.then(() => {
expect(sessionRunSpy.calledOnce);
expect(sessionRunSpy.calledWith('RETURN 1', params));
});
});
it('should close the session after running a query', async () => {
const promise = connection.run((new Query()).raw('RETURN 1'));
await expect(promise).to.be.fulfilled
.then(() => expect(sessionCloseSpy.calledOnce));
});
it('should close the session when run() throws', async () => {
const promise = connection.run((new Query()).raw('RETURN a'));
await expect(promise).to.be.rejectedWith(Error)
.then(() => expect(sessionCloseSpy.calledOnce));
});
describe('when session.close throws', async () => {
const message = 'Fake error';
let sessionCloseStub: SinonStubFor<Session['close']>;
beforeEach(() => {
stubSession.resetBehavior();
stubSession.callsFake((session) => {
sessionCloseStub = stub(session, 'close').throws(new Error(message));
});
});
it('the error should bubble up', async () => {
const promise = connection.run(new Query().raw('RETURN 1'));
await expect(promise).to.be.rejectedWith(Error, message);
});
it('does not call session.close again', async () => {
try {
await connection.run(new Query().raw('RETURN a'));
} catch (e) {}
expect(sessionCloseStub.calledOnce);
});
});
});
describe('stream', () => {
const params = {};
const query = new Query().matchNode('n', 'TestStreamRecord').return('n');
const records = [
{ number: 1 },
{ number: 2 },
{ number: 3 },
];
before('setup session run return value', async () => {
const connection = new Connection(neo4jUrl, neo4jCredentials);
await connection
.unwind(records, 'map')
.createNode('n', 'TestStreamRecord')
.setVariables({ n: 'map' })
.run();
await connection.close();
});
after('clear the database', async () => {
const connection = new Connection(neo4jUrl, neo4jCredentials);
await connection
.matchNode('n', 'TestStreamRecord')
.delete('n')
.run();
await connection.close();
});
it('should return errored observable if there are no clauses in the query', () => {
const observable = connection.stream(connection.query());
expect(observable).to.be.an.instanceOf(Observable);
observable.subscribe({
next: () => expect.fail(null, null, 'Observable should not emit anything'),
error(error) {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.include('no clauses');
},
complete: () => expect.fail(null, null, 'Observable should not complete successfully'),
});
});
it('should return errored observable if the connection has been closed', async () => {
await connection.close();
const observable = connection.stream(new Query().return('1'));
expect(observable).to.be.an.instanceOf(Observable);
observable.subscribe({
next: () => expect.fail(null, null, 'Observable should not emit anything'),
error(error) {
expect(error).to.be.instanceOf(Error);
expect(error.message).to.include('connection is not open');
},
complete: () => expect.fail(null, null, 'Observable should not complete successfully'),
});
});
it('should run the query through a session', () => {
const observable = connection.stream<Node>(query);
expect(observable).to.be.an.instanceOf(Observable);
let count = 0;
return observable.pipe(
tap((row) => {
expect(row.n.properties).to.deep.equal(records[count]);
expect(row.n.labels).to.deep.equal(['TestStreamRecord']);
count += 1;
}),
)
.toPromise()
.then(() => {
expect(count).to.equal(records.length);
expect(sessionRunSpy.calledOnce);
expect(sessionRunSpy.calledWith(query.build(), params));
});
});
it('should close the session after running a query', () => {
const observable = connection.stream(query);
expect(observable).to.be.an.instanceOf(Observable);
return observable.toPromise().then(() => expect(sessionCloseSpy.calledOnce));
});
it('should close the session when run() throws', (done) => {
const query = connection.query().return('a');
const observable = connection.stream(query);
expect(observable).to.be.an.instanceOf(Observable);
observable.subscribe({
next: () => expect.fail(null, null, 'Observable should not emit any items'),
error() {
expect(sessionCloseSpy.calledOnce);
done();
},
complete: () => expect.fail(null, null, 'Observable should not complete without an error'),
});
});
});
describe('query methods', () => {
const methods: Dictionary<Function> = {
create: () => connection.create(new NodePattern('Node')),
createNode: () => connection.createNode('Node'),
createUnique: () => connection.createUnique(new NodePattern('Node')),
createUniqueNode: () => connection.createUniqueNode('Node'),
delete: () => connection.delete('node'),
detachDelete: () => connection.detachDelete('node'),
limit: () => connection.limit(1),
match: () => connection.match(new NodePattern('Node')),
matchNode: () => connection.matchNode('Node'),
merge: () => connection.merge(new NodePattern('Node')),
onCreateSet: () => connection.onCreate.set({}, { merge: false }),
onCreateSetLabels: () => connection.onCreate.setLabels({}),
onCreateSetValues: () => connection.onCreate.setValues({}),
onCreateSetVariables: () => connection.onCreate.setVariables({}, false),
onMatchSet: () => connection.onMatch.set({}, { merge: false }),
onMatchSetLabels: () => connection.onMatch.setLabels({}),
onMatchSetValues: () => connection.onMatch.setValues({}),
onMatchSetVariables: () => connection.onMatch.setVariables({}, false),
optionalMatch: () => connection.optionalMatch(new NodePattern('Node')),
orderBy: () => connection.orderBy('name'),
query: () => connection.query(),
raw: () => connection.raw('name'),
remove: () => connection.remove({ properties: { node: ['prop1', 'prop2'] } }),
removeProperties: () => connection.removeProperties({ node: ['prop1', 'prop2'] }),
removeLabels: () => connection.removeLabels({ node: 'label' }),
return: () => connection.return('node'),
returnDistinct: () => connection.returnDistinct('node'),
set: () => connection.set({}, { merge: false }),
setLabels: () => connection.setLabels({}),
setValues: () => connection.setValues({}),
setVariables: () => connection.setVariables({}, false),
skip: () => connection.skip(1),
unwind: () => connection.unwind([1, 2, 3], 'number'),
where: () => connection.where([]),
with: () => connection.with('node'),
};
each(methods, (fn, name) => {
it(`${name} should return a query object`, () => {
expect(fn()).to.be.an.instanceof(Query);
});
});
});
});