Skip to content

Commit a758662

Browse files
authored
Merge pull request #288 from ali-ince/1.4-check-for-empty-statement
check if statement text is empty on Session.run and Transaction.run
2 parents 548cbec + 0fe3760 commit a758662

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

src/v1/internal/util.js

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ function assertString(obj, objName) {
5858
return obj;
5959
}
6060

61+
function assertCypherStatement(obj) {
62+
assertString(obj, 'Cypher statement');
63+
if (obj.trim().length == 0) {
64+
throw new TypeError('Cypher statement is expected to be a non-empty string.');
65+
}
66+
return obj;
67+
}
68+
6169
function isString(str) {
6270
return Object.prototype.toString.call(str) === '[object String]';
6371
}
@@ -118,6 +126,7 @@ export {
118126
isEmptyObjectOrNull,
119127
isString,
120128
assertString,
129+
assertCypherStatement,
121130
parseScheme,
122131
parseUrl,
123132
parseHost,

src/v1/session.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import StreamObserver from './internal/stream-observer';
2020
import Result from './result';
2121
import Transaction from './transaction';
2222
import {newError} from './error';
23-
import {assertString} from './internal/util';
23+
import {assertCypherStatement} from './internal/util';
2424
import ConnectionHolder from './internal/connection-holder';
2525
import Driver, {READ, WRITE} from './driver';
2626
import TransactionExecutor from './internal/transaction-executor';
@@ -53,7 +53,7 @@ class Session {
5353

5454
/**
5555
* Run Cypher statement
56-
* Could be called with a statement object i.e.: {statement: "MATCH ...", parameters: {param: 1}}
56+
* Could be called with a statement object i.e.: {text: "MATCH ...", parameters: {param: 1}}
5757
* or with the statement and parameters as separate arguments.
5858
* @param {mixed} statement - Cypher statement to execute
5959
* @param {Object} parameters - Map with parameters to use in statement
@@ -64,7 +64,7 @@ class Session {
6464
parameters = statement.parameters || {};
6565
statement = statement.text;
6666
}
67-
assertString(statement, 'Cypher statement');
67+
assertCypherStatement(statement);
6868

6969
return this._run(statement, parameters, (connection, streamObserver) =>
7070
connection.run(statement, parameters, streamObserver)

src/v1/transaction.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
import StreamObserver from './internal/stream-observer';
2020
import Result from './result';
21-
import {assertString} from './internal/util';
21+
import {assertCypherStatement} from './internal/util';
2222
import {EMPTY_CONNECTION_HOLDER} from './internal/connection-holder';
2323
import Bookmark from './internal/bookmark';
2424

@@ -53,7 +53,7 @@ class Transaction {
5353

5454
/**
5555
* Run Cypher statement
56-
* Could be called with a statement object i.e.: <code>{statement: "MATCH ...", parameters: {param: 1}}</code>
56+
* Could be called with a statement object i.e.: <code>{text: "MATCH ...", parameters: {param: 1}}</code>
5757
* or with the statement and parameters as separate arguments.
5858
* @param {mixed} statement - Cypher statement to execute
5959
* @param {Object} parameters - Map with parameters to use in statement
@@ -64,7 +64,7 @@ class Transaction {
6464
parameters = statement.parameters || {};
6565
statement = statement.text;
6666
}
67-
assertString(statement, "Cypher statement");
67+
assertCypherStatement(statement);
6868

6969
return this._state.run(this._connectionHolder, new _TransactionStreamObserver(this), statement, parameters);
7070
}

test/internal/util.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ describe('util', () => {
5555
verifyInvalidString(console.log);
5656
});
5757

58+
it('should check cypher statements (non-empty strings)', () => {
59+
verifyValidString(new String('foo'));
60+
verifyValidString(String('foo'));
61+
verifyValidString("foo");
62+
63+
verifyInvalidCypherStatement('');
64+
verifyInvalidCypherStatement('\n');
65+
verifyInvalidCypherStatement('\t');
66+
verifyInvalidCypherStatement('\r');
67+
verifyInvalidCypherStatement(' ');
68+
verifyInvalidCypherStatement(' \n\r');
69+
verifyInvalidCypherStatement({});
70+
verifyInvalidCypherStatement({foo: 1});
71+
verifyInvalidCypherStatement([]);
72+
verifyInvalidCypherStatement(['1']);
73+
verifyInvalidCypherStatement([1, '2']);
74+
verifyInvalidCypherStatement(console.log);
75+
});
76+
5877
it('should parse scheme', () => {
5978
verifyScheme('bolt://', 'bolt://localhost');
6079
verifyScheme('bolt://', 'bolt://localhost:7687');
@@ -169,6 +188,14 @@ describe('util', () => {
169188
expect(() => util.assertString(str, 'Test string')).toThrowError(TypeError);
170189
}
171190

191+
function verifyValidCypherStatement(str) {
192+
expect(util.assertCypherStatement(str)).toBe(str);
193+
}
194+
195+
function verifyInvalidCypherStatement(str) {
196+
expect(() => util.assertCypherStatement(str)).toThrowError(TypeError);
197+
}
198+
172199
function verifyScheme(expectedScheme, url) {
173200
expect(util.parseScheme(url)).toEqual(expectedScheme);
174201
}

test/v1/session.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ describe('session', () => {
427427
expect(() => session.run({})).toThrowError(TypeError);
428428
expect(() => session.run(42)).toThrowError(TypeError);
429429
expect(() => session.run([])).toThrowError(TypeError);
430+
expect(() => session.run('')).toThrowError(TypeError);
430431
expect(() => session.run(['CREATE ()'])).toThrowError(TypeError);
431432

432433
expect(() => session.run({statement: 'CREATE ()'})).toThrowError(TypeError);

0 commit comments

Comments
 (0)