Skip to content

Commit f54bdde

Browse files
committed
addressed review comments and ignored whitespaces during checks
1 parent edafb7d commit f54bdde

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
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

+2-5
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';
@@ -64,10 +64,7 @@ class Session {
6464
parameters = statement.parameters || {};
6565
statement = statement.text;
6666
}
67-
assertString(statement, 'Cypher statement');
68-
if (statement.length == 0) {
69-
throw new TypeError('Cypher statement is expected to be a non-empty string.');
70-
}
67+
assertCypherStatement(statement);
7168

7269
return this._run(statement, parameters, (connection, streamObserver) =>
7370
connection.run(statement, parameters, streamObserver)

src/v1/transaction.js

+2-5
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

@@ -64,10 +64,7 @@ class Transaction {
6464
parameters = statement.parameters || {};
6565
statement = statement.text;
6666
}
67-
assertString(statement, "Cypher statement");
68-
if (statement.length == 0) {
69-
throw new TypeError('Cypher statement is expected to be a non-empty string.');
70-
}
67+
assertCypherStatement(statement);
7168

7269
return this._state.run(this._connectionHolder, new _TransactionStreamObserver(this), statement, parameters);
7370
}

test/internal/util.test.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import * as util from '../../src/v1/internal/util';
2121

22-
describe('util', () => {
22+
fdescribe('util', () => {
2323

2424
it('should check empty objects', () => {
2525
expect(util.isEmptyObjectOrNull(null)).toBeTruthy();
@@ -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
}

0 commit comments

Comments
 (0)