Skip to content

Commit 11bf0ef

Browse files
committed
adding tests for new method helpers.concat
1 parent 4db4e8a commit 11bf0ef

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

lib/helpers/methods/concat.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ var $arr = require('../../array');
2323
* - a {@link QueryFile} object
2424
*
2525
* @returns {string}
26-
* Resulting query string.
26+
* Concatenated string with all queries.
2727
*
2828
*/
29-
// istanbul ignore next;
3029
function concat(queries) {
3130
if (!Array.isArray(queries)) {
3231
throw new TypeError("Parameter 'queries' must be an array.");
@@ -50,7 +49,6 @@ function concat(queries) {
5049
}).join(';');
5150
}
5251

53-
// istanbul ignore next;
5452
function clean(q) {
5553
// removes from the query all leading and trailing symbols ' ', '\t' and ';'
5654
return q.replace(/^[\s;]*|[\s;]*$/g, '');

test/helpSpec.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ var options = {
55
};
66

77
var os = require('os');
8+
var path = require('path');
89
var utils = require('../lib/utils');
910
var header = require('./db/header');
10-
var helpers = header(options).pgp.helpers;
11+
var pgp = header(options).pgp;
12+
var helpers = pgp.helpers;
13+
var QueryFile = pgp.QueryFile;
1114

1215
var dataSingle = {
1316
val: 123,
@@ -832,3 +835,42 @@ describe("method 'values'", function () {
832835
});
833836
});
834837

838+
describe("method 'concat'", function () {
839+
describe("Negative", function () {
840+
it("must throw on a non-array input", function () {
841+
var error = new TypeError("Parameter 'queries' must be an array.");
842+
expect(function () {
843+
helpers.concat();
844+
}).toThrow(error);
845+
expect(function () {
846+
helpers.concat(123);
847+
}).toThrow(error);
848+
});
849+
it("must throw on invalid elements inside array", function () {
850+
var error = new Error("Invalid query element at index 0.");
851+
expect(function () {
852+
helpers.concat([1]);
853+
}).toThrow(error);
854+
expect(function () {
855+
helpers.concat([{}]);
856+
}).toThrow(error);
857+
});
858+
});
859+
860+
describe("Positive", function () {
861+
describe("with simple strings", function () {
862+
it("must allow an empty array", function () {
863+
expect(helpers.concat([])).toBe('');
864+
});
865+
it("must remove symbols correctly", function () {
866+
expect(helpers.concat(['one', 'two'])).toBe('one;two'); // normal
867+
expect(helpers.concat(['\t;;one;\t;', ' two ;;\t\t'])).toBe('one;two');
868+
expect(helpers.concat(['\t;;one;\t;here ', ' two ;;\t\t'])).toBe('one;\t;here;two');
869+
});
870+
it("must support mixed types correctly", function () {
871+
var qf = new QueryFile(path.join(__dirname, './sql/allUsers.sql'), {minify: true});
872+
expect(helpers.concat(['one', {query: 'two'}, qf])).toBe('one;two;select * from users');
873+
});
874+
});
875+
});
876+
});

0 commit comments

Comments
 (0)