Skip to content

check if statement text is empty on Session.run and Transaction.run #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/v1/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ function assertString(obj, objName) {
return obj;
}

function assertCypherStatement(obj) {
assertString(obj, 'Cypher statement');
if (obj.trim().length == 0) {
throw new TypeError('Cypher statement is expected to be a non-empty string.');
}
return obj;
}

function isString(str) {
return Object.prototype.toString.call(str) === '[object String]';
}
Expand Down Expand Up @@ -118,6 +126,7 @@ export {
isEmptyObjectOrNull,
isString,
assertString,
assertCypherStatement,
parseScheme,
parseUrl,
parseHost,
Expand Down
6 changes: 3 additions & 3 deletions src/v1/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import StreamObserver from './internal/stream-observer';
import Result from './result';
import Transaction from './transaction';
import {newError} from './error';
import {assertString} from './internal/util';
import {assertCypherStatement} from './internal/util';
import ConnectionHolder from './internal/connection-holder';
import Driver, {READ, WRITE} from './driver';
import TransactionExecutor from './internal/transaction-executor';
Expand Down Expand Up @@ -53,7 +53,7 @@ class Session {

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

return this._run(statement, parameters, (connection, streamObserver) =>
connection.run(statement, parameters, streamObserver)
Expand Down
6 changes: 3 additions & 3 deletions src/v1/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
import StreamObserver from './internal/stream-observer';
import Result from './result';
import {assertString} from './internal/util';
import {assertCypherStatement} from './internal/util';
import {EMPTY_CONNECTION_HOLDER} from './internal/connection-holder';
import Bookmark from './internal/bookmark';

Expand Down Expand Up @@ -53,7 +53,7 @@ class Transaction {

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

return this._state.run(this._connectionHolder, new _TransactionStreamObserver(this), statement, parameters);
}
Expand Down
27 changes: 27 additions & 0 deletions test/internal/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ describe('util', () => {
verifyInvalidString(console.log);
});

it('should check cypher statements (non-empty strings)', () => {
verifyValidString(new String('foo'));
verifyValidString(String('foo'));
verifyValidString("foo");

verifyInvalidCypherStatement('');
verifyInvalidCypherStatement('\n');
verifyInvalidCypherStatement('\t');
verifyInvalidCypherStatement('\r');
verifyInvalidCypherStatement(' ');
verifyInvalidCypherStatement(' \n\r');
verifyInvalidCypherStatement({});
verifyInvalidCypherStatement({foo: 1});
verifyInvalidCypherStatement([]);
verifyInvalidCypherStatement(['1']);
verifyInvalidCypherStatement([1, '2']);
verifyInvalidCypherStatement(console.log);
});

it('should parse scheme', () => {
verifyScheme('bolt://', 'bolt://localhost');
verifyScheme('bolt://', 'bolt://localhost:7687');
Expand Down Expand Up @@ -169,6 +188,14 @@ describe('util', () => {
expect(() => util.assertString(str, 'Test string')).toThrowError(TypeError);
}

function verifyValidCypherStatement(str) {
expect(util.assertCypherStatement(str)).toBe(str);
}

function verifyInvalidCypherStatement(str) {
expect(() => util.assertCypherStatement(str)).toThrowError(TypeError);
}

function verifyScheme(expectedScheme, url) {
expect(util.parseScheme(url)).toEqual(expectedScheme);
}
Expand Down
1 change: 1 addition & 0 deletions test/v1/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ describe('session', () => {
expect(() => session.run({})).toThrowError(TypeError);
expect(() => session.run(42)).toThrowError(TypeError);
expect(() => session.run([])).toThrowError(TypeError);
expect(() => session.run('')).toThrowError(TypeError);
expect(() => session.run(['CREATE ()'])).toThrowError(TypeError);

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