@@ -21,28 +21,39 @@ var neo4j = require("../../lib/v1");
21
21
var StatementType = require ( "../../lib/v1/result-summary" ) . statementType ;
22
22
23
23
describe ( 'session' , function ( ) {
24
+
25
+ var driver , session ;
26
+
27
+ beforeEach ( function ( done ) {
28
+ driver = neo4j . driver ( "bolt://localhost" ) ;
29
+ session = driver . session ( ) ;
30
+
31
+ session . run ( "MATCH (n) DETACH DELETE n" ) . then ( done ) ;
32
+ } ) ;
33
+
34
+ afterEach ( function ( ) {
35
+ driver . close ( ) ;
36
+ } ) ;
37
+
24
38
it ( 'should expose basic run/subscribe ' , function ( done ) {
25
39
// Given
26
- var driver = neo4j . driver ( "bolt://localhost" ) ;
27
40
28
41
// When & Then
29
42
var records = [ ] ;
30
- driver . session ( ) . run ( "RETURN 1.0 AS a" ) . subscribe ( {
43
+ session . run ( "RETURN 1.0 AS a" ) . subscribe ( {
31
44
onNext : function ( record ) {
32
45
records . push ( record ) ;
33
46
} ,
34
47
onCompleted : function ( ) {
35
48
expect ( records . length ) . toBe ( 1 ) ;
36
49
expect ( records [ 0 ] [ 'a' ] ) . toBe ( 1 ) ;
37
- driver . close ( ) ;
38
50
done ( ) ;
39
51
}
40
52
} ) ;
41
53
} ) ;
42
54
43
55
it ( 'should keep context in subscribe methods ' , function ( done ) {
44
56
// Given
45
- var driver = neo4j . driver ( "bolt://localhost" ) ;
46
57
function myObserver ( ) {
47
58
this . local = 'hello' ;
48
59
var privateLocal = 'hello' ;
@@ -53,55 +64,47 @@ describe('session', function() {
53
64
this . onCompleted = function ( ) {
54
65
expect ( privateLocal ) . toBe ( 'hello' ) ;
55
66
expect ( this . local ) . toBe ( 'hello' ) ;
56
- driver . close ( ) ;
57
67
done ( ) ;
58
68
}
59
69
}
60
70
61
71
// When & Then
62
- driver . session ( ) . run ( "RETURN 1.0 AS a" ) . subscribe ( new myObserver ( ) ) ;
72
+ session . run ( "RETURN 1.0 AS a" ) . subscribe ( new myObserver ( ) ) ;
63
73
} ) ;
64
74
65
75
it ( 'should call observers onError on error ' , function ( done ) {
66
- // Given
67
- var driver = neo4j . driver ( "bolt://localhost" ) ;
68
76
69
77
// When & Then
70
78
var records = [ ] ;
71
- driver . session ( ) . run ( "RETURN 1 AS" ) . subscribe ( {
79
+ session . run ( "RETURN 1 AS" ) . subscribe ( {
72
80
onError : function ( error ) {
73
81
expect ( error . fields . length ) . toBe ( 1 ) ;
74
- driver . close ( ) ;
75
82
done ( ) ;
76
83
}
77
84
} ) ;
78
85
} ) ;
79
86
80
87
it ( 'should accept a statement object ' , function ( done ) {
81
88
// Given
82
- var driver = neo4j . driver ( "bolt://localhost" ) ;
83
89
var statement = { text : "RETURN 1 = {param} AS a" , parameters : { param : 1 } } ;
84
90
85
91
// When & Then
86
92
var records = [ ] ;
87
- driver . session ( ) . run ( statement ) . subscribe ( {
93
+ session . run ( statement ) . subscribe ( {
88
94
onNext : function ( record ) {
89
95
records . push ( record ) ;
90
96
} ,
91
97
onCompleted : function ( ) {
92
98
expect ( records . length ) . toBe ( 1 ) ;
93
99
expect ( records [ 0 ] [ 'a' ] ) . toBe ( true ) ;
94
- driver . close ( ) ;
95
100
done ( ) ;
96
101
}
97
102
} ) ;
98
103
} ) ;
99
104
100
105
it ( 'should expose basic run/then/then/then ' , function ( done ) {
101
- // Given
102
- var driver = neo4j . driver ( "bolt://localhost" ) ;
103
106
// When & Then
104
- driver . session ( ) . run ( "RETURN 1.0 AS a" )
107
+ session . run ( "RETURN 1.0 AS a" )
105
108
. then (
106
109
function ( records ) {
107
110
expect ( records . length ) . toBe ( 1 ) ;
@@ -112,48 +115,42 @@ describe('session', function() {
112
115
expect ( records . length ) . toBe ( 1 ) ;
113
116
expect ( records [ 0 ] [ 'a' ] ) . toBe ( 1 ) ;
114
117
}
115
- ) . then ( function ( ) { driver . close ( ) ; done ( ) ; } )
118
+ ) . then ( function ( ) { done ( ) ; } )
116
119
} ) ;
117
120
118
121
it ( 'should expose basic run/catch ' , function ( done ) {
119
- // Given
120
- var driver = neo4j . driver ( "bolt://localhost" ) ;
121
122
// When & Then
122
- driver . session ( ) . run ( "RETURN 1 AS" ) . catch (
123
+ session . run ( "RETURN 1 AS" ) . catch (
123
124
function ( error ) {
124
125
expect ( error . fields . length ) . toBe ( 1 ) ;
125
- driver . close ( ) ;
126
126
done ( ) ;
127
127
}
128
128
)
129
129
} ) ;
130
130
131
131
it ( 'should expose summarize method for basic metadata ' , function ( done ) {
132
132
// Given
133
- var driver = neo4j . driver ( "bolt://localhost" ) ;
134
133
var statement = "CREATE (n:Label {prop:{prop}}) RETURN n" ;
135
134
var params = { prop : "string" }
136
135
// When & Then
137
- driver . session ( ) . run ( statement , params )
136
+ session . run ( statement , params )
138
137
. then ( function ( result ) {
139
138
var sum = result . summary ;
140
139
expect ( sum . statement . text ) . toBe ( statement ) ;
141
140
expect ( sum . statement . parameters ) . toBe ( params ) ;
142
141
expect ( sum . updateStatistics . containsUpdates ( ) ) . toBe ( true ) ;
143
142
expect ( sum . updateStatistics . nodesCreated ( ) ) . toBe ( 1 ) ;
144
143
expect ( sum . statementType ) . toBe ( StatementType . READ_WRITE ) ;
145
- driver . close ( ) ;
146
144
done ( ) ;
147
145
} ) ;
148
146
} ) ;
149
147
150
148
it ( 'should expose plan ' , function ( done ) {
151
149
// Given
152
- var driver = neo4j . driver ( "bolt://localhost" ) ;
153
150
var statement = "EXPLAIN CREATE (n:Label {prop:{prop}}) RETURN n" ;
154
151
var params = { prop : "string" }
155
152
// When & Then
156
- driver . session ( )
153
+ session
157
154
. run ( statement , params )
158
155
. then ( function ( result ) {
159
156
var sum = result . summary ;
@@ -163,18 +160,16 @@ describe('session', function() {
163
160
expect ( sum . plan . arguments . runtime ) . toBe ( 'INTERPRETED' ) ;
164
161
expect ( sum . plan . identifiers [ 0 ] ) . toBe ( 'n' ) ;
165
162
expect ( sum . plan . children [ 0 ] . operatorType ) . toBe ( 'CreateNode' ) ;
166
- driver . close ( ) ;
167
163
done ( ) ;
168
164
} ) ;
169
165
} ) ;
170
166
171
167
it ( 'should expose profile ' , function ( done ) {
172
168
// Given
173
- var driver = neo4j . driver ( "bolt://localhost" ) ;
174
169
var statement = "PROFILE MATCH (n:Label {prop:{prop}}) RETURN n" ;
175
170
var params = { prop : "string" }
176
171
// When & Then
177
- driver . session ( )
172
+ session
178
173
. run ( statement , params )
179
174
. then ( function ( result ) {
180
175
var sum = result . summary ;
@@ -184,34 +179,29 @@ describe('session', function() {
184
179
expect ( sum . profile . arguments . runtime ) . toBe ( 'INTERPRETED' ) ;
185
180
expect ( sum . profile . identifiers [ 0 ] ) . toBe ( 'n' ) ;
186
181
expect ( sum . profile . children [ 0 ] . operatorType ) . toBe ( 'Filter' ) ;
187
- expect ( sum . profile . rows ) . toBeGreaterThan ( 0 ) ;
182
+ expect ( sum . profile . rows ) . toBe ( 0 ) ;
188
183
//expect(sum.profile.dbHits).toBeGreaterThan(0);
189
- driver . close ( ) ;
190
184
done ( ) ;
191
185
} ) ;
192
186
} ) ;
193
187
194
188
it ( 'should expose cypher notifications ' , function ( done ) {
195
189
// Given
196
- var driver = neo4j . driver ( "bolt://localhost" ) ;
197
190
var statement = "EXPLAIN MATCH (n), (m) RETURN n, m" ;
198
191
// When & Then
199
- driver . session ( )
192
+ session
200
193
. run ( statement )
201
194
. then ( function ( result ) {
202
195
var sum = result . summary ;
203
196
expect ( sum . notifications . length ) . toBeGreaterThan ( 0 ) ;
204
197
expect ( sum . notifications [ 0 ] . code ) . toBe ( "Neo.ClientNotification.Statement.CartesianProduct" ) ;
205
198
expect ( sum . notifications [ 0 ] . title ) . toBe ( "This query builds a cartesian product between disconnected patterns." ) ;
206
199
expect ( sum . notifications [ 0 ] . position . column ) . toBeGreaterThan ( 0 ) ;
207
- driver . close ( ) ;
208
200
done ( ) ;
209
201
} ) ;
210
202
} ) ;
211
203
212
204
it ( 'should fail when using the session when having an open transaction' , function ( done ) {
213
- // Given
214
- var session = neo4j . driver ( "bolt://localhost" ) . session ( ) ;
215
205
216
206
// When
217
207
session . beginTransaction ( ) ;
@@ -220,14 +210,12 @@ describe('session', function() {
220
210
session . run ( "RETURN 42" )
221
211
. catch ( function ( error ) {
222
212
expect ( error . error ) . toBe ( "Please close the currently open transaction object before running " +
223
- "more statements/transactions in the current session." )
213
+ "more statements/transactions in the current session." ) ;
224
214
done ( ) ;
225
215
} )
226
216
} ) ;
227
217
228
218
it ( 'should fail when opening multiple transactions' , function ( ) {
229
- // Given
230
- var session = neo4j . driver ( "bolt://localhost" ) . session ( ) ;
231
219
232
220
// When
233
221
session . beginTransaction ( ) ;
0 commit comments