19
19
20
20
var neo4jv1 = require ( "../../lib/v1" ) ;
21
21
22
- var _console = console ;
23
-
24
22
/**
25
23
* The tests below are examples that get pulled into the Driver Manual using the tags inside the tests.
26
24
*
@@ -29,7 +27,12 @@ var _console = console;
29
27
*/
30
28
describe ( 'examples' , function ( ) {
31
29
32
- var driverGlobal , out , console , originalTimeout ;
30
+ var driverGlobal ;
31
+ var console ;
32
+ var originalTimeout ;
33
+
34
+ var testResultPromise ;
35
+ var resolveTestResultPromise ;
33
36
34
37
beforeAll ( function ( ) {
35
38
var neo4j = neo4jv1 ;
@@ -44,9 +47,13 @@ describe('examples', function() {
44
47
45
48
beforeEach ( function ( done ) {
46
49
50
+ testResultPromise = new Promise ( function ( resolve , reject ) {
51
+ resolveTestResultPromise = resolve ;
52
+ } ) ;
53
+
47
54
// Override console.log, to assert on stdout output
48
- out = [ ] ;
49
- console = { log : function ( msg ) { out . push ( msg ) ; } } ;
55
+ console = { log : resolveTestResultPromise } ;
56
+
50
57
var session = driverGlobal . session ( ) ;
51
58
session . run ( "MATCH (n) DETACH DELETE n" ) . then ( function ( ) {
52
59
session . close ( ) ;
@@ -81,12 +88,13 @@ describe('examples', function() {
81
88
console . log ( result . records [ 0 ] . get ( "title" ) + " " + result . records [ 0 ] . get ( "name" ) ) ;
82
89
session . close ( ) ;
83
90
driver . close ( ) ;
84
- } )
85
- // end::minimal-example[]
86
- . then ( function ( ) {
87
- expect ( out [ 0 ] ) . toBe ( "King Arthur" ) ;
88
- done ( ) ;
89
91
} ) ;
92
+ // end::minimal-example[]
93
+
94
+ testResultPromise . then ( function ( loggedMsg ) {
95
+ expect ( loggedMsg ) . toBe ( "King Arthur" ) ;
96
+ done ( ) ;
97
+ } ) ;
90
98
} ) ;
91
99
92
100
it ( 'should be able to configure session pool size' , function ( done ) {
@@ -102,10 +110,11 @@ describe('examples', function() {
102
110
console . log ( theOnesCreated ) ;
103
111
s . close ( ) ;
104
112
driver . close ( ) ;
105
- } )
106
- . then ( function ( ) {
107
- expect ( out [ 0 ] ) . toBe ( 1 ) ;
108
- done ( ) ;
113
+ } ) ;
114
+
115
+ testResultPromise . then ( function ( loggedCount ) {
116
+ expect ( loggedCount ) . toBe ( 1 ) ;
117
+ done ( ) ;
109
118
} ) ;
110
119
} ) ;
111
120
@@ -119,11 +128,12 @@ describe('examples', function() {
119
128
var theOnesCreated = result . summary . counters . nodesCreated ( ) ;
120
129
console . log ( "There were " + theOnesCreated + " the ones created." ) ;
121
130
session . close ( ) ;
122
- } )
123
- . then ( function ( ) {
124
- expect ( out [ 0 ] ) . toBe ( "There were 1 the ones created." ) ;
125
- done ( ) ;
126
131
} ) ;
132
+
133
+ testResultPromise . then ( function ( loggedMsg ) {
134
+ expect ( loggedMsg ) . toBe ( "There were 1 the ones created." ) ;
135
+ done ( ) ;
136
+ } ) ;
127
137
} ) ;
128
138
129
139
it ( 'should document a statement without parameters' , function ( done ) {
@@ -139,10 +149,10 @@ describe('examples', function() {
139
149
} ) ;
140
150
141
151
// Then
142
- setTimeout ( function ( ) {
143
- expect ( out [ 0 ] ) . toBe ( "There were 1 the ones created." ) ;
152
+ testResultPromise . then ( function ( loggedMsg ) {
153
+ expect ( loggedMsg ) . toBe ( "There were 1 the ones created." ) ;
144
154
done ( ) ;
145
- } , 1000 )
155
+ } ) ;
146
156
} ) ;
147
157
148
158
it ( 'should be able to iterate results' , function ( done ) {
@@ -167,11 +177,12 @@ describe('examples', function() {
167
177
} ) ;
168
178
// end::result-traversal[]
169
179
} ) ;
180
+
170
181
// Then
171
- setTimeout ( function ( ) {
172
- expect ( out [ 0 ] ) . toBe ( "Sword in the stone" ) ;
182
+ testResultPromise . then ( function ( loggedMsg ) {
183
+ expect ( loggedMsg ) . toBe ( "Sword in the stone" ) ;
173
184
done ( ) ;
174
- } , 1000 ) ;
185
+ } ) ;
175
186
} ) ;
176
187
177
188
it ( 'should be able to access records' , function ( done ) {
@@ -205,10 +216,10 @@ describe('examples', function() {
205
216
} ) ;
206
217
207
218
// Then
208
- setTimeout ( function ( ) {
209
- expect ( out [ 0 ] . length ) . toBe ( 3 ) ;
219
+ testResultPromise . then ( function ( loggedCount ) {
220
+ expect ( loggedCount . length ) . toBe ( 3 ) ;
210
221
done ( ) ;
211
- } , 1000 )
222
+ } ) ;
212
223
} ) ;
213
224
214
225
it ( 'should be able to retain for later processing' , function ( done ) {
@@ -238,15 +249,14 @@ describe('examples', function() {
238
249
// end::retain-result[]
239
250
} ) ;
240
251
241
- //await the result
242
- setTimeout ( function ( ) {
243
- expect ( out [ 0 ] ) . toBe ( "Lancelot is a knight of Camelot" ) ;
252
+ testResultPromise . then ( function ( loggedMsg ) {
253
+ expect ( loggedMsg ) . toBe ( "Lancelot is a knight of Camelot" ) ;
244
254
done ( ) ;
245
- } , 1000 ) ;
255
+ } ) ;
246
256
} ) ;
247
257
248
258
it ( 'should be able to do nested queries' , function ( done ) {
249
- var session = driverGlobal . session ( ) ; ;
259
+ var session = driverGlobal . session ( ) ;
250
260
session
251
261
. run ( "CREATE (knight:Person:Knight {name: {name1}, castle: {castle}})" +
252
262
"CREATE (king:Person {name: {name2}, title: {title}})" ,
@@ -277,25 +287,28 @@ describe('examples', function() {
277
287
// end::nested-statements[]
278
288
} ) ;
279
289
280
- //await the result
281
- setTimeout ( function ( ) {
282
- expect ( out [ 0 ] ) . toBe ( "Count is 1" ) ;
290
+ testResultPromise . then ( function ( loggedMsg ) {
291
+ expect ( loggedMsg ) . toBe ( "Count is 1" ) ;
283
292
done ( ) ;
284
- } , 1000 ) ;
293
+ } ) ;
285
294
} ) ;
286
295
287
296
it ( 'should be able to handle cypher error' , function ( done ) {
288
297
var session = driverGlobal . session ( ) ;
289
298
290
299
// tag::handle-cypher-error[]
291
300
session
292
- . run ( "Then will cause a syntax error" )
301
+ . run ( "This will cause a syntax error" )
293
302
. catch ( function ( err ) {
294
- expect ( err . fields [ 0 ] . code ) . toBe ( "Neo.ClientError.Statement.SyntaxError" ) ;
303
+ console . log ( err ) ;
295
304
session . close ( ) ;
296
- done ( ) ;
297
305
} ) ;
298
306
// end::handle-cypher-error[]
307
+
308
+ testResultPromise . then ( function ( loggedError ) {
309
+ expect ( loggedError . fields [ 0 ] . code ) . toBe ( "Neo.ClientError.Statement.SyntaxError" ) ;
310
+ done ( ) ;
311
+ } ) ;
299
312
} ) ;
300
313
301
314
it ( 'should be able to profile' , function ( done ) {
@@ -306,18 +319,16 @@ describe('examples', function() {
306
319
session
307
320
. run ( "PROFILE MATCH (p:Person {name: {name}}) RETURN id(p)" , { name : "Arthur" } )
308
321
. then ( function ( result ) {
309
- //_console.log(result.summary.profile);
310
322
console . log ( result . summary . profile ) ;
311
323
session . close ( ) ;
312
324
} ) ;
313
325
// end::result-summary-query-profile[]
314
326
} ) ;
315
327
316
- //await the result
317
- setTimeout ( function ( ) {
318
- expect ( out . length ) . toBe ( 1 ) ;
328
+ testResultPromise . then ( function ( loggedMsg ) {
329
+ expect ( loggedMsg ) . toBeDefined ( ) ;
319
330
done ( ) ;
320
- } , 2000 ) ;
331
+ } ) ;
321
332
} ) ;
322
333
323
334
it ( 'should be able to see notifications' , function ( done ) {
@@ -335,10 +346,10 @@ describe('examples', function() {
335
346
} ) ;
336
347
// end::result-summary-notifications[]
337
348
338
- setTimeout ( function ( ) {
339
- expect ( out [ 0 ] ) . toBe ( "Neo.ClientNotification.Statement.CartesianProductWarning" ) ;
349
+ testResultPromise . then ( function ( loggedMsg ) {
350
+ expect ( loggedMsg ) . toBe ( "Neo.ClientNotification.Statement.CartesianProductWarning" ) ;
340
351
done ( ) ;
341
- } , 1000 ) ;
352
+ } ) ;
342
353
} ) ;
343
354
344
355
it ( 'should document committing a transaction' , function ( ) {
@@ -352,7 +363,7 @@ describe('examples', function() {
352
363
} ) ;
353
364
354
365
it ( 'should document rolling back a transaction' , function ( ) {
355
- var session = driverGlobal . session ( ) ; ;
366
+ var session = driverGlobal . session ( ) ;
356
367
357
368
// tag::transaction-rollback[]
358
369
var tx = session . beginTransaction ( ) ;
0 commit comments