Skip to content

Commit 293a05e

Browse files
jakewinspontusmelke
authored andcommitted
Basic test for transactions
1 parent 1eff84f commit 293a05e

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

src/v1/internal/connector.js

+1
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ class Connection {
219219
}
220220

221221
_handleMessage( msg ) {
222+
222223
switch( msg.signature ) {
223224
case RECORD:
224225
this._currentObserver.onNext( msg.fields[0] );

src/v1/result.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,4 @@ class Result {
113113
}
114114
}
115115

116-
export default {
117-
Result
118-
}
116+
export default Result

src/v1/session.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
*/
1919

2020
import StreamObserver from './internal/stream-observer';
21-
import {Result} from './result';
21+
import Result from './result';
22+
import Transaction from './transaction';
2223

2324
/**
2425
* A Session instance is used for handling the connection and
@@ -40,7 +41,7 @@ class Session {
4041
/**
4142
* Run Cypher statement
4243
* Could be called with a statement object i.e.: {statement: "MATCH ...", parameters: {param: 1}}
43-
* or with the statement and parameters as separate arguments.
44+
* or with the statem ent and parameters as separate arguments.
4445
* @param {mixed} statement - Cypher statement to execute
4546
* @param {Object} parameters - Map with parameters to use in statement
4647
* @return {Result} - New Result
@@ -57,6 +58,10 @@ class Session {
5758
return new Result( streamObserver, statement, parameters );
5859
}
5960

61+
beginTransaction() {
62+
return new Transaction(this);
63+
}
64+
6065
/**
6166
* Close connection
6267
* @param {function()} cb - Function to be called on connection close

src/v1/transaction.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2002-2015 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
class Transaction {
21+
constructor( session ) {
22+
this._session = session;
23+
this._session.run("BEGIN");
24+
}
25+
26+
run(statement, parameters) {
27+
return this._session.run(statement, parameters)
28+
}
29+
30+
commit() {
31+
return this._session.run("COMMIT");
32+
}
33+
34+
}
35+
36+
export default Transaction;

test/v1/transaction.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) 2002-2015 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
var neo4j = require("../../lib/v1");
21+
var StatementType = require("../../lib/v1/result-summary").statementType;
22+
23+
fdescribe('transaction', 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+
it('should handle simple transaction', function(done) {
35+
// When
36+
var tx = session.beginTransaction();
37+
tx.run("CREATE (:TXNode1)");
38+
tx.run("CREATE (:TXNode2)");
39+
tx.commit()
40+
.then(function() {
41+
session.run("MATCH (t1:TXNode1), (t2:TXNode2) RETURN count(t1), count(t2)").then(function(records) {
42+
43+
expect( records.length ).toBe( 1 );
44+
expect( records[0]['count(t1)'].toInt() )
45+
.toBe( 1 );
46+
expect( records[0]['count(t2)'].toInt() )
47+
.toBe( 1 );
48+
done();
49+
});
50+
});
51+
});
52+
53+
});

0 commit comments

Comments
 (0)