diff --git a/Makefile b/Makefile index 3535624..f221be0 100755 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ -#TESTS = test/mysql/*.js test/mssql/*.js +TESTS = test/mysql/*.js test/mssql/*.js #TESTS = test/mssql/*.js #TESTS = test/mssql/04-tests-query-response.js -TESTS = test/mssql/05-tests-multiple-queries.js +#TESTS = test/mssql/05-tests-multiple-queries.js #TESTS = test/mssql/03-tests-insert.js test: mocha --timeout 5000 --reporter spec $(TESTS) diff --git a/drivers/mssql/query_exec.js b/drivers/mssql/query_exec.js index ace6fe5..5d972e3 100755 --- a/drivers/mssql/query_exec.js +++ b/drivers/mssql/query_exec.js @@ -11,11 +11,7 @@ class QueryExec extends QueryBuilder { _exec(sql, cb) { if (Object.prototype.toString.call(this._connection) === Object.prototype.toString.call({})) { - console.log("The Query: ", sql); const request = new Request(sql, (err, count, results) => { - // console.log("Results:" , results); - // console.log("Count:" , count); - // Standardize some important properties if (!err && results && Array.isArray(results)) { if (results.length === 0) { diff --git a/test/05-multiple-drivers.js b/test/05-multiple-drivers.js new file mode 100755 index 0000000..e69de29 diff --git a/test/mssql/00-AA-tests-general.js b/test/mssql/00-AA-tests-general.js index ac6f502..ac5ad6d 100755 --- a/test/mssql/00-AA-tests-general.js +++ b/test/mssql/00-AA-tests-general.js @@ -1,6 +1,6 @@ const should = require('chai').should(); -describe('QueryBuilder', () => { +describe('MSSQL: QueryBuilder', () => { it('actually exists and can be initialized', () => { const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); diff --git a/test/mssql/00-tests-adapter.js b/test/mssql/00-tests-adapter.js index ab07417..e242004 100755 --- a/test/mssql/00-tests-adapter.js +++ b/test/mssql/00-tests-adapter.js @@ -34,7 +34,7 @@ const connection_released = qb => { }, 100); }; -describe('QueryBuilder() - MS SQL Adapter', () => { +describe('MSSQL: QueryBuilder() - Adapter', () => { const driver = 'mssql'; const settings = { host: 'localhost', diff --git a/test/mssql/01-tests-distinct.js b/test/mssql/01-tests-distinct.js index 8733cc5..60bc83b 100755 --- a/test/mssql/01-tests-distinct.js +++ b/test/mssql/01-tests-distinct.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('distinct()', () => { +describe('MSSQL: distinct()', () => { it('should exist', () => { should.exist(qb.distinct); }); diff --git a/test/mssql/01-tests-from.js b/test/mssql/01-tests-from.js index 1c7a200..e05120c 100755 --- a/test/mssql/01-tests-from.js +++ b/test/mssql/01-tests-from.js @@ -2,7 +2,7 @@ const should = require('chai').should(); const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('from()', () => { +describe('MSSQL: from()', () => { it('should exist', () => { should.exist(qb.from); }); diff --git a/test/mssql/01-tests-get.js b/test/mssql/01-tests-get.js index 7507ad7..850a8d6 100755 --- a/test/mssql/01-tests-get.js +++ b/test/mssql/01-tests-get.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('get()', () => { +describe('MSSQL: get()', () => { it('should exist', () => { should.exist(qb.get); }); @@ -90,7 +90,7 @@ describe('get()', () => { it('should accept a limit on the number of rows selected', () => { qb.reset_query(); const sql = qb.limit(20).get('galaxies'); - sql.should.eql("SELECT TOP 20 * FROM [galaxies]"); + sql.should.eql("SELECT TOP (20) * FROM [galaxies]"); }); it('should accept a LIMIT on the number of rows to select and an OFFSET at which to start selecting the rows', () => { qb.reset_query(); @@ -153,7 +153,7 @@ describe('get()', () => { }); }); -describe('get_where()', () => { +describe('MSSQL: get_where()', () => { it('should exist', () => { should.exist(qb.get_where); }); diff --git a/test/mssql/01-tests-groupby.js b/test/mssql/01-tests-groupby.js index b5553f6..0785ed3 100755 --- a/test/mssql/01-tests-groupby.js +++ b/test/mssql/01-tests-groupby.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('group_by()', () => { +describe('MSSQL: group_by()', () => { it('should exist', () => { should.exist(qb.group_by); }); diff --git a/test/mssql/01-tests-having.js b/test/mssql/01-tests-having.js index 93f13ef..f5d6464 100755 --- a/test/mssql/01-tests-having.js +++ b/test/mssql/01-tests-having.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('having()', () => { +describe('MSSQL: having()', () => { it('should exist', () => { should.exist(qb.having); }); @@ -92,7 +92,7 @@ describe('having()', () => { }); }); -describe('or_having()', () => { +describe('MSSQL: or_having()', () => { it('should exist', () => { should.exist(qb.or_having); }); diff --git a/test/mssql/01-tests-join.js b/test/mssql/01-tests-join.js index c73dcae..e537f2d 100755 --- a/test/mssql/01-tests-join.js +++ b/test/mssql/01-tests-join.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('join()', () => { +describe('MSSQL: join()', () => { it('should exist', () => { should.exist(qb.join); }); diff --git a/test/mssql/01-tests-like.js b/test/mssql/01-tests-like.js index e2f2846..b23de2d 100755 --- a/test/mssql/01-tests-like.js +++ b/test/mssql/01-tests-like.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('like()', () => { +describe('MSSQL: like()', () => { it('should exist', () => { should.exist(qb.like); }); @@ -103,7 +103,7 @@ describe('like()', () => { }); }); -describe('or_like()', () => { +describe('MSSQL: or_like()', () => { it('should exist', () => { should.exist(qb.or_like); }); @@ -128,7 +128,7 @@ describe('or_like()', () => { }); }); -describe('not_like()', () => { +describe('MSSQL: not_like()', () => { it('should exist', () => { should.exist(qb.not_like); }); @@ -158,7 +158,7 @@ describe('not_like()', () => { }); }); -describe('or_not_like()', () => { +describe('MSSQL: or_not_like()', () => { it('should exist', () => { should.exist(qb.or_not_like); }); diff --git a/test/mssql/01-tests-limit.js b/test/mssql/01-tests-limit.js index 3a5fe8c..ade72a7 100755 --- a/test/mssql/01-tests-limit.js +++ b/test/mssql/01-tests-limit.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('limit()', () => { +describe('MSSQL: limit()', () => { it('should exist', () => { should.exist(qb.limit); }); diff --git a/test/mssql/01-tests-offset.js b/test/mssql/01-tests-offset.js index 3a5d2b7..9a0c9d3 100755 --- a/test/mssql/01-tests-offset.js +++ b/test/mssql/01-tests-offset.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('offset()', () => { +describe('MSSQL: offset()', () => { it('should exist', () => { should.exist(qb.offset); }); diff --git a/test/mssql/01-tests-orderby.js b/test/mssql/01-tests-orderby.js index 21fc46f..6c2799d 100755 --- a/test/mssql/01-tests-orderby.js +++ b/test/mssql/01-tests-orderby.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('order_by()', () => { +describe('MSSQL: order_by()', () => { it('should exist', () => { should.exist(qb.order_by); }); diff --git a/test/mssql/01-tests-returning.js b/test/mssql/01-tests-returning.js index cd1fa74..ad69f41 100755 --- a/test/mssql/01-tests-returning.js +++ b/test/mssql/01-tests-returning.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('returning()', () => { +describe('MSSQL: returning()', () => { it('should exist', () => { should.exist(qb.returning); }); diff --git a/test/mssql/01-tests-select.js b/test/mssql/01-tests-select.js index ccd99ab..8c87621 100755 --- a/test/mssql/01-tests-select.js +++ b/test/mssql/01-tests-select.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('select()', () => { +describe('MSSQL: select()', () => { it('should exist', () => { should.exist(qb.select); }); @@ -191,7 +191,7 @@ describe('select()', () => { const prefixes = ['min','max','avg','sum']; for (const i in prefixes) { const type = prefixes[i]; - describe('select_' + type+'()', () => { + describe('MSSQL: select_' + type+'()', () => { it('should exist', () => { should.exist(qb['select_' + type]); }); diff --git a/test/mssql/01-tests-set.js b/test/mssql/01-tests-set.js index 3895973..5896110 100755 --- a/test/mssql/01-tests-set.js +++ b/test/mssql/01-tests-set.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('set()', () => { +describe('MSSQL: set()', () => { it('should exist', () => { should.exist(qb.set); }); diff --git a/test/mssql/01-tests-where.js b/test/mssql/01-tests-where.js index 33647c6..b232908 100755 --- a/test/mssql/01-tests-where.js +++ b/test/mssql/01-tests-where.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('where()', () => { +describe('MSSQL: where()', () => { it('should exist', () => { should.exist(qb.where); }); @@ -148,7 +148,7 @@ describe('where()', () => { }); }); -describe('or_where()', () => { +describe('MSSQL: or_where()', () => { it('should exist', () => { should.exist(qb.or_where); }); diff --git a/test/mssql/01-tests-where_in.js b/test/mssql/01-tests-where_in.js index d4a745a..39696cc 100755 --- a/test/mssql/01-tests-where_in.js +++ b/test/mssql/01-tests-where_in.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('where_in()', () => { +describe('MSSQL: where_in()', () => { it('should exist', () => { should.exist(qb.where_in); }); @@ -76,7 +76,7 @@ describe('where_in()', () => { }); }); -describe('where_not_in()', () => { +describe('MSSQL: where_not_in()', () => { it('should exist', () => { should.exist(qb.where_not_in); }); @@ -111,7 +111,7 @@ describe('where_not_in()', () => { }); }); -describe('or_where_in()', () => { +describe('MSSQL: or_where_in()', () => { it('should exist', () => { should.exist(qb.or_where_in); }); @@ -141,7 +141,7 @@ describe('or_where_in()', () => { }); }); -describe('or_where_not_in()', () => { +describe('MSSQL: or_where_not_in()', () => { it('should exist', () => { should.exist(qb.or_where_in); }); diff --git a/test/mssql/02-tests-compilation_methods.js b/test/mssql/02-tests-compilation_methods.js index 2a0ad4f..ff1e5c7 100755 --- a/test/mssql/02-tests-compilation_methods.js +++ b/test/mssql/02-tests-compilation_methods.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('get_compiled_select()', () => { +describe('MSSQL: get_compiled_select()', () => { it('should exist', () => { should.exist(qb.get_compiled_select); }); @@ -27,7 +27,7 @@ describe('get_compiled_select()', () => { }); }); -describe('get_compiled_insert()', () => { +describe('MSSQL: get_compiled_insert()', () => { it('should exist', () => { should.exist(qb.get_compiled_insert); }); @@ -41,7 +41,7 @@ describe('get_compiled_insert()', () => { }); }); -describe('get_compiled_update()', () => { +describe('MSSQL: get_compiled_update()', () => { it('should exist', () => { should.exist(qb.get_compiled_update); }); @@ -55,7 +55,7 @@ describe('get_compiled_update()', () => { }); }); -describe('get_compiled_delete()', () => { +describe('MSSQL: get_compiled_delete()', () => { it('should exist', () => { should.exist(qb.get_compiled_delete); }); diff --git a/test/mssql/03-tests-count.js b/test/mssql/03-tests-count.js index 69de821..f6c576a 100755 --- a/test/mssql/03-tests-count.js +++ b/test/mssql/03-tests-count.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('count()', () => { +describe('MSSQL: count()', () => { it('should exist', () => { should.exist(qb.count); }); diff --git a/test/mssql/03-tests-delete.js b/test/mssql/03-tests-delete.js index dbb4c76..bbd6976 100755 --- a/test/mssql/03-tests-delete.js +++ b/test/mssql/03-tests-delete.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('delete()', () => { +describe('MSSQL: delete()', () => { it('should exist', () => { should.exist(qb.delete); }); @@ -81,7 +81,7 @@ describe('delete()', () => { it('should accept a limit on the number of rows deleted', () => { qb.reset_query(); const sql = qb.limit(20).delete('galaxies'); - sql.should.eql("DELETE TOP 20 FROM [galaxies]"); + sql.should.eql("DELETE TOP (20) FROM [galaxies]"); }); it('should not a allow an offset delete', () => { qb.reset_query(); diff --git a/test/mssql/03-tests-empty_table.js b/test/mssql/03-tests-empty_table.js index a1d10b2..60697f9 100755 --- a/test/mssql/03-tests-empty_table.js +++ b/test/mssql/03-tests-empty_table.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('emtpy_table()', () => { +describe('MSSQL: emtpy_table()', () => { it('should exist', () => { should.exist(qb.empty_table); }); diff --git a/test/mssql/03-tests-insert.js b/test/mssql/03-tests-insert.js index 5781c58..5bc4c8b 100755 --- a/test/mssql/03-tests-insert.js +++ b/test/mssql/03-tests-insert.js @@ -8,7 +8,7 @@ const test_data_set = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'A // table, data, callback, ignore, suffix -describe('insert()', () => { +describe('MSSQL: insert()', () => { it('should exist', () => { should.exist(qb.insert); }); @@ -131,7 +131,7 @@ describe('insert()', () => { }); }); -describe('insert_ignore()', () => { +describe('MSSQL: insert_ignore()', () => { it('should exist', () => { should.exist(qb.insert_ignore); }); diff --git a/test/mssql/03-tests-insert_batch.js b/test/mssql/03-tests-insert_batch.js index 36a4b29..fe2b9b6 100755 --- a/test/mssql/03-tests-insert_batch.js +++ b/test/mssql/03-tests-insert_batch.js @@ -6,7 +6,7 @@ const qb = new QueryBuilder(); const test_where = {id:3}; const test_data = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'Andromeda', type: 'spiral'}]; -describe('insert_batch()', () => { +describe('MSSQL: insert_batch()', () => { it('should exist', () => { should.exist(qb.insert_batch); }); diff --git a/test/mssql/03-tests-truncate.js b/test/mssql/03-tests-truncate.js index 5b1592e..bd4a6d1 100755 --- a/test/mssql/03-tests-truncate.js +++ b/test/mssql/03-tests-truncate.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mssql/query_builder.js'); const qb = new QueryBuilder(); -describe('truncate()', () => { +describe('MSSQL: truncate()', () => { it('should exist', () => { should.exist(qb.truncate); }); diff --git a/test/mssql/03-tests-update.js b/test/mssql/03-tests-update.js index 01c4808..06446c7 100755 --- a/test/mssql/03-tests-update.js +++ b/test/mssql/03-tests-update.js @@ -9,7 +9,7 @@ const test_data_set = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'A // table, data, callback, ignore, suffix -describe('update()', () => { +describe('MSSQL: update()', () => { it('should exist', () => { should.exist(qb.update); }); @@ -121,6 +121,6 @@ describe('update()', () => { it('should allow for a LIMITed update', () => { qb.reset_query(); const sql = qb.limit(10).update('galaxies', test_data, test_where); - sql.should.eql("UPDATE [t] SET [name] = 'Milky Way', [type] = 'spiral' FROM (SELECT TOP 10 * FROM [galaxies] WHERE [id] = 3) [t]"); + sql.should.eql("UPDATE [t] SET [name] = 'Milky Way', [type] = 'spiral' FROM (SELECT TOP (10) * FROM [galaxies] WHERE [id] = 3) [t]"); }); }); diff --git a/test/mssql/03-tests-update_batch.js b/test/mssql/03-tests-update_batch.js index 50aaeb5..9e8f731 100755 --- a/test/mssql/03-tests-update_batch.js +++ b/test/mssql/03-tests-update_batch.js @@ -6,7 +6,7 @@ const qb = new QueryBuilder(); const test_where = {id:3}; const test_data = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'Andromeda', type: 'spiral'}]; -describe('update_batch()', () => { +describe('MSSQL: update_batch()', () => { it('should exist', () => { should.exist(qb.update_batch); }); diff --git a/test/mssql/04-tests-query-response.js b/test/mssql/04-tests-query-response.js index df5f428..60eccd0 100755 --- a/test/mssql/04-tests-query-response.js +++ b/test/mssql/04-tests-query-response.js @@ -11,7 +11,7 @@ const check = (done, f) => { } }; -describe('Query Responses', () => { +describe('MSSQL: Query Responses', () => { const driver = 'mssql'; const settings = { host: 'localhost', diff --git a/test/mysql/00-AA-tests-general.js b/test/mysql/00-AA-tests-general.js index 7c8ce90..b8add19 100755 --- a/test/mysql/00-AA-tests-general.js +++ b/test/mysql/00-AA-tests-general.js @@ -1,9 +1,9 @@ const should = require('chai').should(); -describe('QueryBuilder', () => { - it('actually exists and can be initialized', () => { - const QueryBuilder = require('../../drivers/mysql/query_builder.js'); - const qb = new QueryBuilder(); - qb.should.be.instanceOf(QueryBuilder); - }); +describe('MySQL: QueryBuilder', () => { + it('actually exists and can be initialized', () => { + const QueryBuilder = require('../../drivers/mysql/query_builder.js'); + const qb = new QueryBuilder(); + qb.should.be.instanceOf(QueryBuilder); + }); }); diff --git a/test/mysql/01-tests-distinct.js b/test/mysql/01-tests-distinct.js index 0cb4afd..5d88134 100755 --- a/test/mysql/01-tests-distinct.js +++ b/test/mysql/01-tests-distinct.js @@ -3,16 +3,16 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('distinct()', () => { - it('should exist', () => { - should.exist(qb.distinct); - }); - it('should be a function', () => { - qb.distinct.should.be.a('function'); - }); - it('should override the default distinct_clause with the "DISTINCT " keyword', () => { - qb.reset_query(); - qb.distinct(); - qb.distinct_clause.should.eql(['DISTINCT ']); - }); +describe('MySQL: distinct()', () => { + it('should exist', () => { + should.exist(qb.distinct); + }); + it('should be a function', () => { + qb.distinct.should.be.a('function'); + }); + it('should override the default distinct_clause with the "DISTINCT " keyword', () => { + qb.reset_query(); + qb.distinct(); + qb.distinct_clause.should.eql(['DISTINCT ']); + }); }); diff --git a/test/mysql/01-tests-from.js b/test/mysql/01-tests-from.js index 412e4b2..0ec0bcd 100755 --- a/test/mysql/01-tests-from.js +++ b/test/mysql/01-tests-from.js @@ -2,7 +2,7 @@ const should = require('chai').should(); const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('from()', () => { +describe('MySQL: from()', () => { it('should exist', () => { should.exist(qb.from); }); diff --git a/test/mysql/01-tests-get.js b/test/mysql/01-tests-get.js index a22a31d..6e9645e 100755 --- a/test/mysql/01-tests-get.js +++ b/test/mysql/01-tests-get.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('get()', () => { +describe('MySQL: get()', () => { it('should exist', () => { should.exist(qb.get); }); @@ -148,7 +148,7 @@ describe('get()', () => { }); }); -describe('get_where()', () => { +describe('MySQL: get_where()', () => { it('should exist', () => { should.exist(qb.get_where); }); diff --git a/test/mysql/01-tests-groupby.js b/test/mysql/01-tests-groupby.js index ca633d5..cb6c0ad 100755 --- a/test/mysql/01-tests-groupby.js +++ b/test/mysql/01-tests-groupby.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('group_by()', () => { +describe('MySQL: group_by()', () => { it('should exist', () => { should.exist(qb.group_by); }); diff --git a/test/mysql/01-tests-having.js b/test/mysql/01-tests-having.js index 1a4da12..0c8df2a 100755 --- a/test/mysql/01-tests-having.js +++ b/test/mysql/01-tests-having.js @@ -3,110 +3,110 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('having()', () => { - it('should exist', () => { - should.exist(qb.having); - }); - it('should be a function', () => { - qb.having.should.be.a('function'); - }); - it('should have an array to put fields into', () => { - qb.should.have.property('having_array'); - }); - it('should accept a string only in this format: a [>|<|<>|>=|<=|=|!=] b for the first parameter', () => { - qb.reset_query(); - qb.having('planet_class > "M"'); - qb.having_array.should.eql(["`planet_class` > 'M'"]); +describe('MySQL: having()', () => { + it('should exist', () => { + should.exist(qb.having); + }); + it('should be a function', () => { + qb.having.should.be.a('function'); + }); + it('should have an array to put fields into', () => { + qb.should.have.property('having_array'); + }); + it('should accept a string only in this format: a [>|<|<>|>=|<=|=|!=] b for the first parameter', () => { + qb.reset_query(); + qb.having('planet_class > "M"'); + qb.having_array.should.eql(["`planet_class` > 'M'"]); - qb.reset_query(); - qb.having('planet_class < "M"'); - qb.having_array.should.eql(["`planet_class` < 'M'"]); + qb.reset_query(); + qb.having('planet_class < "M"'); + qb.having_array.should.eql(["`planet_class` < 'M'"]); - qb.reset_query(); - qb.having('planet_class <> "M"'); - qb.having_array.should.eql(["`planet_class` <> 'M'"]); + qb.reset_query(); + qb.having('planet_class <> "M"'); + qb.having_array.should.eql(["`planet_class` <> 'M'"]); - qb.reset_query(); - qb.having('planet_class >= "M"'); - qb.having_array.should.eql(["`planet_class` >= 'M'"]); + qb.reset_query(); + qb.having('planet_class >= "M"'); + qb.having_array.should.eql(["`planet_class` >= 'M'"]); - qb.reset_query(); - qb.having('planet_class <= "M"'); - qb.having_array.should.eql(["`planet_class` <= 'M'"]); + qb.reset_query(); + qb.having('planet_class <= "M"'); + qb.having_array.should.eql(["`planet_class` <= 'M'"]); - qb.reset_query(); - qb.having('planet_class = "M"'); - qb.having_array.should.eql(["`planet_class` = 'M'"]); + qb.reset_query(); + qb.having('planet_class = "M"'); + qb.having_array.should.eql(["`planet_class` = 'M'"]); - qb.reset_query(); - qb.having('planet_class != "M"'); - qb.having_array.should.eql(["`planet_class` != 'M'"]); - }); - it('should not accept compound conditions in this format: a [>|<|<>|>=|<=|=|!=] b[, repeat[, etc...]]', () => { - qb.reset_query(); - expect(() => qb.having('planet_class = "M", sentient_life = 1'), 'two conditions provided').to.throw(Error); - }); - it('should accept an array of conditions and prepend AND to each condition following the first one', () => { - qb.reset_query(); - qb.having(["planet_class = 'M'", 'sentient_life = 1']); - qb.having_array.should.eql(["`planet_class` = 'M'", 'AND `sentient_life` = 1']); - }); - it('should accept an object of conditions and prepend AND to each condition following the first one', () => { - qb.reset_query(); - const object = {planet_class: 'M', sentient_life: 1}; - object['planet_order <='] = 3; - qb.having(object); - qb.having_array.should.eql(["`planet_class` = 'M'", 'AND `sentient_life` = 1','AND `planet_order` <= 3']); - }); - it('should not accept anything but a non-empty array, object, or string', () => { - qb.reset_query(); - expect(() => qb.group_by(), 'nothing provided').to.throw(Error); - expect(() => qb.group_by(null), 'null provided').to.throw(Error); - expect(() => qb.group_by(false), 'false provided').to.throw(Error); - expect(() => qb.group_by(true), 'true provided').to.throw(Error); - expect(() => qb.group_by({}), 'empty object provided').to.throw(Error); - expect(() => qb.group_by(3), 'integer provided').to.throw(Error); - expect(() => qb.group_by(3.5), 'float provided').to.throw(Error); - expect(() => qb.group_by([]), 'empty array provided').to.throw(Error); - expect(() => qb.group_by([1,2]), 'array of numbers provided').to.throw(Error); - expect(() => qb.group_by(''), 'empty string provided').to.throw(Error); + qb.reset_query(); + qb.having('planet_class != "M"'); + qb.having_array.should.eql(["`planet_class` != 'M'"]); + }); + it('should not accept compound conditions in this format: a [>|<|<>|>=|<=|=|!=] b[, repeat[, etc...]]', () => { + qb.reset_query(); + expect(() => qb.having('planet_class = "M", sentient_life = 1'), 'two conditions provided').to.throw(Error); + }); + it('should accept an array of conditions and prepend AND to each condition following the first one', () => { + qb.reset_query(); + qb.having(["planet_class = 'M'", 'sentient_life = 1']); + qb.having_array.should.eql(["`planet_class` = 'M'", 'AND `sentient_life` = 1']); + }); + it('should accept an object of conditions and prepend AND to each condition following the first one', () => { + qb.reset_query(); + const object = {planet_class: 'M', sentient_life: 1}; + object['planet_order <='] = 3; + qb.having(object); + qb.having_array.should.eql(["`planet_class` = 'M'", 'AND `sentient_life` = 1','AND `planet_order` <= 3']); + }); + it('should not accept anything but a non-empty array, object, or string', () => { + qb.reset_query(); + expect(() => qb.group_by(), 'nothing provided').to.throw(Error); + expect(() => qb.group_by(null), 'null provided').to.throw(Error); + expect(() => qb.group_by(false), 'false provided').to.throw(Error); + expect(() => qb.group_by(true), 'true provided').to.throw(Error); + expect(() => qb.group_by({}), 'empty object provided').to.throw(Error); + expect(() => qb.group_by(3), 'integer provided').to.throw(Error); + expect(() => qb.group_by(3.5), 'float provided').to.throw(Error); + expect(() => qb.group_by([]), 'empty array provided').to.throw(Error); + expect(() => qb.group_by([1,2]), 'array of numbers provided').to.throw(Error); + expect(() => qb.group_by(''), 'empty string provided').to.throw(Error); - // valid string - expect(() => qb.group_by('planet_type = "M"'), 'valid string provided').to.not.throw(Error); - expect(() => qb.group_by(['planet_type = "M"']), 'array of string(s) provided').to.not.throw(Error); - }); - it('should accept 2 parameters where the first one is the field with optional condition and the second one is the value', () => { - qb.reset_query(); - qb.having('planet_class','M'); - qb.having_array.should.eql(["`planet_class` = 'M'"]); - }); - it('should not escape conditions if asked not to', () => { - qb.reset_query(); - qb.having(["planet_class = 'M'", 'sentient_life = 1'], null, false); - qb.having_array.should.eql(["planet_class = 'M'", 'AND sentient_life = 1']); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.having('planet_class','M').having('sentient_life',true).having('planet_order <=',3); - qb.having_array.should.eql(["`planet_class` = 'M'", 'AND `sentient_life` = 1','AND `planet_order` <= 3']); - }); + // valid string + expect(() => qb.group_by('planet_type = "M"'), 'valid string provided').to.not.throw(Error); + expect(() => qb.group_by(['planet_type = "M"']), 'array of string(s) provided').to.not.throw(Error); + }); + it('should accept 2 parameters where the first one is the field with optional condition and the second one is the value', () => { + qb.reset_query(); + qb.having('planet_class','M'); + qb.having_array.should.eql(["`planet_class` = 'M'"]); + }); + it('should not escape conditions if asked not to', () => { + qb.reset_query(); + qb.having(["planet_class = 'M'", 'sentient_life = 1'], null, false); + qb.having_array.should.eql(["planet_class = 'M'", 'AND sentient_life = 1']); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.having('planet_class','M').having('sentient_life',true).having('planet_order <=',3); + qb.having_array.should.eql(["`planet_class` = 'M'", 'AND `sentient_life` = 1','AND `planet_order` <= 3']); + }); }); -describe('or_having()', () => { - it('should exist', () => { - should.exist(qb.or_having); - }); - it('should be a function', () => { - qb.or_having.should.be.a('function'); - }); - it('should accept an array of conditions and prepend OR to each condition following the first one', () => { - qb.reset_query(); - qb.or_having(["planet_class = 'M'", 'sentient_life = 1']); - qb.having_array.should.eql(["`planet_class` = 'M'", 'OR `sentient_life` = 1']); - }); - it('should be chainable with normal having', () => { - qb.reset_query(); - qb.having('planet_class','M').having('sentient_life',true).or_having('planet_order <=',3); - qb.having_array.should.eql(["`planet_class` = 'M'", 'AND `sentient_life` = 1','OR `planet_order` <= 3']); - }); +describe('MySQL: or_having()', () => { + it('should exist', () => { + should.exist(qb.or_having); + }); + it('should be a function', () => { + qb.or_having.should.be.a('function'); + }); + it('should accept an array of conditions and prepend OR to each condition following the first one', () => { + qb.reset_query(); + qb.or_having(["planet_class = 'M'", 'sentient_life = 1']); + qb.having_array.should.eql(["`planet_class` = 'M'", 'OR `sentient_life` = 1']); + }); + it('should be chainable with normal having', () => { + qb.reset_query(); + qb.having('planet_class','M').having('sentient_life',true).or_having('planet_order <=',3); + qb.having_array.should.eql(["`planet_class` = 'M'", 'AND `sentient_life` = 1','OR `planet_order` <= 3']); + }); }); diff --git a/test/mysql/01-tests-join.js b/test/mysql/01-tests-join.js index d05fbe0..13d85d8 100755 --- a/test/mysql/01-tests-join.js +++ b/test/mysql/01-tests-join.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('join()', () => { +describe('MySQL: join()', () => { it('should exist', () => { should.exist(qb.join); }); diff --git a/test/mysql/01-tests-like.js b/test/mysql/01-tests-like.js index c0d698d..0cc339e 100755 --- a/test/mysql/01-tests-like.js +++ b/test/mysql/01-tests-like.js @@ -3,187 +3,187 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('like()', () => { - it('should exist', () => { - should.exist(qb.like); - }); - it('should be a function', () => { - qb.like.should.be.a('function'); - }); - it('should require first parameter to be a valid string or object with key value pairs', () => { - expect(() => qb.like(), 'nothing provided').to.throw(Error); - expect(() => qb.like(null), 'null provided').to.throw(Error); - expect(() => qb.like(false), 'false provided').to.throw(Error); - expect(() => qb.like(true), 'true provided').to.throw(Error); - expect(() => qb.like({}), 'empty object provided').to.throw(Error); - expect(() => qb.like(3), 'integer provided').to.throw(Error); - expect(() => qb.like(3.5), 'float provided').to.throw(Error); - expect(() => qb.like([]), 'empty array provided').to.throw(Error); - expect(() => qb.like(''), 'empty string provided').to.throw(Error); +describe('MySQL: like()', () => { + it('should exist', () => { + should.exist(qb.like); + }); + it('should be a function', () => { + qb.like.should.be.a('function'); + }); + it('should require first parameter to be a valid string or object with key value pairs', () => { + expect(() => qb.like(), 'nothing provided').to.throw(Error); + expect(() => qb.like(null), 'null provided').to.throw(Error); + expect(() => qb.like(false), 'false provided').to.throw(Error); + expect(() => qb.like(true), 'true provided').to.throw(Error); + expect(() => qb.like({}), 'empty object provided').to.throw(Error); + expect(() => qb.like(3), 'integer provided').to.throw(Error); + expect(() => qb.like(3.5), 'float provided').to.throw(Error); + expect(() => qb.like([]), 'empty array provided').to.throw(Error); + expect(() => qb.like(''), 'empty string provided').to.throw(Error); - expect(() => qb.like('planet_name','ear','after'), 'valid string').to.not.throw(Error); - expect(() => qb.like({planet_name: 'ear'}), 'valid object').to.not.throw(Error); - }); - it('should require second parameter if first paramter is a string', () => { - expect(() => qb.like('planet_name'), 'no second param provided').to.throw(Error); - expect(() => qb.like('planet_name','ear'), 'valid second param provided').to.not.throw(Error); - expect(() => qb.like({planet_name: 'ear'}), 'object provided as first param').to.not.throw(Error); - }); - it('should require second parameter (when provided) to be a string, number, or boolean', () => { - expect(() => qb.like('planet_name',null), 'null provided').to.throw(Error); - expect(() => qb.like('planet_name',{}), 'empty object provided').to.throw(Error); - expect(() => qb.like('planet_name',[]), 'empty array provided').to.throw(Error); - expect(() => qb.like('planet_name',NaN), 'empty array provided').to.throw(Error); - expect(() => qb.like('planet_name',Infinity), 'empty array provided').to.throw(Error); + expect(() => qb.like('planet_name','ear','after'), 'valid string').to.not.throw(Error); + expect(() => qb.like({planet_name: 'ear'}), 'valid object').to.not.throw(Error); + }); + it('should require second parameter if first paramter is a string', () => { + expect(() => qb.like('planet_name'), 'no second param provided').to.throw(Error); + expect(() => qb.like('planet_name','ear'), 'valid second param provided').to.not.throw(Error); + expect(() => qb.like({planet_name: 'ear'}), 'object provided as first param').to.not.throw(Error); + }); + it('should require second parameter (when provided) to be a string, number, or boolean', () => { + expect(() => qb.like('planet_name',null), 'null provided').to.throw(Error); + expect(() => qb.like('planet_name',{}), 'empty object provided').to.throw(Error); + expect(() => qb.like('planet_name',[]), 'empty array provided').to.throw(Error); + expect(() => qb.like('planet_name',NaN), 'empty array provided').to.throw(Error); + expect(() => qb.like('planet_name',Infinity), 'empty array provided').to.throw(Error); - expect(() => qb.like('planet_name',false), 'false provided').to.not.throw(Error); - expect(() => qb.like('planet_name',true), 'true provided').to.not.throw(Error); - expect(() => qb.like('planet_name',3), 'integer provided').to.not.throw(Error); - expect(() => qb.like('planet_name',3.5), 'float provided').to.not.throw(Error); - expect(() => qb.like('planet_name',''), 'empty string provided').to.not.throw(Error); - expect(() => qb.like('planet_name','ear'), 'non-empty string provided').to.not.throw(Error); - }); - it('should only accept the following direction strings in the third parameter: undefined, "both", "right", "left", "before", "after"', () => { - expect(() => qb.like('galaxy_name','milk',null), 'null provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',{}), 'empty object provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',[]), 'empty array provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',NaN), 'empty array provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',Infinity), 'empty array provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',false), 'false provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',true), 'true provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',3), 'integer provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',3.5), 'float provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk',''), 'empty string provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk','foo'), 'non-empty string provided').to.throw(Error); + expect(() => qb.like('planet_name',false), 'false provided').to.not.throw(Error); + expect(() => qb.like('planet_name',true), 'true provided').to.not.throw(Error); + expect(() => qb.like('planet_name',3), 'integer provided').to.not.throw(Error); + expect(() => qb.like('planet_name',3.5), 'float provided').to.not.throw(Error); + expect(() => qb.like('planet_name',''), 'empty string provided').to.not.throw(Error); + expect(() => qb.like('planet_name','ear'), 'non-empty string provided').to.not.throw(Error); + }); + it('should only accept the following direction strings in the third parameter: undefined, "both", "right", "left", "before", "after"', () => { + expect(() => qb.like('galaxy_name','milk',null), 'null provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',{}), 'empty object provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',[]), 'empty array provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',NaN), 'empty array provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',Infinity), 'empty array provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',false), 'false provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',true), 'true provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',3), 'integer provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',3.5), 'float provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk',''), 'empty string provided').to.throw(Error); + expect(() => qb.like('galaxy_name','milk','foo'), 'non-empty string provided').to.throw(Error); - expect(() => qb.like('galaxy_name','milk'), 'no third param provided').to.not.throw(Error); - expect(() => qb.like('galaxy_name','milk','right'), 'right as third param').to.not.throw(Error); - expect(() => qb.like('galaxy_name','milk','left'), 'left as third param').to.not.throw(Error); - expect(() => qb.like('galaxy_name','milk','both'), 'both as third param').to.not.throw(Error); - expect(() => qb.like('galaxy_name','milk','before'), 'before as third param').to.not.throw(Error); - expect(() => qb.like('galaxy_name','milk','after'), 'after as third param').to.not.throw(Error); - }); - it('should put percentage signs on the right side of the condition if "right" or "after" are passed as the 3rd parameter', () => { - qb.reset_query(); - qb.like('galaxy_name', 'milky', 'after'); - qb.where_array.should.eql(["`galaxy_name` LIKE 'milky%'"]); + expect(() => qb.like('galaxy_name','milk'), 'no third param provided').to.not.throw(Error); + expect(() => qb.like('galaxy_name','milk','right'), 'right as third param').to.not.throw(Error); + expect(() => qb.like('galaxy_name','milk','left'), 'left as third param').to.not.throw(Error); + expect(() => qb.like('galaxy_name','milk','both'), 'both as third param').to.not.throw(Error); + expect(() => qb.like('galaxy_name','milk','before'), 'before as third param').to.not.throw(Error); + expect(() => qb.like('galaxy_name','milk','after'), 'after as third param').to.not.throw(Error); + }); + it('should put percentage signs on the right side of the condition if "right" or "after" are passed as the 3rd parameter', () => { + qb.reset_query(); + qb.like('galaxy_name', 'milky', 'after'); + qb.where_array.should.eql(["`galaxy_name` LIKE 'milky%'"]); - qb.reset_query(); - qb.like('galaxy_name', 'milky', 'right'); - qb.where_array.should.eql(["`galaxy_name` LIKE 'milky%'"]); - }); - it('should put percentage signs on the left side of the condition if "before" or "left" are passed as the 3rd parameter', () => { - qb.reset_query(); - qb.like('galaxy_name', 'milky', 'before'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky'"]); + qb.reset_query(); + qb.like('galaxy_name', 'milky', 'right'); + qb.where_array.should.eql(["`galaxy_name` LIKE 'milky%'"]); + }); + it('should put percentage signs on the left side of the condition if "before" or "left" are passed as the 3rd parameter', () => { + qb.reset_query(); + qb.like('galaxy_name', 'milky', 'before'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky'"]); - qb.reset_query(); - qb.like('galaxy_name', 'milky', 'left'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky'"]); - }); - it('should put percentage signs on both sides of the condition if "both" or undefined are passed as the 3rd parameter', () => { - qb.reset_query(); - qb.like('galaxy_name', 'milky'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'"]); + qb.reset_query(); + qb.like('galaxy_name', 'milky', 'left'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky'"]); + }); + it('should put percentage signs on both sides of the condition if "both" or undefined are passed as the 3rd parameter', () => { + qb.reset_query(); + qb.like('galaxy_name', 'milky'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'"]); - qb.reset_query(); - qb.like('galaxy_name', 'milky', 'both'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'"]); - }); - it('should put AND in between multiple LIKE clauses', () => { - qb.reset_query(); - qb.like('galaxy_name', 'milky'); - qb.like('planet_name', 'ear', 'right'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `planet_name` LIKE 'ear%'"]); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.like('galaxy_name', 'milky').like('planet_name', 'ear', 'right'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `planet_name` LIKE 'ear%'"]); - }); + qb.reset_query(); + qb.like('galaxy_name', 'milky', 'both'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'"]); + }); + it('should put AND in between multiple LIKE clauses', () => { + qb.reset_query(); + qb.like('galaxy_name', 'milky'); + qb.like('planet_name', 'ear', 'right'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `planet_name` LIKE 'ear%'"]); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.like('galaxy_name', 'milky').like('planet_name', 'ear', 'right'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `planet_name` LIKE 'ear%'"]); + }); }); -describe('or_like()', () => { - it('should exist', () => { - should.exist(qb.or_like); - }); - it('should be a function', () => { - qb.or_like.should.be.a('function'); - }); - it('should put OR in between multiple OR LIKE clauses', () => { - qb.reset_query(); - qb.or_like('galaxy_name', 'milky'); - qb.or_like('planet_name', 'ear', 'right'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "OR `planet_name` LIKE 'ear%'"]); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.or_like('galaxy_name', 'milky').or_like('planet_name', 'ear', 'right'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "OR `planet_name` LIKE 'ear%'"]); - }); - it('should be chainable with regular like clauses', () => { - qb.reset_query(); - qb.like('galaxy_name', 'milky').like('planet_name', 'ear', 'right').or_like('planet_name','Jup','right'); - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `planet_name` LIKE 'ear%'", "OR `planet_name` LIKE 'Jup%'"]); - }); +describe('MySQL: or_like()', () => { + it('should exist', () => { + should.exist(qb.or_like); + }); + it('should be a function', () => { + qb.or_like.should.be.a('function'); + }); + it('should put OR in between multiple OR LIKE clauses', () => { + qb.reset_query(); + qb.or_like('galaxy_name', 'milky'); + qb.or_like('planet_name', 'ear', 'right'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "OR `planet_name` LIKE 'ear%'"]); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.or_like('galaxy_name', 'milky').or_like('planet_name', 'ear', 'right'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "OR `planet_name` LIKE 'ear%'"]); + }); + it('should be chainable with regular like clauses', () => { + qb.reset_query(); + qb.like('galaxy_name', 'milky').like('planet_name', 'ear', 'right').or_like('planet_name','Jup','right'); + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `planet_name` LIKE 'ear%'", "OR `planet_name` LIKE 'Jup%'"]); + }); }); -describe('not_like()', () => { - it('should exist', () => { - should.exist(qb.not_like); - }); - it('should be a function', () => { - qb.not_like.should.be.a('function'); - }); - it('should put NOT before LIKE', () => { - qb.reset_query(); - qb.not_like('galaxy_name', 'milky'); - qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'"]); - }); - it('should put AND in between multiple NOT LIKE clauses', () => { - qb.reset_query(); - qb.not_like('galaxy_name', 'milky'); - qb.not_like('planet_name', 'ear', 'right'); - qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'", "AND `planet_name` NOT LIKE 'ear%'"]); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.not_like('galaxy_name', 'milky').not_like('planet_name', 'ear', 'right'); - qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'", "AND `planet_name` NOT LIKE 'ear%'"]); - }); - it('should be chainable with regular like clauses', () => { - qb.reset_query(); - qb.like('galaxy_name', 'milky').not_like('planet_name', 'ear', 'right') - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `planet_name` NOT LIKE 'ear%'"]); - }); +describe('MySQL: not_like()', () => { + it('should exist', () => { + should.exist(qb.not_like); + }); + it('should be a function', () => { + qb.not_like.should.be.a('function'); + }); + it('should put NOT before LIKE', () => { + qb.reset_query(); + qb.not_like('galaxy_name', 'milky'); + qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'"]); + }); + it('should put AND in between multiple NOT LIKE clauses', () => { + qb.reset_query(); + qb.not_like('galaxy_name', 'milky'); + qb.not_like('planet_name', 'ear', 'right'); + qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'", "AND `planet_name` NOT LIKE 'ear%'"]); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.not_like('galaxy_name', 'milky').not_like('planet_name', 'ear', 'right'); + qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'", "AND `planet_name` NOT LIKE 'ear%'"]); + }); + it('should be chainable with regular like clauses', () => { + qb.reset_query(); + qb.like('galaxy_name', 'milky').not_like('planet_name', 'ear', 'right') + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `planet_name` NOT LIKE 'ear%'"]); + }); }); -describe('or_not_like()', () => { - it('should exist', () => { - should.exist(qb.or_not_like); - }); - it('should be a function', () => { - qb.or_not_like.should.be.a('function'); - }); - it('should put NOT before LIKE', () => { - qb.reset_query(); - qb.or_not_like('galaxy_name', 'milky'); - qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'"]); - }); - it('should put OR in between multiple NOT LIKE clauses', () => { - qb.reset_query(); - qb.or_not_like('galaxy_name', 'milky'); - qb.or_not_like('planet_name', 'ear', 'right'); - qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'", "OR `planet_name` NOT LIKE 'ear%'"]); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.or_not_like('galaxy_name', 'milky').or_not_like('planet_name', 'ear', 'right'); - qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'", "OR `planet_name` NOT LIKE 'ear%'"]); - }); - it('should be chainable with regular like clauses', () => { - qb.reset_query(); - qb.like('galaxy_name', 'milky').like('galaxy_name', 'meda', 'before').or_not_like('planet_name', 'ear', 'right') - qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `galaxy_name` LIKE '%meda'", "OR `planet_name` NOT LIKE 'ear%'"]); - }); +describe('MySQL: or_not_like()', () => { + it('should exist', () => { + should.exist(qb.or_not_like); + }); + it('should be a function', () => { + qb.or_not_like.should.be.a('function'); + }); + it('should put NOT before LIKE', () => { + qb.reset_query(); + qb.or_not_like('galaxy_name', 'milky'); + qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'"]); + }); + it('should put OR in between multiple NOT LIKE clauses', () => { + qb.reset_query(); + qb.or_not_like('galaxy_name', 'milky'); + qb.or_not_like('planet_name', 'ear', 'right'); + qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'", "OR `planet_name` NOT LIKE 'ear%'"]); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.or_not_like('galaxy_name', 'milky').or_not_like('planet_name', 'ear', 'right'); + qb.where_array.should.eql(["`galaxy_name` NOT LIKE '%milky%'", "OR `planet_name` NOT LIKE 'ear%'"]); + }); + it('should be chainable with regular like clauses', () => { + qb.reset_query(); + qb.like('galaxy_name', 'milky').like('galaxy_name', 'meda', 'before').or_not_like('planet_name', 'ear', 'right') + qb.where_array.should.eql(["`galaxy_name` LIKE '%milky%'", "AND `galaxy_name` LIKE '%meda'", "OR `planet_name` NOT LIKE 'ear%'"]); + }); }); diff --git a/test/mysql/01-tests-limit.js b/test/mysql/01-tests-limit.js index 1616b35..719450b 100755 --- a/test/mysql/01-tests-limit.js +++ b/test/mysql/01-tests-limit.js @@ -3,85 +3,85 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('limit()', () => { - it('should exist', () => { - should.exist(qb.limit); - }); - it('should be a function', () => { - qb.limit.should.be.a('function'); - }); - it('should have an array to put fields into', () => { - qb.should.have.property('limit_to'); - }); - it('should have an empty array to put fields into at the beginning', () => { - qb.limit_to.should.be.empty; - }); - it('should require an integer (or integer in string form) in first parameter', () => { - qb.reset_query(); - expect(() => qb.limit(5), 'integer provided').to.not.throw(Error); - expect(() => qb.limit('5'), '5 in string form provided').to.not.throw(Error); - expect(() => qb.limit(5.7), 'float provided').to.throw(Error); - expect(() => qb.limit('5.7'), 'float provided').to.throw(Error); - expect(() => qb.limit('abc'), 'alpha provided').to.throw(Error); - expect(() => qb.limit('abc7'), 'alpha numerics provided').to.throw(Error); - expect(() => qb.limit(), 'nothing provided').to.throw(Error); - expect(() => qb.limit(null), 'null provided').to.throw(Error); - expect(() => qb.limit(true), 'true provided').to.throw(Error); - expect(() => qb.limit(false), 'false provided').to.throw(Error); - expect(() => qb.limit(''), 'empty string provided').to.throw(Error); - expect(() => qb.limit({}), 'empty object provided').to.throw(Error); - expect(() => qb.limit([]), 'empty array provided').to.throw(Error); - expect(() => qb.limit([5]), 'array with integer in it provided').to.throw(Error); - }); - it('should allow an integer (or integer in string form) in second parameter. Nothing else is allowed.', () => { - qb.reset_query(); - expect(() => qb.limit(10,5), 'integer provided').to.not.throw(Error); - expect(() => qb.limit(10,'5'), '5 in string form provided').to.not.throw(Error); - expect(() => qb.limit(10,5.7), 'float provided').to.throw(Error); - expect(() => qb.limit(10,'5.7'), 'float provided').to.throw(Error); - expect(() => qb.limit(10,'abc'), 'alpha provided').to.throw(Error); - expect(() => qb.limit(10,'abc7'), 'alphanumerics provided').to.throw(Error); - expect(() => qb.limit(10,null), 'null provided').to.throw(Error); - expect(() => qb.limit(10,true), 'true provided').to.throw(Error); - expect(() => qb.limit(10,false), 'false provided').to.throw(Error); - expect(() => qb.limit(10,''), 'empty string provided').to.throw(Error); - expect(() => qb.limit(10,{}), 'empty object provided').to.throw(Error); - expect(() => qb.limit(10,[]), 'empty array provided').to.throw(Error); - expect(() => qb.limit(10,[5]), 'array with integer in it provided').to.throw(Error); - }); - it('should override the default limit_to value when a limit is provided', () => { - qb.reset_query(); - qb.limit(10); - qb.limit_to.should.eql([10]); - }); - it('should override the default limit_to and offset_val values when a limit and an offset are provided', () => { - qb.reset_query(); - qb.limit(10,20); - qb.limit_to.should.eql([10]); - qb.offset_val.should.eql([20]); - }); - it('should trim string values that are provided', () => { - qb.reset_query(); - qb.limit('10 '); - qb.limit_to.should.eql([10]); - }); - it('should trim string values that are provided', () => { - qb.reset_query(); - qb.limit(' 10 ',' 12'); - qb.limit_to.should.eql([10]); - qb.offset_val.should.eql([12]); - }); - it('should override values set by any previous calls to itself', () => { - qb.reset_query(); - qb.limit(10); - qb.limit_to.should.eql([10]); - qb.limit(20); - qb.limit_to.should.eql([20]); - }); - it('should be chainable whereby the last call to the method will contain the value(s) used', () => { - qb.reset_query(); - qb.limit(10,5).limit(20).limit(100,30); - qb.limit_to.should.eql([100]); - qb.offset_val.should.eql([30]); - }); +describe('MySQL: limit()', () => { + it('should exist', () => { + should.exist(qb.limit); + }); + it('should be a function', () => { + qb.limit.should.be.a('function'); + }); + it('should have an array to put fields into', () => { + qb.should.have.property('limit_to'); + }); + it('should have an empty array to put fields into at the beginning', () => { + qb.limit_to.should.be.empty; + }); + it('should require an integer (or integer in string form) in first parameter', () => { + qb.reset_query(); + expect(() => qb.limit(5), 'integer provided').to.not.throw(Error); + expect(() => qb.limit('5'), '5 in string form provided').to.not.throw(Error); + expect(() => qb.limit(5.7), 'float provided').to.throw(Error); + expect(() => qb.limit('5.7'), 'float provided').to.throw(Error); + expect(() => qb.limit('abc'), 'alpha provided').to.throw(Error); + expect(() => qb.limit('abc7'), 'alpha numerics provided').to.throw(Error); + expect(() => qb.limit(), 'nothing provided').to.throw(Error); + expect(() => qb.limit(null), 'null provided').to.throw(Error); + expect(() => qb.limit(true), 'true provided').to.throw(Error); + expect(() => qb.limit(false), 'false provided').to.throw(Error); + expect(() => qb.limit(''), 'empty string provided').to.throw(Error); + expect(() => qb.limit({}), 'empty object provided').to.throw(Error); + expect(() => qb.limit([]), 'empty array provided').to.throw(Error); + expect(() => qb.limit([5]), 'array with integer in it provided').to.throw(Error); + }); + it('should allow an integer (or integer in string form) in second parameter. Nothing else is allowed.', () => { + qb.reset_query(); + expect(() => qb.limit(10,5), 'integer provided').to.not.throw(Error); + expect(() => qb.limit(10,'5'), '5 in string form provided').to.not.throw(Error); + expect(() => qb.limit(10,5.7), 'float provided').to.throw(Error); + expect(() => qb.limit(10,'5.7'), 'float provided').to.throw(Error); + expect(() => qb.limit(10,'abc'), 'alpha provided').to.throw(Error); + expect(() => qb.limit(10,'abc7'), 'alphanumerics provided').to.throw(Error); + expect(() => qb.limit(10,null), 'null provided').to.throw(Error); + expect(() => qb.limit(10,true), 'true provided').to.throw(Error); + expect(() => qb.limit(10,false), 'false provided').to.throw(Error); + expect(() => qb.limit(10,''), 'empty string provided').to.throw(Error); + expect(() => qb.limit(10,{}), 'empty object provided').to.throw(Error); + expect(() => qb.limit(10,[]), 'empty array provided').to.throw(Error); + expect(() => qb.limit(10,[5]), 'array with integer in it provided').to.throw(Error); + }); + it('should override the default limit_to value when a limit is provided', () => { + qb.reset_query(); + qb.limit(10); + qb.limit_to.should.eql([10]); + }); + it('should override the default limit_to and offset_val values when a limit and an offset are provided', () => { + qb.reset_query(); + qb.limit(10,20); + qb.limit_to.should.eql([10]); + qb.offset_val.should.eql([20]); + }); + it('should trim string values that are provided', () => { + qb.reset_query(); + qb.limit('10 '); + qb.limit_to.should.eql([10]); + }); + it('should trim string values that are provided', () => { + qb.reset_query(); + qb.limit(' 10 ',' 12'); + qb.limit_to.should.eql([10]); + qb.offset_val.should.eql([12]); + }); + it('should override values set by any previous calls to itself', () => { + qb.reset_query(); + qb.limit(10); + qb.limit_to.should.eql([10]); + qb.limit(20); + qb.limit_to.should.eql([20]); + }); + it('should be chainable whereby the last call to the method will contain the value(s) used', () => { + qb.reset_query(); + qb.limit(10,5).limit(20).limit(100,30); + qb.limit_to.should.eql([100]); + qb.offset_val.should.eql([30]); + }); }); diff --git a/test/mysql/01-tests-offset.js b/test/mysql/01-tests-offset.js index 3857573..2e6bc7e 100755 --- a/test/mysql/01-tests-offset.js +++ b/test/mysql/01-tests-offset.js @@ -3,58 +3,58 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('offset()', () => { - it('should exist', () => { - should.exist(qb.offset); - }); - it('should be a function', () => { - qb.offset.should.be.a('function'); - }); - it('should have an array to put fields into', () => { - qb.should.have.property('offset_val'); - }); - it('should have an empty array to put fields into at the beginning', () => { - qb.offset_val.should.be.empty; - }); - it('should require an integer (or integer in string form) in first parameter', () => { - qb.reset_query(); - expect(() => qb.offset(5), 'integer provided').to.not.throw(Error); - expect(() => qb.offset('5'), '5 in string form provided').to.not.throw(Error); - expect(() => qb.offset(5.7), 'float provided').to.throw(Error); - expect(() => qb.offset('5.7'), 'float provided').to.throw(Error); - expect(() => qb.offset('abc'), 'alpha provided').to.throw(Error); - expect(() => qb.offset('abc7'), 'alpha numerics provided').to.throw(Error); - expect(() => qb.offset(), 'nothing provided').to.throw(Error); - expect(() => qb.offset(null), 'null provided').to.throw(Error); - expect(() => qb.offset(true), 'true provided').to.throw(Error); - expect(() => qb.offset(false), 'false provided').to.throw(Error); - expect(() => qb.offset(''), 'empty string provided').to.throw(Error); - expect(() => qb.offset({}), 'empty object provided').to.throw(Error); - expect(() => qb.offset([]), 'empty array provided').to.throw(Error); - expect(() => qb.offset([5]), 'array with integer in it provided').to.throw(Error); - }); - it('should override the default offset_val value when a offset is provided', () => { - qb.reset_query(); - qb.offset(10); - qb.offset_val.should.eql([10]); - }); - it('should trim string values that are provided', () => { - qb.reset_query(); - qb.offset('10 '); - qb.offset(' 10'); - qb.offset(' 10 '); - qb.offset_val.should.eql([10]); - }); - it('should override values set by any previous calls to itself', () => { - qb.reset_query(); - qb.offset(10); - qb.offset_val.should.eql([10]); - qb.offset(20); - qb.offset_val.should.eql([20]); - }); - it('should be chainable whereby the last call to the method will contain the value used', () => { - qb.reset_query(); - qb.offset(10).offset(20).offset(100); - qb.offset_val.should.eql([100]); - }); +describe('MySQL: offset()', () => { + it('should exist', () => { + should.exist(qb.offset); + }); + it('should be a function', () => { + qb.offset.should.be.a('function'); + }); + it('should have an array to put fields into', () => { + qb.should.have.property('offset_val'); + }); + it('should have an empty array to put fields into at the beginning', () => { + qb.offset_val.should.be.empty; + }); + it('should require an integer (or integer in string form) in first parameter', () => { + qb.reset_query(); + expect(() => qb.offset(5), 'integer provided').to.not.throw(Error); + expect(() => qb.offset('5'), '5 in string form provided').to.not.throw(Error); + expect(() => qb.offset(5.7), 'float provided').to.throw(Error); + expect(() => qb.offset('5.7'), 'float provided').to.throw(Error); + expect(() => qb.offset('abc'), 'alpha provided').to.throw(Error); + expect(() => qb.offset('abc7'), 'alpha numerics provided').to.throw(Error); + expect(() => qb.offset(), 'nothing provided').to.throw(Error); + expect(() => qb.offset(null), 'null provided').to.throw(Error); + expect(() => qb.offset(true), 'true provided').to.throw(Error); + expect(() => qb.offset(false), 'false provided').to.throw(Error); + expect(() => qb.offset(''), 'empty string provided').to.throw(Error); + expect(() => qb.offset({}), 'empty object provided').to.throw(Error); + expect(() => qb.offset([]), 'empty array provided').to.throw(Error); + expect(() => qb.offset([5]), 'array with integer in it provided').to.throw(Error); + }); + it('should override the default offset_val value when a offset is provided', () => { + qb.reset_query(); + qb.offset(10); + qb.offset_val.should.eql([10]); + }); + it('should trim string values that are provided', () => { + qb.reset_query(); + qb.offset('10 '); + qb.offset(' 10'); + qb.offset(' 10 '); + qb.offset_val.should.eql([10]); + }); + it('should override values set by any previous calls to itself', () => { + qb.reset_query(); + qb.offset(10); + qb.offset_val.should.eql([10]); + qb.offset(20); + qb.offset_val.should.eql([20]); + }); + it('should be chainable whereby the last call to the method will contain the value used', () => { + qb.reset_query(); + qb.offset(10).offset(20).offset(100); + qb.offset_val.should.eql([100]); + }); }); diff --git a/test/mysql/01-tests-orderby.js b/test/mysql/01-tests-orderby.js index 470a4c2..e8e6a9b 100755 --- a/test/mysql/01-tests-orderby.js +++ b/test/mysql/01-tests-orderby.js @@ -3,129 +3,129 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('order_by()', () => { - it('should exist', () => { - should.exist(qb.order_by); - }); - it('should be a function', () => { - qb.order_by.should.be.a('function'); - }); - it('should have an array to put fields into', () => { - qb.should.have.property('order_by_array'); - }); - it('should have an empty array to put fields into at the beginning', () => { - qb.order_by_array.should.be.empty; - }); - it('should require non-empty string or array as first param unless random is provided as second parameter', () => { - expect(() => qb.order_by(), 'nothing provided').to.throw(Error); - expect(() => qb.order_by(null), 'null provided').to.throw(Error); - expect(() => qb.order_by(false), 'false provided').to.throw(Error); - expect(() => qb.order_by(true), 'true provided').to.throw(Error); - expect(() => qb.order_by({}), 'empty object provided').to.throw(Error); - expect(() => qb.order_by(3), 'integer provided').to.throw(Error); - expect(() => qb.order_by(3.5), 'float provided').to.throw(Error); - expect(() => qb.order_by([]), 'empty array provided').to.throw(Error); - expect(() => qb.order_by(''), 'empty string provided').to.throw(Error); - // If random - expect(() => qb.order_by('','rand'), 'empty string and random direction provided').to.not.throw(Error); - expect(() => qb.order_by(undefined,'rand'), 'undefined and random direction provided').to.not.throw(Error); - expect(() => qb.order_by(null,'rand'), 'null and random direction provided').to.not.throw(Error); - expect(() => qb.order_by(false,'rand'), 'false and random direction provided').to.not.throw(Error); - expect(() => qb.order_by([],'rand'), 'empty array and random direction provided').to.not.throw(Error); - }); - it('should accept a field and direction separated by a space as first parameter and escape the field', () => { - qb.reset_query(); - qb.order_by('planet_position asc'); - qb.order_by_array.should.eql(['`planet_position` ASC']); - }); - it('should accept a field and direction as seperate parameters and escape the field', () => { - qb.reset_query(); - qb.order_by('planet_position', 'asc'); - qb.order_by_array.should.eql(['`planet_position` ASC']); - }); - it('should add additional order_by calls to teh order by array', () => { - qb.reset_query(); - qb.order_by('planet_position', 'asc'); - qb.order_by('planet_size', 'desc'); - qb.order_by_array.should.eql(['`planet_position` ASC', '`planet_size` DESC']); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.order_by('planet_position', 'asc').order_by('planet_size', 'desc'); - qb.order_by_array.should.eql(['`planet_position` ASC', '`planet_size` DESC']); - }); - it('should assume ASC when no direction is provided', () => { - qb.reset_query(); - qb.order_by('planet_position'); - qb.order_by_array.should.eql(['`planet_position` ASC']); - }); - it('should only accept valid ordering directions (ASC, DESC, random)', () => { - qb.reset_query(); - expect(() => qb.order_by('planet_position')).to.not.throw(Error); - expect(() => qb.order_by('planet_position','ASC')).to.not.throw(Error); - expect(() => qb.order_by('planet_position','DESC')).to.not.throw(Error); - expect(() => qb.order_by('planet_position','random')).to.not.throw(Error); - expect(() => qb.order_by('planet_position',null)).to.not.throw(Error); - expect(() => qb.order_by('planet_position',undefined)).to.not.throw(Error); - expect(() => qb.order_by('planet_position',false)).to.not.throw(Error); - expect(() => qb.order_by('planet_position',3)).to.not.throw(Error); - expect(() => qb.order_by('planet_position',true)).to.not.throw(Error); - expect(() => qb.order_by('planet_position',[])).to.not.throw(Error); - expect(() => qb.order_by('planet_position',{})).to.not.throw(Error); - expect(() => qb.order_by('planet_position','')).to.not.throw(Error); +describe('MySQL: order_by()', () => { + it('should exist', () => { + should.exist(qb.order_by); + }); + it('should be a function', () => { + qb.order_by.should.be.a('function'); + }); + it('should have an array to put fields into', () => { + qb.should.have.property('order_by_array'); + }); + it('should have an empty array to put fields into at the beginning', () => { + qb.order_by_array.should.be.empty; + }); + it('should require non-empty string or array as first param unless random is provided as second parameter', () => { + expect(() => qb.order_by(), 'nothing provided').to.throw(Error); + expect(() => qb.order_by(null), 'null provided').to.throw(Error); + expect(() => qb.order_by(false), 'false provided').to.throw(Error); + expect(() => qb.order_by(true), 'true provided').to.throw(Error); + expect(() => qb.order_by({}), 'empty object provided').to.throw(Error); + expect(() => qb.order_by(3), 'integer provided').to.throw(Error); + expect(() => qb.order_by(3.5), 'float provided').to.throw(Error); + expect(() => qb.order_by([]), 'empty array provided').to.throw(Error); + expect(() => qb.order_by(''), 'empty string provided').to.throw(Error); + // If random + expect(() => qb.order_by('','rand'), 'empty string and random direction provided').to.not.throw(Error); + expect(() => qb.order_by(undefined,'rand'), 'undefined and random direction provided').to.not.throw(Error); + expect(() => qb.order_by(null,'rand'), 'null and random direction provided').to.not.throw(Error); + expect(() => qb.order_by(false,'rand'), 'false and random direction provided').to.not.throw(Error); + expect(() => qb.order_by([],'rand'), 'empty array and random direction provided').to.not.throw(Error); + }); + it('should accept a field and direction separated by a space as first parameter and escape the field', () => { + qb.reset_query(); + qb.order_by('planet_position asc'); + qb.order_by_array.should.eql(['`planet_position` ASC']); + }); + it('should accept a field and direction as seperate parameters and escape the field', () => { + qb.reset_query(); + qb.order_by('planet_position', 'asc'); + qb.order_by_array.should.eql(['`planet_position` ASC']); + }); + it('should add additional order_by calls to teh order by array', () => { + qb.reset_query(); + qb.order_by('planet_position', 'asc'); + qb.order_by('planet_size', 'desc'); + qb.order_by_array.should.eql(['`planet_position` ASC', '`planet_size` DESC']); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.order_by('planet_position', 'asc').order_by('planet_size', 'desc'); + qb.order_by_array.should.eql(['`planet_position` ASC', '`planet_size` DESC']); + }); + it('should assume ASC when no direction is provided', () => { + qb.reset_query(); + qb.order_by('planet_position'); + qb.order_by_array.should.eql(['`planet_position` ASC']); + }); + it('should only accept valid ordering directions (ASC, DESC, random)', () => { + qb.reset_query(); + expect(() => qb.order_by('planet_position')).to.not.throw(Error); + expect(() => qb.order_by('planet_position','ASC')).to.not.throw(Error); + expect(() => qb.order_by('planet_position','DESC')).to.not.throw(Error); + expect(() => qb.order_by('planet_position','random')).to.not.throw(Error); + expect(() => qb.order_by('planet_position',null)).to.not.throw(Error); + expect(() => qb.order_by('planet_position',undefined)).to.not.throw(Error); + expect(() => qb.order_by('planet_position',false)).to.not.throw(Error); + expect(() => qb.order_by('planet_position',3)).to.not.throw(Error); + expect(() => qb.order_by('planet_position',true)).to.not.throw(Error); + expect(() => qb.order_by('planet_position',[])).to.not.throw(Error); + expect(() => qb.order_by('planet_position',{})).to.not.throw(Error); + expect(() => qb.order_by('planet_position','')).to.not.throw(Error); - // Only an invalid string will throw an error - expect(() => qb.order_by('planet_position','FAKE')).to.throw(Error); - }); - it('should accept a comma-separated list of fields to order by with a single direction at the end', () => { - qb.reset_query(); - qb.order_by('planet_position, planet_size asc'); - qb.order_by_array.should.eql(['`planet_position` ASC', '`planet_size` ASC']); - }); - it('should accept a comma-separated list of field + direction pairs', () => { - qb.reset_query(); - qb.order_by('planet_position desc, planet_size asc'); - qb.order_by_array.should.eql(['`planet_position` DESC', '`planet_size` ASC']); - }); - it('should accept a random direction in three forms: "random", "RAND", "RAND()" (case-insensitively) and remove field(s) from statement', () => { - qb.reset_query(); - qb.order_by('planet_position', 'random'); - qb.order_by_array.should.eql(['RAND()']); + // Only an invalid string will throw an error + expect(() => qb.order_by('planet_position','FAKE')).to.throw(Error); + }); + it('should accept a comma-separated list of fields to order by with a single direction at the end', () => { + qb.reset_query(); + qb.order_by('planet_position, planet_size asc'); + qb.order_by_array.should.eql(['`planet_position` ASC', '`planet_size` ASC']); + }); + it('should accept a comma-separated list of field + direction pairs', () => { + qb.reset_query(); + qb.order_by('planet_position desc, planet_size asc'); + qb.order_by_array.should.eql(['`planet_position` DESC', '`planet_size` ASC']); + }); + it('should accept a random direction in three forms: "random", "RAND", "RAND()" (case-insensitively) and remove field(s) from statement', () => { + qb.reset_query(); + qb.order_by('planet_position', 'random'); + qb.order_by_array.should.eql(['RAND()']); - qb.reset_query(); - qb.order_by('planet_size', 'RAND'); - qb.order_by_array.should.eql(['RAND()']); + qb.reset_query(); + qb.order_by('planet_size', 'RAND'); + qb.order_by_array.should.eql(['RAND()']); - qb.reset_query(); - qb.order_by('planet_position, planet_size', 'rand'); - qb.order_by_array.should.eql(['RAND()']); + qb.reset_query(); + qb.order_by('planet_position, planet_size', 'rand'); + qb.order_by_array.should.eql(['RAND()']); - qb.reset_query(); - qb.order_by(null, 'RAND()'); - qb.order_by_array.should.eql(['RAND()']); + qb.reset_query(); + qb.order_by(null, 'RAND()'); + qb.order_by_array.should.eql(['RAND()']); - qb.reset_query(); - qb.order_by('', 'rand'); - qb.order_by_array.should.eql(['RAND()']); - }); - it('should accept an array of field + direction pairs', () => { - qb.reset_query(); - qb.order_by(['planet_position DESC', 'planet_size ASC']); - qb.order_by_array.should.eql(['`planet_position` DESC', '`planet_size` ASC']); - }); - it('should use direction parameter as default when an array of field + direction pairs is provided (when a pair does not contain a direction)', () => { - qb.reset_query(); - qb.order_by(['planet_position', 'planet_size'], 'desc'); - qb.order_by_array.should.eql(['`planet_position` DESC', '`planet_size` DESC']); + qb.reset_query(); + qb.order_by('', 'rand'); + qb.order_by_array.should.eql(['RAND()']); + }); + it('should accept an array of field + direction pairs', () => { + qb.reset_query(); + qb.order_by(['planet_position DESC', 'planet_size ASC']); + qb.order_by_array.should.eql(['`planet_position` DESC', '`planet_size` ASC']); + }); + it('should use direction parameter as default when an array of field + direction pairs is provided (when a pair does not contain a direction)', () => { + qb.reset_query(); + qb.order_by(['planet_position', 'planet_size'], 'desc'); + qb.order_by_array.should.eql(['`planet_position` DESC', '`planet_size` DESC']); - qb.reset_query(); - qb.order_by(['planet_position DESC', 'planet_size'], 'desc'); - qb.order_by_array.should.eql(['`planet_position` DESC', '`planet_size` DESC']); - }); - it('should accept a simple array of fields as first param and default to ASC for the direction if none is provided', () => { - qb.reset_query(); - qb.order_by(['planet_position', 'planet_size']); - qb.order_by_array.should.eql(['`planet_position` ASC', '`planet_size` ASC']); + qb.reset_query(); + qb.order_by(['planet_position DESC', 'planet_size'], 'desc'); + qb.order_by_array.should.eql(['`planet_position` DESC', '`planet_size` DESC']); + }); + it('should accept a simple array of fields as first param and default to ASC for the direction if none is provided', () => { + qb.reset_query(); + qb.order_by(['planet_position', 'planet_size']); + qb.order_by_array.should.eql(['`planet_position` ASC', '`planet_size` ASC']); - }); + }); }); diff --git a/test/mysql/01-tests-select.js b/test/mysql/01-tests-select.js index 2fe8e96..5c1af94 100755 --- a/test/mysql/01-tests-select.js +++ b/test/mysql/01-tests-select.js @@ -3,205 +3,205 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('select()', () => { - it('should exist', () => { - should.exist(qb.select); - }); - it('should be a function', () => { - qb.select.should.be.a('function'); - }); - it('should have an array to put fields into', () => { - qb.should.have.property('select_array'); - }); - it('should have an empty array to put fields into at the beginning', () => { - qb.select_array.should.be.empty; - }); - it('should require an array or string to be passed as first parameter', () => { - const invalid_match = /requires a string or array/; - const empty_str_match = /string is empty/; - const empty_arr_match = /array is empty/; - expect(() => qb.select(), 'nothing provided').to.throw(Error, invalid_match); - expect(() => qb.select(true), 'true provided').to.throw(Error, invalid_match); - expect(() => qb.select(null), 'null provided').to.throw(Error, invalid_match); - expect(() => qb.select(false), 'false provided').to.throw(Error, invalid_match); - expect(() => qb.select({}), 'object provided').to.throw(Error, invalid_match); - expect(() => qb.select([]), 'empty array provided').to.throw(Error, empty_arr_match); - expect(() => qb.select(''), 'empty string provided').to.throw(Error, empty_str_match); - expect(() => qb.select(' '), 'string of spaces provided').to.throw(Error, empty_str_match); - expect(() => qb.select('blah'), 'valid string provided').to.not.throw(Error); - }); - it('should add field to array and escape it properly', () => { - qb.reset_query(); - qb.select('notes'); - qb.select_array.should.eql(['`notes`']); - }); - it('should trim fields properly before placing them into the select array', () => { - qb.reset_query(); - qb.select(' notes '); - qb.select_array.should.eql(['`notes`']); - }); - it('should have an empty array after resetting', () => { - qb.reset_query(); - qb.select_array.should.be.empty; - }); - it('should not escape fields if asked not to', () => { - qb.reset_query(); - qb.select('foo',false); - qb.select_array.should.eql(['foo']); - }); - it('should accept a comma-delimited string of field names and trim and escape each properly', () => { - qb.reset_query(); - qb.select('do,re , mi, fa'); - qb.select_array.should.eql(['`do`','`re`','`mi`','`fa`']); - }); - it('should be allowed to be called multiple times to add multiple fields to the select array', () => { - qb.reset_query(); - qb.select('do').select('re').select('mi').select('fa'); - qb.select_array.should.eql(['`do`','`re`','`mi`','`fa`']); - }); - it('should be allowed to be called multiple times to add multiple escaped and/or non-escaped fields to the select array', () => { - qb.reset_query(); - qb.select('do').select('re',false).select('mi',false).select('fa'); - qb.select_array.should.eql(['`do`','re','mi','`fa`']); - }); - it('should accept an array of fields and add them individually to the select array', () => { - qb.reset_query(); - qb.select(['sol','la','ti','do']); - qb.select_array.should.eql(['`sol`','`la`','`ti`','`do`']); - }); - it('should accept an array of fields and add them individually to the select array without escaping, if asked not to', () => { - qb.reset_query(); - qb.select(['sol','la','ti','do'],false); - qb.select_array.should.eql(['sol','la','ti','do']); - }); - it('should accept an array of fields (some manually escaped) and add them individually to the select array without auto-escaping, if asked not to', () => { - qb.reset_query(); - qb.select(['`sol`','la','ti','`do`'],false); - qb.select_array.should.eql(['`sol`','la','ti','`do`']); - }); - it('should not double-escape a field', () => { - qb.reset_query(); - qb.select('`do`'); - qb.select_array.should.eql(['`do`']); - }); - it('should not double-escape fields when provided with an array of pre-escaped fields', () => { - qb.reset_query(); - qb.select(['`do`','`re`','`mi`']); - qb.select_array.should.eql(['`do`','`re`','`mi`']); - }); - it('should not double-escape fields when provided with an array of pre-escaped fields but should escpae non-pre-escaped fields', () => { - qb.reset_query(); - qb.select(['`do`','re','`mi`']); - qb.select_array.should.eql(['`do`','`re`','`mi`']); - }); - it('should allow for field aliases to be provided and those fields and aliases should be properly escaped', () => { - qb.reset_query(); - qb.select('foo as bar'); - qb.select_array.should.eql(['`foo` as `bar`']); - }); - it('should not double-escape aliases', () => { - qb.reset_query(); - qb.select(['foo as `bar`']); - qb.select_array.should.eql(['`foo` as `bar`']); - }); - it('should allow for multiple fields with aliases to be provided and those fields and aliases should be properly escaped', () => { - qb.reset_query(); - qb.select(['foo as bar','bar as foo']); - qb.select_array.should.eql(['`foo` as `bar`','`bar` as `foo`']); - }); - it('should allow for field aliases with spaces in them', () => { - qb.reset_query(); - qb.select('notes as The Notes'); - qb.select_array.should.eql(['`notes` as `The Notes`']); - }); - it('should allow for a comma-delimited list of fields with aliases to be provided and those fields and aliases should be properly escaped', () => { - qb.reset_query(); - qb.select('foo as bar, bar as foo, foobar as `Foo Bar`'); - qb.select_array.should.eql(['`foo` as `bar`','`bar` as `foo`','`foobar` as `Foo Bar`']); - }); - it('should allow for namespacing in field name (host.db.table.field)', () => { - qb.reset_query(); - qb.select('star_system.planet'); - qb.select_array.should.eql(['`star_system`.`planet`']); +describe('MySQL: select()', () => { + it('should exist', () => { + should.exist(qb.select); + }); + it('should be a function', () => { + qb.select.should.be.a('function'); + }); + it('should have an array to put fields into', () => { + qb.should.have.property('select_array'); + }); + it('should have an empty array to put fields into at the beginning', () => { + qb.select_array.should.be.empty; + }); + it('should require an array or string to be passed as first parameter', () => { + const invalid_match = /requires a string or array/; + const empty_str_match = /string is empty/; + const empty_arr_match = /array is empty/; + expect(() => qb.select(), 'nothing provided').to.throw(Error, invalid_match); + expect(() => qb.select(true), 'true provided').to.throw(Error, invalid_match); + expect(() => qb.select(null), 'null provided').to.throw(Error, invalid_match); + expect(() => qb.select(false), 'false provided').to.throw(Error, invalid_match); + expect(() => qb.select({}), 'object provided').to.throw(Error, invalid_match); + expect(() => qb.select([]), 'empty array provided').to.throw(Error, empty_arr_match); + expect(() => qb.select(''), 'empty string provided').to.throw(Error, empty_str_match); + expect(() => qb.select(' '), 'string of spaces provided').to.throw(Error, empty_str_match); + expect(() => qb.select('blah'), 'valid string provided').to.not.throw(Error); + }); + it('should add field to array and escape it properly', () => { + qb.reset_query(); + qb.select('notes'); + qb.select_array.should.eql(['`notes`']); + }); + it('should trim fields properly before placing them into the select array', () => { + qb.reset_query(); + qb.select(' notes '); + qb.select_array.should.eql(['`notes`']); + }); + it('should have an empty array after resetting', () => { + qb.reset_query(); + qb.select_array.should.be.empty; + }); + it('should not escape fields if asked not to', () => { + qb.reset_query(); + qb.select('foo',false); + qb.select_array.should.eql(['foo']); + }); + it('should accept a comma-delimited string of field names and trim and escape each properly', () => { + qb.reset_query(); + qb.select('do,re , mi, fa'); + qb.select_array.should.eql(['`do`','`re`','`mi`','`fa`']); + }); + it('should be allowed to be called multiple times to add multiple fields to the select array', () => { + qb.reset_query(); + qb.select('do').select('re').select('mi').select('fa'); + qb.select_array.should.eql(['`do`','`re`','`mi`','`fa`']); + }); + it('should be allowed to be called multiple times to add multiple escaped and/or non-escaped fields to the select array', () => { + qb.reset_query(); + qb.select('do').select('re',false).select('mi',false).select('fa'); + qb.select_array.should.eql(['`do`','re','mi','`fa`']); + }); + it('should accept an array of fields and add them individually to the select array', () => { + qb.reset_query(); + qb.select(['sol','la','ti','do']); + qb.select_array.should.eql(['`sol`','`la`','`ti`','`do`']); + }); + it('should accept an array of fields and add them individually to the select array without escaping, if asked not to', () => { + qb.reset_query(); + qb.select(['sol','la','ti','do'],false); + qb.select_array.should.eql(['sol','la','ti','do']); + }); + it('should accept an array of fields (some manually escaped) and add them individually to the select array without auto-escaping, if asked not to', () => { + qb.reset_query(); + qb.select(['`sol`','la','ti','`do`'],false); + qb.select_array.should.eql(['`sol`','la','ti','`do`']); + }); + it('should not double-escape a field', () => { + qb.reset_query(); + qb.select('`do`'); + qb.select_array.should.eql(['`do`']); + }); + it('should not double-escape fields when provided with an array of pre-escaped fields', () => { + qb.reset_query(); + qb.select(['`do`','`re`','`mi`']); + qb.select_array.should.eql(['`do`','`re`','`mi`']); + }); + it('should not double-escape fields when provided with an array of pre-escaped fields but should escpae non-pre-escaped fields', () => { + qb.reset_query(); + qb.select(['`do`','re','`mi`']); + qb.select_array.should.eql(['`do`','`re`','`mi`']); + }); + it('should allow for field aliases to be provided and those fields and aliases should be properly escaped', () => { + qb.reset_query(); + qb.select('foo as bar'); + qb.select_array.should.eql(['`foo` as `bar`']); + }); + it('should not double-escape aliases', () => { + qb.reset_query(); + qb.select(['foo as `bar`']); + qb.select_array.should.eql(['`foo` as `bar`']); + }); + it('should allow for multiple fields with aliases to be provided and those fields and aliases should be properly escaped', () => { + qb.reset_query(); + qb.select(['foo as bar','bar as foo']); + qb.select_array.should.eql(['`foo` as `bar`','`bar` as `foo`']); + }); + it('should allow for field aliases with spaces in them', () => { + qb.reset_query(); + qb.select('notes as The Notes'); + qb.select_array.should.eql(['`notes` as `The Notes`']); + }); + it('should allow for a comma-delimited list of fields with aliases to be provided and those fields and aliases should be properly escaped', () => { + qb.reset_query(); + qb.select('foo as bar, bar as foo, foobar as `Foo Bar`'); + qb.select_array.should.eql(['`foo` as `bar`','`bar` as `foo`','`foobar` as `Foo Bar`']); + }); + it('should allow for namespacing in field name (host.db.table.field)', () => { + qb.reset_query(); + qb.select('star_system.planet'); + qb.select_array.should.eql(['`star_system`.`planet`']); - qb.reset_query(); - qb.select('galaxy.star_system.planet'); - qb.select_array.should.eql(['`galaxy`.`star_system`.`planet`']); + qb.reset_query(); + qb.select('galaxy.star_system.planet'); + qb.select_array.should.eql(['`galaxy`.`star_system`.`planet`']); - qb.reset_query(); - qb.select('universe.galaxy.star_system.planet'); - qb.select_array.should.eql(['`universe`.`galaxy`.`star_system`.`planet`']); - }); - it('should allow for namespacing in field name (host.db.table.column) + alias', () => { - qb.reset_query(); - qb.select('universe.galaxy.star_system.planet as planet'); - qb.select_array.should.eql(['`universe`.`galaxy`.`star_system`.`planet` as `planet`']); - }); - it('should not allow subqueries without the second parameter being false', () => { - qb.reset_query(); - expect( - () => qb.select('s.star_systems, (select count(p.*) as count from planets p where p.star_system_id IN(2,3,5)) as num_planets') - ).to.throw(Error); + qb.reset_query(); + qb.select('universe.galaxy.star_system.planet'); + qb.select_array.should.eql(['`universe`.`galaxy`.`star_system`.`planet`']); + }); + it('should allow for namespacing in field name (host.db.table.column) + alias', () => { + qb.reset_query(); + qb.select('universe.galaxy.star_system.planet as planet'); + qb.select_array.should.eql(['`universe`.`galaxy`.`star_system`.`planet` as `planet`']); + }); + it('should not allow subqueries without the second parameter being false', () => { + qb.reset_query(); + expect( + () => qb.select('s.star_systems, (select count(p.*) as count from planets p where p.star_system_id IN(2,3,5)) as num_planets') + ).to.throw(Error); - expect( - () => qb.select('s.star_systems, (select count(p.*) as count from planets p where p.star_system_id = 42) as num_planets') - ).to.throw(Error); + expect( + () => qb.select('s.star_systems, (select count(p.*) as count from planets p where p.star_system_id = 42) as num_planets') + ).to.throw(Error); - expect( - () => qb.select('s.star_systems, (select count(p.*) as count from planets p where p.star_system_id IN(2,3,5)) as num_planets', false) - ).to.not.throw(Error); - }); - it('should not allow functions without the second paramter being false', () => { - expect( - () => qb.select('s.star_systems, count(planets) as num_planets') - ).to.throw(Error); + expect( + () => qb.select('s.star_systems, (select count(p.*) as count from planets p where p.star_system_id IN(2,3,5)) as num_planets', false) + ).to.not.throw(Error); + }); + it('should not allow functions without the second paramter being false', () => { + expect( + () => qb.select('s.star_systems, count(planets) as num_planets') + ).to.throw(Error); - expect( - () => qb.select('s.star_systems, if(num_planets > 0, true, false) as has_planets') - ).to.throw(Error); + expect( + () => qb.select('s.star_systems, if(num_planets > 0, true, false) as has_planets') + ).to.throw(Error); - expect( - () => qb.select('s.star_systems, count(planets) as num_planets', false) - ).to.not.throw(Error); + expect( + () => qb.select('s.star_systems, count(planets) as num_planets', false) + ).to.not.throw(Error); - expect( - () => qb.select('s.star_systems, if(num_planets > 0, true, false) as has_planets', false) - ).to.not.throw(Error); - }); - it('should allow for functions and subqueries in statement without escaping them (aliases at the end will still be escaped)', () => { - qb.reset_query(); - qb.select('count(*) as count', false); - qb.select_array.should.eql(['count(*) AS `count`']); + expect( + () => qb.select('s.star_systems, if(num_planets > 0, true, false) as has_planets', false) + ).to.not.throw(Error); + }); + it('should allow for functions and subqueries in statement without escaping them (aliases at the end will still be escaped)', () => { + qb.reset_query(); + qb.select('count(*) as count', false); + qb.select_array.should.eql(['count(*) AS `count`']); - qb.reset_query(); - qb.select('count(*) as count, m.*, MIN(id) as min', false); - qb.select_array.should.eql(['count(*) as count, m.*, MIN(id) AS `min`']); + qb.reset_query(); + qb.select('count(*) as count, m.*, MIN(id) as min', false); + qb.select_array.should.eql(['count(*) as count, m.*, MIN(id) AS `min`']); - qb.reset_query(); - qb.select('(select count(p.*) as count from planets p) as num_planets', false); - qb.select_array.should.eql(['(select count(p.*) as count from planets p) AS `num_planets`']); + qb.reset_query(); + qb.select('(select count(p.*) as count from planets p) as num_planets', false); + qb.select_array.should.eql(['(select count(p.*) as count from planets p) AS `num_planets`']); - qb.reset_query(); - qb.select('s.star_systems, (select count(p.*) as count from planets p where p.star_system_id = s.id) as num_planets', false); - qb.select_array.should.eql(['s.star_systems, (select count(p.*) as count from planets p where p.star_system_id = s.id) AS `num_planets`']); + qb.reset_query(); + qb.select('s.star_systems, (select count(p.*) as count from planets p where p.star_system_id = s.id) as num_planets', false); + qb.select_array.should.eql(['s.star_systems, (select count(p.*) as count from planets p where p.star_system_id = s.id) AS `num_planets`']); - }); + }); }); const prefixes = ['min','max','avg','sum']; for (const i in prefixes) { - const type = prefixes[i]; - describe('select_' + type+'()', () => { - it('should exist', () => { - should.exist(qb['select_' + type]); - }); - it('should be a function', () => { - qb['select_' + type].should.be.a('function'); - }); - it('should place given field as parameter in a ' + type.toUpperCase() + '() MySQL function and alias the result with the original field name', () => { - qb.reset_query(); - qb['select_' + type]('s.star_systems'); - qb.select_array.should.eql([type.toUpperCase() + '(`s`.`star_systems`) AS star_systems']); - }); - }); + const type = prefixes[i]; + describe('MySQL: select_' + type+'()', () => { + it('should exist', () => { + should.exist(qb['select_' + type]); + }); + it('should be a function', () => { + qb['select_' + type].should.be.a('function'); + }); + it('should place given field as parameter in a ' + type.toUpperCase() + '() MySQL function and alias the result with the original field name', () => { + qb.reset_query(); + qb['select_' + type]('s.star_systems'); + qb.select_array.should.eql([type.toUpperCase() + '(`s`.`star_systems`) AS star_systems']); + }); + }); } diff --git a/test/mysql/01-tests-set.js b/test/mysql/01-tests-set.js index eb70c76..2ec5f11 100755 --- a/test/mysql/01-tests-set.js +++ b/test/mysql/01-tests-set.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('set()', () => { +describe('MySQL: set()', () => { it('should exist', () => { should.exist(qb.set); }); diff --git a/test/mysql/01-tests-where.js b/test/mysql/01-tests-where.js index 05d855a..9a84dd4 100755 --- a/test/mysql/01-tests-where.js +++ b/test/mysql/01-tests-where.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('where()', () => { +describe('MySQL: where()', () => { it('should exist', () => { should.exist(qb.where); }); @@ -148,7 +148,7 @@ describe('where()', () => { }); }); -describe('or_where()', () => { +describe('MySQL: or_where()', () => { it('should exist', () => { should.exist(qb.or_where); }); diff --git a/test/mysql/01-tests-where_in.js b/test/mysql/01-tests-where_in.js index 1abe16f..4e70aa8 100755 --- a/test/mysql/01-tests-where_in.js +++ b/test/mysql/01-tests-where_in.js @@ -3,175 +3,175 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('where_in()', () => { - it('should exist', () => { - should.exist(qb.where_in); - }); - it('should be a function', () => { - qb.where_in.should.be.a('function'); - }); - it('should have an array to put fields into', () => { - qb.should.have.property('where_in_array'); - }); - it('should have an empty array to put fields into at the beginning', () => { - qb.where_in_array.should.be.empty; - }); - it('should not accept anything but a non-empty string as first parameter', () => { - qb.reset_query(); - expect(() => qb.where_in(), 'nothing provided').to.throw(Error); - expect(() => qb.where_in(null), 'null provided').to.throw(Error); - expect(() => qb.where_in(false), 'false provided').to.throw(Error); - expect(() => qb.where_in(true), 'true provided').to.throw(Error); - expect(() => qb.where_in({}), 'empty object provided').to.throw(Error); - expect(() => qb.where_in({foo:'bar'}), 'empty object provided').to.throw(Error); - expect(() => qb.where_in(3), 'integer provided').to.throw(Error); - expect(() => qb.where_in(3.5), 'float provided').to.throw(Error); - expect(() => qb.where_in(NaN), 'NaN provided').to.throw(Error); - expect(() => qb.where_in(Infinity), 'Infinity provided').to.throw(Error); - expect(() => qb.where_in([]), 'empty array provided').to.throw(Error); - expect(() => qb.where_in([1,2]), 'array of numbers provided').to.throw(Error); - expect(() => qb.where_in(''), 'empty string provided').to.throw(Error); - expect(() => qb.where_in(/foobar/), 'regex provided').to.throw(Error); +describe('MySQL: where_in()', () => { + it('should exist', () => { + should.exist(qb.where_in); + }); + it('should be a function', () => { + qb.where_in.should.be.a('function'); + }); + it('should have an array to put fields into', () => { + qb.should.have.property('where_in_array'); + }); + it('should have an empty array to put fields into at the beginning', () => { + qb.where_in_array.should.be.empty; + }); + it('should not accept anything but a non-empty string as first parameter', () => { + qb.reset_query(); + expect(() => qb.where_in(), 'nothing provided').to.throw(Error); + expect(() => qb.where_in(null), 'null provided').to.throw(Error); + expect(() => qb.where_in(false), 'false provided').to.throw(Error); + expect(() => qb.where_in(true), 'true provided').to.throw(Error); + expect(() => qb.where_in({}), 'empty object provided').to.throw(Error); + expect(() => qb.where_in({foo:'bar'}), 'empty object provided').to.throw(Error); + expect(() => qb.where_in(3), 'integer provided').to.throw(Error); + expect(() => qb.where_in(3.5), 'float provided').to.throw(Error); + expect(() => qb.where_in(NaN), 'NaN provided').to.throw(Error); + expect(() => qb.where_in(Infinity), 'Infinity provided').to.throw(Error); + expect(() => qb.where_in([]), 'empty array provided').to.throw(Error); + expect(() => qb.where_in([1,2]), 'array of numbers provided').to.throw(Error); + expect(() => qb.where_in(''), 'empty string provided').to.throw(Error); + expect(() => qb.where_in(/foobar/), 'regex provided').to.throw(Error); - expect(() => qb.where_in('planet_position',[1,2,3]), 'valid string provided').to.not.throw(Error); - }); - it('should not accept anything but a non-empty array of values as second parameter', () => { - qb.reset_query(); - expect(() => qb.where_in('planet'), 'nothing provided').to.throw(Error); - expect(() => qb.where_in('planet',null), 'null provided').to.throw(Error); - expect(() => qb.where_in('planet',false), 'false provided').to.throw(Error); - expect(() => qb.where_in('planet',true), 'true provided').to.throw(Error); - expect(() => qb.where_in('planet',{}), 'empty object provided').to.throw(Error); - expect(() => qb.where_in('planet',{foo:'bar'}), 'empty object provided').to.throw(Error); - expect(() => qb.where_in('planet',3), 'integer provided').to.throw(Error); - expect(() => qb.where_in('planet',3.5), 'float provided').to.throw(Error); - expect(() => qb.where_in('planet',NaN), 'NaN provided').to.throw(Error); - expect(() => qb.where_in('planet',Infinity), 'Infinity provided').to.throw(Error); - expect(() => qb.where_in('planet',[]), 'empty array provided').to.throw(Error); - expect(() => qb.where_in('planet',''), 'empty string provided').to.throw(Error); - expect(() => qb.where_in('planet',/foobar/), 'regex provided').to.throw(Error); + expect(() => qb.where_in('planet_position',[1,2,3]), 'valid string provided').to.not.throw(Error); + }); + it('should not accept anything but a non-empty array of values as second parameter', () => { + qb.reset_query(); + expect(() => qb.where_in('planet'), 'nothing provided').to.throw(Error); + expect(() => qb.where_in('planet',null), 'null provided').to.throw(Error); + expect(() => qb.where_in('planet',false), 'false provided').to.throw(Error); + expect(() => qb.where_in('planet',true), 'true provided').to.throw(Error); + expect(() => qb.where_in('planet',{}), 'empty object provided').to.throw(Error); + expect(() => qb.where_in('planet',{foo:'bar'}), 'empty object provided').to.throw(Error); + expect(() => qb.where_in('planet',3), 'integer provided').to.throw(Error); + expect(() => qb.where_in('planet',3.5), 'float provided').to.throw(Error); + expect(() => qb.where_in('planet',NaN), 'NaN provided').to.throw(Error); + expect(() => qb.where_in('planet',Infinity), 'Infinity provided').to.throw(Error); + expect(() => qb.where_in('planet',[]), 'empty array provided').to.throw(Error); + expect(() => qb.where_in('planet',''), 'empty string provided').to.throw(Error); + expect(() => qb.where_in('planet',/foobar/), 'regex provided').to.throw(Error); - expect(() => qb.where_in('planet',['Mars','Earth','Venus','Mercury']), 'non-empty array provided').to.not.throw(Error); - }); - it('should require both a field name an array of values as first and second parameters, respectively', () => { - qb.reset_query(); - qb.where_in('planet_position',[1,2,3]); - qb.where_array.should.eql(['`planet_position` IN (1, 2, 3)']); - }); - it('should concatenate multiple WHERE IN clauses with AND ', () => { - qb.reset_query(); - qb.where_in('planet',['Mercury','Venus','Earth','Mars']); - qb.where_in('galaxy_id',[123,456,789,0110]); - qb.where_array.should.eql(["`planet` IN ('Mercury', 'Venus', 'Earth', 'Mars')","AND `galaxy_id` IN (123, 456, 789, 72)"]); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.where_in('planet',['Mercury','Venus','Earth','Mars']).where_in('planet_position',[1,2,3,4]); - qb.where_array.should.eql(["`planet` IN ('Mercury', 'Venus', 'Earth', 'Mars')","AND `planet_position` IN (1, 2, 3, 4)"]); - }); - it('should not escape fields if asked not to', () => { - qb.reset_query(); - qb.where_in('planet_position',[1,2,3],false); - qb.where_array.should.eql(['planet_position IN (1, 2, 3)']); - }); + expect(() => qb.where_in('planet',['Mars','Earth','Venus','Mercury']), 'non-empty array provided').to.not.throw(Error); + }); + it('should require both a field name an array of values as first and second parameters, respectively', () => { + qb.reset_query(); + qb.where_in('planet_position',[1,2,3]); + qb.where_array.should.eql(['`planet_position` IN (1, 2, 3)']); + }); + it('should concatenate multiple WHERE IN clauses with AND ', () => { + qb.reset_query(); + qb.where_in('planet',['Mercury','Venus','Earth','Mars']); + qb.where_in('galaxy_id',[123,456,789,0110]); + qb.where_array.should.eql(["`planet` IN ('Mercury', 'Venus', 'Earth', 'Mars')","AND `galaxy_id` IN (123, 456, 789, 72)"]); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.where_in('planet',['Mercury','Venus','Earth','Mars']).where_in('planet_position',[1,2,3,4]); + qb.where_array.should.eql(["`planet` IN ('Mercury', 'Venus', 'Earth', 'Mars')","AND `planet_position` IN (1, 2, 3, 4)"]); + }); + it('should not escape fields if asked not to', () => { + qb.reset_query(); + qb.where_in('planet_position',[1,2,3],false); + qb.where_array.should.eql(['planet_position IN (1, 2, 3)']); + }); }); -describe('where_not_in()', () => { - it('should exist', () => { - should.exist(qb.where_not_in); - }); - it('should be a function', () => { - qb.where_not_in.should.be.a('function'); - }); - it('should prepend "NOT " to "IN"', () => { - qb.reset_query(); - qb.where_not_in('planet_position',[1,2,3]); - qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)']); - }); - it('should prepend tertiary WHERE clauses with "AND"', () => { - qb.reset_query(); - qb.where_not_in('planet_position',[1,2,3]); - qb.where_not_in('planet_position',[5,6,7]); - qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)', "AND `planet_position` NOT IN (5, 6, 7)"]); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.where_not_in('planet_position',[1,2,3]).where_not_in('planet_position',[5,6,7]); - qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)', "AND `planet_position` NOT IN (5, 6, 7)"]); - }); - it('should be chainable with normal where', () => { - qb.reset_query(); - qb.where('planet','Mars').where('galaxy','Milky Way').where_not_in('planet_position',[5,6,7]); - qb.where_array.should.eql(["`planet` = 'Mars'", "AND `galaxy` = 'Milky Way'", "AND `planet_position` NOT IN (5, 6, 7)"]); - }); - it('should not escape fields if asked not to', () => { - qb.reset_query(); - qb.where_not_in('planet_position',[1,2,3],false); - qb.where_array.should.eql(['planet_position NOT IN (1, 2, 3)']); - }); +describe('MySQL: where_not_in()', () => { + it('should exist', () => { + should.exist(qb.where_not_in); + }); + it('should be a function', () => { + qb.where_not_in.should.be.a('function'); + }); + it('should prepend "NOT " to "IN"', () => { + qb.reset_query(); + qb.where_not_in('planet_position',[1,2,3]); + qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)']); + }); + it('should prepend tertiary WHERE clauses with "AND"', () => { + qb.reset_query(); + qb.where_not_in('planet_position',[1,2,3]); + qb.where_not_in('planet_position',[5,6,7]); + qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)', "AND `planet_position` NOT IN (5, 6, 7)"]); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.where_not_in('planet_position',[1,2,3]).where_not_in('planet_position',[5,6,7]); + qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)', "AND `planet_position` NOT IN (5, 6, 7)"]); + }); + it('should be chainable with normal where', () => { + qb.reset_query(); + qb.where('planet','Mars').where('galaxy','Milky Way').where_not_in('planet_position',[5,6,7]); + qb.where_array.should.eql(["`planet` = 'Mars'", "AND `galaxy` = 'Milky Way'", "AND `planet_position` NOT IN (5, 6, 7)"]); + }); + it('should not escape fields if asked not to', () => { + qb.reset_query(); + qb.where_not_in('planet_position',[1,2,3],false); + qb.where_array.should.eql(['planet_position NOT IN (1, 2, 3)']); + }); }); -describe('or_where_in()', () => { - it('should exist', () => { - should.exist(qb.or_where_in); - }); - it('should be a function', () => { - qb.or_where_in.should.be.a('function'); - }); - it('should prepend tertiary WHERE clauses with "OR"', () => { - qb.reset_query(); - qb.or_where_in('planet_position',[1,2,3]); - qb.or_where_in('planet_position',[5,6,7]); - qb.where_array.should.eql(['`planet_position` IN (1, 2, 3)', "OR `planet_position` IN (5, 6, 7)"]); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.or_where_in('planet_position',[1,2,3]).or_where_in('planet_position',[5,6,7]); - qb.where_array.should.eql(['`planet_position` IN (1, 2, 3)', "OR `planet_position` IN (5, 6, 7)"]); - }); - it('should be chainable with normal where', () => { - qb.reset_query(); - qb.where('planet','Mars').where('galaxy','Milky Way').or_where_in('planet_position',[5,6,7]); - qb.where_array.should.eql(["`planet` = 'Mars'", "AND `galaxy` = 'Milky Way'", "OR `planet_position` IN (5, 6, 7)"]); - }); - it('should not escape fields if asked not to', () => { - qb.reset_query(); - qb.or_where_in('planet_position',[1,2,3],false); - qb.where_array.should.eql(['planet_position IN (1, 2, 3)']); - }); +describe('MySQL: or_where_in()', () => { + it('should exist', () => { + should.exist(qb.or_where_in); + }); + it('should be a function', () => { + qb.or_where_in.should.be.a('function'); + }); + it('should prepend tertiary WHERE clauses with "OR"', () => { + qb.reset_query(); + qb.or_where_in('planet_position',[1,2,3]); + qb.or_where_in('planet_position',[5,6,7]); + qb.where_array.should.eql(['`planet_position` IN (1, 2, 3)', "OR `planet_position` IN (5, 6, 7)"]); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.or_where_in('planet_position',[1,2,3]).or_where_in('planet_position',[5,6,7]); + qb.where_array.should.eql(['`planet_position` IN (1, 2, 3)', "OR `planet_position` IN (5, 6, 7)"]); + }); + it('should be chainable with normal where', () => { + qb.reset_query(); + qb.where('planet','Mars').where('galaxy','Milky Way').or_where_in('planet_position',[5,6,7]); + qb.where_array.should.eql(["`planet` = 'Mars'", "AND `galaxy` = 'Milky Way'", "OR `planet_position` IN (5, 6, 7)"]); + }); + it('should not escape fields if asked not to', () => { + qb.reset_query(); + qb.or_where_in('planet_position',[1,2,3],false); + qb.where_array.should.eql(['planet_position IN (1, 2, 3)']); + }); }); -describe('or_where_not_in()', () => { - it('should exist', () => { - should.exist(qb.or_where_in); - }); - it('should be a function', () => { - qb.or_where_in.should.be.a('function'); - }); - it('should prepend "NOT " to "IN"', () => { - qb.reset_query(); - qb.or_where_not_in('planet_position',[1,2,3]); - qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)']); - }); - it('should prepend tertiary WHERE clauses with "OR"', () => { - qb.reset_query(); - qb.or_where_not_in('planet_position',[1,2,3]); - qb.or_where_not_in('planet_position',[5,6,7]); - qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)', "OR `planet_position` NOT IN (5, 6, 7)"]); - }); - it('should be chainable', () => { - qb.reset_query(); - qb.or_where_not_in('planet_position',[1,2,3]).or_where_not_in('planet_position',[5,6,7]); - qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)', "OR `planet_position` NOT IN (5, 6, 7)"]); - }); - it('should be chainable with normal where', () => { - qb.reset_query(); - qb.where('planet','Mars').where('galaxy','Milky Way').or_where_not_in('planet_position',[5,6,7]); - qb.where_array.should.eql(["`planet` = 'Mars'", "AND `galaxy` = 'Milky Way'", "OR `planet_position` NOT IN (5, 6, 7)"]); - }); - it('should not escape fields if asked not to', () => { - qb.reset_query(); - qb.or_where_not_in('planet_position',[1,2,3],false); - qb.where_array.should.eql(['planet_position NOT IN (1, 2, 3)']); - }); +describe('MySQL: or_where_not_in()', () => { + it('should exist', () => { + should.exist(qb.or_where_in); + }); + it('should be a function', () => { + qb.or_where_in.should.be.a('function'); + }); + it('should prepend "NOT " to "IN"', () => { + qb.reset_query(); + qb.or_where_not_in('planet_position',[1,2,3]); + qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)']); + }); + it('should prepend tertiary WHERE clauses with "OR"', () => { + qb.reset_query(); + qb.or_where_not_in('planet_position',[1,2,3]); + qb.or_where_not_in('planet_position',[5,6,7]); + qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)', "OR `planet_position` NOT IN (5, 6, 7)"]); + }); + it('should be chainable', () => { + qb.reset_query(); + qb.or_where_not_in('planet_position',[1,2,3]).or_where_not_in('planet_position',[5,6,7]); + qb.where_array.should.eql(['`planet_position` NOT IN (1, 2, 3)', "OR `planet_position` NOT IN (5, 6, 7)"]); + }); + it('should be chainable with normal where', () => { + qb.reset_query(); + qb.where('planet','Mars').where('galaxy','Milky Way').or_where_not_in('planet_position',[5,6,7]); + qb.where_array.should.eql(["`planet` = 'Mars'", "AND `galaxy` = 'Milky Way'", "OR `planet_position` NOT IN (5, 6, 7)"]); + }); + it('should not escape fields if asked not to', () => { + qb.reset_query(); + qb.or_where_not_in('planet_position',[1,2,3],false); + qb.where_array.should.eql(['planet_position NOT IN (1, 2, 3)']); + }); }); diff --git a/test/mysql/02-tests-compilation_methods.js b/test/mysql/02-tests-compilation_methods.js index 7e02e3a..a0547fc 100755 --- a/test/mysql/02-tests-compilation_methods.js +++ b/test/mysql/02-tests-compilation_methods.js @@ -3,68 +3,68 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('get_compiled_select()', () => { - it('should exist', () => { - should.exist(qb.get_compiled_select); - }); - it('should be a function', () => { - qb.get_compiled_select.should.be.a('function'); - }); - it('should add a table to from_array when a table is supplied', () => { - qb.reset_query(); - qb.get_compiled_select('galaxies'); - qb.from_array.should.eql(['`galaxies`']); - }); - it('should add a set of tables to from_array when an array of tables is supplied', () => { - qb.reset_query(); - qb.get_compiled_select(['galaxies','star_systems','planets']); - qb.from_array.should.eql(['`galaxies`','`star_systems`','`planets`']); - }); - it('should return a SQL string', () => { - qb.reset_query(); - const sql = qb.get_compiled_select('galaxies'); - sql.should.eql('SELECT * FROM `galaxies`'); - }); +describe('MySQL: get_compiled_select()', () => { + it('should exist', () => { + should.exist(qb.get_compiled_select); + }); + it('should be a function', () => { + qb.get_compiled_select.should.be.a('function'); + }); + it('should add a table to from_array when a table is supplied', () => { + qb.reset_query(); + qb.get_compiled_select('galaxies'); + qb.from_array.should.eql(['`galaxies`']); + }); + it('should add a set of tables to from_array when an array of tables is supplied', () => { + qb.reset_query(); + qb.get_compiled_select(['galaxies','star_systems','planets']); + qb.from_array.should.eql(['`galaxies`','`star_systems`','`planets`']); + }); + it('should return a SQL string', () => { + qb.reset_query(); + const sql = qb.get_compiled_select('galaxies'); + sql.should.eql('SELECT * FROM `galaxies`'); + }); }); -describe('get_compiled_insert()', () => { - it('should exist', () => { - should.exist(qb.get_compiled_insert); - }); - it('should be a function', () => { - qb.get_compiled_insert.should.be.a('function'); - }); - it('should return a SQL string', () => { - qb.reset_query(); - const sql = qb.set({foo:'bar'}).get_compiled_insert('galaxies'); - sql.should.eql("INSERT INTO `galaxies` (`foo`) VALUES ('bar')"); - }); +describe('MySQL: get_compiled_insert()', () => { + it('should exist', () => { + should.exist(qb.get_compiled_insert); + }); + it('should be a function', () => { + qb.get_compiled_insert.should.be.a('function'); + }); + it('should return a SQL string', () => { + qb.reset_query(); + const sql = qb.set({foo:'bar'}).get_compiled_insert('galaxies'); + sql.should.eql("INSERT INTO `galaxies` (`foo`) VALUES ('bar')"); + }); }); -describe('get_compiled_update()', () => { - it('should exist', () => { - should.exist(qb.get_compiled_update); - }); - it('should be a function', () => { - qb.get_compiled_update.should.be.a('function'); - }); - it('should return a SQL string', () => { - qb.reset_query(); - const sql = qb.set({foo:'bar'}).where('id',45).get_compiled_update('galaxies'); - sql.should.eql("UPDATE (`galaxies`) SET `foo` = 'bar' WHERE `id` = 45"); - }); +describe('MySQL: get_compiled_update()', () => { + it('should exist', () => { + should.exist(qb.get_compiled_update); + }); + it('should be a function', () => { + qb.get_compiled_update.should.be.a('function'); + }); + it('should return a SQL string', () => { + qb.reset_query(); + const sql = qb.set({foo:'bar'}).where('id',45).get_compiled_update('galaxies'); + sql.should.eql("UPDATE (`galaxies`) SET `foo` = 'bar' WHERE `id` = 45"); + }); }); -describe('get_compiled_delete()', () => { - it('should exist', () => { - should.exist(qb.get_compiled_delete); - }); - it('should be a function', () => { - qb.get_compiled_delete.should.be.a('function'); - }); - it('should return a SQL string', () => { - qb.reset_query(); - const sql = qb.where('id',45).get_compiled_delete('galaxies'); - sql.should.eql("DELETE FROM `galaxies` WHERE `id` = 45"); - }); +describe('MySQL: get_compiled_delete()', () => { + it('should exist', () => { + should.exist(qb.get_compiled_delete); + }); + it('should be a function', () => { + qb.get_compiled_delete.should.be.a('function'); + }); + it('should return a SQL string', () => { + qb.reset_query(); + const sql = qb.where('id',45).get_compiled_delete('galaxies'); + sql.should.eql("DELETE FROM `galaxies` WHERE `id` = 45"); + }); }); diff --git a/test/mysql/03-tests-count.js b/test/mysql/03-tests-count.js index a50d4e1..1a8efe2 100755 --- a/test/mysql/03-tests-count.js +++ b/test/mysql/03-tests-count.js @@ -3,7 +3,7 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('count()', () => { +describe('MySQL: count()', () => { it('should exist', () => { should.exist(qb.count); }); diff --git a/test/mysql/03-tests-delete.js b/test/mysql/03-tests-delete.js index 4744e41..392fd8a 100755 --- a/test/mysql/03-tests-delete.js +++ b/test/mysql/03-tests-delete.js @@ -3,89 +3,89 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('delete()', () => { - it('should exist', () => { - should.exist(qb.delete); - }); - it('should be a function', () => { - qb.delete.should.be.a('function'); - }); - it('should add a table to from_array when a table is supplied', () => { - qb.reset_query(); - qb.delete('galaxies'); - qb.from_array.should.eql(['`galaxies`']); - }); - it('should only accept nothing or a non-empty-string for the table (first) parameter', () => { - qb.reset_query(); +describe('MySQL: delete()', () => { + it('should exist', () => { + should.exist(qb.delete); + }); + it('should be a function', () => { + qb.delete.should.be.a('function'); + }); + it('should add a table to from_array when a table is supplied', () => { + qb.reset_query(); + qb.delete('galaxies'); + qb.from_array.should.eql(['`galaxies`']); + }); + it('should only accept nothing or a non-empty-string for the table (first) parameter', () => { + qb.reset_query(); - expect(() => qb.delete([]), 'empty array provided').to.throw(Error); - expect(() => qb.delete({}), 'empty object provided').to.throw(Error); - expect(() => qb.delete(3), 'integer provided').to.throw(Error); - expect(() => qb.delete(3.5), 'float provided').to.throw(Error); - expect(() => qb.delete(true), 'true provided').to.throw(Error); - expect(() => qb.delete(Infinity), 'Infinity provided').to.throw(Error); - expect(() => qb.delete([1,2]), 'array of numbers provided').to.throw(Error); - expect(() => qb.delete(/foobar/), 'regex provided').to.throw(Error); - expect(() => qb.delete(NaN), 'NaN provided').to.throw(Error); - expect(() => qb.delete(false), 'false provided').to.throw(Error); - expect(() => qb.delete(''), 'empty string provided').to.throw(Error); - expect(() => qb.delete(' '), 'string full of spaces provided').to.throw(Error); - expect(() => qb.delete(null), 'null provided').to.throw(Error); + expect(() => qb.delete([]), 'empty array provided').to.throw(Error); + expect(() => qb.delete({}), 'empty object provided').to.throw(Error); + expect(() => qb.delete(3), 'integer provided').to.throw(Error); + expect(() => qb.delete(3.5), 'float provided').to.throw(Error); + expect(() => qb.delete(true), 'true provided').to.throw(Error); + expect(() => qb.delete(Infinity), 'Infinity provided').to.throw(Error); + expect(() => qb.delete([1,2]), 'array of numbers provided').to.throw(Error); + expect(() => qb.delete(/foobar/), 'regex provided').to.throw(Error); + expect(() => qb.delete(NaN), 'NaN provided').to.throw(Error); + expect(() => qb.delete(false), 'false provided').to.throw(Error); + expect(() => qb.delete(''), 'empty string provided').to.throw(Error); + expect(() => qb.delete(' '), 'string full of spaces provided').to.throw(Error); + expect(() => qb.delete(null), 'null provided').to.throw(Error); - // An undefined/nothing option will only work if a table has already been provided - qb.from('galaxies'); expect(() => qb.delete(undefined),'undefined provided').to.not.throw(Error); - qb.from('galaxies'); expect(() => qb.delete(),'nothing provided').to.not.throw(Error); - }); - it('should only use the first table supplied in a list if an array of table is supplied with the from() method.', () => { - qb.reset_query(); - const sql = qb.from(['galaxies','star_systems','planets']).delete(); - sql.should.eql("DELETE FROM `galaxies`"); - }); - it('should add where conditions to where_array when conditions are supplied', () => { - qb.reset_query(); - qb.delete('planets', {continents: 7, star_system: 'Solar'}); - qb.where_array.should.eql(["`continents` = 7", "AND `star_system` = 'Solar'"]); - }); - it('should return a string', () => { - qb.reset_query(); - const sql = qb.delete('galaxies', {continents: 7, star_system: 'Solar'}); - expect(sql).to.be.a('string'); - expect(sql).to.exist; - expect(sql).to.not.eql(''); - }); - it('should build a properly-escaped delete statement that deletes all records in a table if only a table is given', () => { - qb.reset_query(); - const sql = qb.delete('galaxies'); - sql.should.eql('DELETE FROM `galaxies`'); - }); - it('should build a properly-escaped delete statement that deletes all records in a table that matched passed conditions', () => { - qb.reset_query(); - const sql = qb.delete('galaxies', {class: 'M'}); - sql.should.eql("DELETE FROM `galaxies` WHERE `class` = 'M'"); - }); - it('should use ONLY the FIRST table added previously via the from() method', () => { - qb.reset_query(); - qb.from('galaxies'); - let sql = qb.delete(); - sql.should.eql('DELETE FROM `galaxies`'); + // An undefined/nothing option will only work if a table has already been provided + qb.from('galaxies'); expect(() => qb.delete(undefined),'undefined provided').to.not.throw(Error); + qb.from('galaxies'); expect(() => qb.delete(),'nothing provided').to.not.throw(Error); + }); + it('should only use the first table supplied in a list if an array of table is supplied with the from() method.', () => { + qb.reset_query(); + const sql = qb.from(['galaxies','star_systems','planets']).delete(); + sql.should.eql("DELETE FROM `galaxies`"); + }); + it('should add where conditions to where_array when conditions are supplied', () => { + qb.reset_query(); + qb.delete('planets', {continents: 7, star_system: 'Solar'}); + qb.where_array.should.eql(["`continents` = 7", "AND `star_system` = 'Solar'"]); + }); + it('should return a string', () => { + qb.reset_query(); + const sql = qb.delete('galaxies', {continents: 7, star_system: 'Solar'}); + expect(sql).to.be.a('string'); + expect(sql).to.exist; + expect(sql).to.not.eql(''); + }); + it('should build a properly-escaped delete statement that deletes all records in a table if only a table is given', () => { + qb.reset_query(); + const sql = qb.delete('galaxies'); + sql.should.eql('DELETE FROM `galaxies`'); + }); + it('should build a properly-escaped delete statement that deletes all records in a table that matched passed conditions', () => { + qb.reset_query(); + const sql = qb.delete('galaxies', {class: 'M'}); + sql.should.eql("DELETE FROM `galaxies` WHERE `class` = 'M'"); + }); + it('should use ONLY the FIRST table added previously via the from() method', () => { + qb.reset_query(); + qb.from('galaxies'); + let sql = qb.delete(); + sql.should.eql('DELETE FROM `galaxies`'); - qb.reset_query(); - sql = qb.from(['galaxies','star_systems','planets']).delete(); - sql.should.eql('DELETE FROM `galaxies`'); - }); - it('should accept where conditions added previously via the where() method', () => { - qb.reset_query(); - const sql = qb.where('created >=',4.6E9).where({class: 'M'}).delete('galaxies'); - sql.should.eql("DELETE FROM `galaxies` WHERE `created` >= 4600000000 AND `class` = 'M'"); - }); - it('should accept a limit on the number of rows deleted', () => { - qb.reset_query(); - const sql = qb.limit(20).delete('galaxies'); - sql.should.eql("DELETE FROM `galaxies` LIMIT 20"); - }); - it('should accept a LIMIT on the number of rows to delete and an OFFSET at which to start deleting the rows', () => { - qb.reset_query(); - const sql = qb.limit(20,10).delete('galaxies'); - sql.should.eql("DELETE FROM `galaxies` LIMIT 10, 20"); - }); + qb.reset_query(); + sql = qb.from(['galaxies','star_systems','planets']).delete(); + sql.should.eql('DELETE FROM `galaxies`'); + }); + it('should accept where conditions added previously via the where() method', () => { + qb.reset_query(); + const sql = qb.where('created >=',4.6E9).where({class: 'M'}).delete('galaxies'); + sql.should.eql("DELETE FROM `galaxies` WHERE `created` >= 4600000000 AND `class` = 'M'"); + }); + it('should accept a limit on the number of rows deleted', () => { + qb.reset_query(); + const sql = qb.limit(20).delete('galaxies'); + sql.should.eql("DELETE FROM `galaxies` LIMIT 20"); + }); + it('should accept a LIMIT on the number of rows to delete and an OFFSET at which to start deleting the rows', () => { + qb.reset_query(); + const sql = qb.limit(20,10).delete('galaxies'); + sql.should.eql("DELETE FROM `galaxies` LIMIT 10, 20"); + }); }); diff --git a/test/mysql/03-tests-empty_table.js b/test/mysql/03-tests-empty_table.js index 80895fa..af383cf 100755 --- a/test/mysql/03-tests-empty_table.js +++ b/test/mysql/03-tests-empty_table.js @@ -3,49 +3,49 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('emtpy_table()', () => { - it('should exist', () => { - should.exist(qb.empty_table); - }); - it('should be a function', () => { - qb.empty_table.should.be.a('function'); - }); - it('should return a string', () => { - qb.reset_query(); - const sql = qb.empty_table('galaxies'); - expect(sql).to.be.a('string'); - expect(sql).to.exist; - expect(sql).to.not.eql(''); - }); - it('should build a proper DELETE statement', () => { - qb.reset_query(); - const sql = qb.empty_table('galaxies'); - sql.should.eql('DELETE FROM `galaxies`'); - }); - it('should only accept nothing or a non-empty-string for the table (first) parameter', () => { - qb.reset_query(); +describe('MySQL: empty_table()', () => { + it('should exist', () => { + should.exist(qb.empty_table); + }); + it('should be a function', () => { + qb.empty_table.should.be.a('function'); + }); + it('should return a string', () => { + qb.reset_query(); + const sql = qb.empty_table('galaxies'); + expect(sql).to.be.a('string'); + expect(sql).to.exist; + expect(sql).to.not.eql(''); + }); + it('should build a proper DELETE statement', () => { + qb.reset_query(); + const sql = qb.empty_table('galaxies'); + sql.should.eql('DELETE FROM `galaxies`'); + }); + it('should only accept nothing or a non-empty-string for the table (first) parameter', () => { + qb.reset_query(); - expect(() => qb.empty_table([]), 'empty array provided').to.throw(Error); - expect(() => qb.empty_table({}), 'empty object provided').to.throw(Error); - expect(() => qb.empty_table(3), 'integer provided').to.throw(Error); - expect(() => qb.empty_table(3.5), 'float provided').to.throw(Error); - expect(() => qb.empty_table(true), 'true provided').to.throw(Error); - expect(() => qb.empty_table(Infinity), 'Infinity provided').to.throw(Error); - expect(() => qb.empty_table([1,2]), 'array of numbers provided').to.throw(Error); - expect(() => qb.empty_table(/foobar/), 'regex provided').to.throw(Error); - expect(() => qb.empty_table(NaN), 'NaN provided').to.throw(Error); - expect(() => qb.empty_table(false), 'false provided').to.throw(Error); - expect(() => qb.empty_table(''), 'empty string provided').to.throw(Error); - expect(() => qb.empty_table(' '), 'string full of spaces provided').to.throw(Error); - expect(() => qb.empty_table(null), 'null provided').to.throw(Error); + expect(() => qb.empty_table([]), 'empty array provided').to.throw(Error); + expect(() => qb.empty_table({}), 'empty object provided').to.throw(Error); + expect(() => qb.empty_table(3), 'integer provided').to.throw(Error); + expect(() => qb.empty_table(3.5), 'float provided').to.throw(Error); + expect(() => qb.empty_table(true), 'true provided').to.throw(Error); + expect(() => qb.empty_table(Infinity), 'Infinity provided').to.throw(Error); + expect(() => qb.empty_table([1,2]), 'array of numbers provided').to.throw(Error); + expect(() => qb.empty_table(/foobar/), 'regex provided').to.throw(Error); + expect(() => qb.empty_table(NaN), 'NaN provided').to.throw(Error); + expect(() => qb.empty_table(false), 'false provided').to.throw(Error); + expect(() => qb.empty_table(''), 'empty string provided').to.throw(Error); + expect(() => qb.empty_table(' '), 'string full of spaces provided').to.throw(Error); + expect(() => qb.empty_table(null), 'null provided').to.throw(Error); - // An undefined/nothing option will only work if a table has already been provided - qb.from('galaxies'); expect(() => qb.empty_table(undefined),'undefined provided').to.not.throw(Error); - qb.from('galaxies'); expect(() => qb.empty_table(),'nothing provided').to.not.throw(Error); - }); - it('should only use the first table supplied in a list if an array of table is supplied with the from() method.', () => { - qb.reset_query(); - const sql = qb.from(['galaxies','star_systems','planets']).empty_table(); - sql.should.eql("DELETE FROM `galaxies`"); - }); + // An undefined/nothing option will only work if a table has already been provided + qb.from('galaxies'); expect(() => qb.empty_table(undefined),'undefined provided').to.not.throw(Error); + qb.from('galaxies'); expect(() => qb.empty_table(),'nothing provided').to.not.throw(Error); + }); + it('should only use the first table supplied in a list if an array of table is supplied with the from() method.', () => { + qb.reset_query(); + const sql = qb.from(['galaxies','star_systems','planets']).empty_table(); + sql.should.eql("DELETE FROM `galaxies`"); + }); }); diff --git a/test/mysql/03-tests-insert.js b/test/mysql/03-tests-insert.js index 7fb93eb..5731d54 100755 --- a/test/mysql/03-tests-insert.js +++ b/test/mysql/03-tests-insert.js @@ -8,7 +8,7 @@ const test_data_set = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'A // table, data, callback, ignore, suffix -describe('insert()', () => { +describe('MySQL: insert()', () => { it('should exist', () => { should.exist(qb.insert); }); @@ -122,7 +122,7 @@ describe('insert()', () => { }); }); -describe('insert_ignore()', () => { +describe('MySQL: insert_ignore()', () => { it('should exist', () => { should.exist(qb.insert_ignore); }); diff --git a/test/mysql/03-tests-insert_batch.js b/test/mysql/03-tests-insert_batch.js index 966ae59..c3a55a3 100755 --- a/test/mysql/03-tests-insert_batch.js +++ b/test/mysql/03-tests-insert_batch.js @@ -6,99 +6,99 @@ const qb = new QueryBuilder(); const test_where = {id:3}; const test_data = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'Andromeda', type: 'spiral'}]; -describe('insert_batch()', () => { - it('should exist', () => { - should.exist(qb.insert_batch); - }); - it('should be a function', () => { - qb.insert_batch.should.be.a('function'); - }); - it('should add a table to from_array when a table is supplied', () => { - qb.reset_query(); - qb.insert_batch('galaxies', test_data); - qb.from_array.should.eql(['`galaxies`']); - }); - it('should only accept nothing or a string for the table (first) parameter', () => { - qb.reset_query(); +describe('MySQL: insert_batch()', () => { + it('should exist', () => { + should.exist(qb.insert_batch); + }); + it('should be a function', () => { + qb.insert_batch.should.be.a('function'); + }); + it('should add a table to from_array when a table is supplied', () => { + qb.reset_query(); + qb.insert_batch('galaxies', test_data); + qb.from_array.should.eql(['`galaxies`']); + }); + it('should only accept nothing or a string for the table (first) parameter', () => { + qb.reset_query(); - // Doing these to prevent other errors - qb.from('galaxies'); + // Doing these to prevent other errors + qb.from('galaxies'); - expect(() => qb.insert_batch([], test_data), 'empty array provided').to.throw(Error); - expect(() => qb.insert_batch({}, test_data), 'empty object provided').to.throw(Error); - expect(() => qb.insert_batch(3, test_data), 'integer provided').to.throw(Error); - expect(() => qb.insert_batch(3.5, test_data), 'float provided').to.throw(Error); - expect(() => qb.insert_batch(true, test_data), 'true provided').to.throw(Error); - expect(() => qb.insert_batch(Infinity, test_data), 'Infinity provided').to.throw(Error); - expect(() => qb.insert_batch([1,2], test_data), 'array of numbers provided').to.throw(Error); - expect(() => qb.insert_batch(/foobar/, test_data), 'regex provided').to.throw(Error); + expect(() => qb.insert_batch([], test_data), 'empty array provided').to.throw(Error); + expect(() => qb.insert_batch({}, test_data), 'empty object provided').to.throw(Error); + expect(() => qb.insert_batch(3, test_data), 'integer provided').to.throw(Error); + expect(() => qb.insert_batch(3.5, test_data), 'float provided').to.throw(Error); + expect(() => qb.insert_batch(true, test_data), 'true provided').to.throw(Error); + expect(() => qb.insert_batch(Infinity, test_data), 'Infinity provided').to.throw(Error); + expect(() => qb.insert_batch([1,2], test_data), 'array of numbers provided').to.throw(Error); + expect(() => qb.insert_batch(/foobar/, test_data), 'regex provided').to.throw(Error); - expect(() => qb.insert_batch(NaN, test_data), 'NaN provided').to.not.throw(Error); - expect(() => qb.insert_batch(false, test_data), 'false provided').to.not.throw(Error); - expect(() => qb.insert_batch('', test_data), 'empty string provided').to.not.throw(Error); - expect(() => qb.insert_batch(' ', test_data), 'string full of spaces provided').to.not.throw(Error); - expect(() => qb.insert_batch(null, test_data), 'null provided').to.not.throw(Error); - expect(() => qb.insert_batch(undefined, test_data), 'undefined provided').to.not.throw(Error); - }); - it('should build a proper batch INSERT string', () => { - qb.reset_query(); - const sql = qb.insert_batch('galaxies', test_data); - sql.should.eql("INSERT INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral')"); - }); - it('should only accept an array as the second parameter', () => { - qb.reset_query(); + expect(() => qb.insert_batch(NaN, test_data), 'NaN provided').to.not.throw(Error); + expect(() => qb.insert_batch(false, test_data), 'false provided').to.not.throw(Error); + expect(() => qb.insert_batch('', test_data), 'empty string provided').to.not.throw(Error); + expect(() => qb.insert_batch(' ', test_data), 'string full of spaces provided').to.not.throw(Error); + expect(() => qb.insert_batch(null, test_data), 'null provided').to.not.throw(Error); + expect(() => qb.insert_batch(undefined, test_data), 'undefined provided').to.not.throw(Error); + }); + it('should build a proper batch INSERT string', () => { + qb.reset_query(); + const sql = qb.insert_batch('galaxies', test_data); + sql.should.eql("INSERT INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral')"); + }); + it('should only accept an array as the second parameter', () => { + qb.reset_query(); - expect(() => qb.insert_batch('galaxies',test_data), 'array of objects provided').to.not.throw(Error); - expect(() => qb.insert_batch('galaxies',[]), 'empty array provided').to.not.throw(Error); + expect(() => qb.insert_batch('galaxies',test_data), 'array of objects provided').to.not.throw(Error); + expect(() => qb.insert_batch('galaxies',[]), 'empty array provided').to.not.throw(Error); - expect(() => qb.insert_batch('galaxies',[{},{}]), 'array of empty objects provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[test_data,test_data]), 'array of arrays provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',{}), 'empty object provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',''), 'empty string provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',null), 'null provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',undefined), 'undefined provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies'), 'nothing provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',3), 'integer provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',3.5), 'float provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',true), 'true provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',Infinity), 'Infinity provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[1,2]), 'array of numbers provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[Date, /foobar/, null]), 'array of non-standard objects provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',['abc',2,{foo:'bar'}]), 'array of mixed values provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',/foobar/), 'regex provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',NaN), 'NaN provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',false), 'false provided').to.throw(Error); - expect(() => qb.insert_batch('galaxies',' '), 'string full of spaces provided').to.throw(Error); - }); - it('should allow for an empty data parameter', () => { - qb.reset_query(); - const sql = qb.insert_batch('galaxies',[]); - sql.should.eql("INSERT INTO `galaxies` () VALUES ()"); - }); - it('should utilize pre-existing tables set in from_array', () => { - qb.reset_query(); - qb.from('galaxies'); - const sql = qb.insert_batch(null,[]); - sql.should.eql("INSERT INTO `galaxies` () VALUES ()"); - }); - it('should fail if any invalid values are passed into one of the data objects in the dataset', () => { - qb.reset_query(); - const func = () => console.log("foo"); - const regex = /foobar/; - const arr = [1,2,3]; - const obj = {foo: 'bar'}; + expect(() => qb.insert_batch('galaxies',[{},{}]), 'array of empty objects provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[test_data,test_data]), 'array of arrays provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',{}), 'empty object provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',''), 'empty string provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',null), 'null provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',undefined), 'undefined provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies'), 'nothing provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',3), 'integer provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',3.5), 'float provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',true), 'true provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',Infinity), 'Infinity provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[1,2]), 'array of numbers provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[Date, /foobar/, null]), 'array of non-standard objects provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',['abc',2,{foo:'bar'}]), 'array of mixed values provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',/foobar/), 'regex provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',NaN), 'NaN provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',false), 'false provided').to.throw(Error); + expect(() => qb.insert_batch('galaxies',' '), 'string full of spaces provided').to.throw(Error); + }); + it('should allow for an empty data parameter', () => { + qb.reset_query(); + const sql = qb.insert_batch('galaxies',[]); + sql.should.eql("INSERT INTO `galaxies` () VALUES ()"); + }); + it('should utilize pre-existing tables set in from_array', () => { + qb.reset_query(); + qb.from('galaxies'); + const sql = qb.insert_batch(null,[]); + sql.should.eql("INSERT INTO `galaxies` () VALUES ()"); + }); + it('should fail if any invalid values are passed into one of the data objects in the dataset', () => { + qb.reset_query(); + const func = () => console.log("foo"); + const regex = /foobar/; + const arr = [1,2,3]; + const obj = {foo: 'bar'}; - expect(() => qb.insert_batch('galaxies',[{id: func}]), 'function in data').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[{id: regex}]), 'regex in data').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[{id: Infinity}]), 'Infinity in data').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[{id: undefined}]), 'undefined in data').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[{id: NaN}]), 'NaN in data').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[{id: arr}]), 'array in data').to.throw(Error); - expect(() => qb.insert_batch('galaxies',[{id: obj}]), 'object in data').to.throw(Error); - }); - it('should support insert ignore statements', () => { - qb.reset_query(); - const sql = qb.insert_batch('galaxies', test_data, true, 'ON DUPLICATE KEY UPDATE last_update = NOW()'); - sql.should.eql("INSERT IGNORE INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral') ON DUPLICATE KEY UPDATE last_update = NOW()"); - }); + expect(() => qb.insert_batch('galaxies',[{id: func}]), 'function in data').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[{id: regex}]), 'regex in data').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[{id: Infinity}]), 'Infinity in data').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[{id: undefined}]), 'undefined in data').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[{id: NaN}]), 'NaN in data').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[{id: arr}]), 'array in data').to.throw(Error); + expect(() => qb.insert_batch('galaxies',[{id: obj}]), 'object in data').to.throw(Error); + }); + it('should support insert ignore statements', () => { + qb.reset_query(); + const sql = qb.insert_batch('galaxies', test_data, true, 'ON DUPLICATE KEY UPDATE last_update = NOW()'); + sql.should.eql("INSERT IGNORE INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral'), (4, 'Andromeda', 'spiral') ON DUPLICATE KEY UPDATE last_update = NOW()"); + }); }); diff --git a/test/mysql/03-tests-truncate.js b/test/mysql/03-tests-truncate.js index 939abe5..a257bee 100755 --- a/test/mysql/03-tests-truncate.js +++ b/test/mysql/03-tests-truncate.js @@ -3,23 +3,23 @@ const expect = require('chai').expect; const QueryBuilder = require('../../drivers/mysql/query_builder.js'); const qb = new QueryBuilder(); -describe('truncate()', () => { - it('should exist', () => { - should.exist(qb.truncate); - }); - it('should be a function', () => { - qb.truncate.should.be.a('function'); - }); - it('should return a string', () => { - qb.reset_query(); - const sql = qb.truncate('galaxies'); - expect(sql).to.be.a('string'); - expect(sql).to.exist; - expect(sql).to.not.eql(''); - }); - it('should build a proper truncate statement', () => { - qb.reset_query(); - const sql = qb.truncate('galaxies'); - sql.should.eql('TRUNCATE `galaxies`'); - }); +describe('MySQL: truncate()', () => { + it('should exist', () => { + should.exist(qb.truncate); + }); + it('should be a function', () => { + qb.truncate.should.be.a('function'); + }); + it('should return a string', () => { + qb.reset_query(); + const sql = qb.truncate('galaxies'); + expect(sql).to.be.a('string'); + expect(sql).to.exist; + expect(sql).to.not.eql(''); + }); + it('should build a proper truncate statement', () => { + qb.reset_query(); + const sql = qb.truncate('galaxies'); + sql.should.eql('TRUNCATE `galaxies`'); + }); }); diff --git a/test/mysql/03-tests-update.js b/test/mysql/03-tests-update.js index 7992a80..49c7864 100755 --- a/test/mysql/03-tests-update.js +++ b/test/mysql/03-tests-update.js @@ -9,119 +9,113 @@ const test_data_set = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'A // table, data, callback, ignore, suffix -describe('update()', () => { - it('should exist', () => { - should.exist(qb.update); - }); - it('should be a function', () => { - qb.update.should.be.a('function'); - }); - it('should add a table to from_array when a table is supplied', () => { - qb.reset_query(); - qb.update('galaxies', test_data, test_where); - qb.from_array.should.eql(['`galaxies`']); - }); - it('should accept a string or falsy value for the table (first) parameter', () => { - qb.reset_query(); +describe('MySQL: update()', () => { + it('should exist', () => { + should.exist(qb.update); + }); + it('should be a function', () => { + qb.update.should.be.a('function'); + }); + it('should add a table to from_array when a table is supplied', () => { + qb.reset_query(); + qb.update('galaxies', test_data, test_where); + qb.from_array.should.eql(['`galaxies`']); + }); + it('should accept a string or falsy value for the table (first) parameter', () => { + qb.reset_query(); - // One could choose to pass a falsy value to the first param because they have or will - // supply it with the from() method instead. + // One could choose to pass a falsy value to the first param because they have or will + // supply it with the from() method instead. - qb.reset_query(); expect(() => qb.from('galaxies').update([], test_data), 'empty array provided').to.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update({}, test_data), 'empty object provided').to.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(3, test_data), 'integer provided').to.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(3.5, test_data), 'float provided').to.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(true, test_data), 'true provided').to.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(Infinity, test_data), 'Infinity provided').to.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update([1,2], test_data), 'array of numbers provided').to.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(/foobar/, test_data), 'regex provided').to.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update([], test_data), 'empty array provided').to.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update({}, test_data), 'empty object provided').to.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(3, test_data), 'integer provided').to.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(3.5, test_data), 'float provided').to.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(true, test_data), 'true provided').to.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(Infinity, test_data), 'Infinity provided').to.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update([1,2], test_data), 'array of numbers provided').to.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(/foobar/, test_data), 'regex provided').to.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(NaN, test_data), 'NaN provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(false, test_data), 'false provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update('', test_data), 'empty string provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(' ', test_data), 'string full of spaces provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(null, test_data), 'null provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.from('galaxies').update(undefined, test_data),'undefined provided').to.not.throw(Error); - }); - it('should fail if a number, non-standard object, regex, boolean, array of non-objects, or non-empty string is provided in data parameter', () => { - // One could choose to pass a falsy value to the second param because they have or will - // supply data with the set() method instead. + qb.reset_query(); expect(() => qb.from('galaxies').update(NaN, test_data), 'NaN provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(false, test_data), 'false provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update('', test_data), 'empty string provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(' ', test_data), 'string full of spaces provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(null, test_data), 'null provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.from('galaxies').update(undefined, test_data),'undefined provided').to.not.throw(Error); + }); + it('should fail if a number, non-standard object, regex, boolean, array of non-objects, or non-empty string is provided in data parameter', () => { + // One could choose to pass a falsy value to the second param because they have or will + // supply data with the set() method instead. - qb.reset_query(); expect(() => qb.update('galaxies',test_data), 'non-empty array provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.update('galaxies',test_data_set), 'array of non-empty standard objects provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',test_data), 'non-empty array provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',test_data_set), 'array of non-empty standard objects provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',NaN), 'NaN provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',false), 'false provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',''), 'empty string provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',null), 'null provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',undefined), 'undefined provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies'), 'nothing provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',NaN), 'NaN provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',false), 'false provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',''), 'empty string provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',null), 'null provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',undefined), 'undefined provided').to.not.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies'), 'nothing provided').to.not.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',3), 'integer provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',3.5), 'float provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',true), 'true provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',Infinity), 'Infinity provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('foobar',{}), 'empty object provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',[{},{}]), 'array of empty objects provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',[]), 'empty array provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',[1,2]), 'array of numbers provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',['abc',2,{foo:'bar'}]), 'array of mixed values provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',/foobar/), 'regex provided').to.throw(Error); - qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',' '), 'string full of spaces provided').to.throw(Error); - }); - it('should require that there is at least something being updated', () => { - // @todo - }); - it('should utilize pre-existing tables set in from_array', () => { - qb.reset_query(); - qb.from('galaxies'); - const sql = qb.update(null, test_data, test_where); - sql.should.eql("UPDATE (`galaxies`) SET `name` = 'Milky Way', `type` = 'spiral' WHERE `id` = 3"); - }); - it('should utilize pre-existing value set in in set_array', () => { - qb.reset_query(); - qb.set(test_data); - const sql = qb.update('galaxies'); - sql.should.eql("UPDATE (`galaxies`) SET `name` = 'Milky Way', `type` = 'spiral'"); - }); - it('should utilize pre-existing tables and values from from_aray and set_array, respectively', () => { - qb.reset_query(); - qb.from('galaxies').set(test_data); - const sql = qb.update(); - sql.should.eql("UPDATE (`galaxies`) SET `name` = 'Milky Way', `type` = 'spiral'"); - }); - it('should accept a non-empty object for the data parameter', () => { - qb.reset_query(); - const sql = qb.update('galaxies', test_data); - sql.should.eql("UPDATE (`galaxies`) SET `name` = 'Milky Way', `type` = 'spiral'"); - }); - it('should convert call to update_batch() if an array of non-emtpy objects is passed in the data parameter', () => { - qb.reset_query(); - const sql = qb.update('galaxies', test_data_set); - qb.reset_query(); - const sql_b = qb.update_batch('galaxies', test_data_set, 'id'); - sql.should.eql(sql_b); - }); - it('should fail if any invalid values are passed in the data object.', () => { - qb.reset_query(); - const func = () => console.log("foo"); - const regex = /foobar/; - const arr = [1,2,3]; - const obj = {foo: 'bar'}; + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',3), 'integer provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',3.5), 'float provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',true), 'true provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',Infinity), 'Infinity provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('foobar',{}), 'empty object provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',[{},{}]), 'array of empty objects provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',[]), 'empty array provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',[1,2]), 'array of numbers provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',['abc',2,{foo:'bar'}]), 'array of mixed values provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',/foobar/), 'regex provided').to.throw(Error); + qb.reset_query(); expect(() => qb.set({id:2}).update('galaxies',' '), 'string full of spaces provided').to.throw(Error); + }); + it('should require that there is at least something being updated', () => { + // @todo + }); + it('should utilize pre-existing tables set in from_array', () => { + qb.reset_query(); + qb.from('galaxies'); + const sql = qb.update(null, test_data, test_where); + sql.should.eql("UPDATE (`galaxies`) SET `name` = 'Milky Way', `type` = 'spiral' WHERE `id` = 3"); + }); + it('should utilize pre-existing value set in in set_array', () => { + qb.reset_query(); + qb.set(test_data); + const sql = qb.update('galaxies'); + sql.should.eql("UPDATE (`galaxies`) SET `name` = 'Milky Way', `type` = 'spiral'"); + }); + it('should utilize pre-existing tables and values from from_aray and set_array, respectively', () => { + qb.reset_query(); + qb.from('galaxies').set(test_data); + const sql = qb.update(); + sql.should.eql("UPDATE (`galaxies`) SET `name` = 'Milky Way', `type` = 'spiral'"); + }); + it('should accept a non-empty object for the data parameter', () => { + qb.reset_query(); + const sql = qb.update('galaxies', test_data); + sql.should.eql("UPDATE (`galaxies`) SET `name` = 'Milky Way', `type` = 'spiral'"); + }); + it('should convert call to update_batch() if an array of non-emtpy objects is passed in the data parameter', () => { + qb.reset_query(); + const sql = qb.update('galaxies', test_data_set); + qb.reset_query(); + const sql_b = qb.update_batch('galaxies', test_data_set, 'id'); + sql.should.eql(sql_b); + }); + it('should fail if any invalid values are passed in the data object.', () => { + qb.reset_query(); + const func = () => console.log("foo"); + const regex = /foobar/; + const arr = [1,2,3]; + const obj = {foo: 'bar'}; - qb.reset_query(); expect(() => qb.update('galaxies',{id: func}), 'function in data').to.throw(Error); - qb.reset_query(); expect(() => qb.update('galaxies',{id: regex}), 'regex in data').to.throw(Error); - qb.reset_query(); expect(() => qb.update('galaxies',{id: Infinity}), 'Infinity in data').to.throw(Error); - qb.reset_query(); expect(() => qb.update('galaxies',{id: undefined}), 'undefined in data').to.throw(Error); - qb.reset_query(); expect(() => qb.update('galaxies',{id: NaN}), 'NaN in data').to.throw(Error); - qb.reset_query(); expect(() => qb.update('galaxies',{id: arr}), 'array in data').to.throw(Error); - qb.reset_query(); expect(() => qb.update('galaxies',{id: obj}), 'object in data').to.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',{id: func}), 'function in data').to.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',{id: regex}), 'regex in data').to.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',{id: Infinity}), 'Infinity in data').to.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',{id: undefined}), 'undefined in data').to.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',{id: NaN}), 'NaN in data').to.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',{id: arr}), 'array in data').to.throw(Error); + qb.reset_query(); expect(() => qb.update('galaxies',{id: obj}), 'object in data').to.throw(Error); - }); - /* - it('should support insert ignore statements', () => { - qb.reset_query(); - const sql = qb.insert_ignore('galaxies', test_data, 'ON DUPLICATE KEY UPDATE last_update = NOW()'); - sql.should.eql("INSERT IGNORE INTO `galaxies` (`id`, `name`, `type`) VALUES (3, 'Milky Way', 'spiral') ON DUPLICATE KEY UPDATE last_update = NOW()"); - }); */ + }); }); diff --git a/test/mysql/03-tests-update_batch.js b/test/mysql/03-tests-update_batch.js index b81d4c0..be4ddea 100755 --- a/test/mysql/03-tests-update_batch.js +++ b/test/mysql/03-tests-update_batch.js @@ -6,16 +6,16 @@ const qb = new QueryBuilder(); const test_where = {id:3}; const test_data = [{id:3, name:'Milky Way', type: 'spiral'}, {id:4, name: 'Andromeda', type: 'spiral'}]; -describe('update_batch()', () => { - it('should exist', () => { - should.exist(qb.update_batch); - }); - it('should be a function', () => { - qb.update_batch.should.be.a('function'); - }); - it('should build a proper batch UPDATE string', () => { - qb.reset_query(); - const sql = qb.update_batch('galaxies', test_data, 'id'); - sql.should.eql(["UPDATE (`galaxies`) SET `name` = CASE WHEN `id` = 3 THEN 'Milky Way' WHEN `id` = 4 THEN 'Andromeda' ELSE `name` END, `type` = CASE WHEN `id` = 3 THEN 'spiral' WHEN `id` = 4 THEN 'spiral' ELSE `type` END WHERE `id` IN (3,4)"]); - }); +describe('MySQL: update_batch()', () => { + it('should exist', () => { + should.exist(qb.update_batch); + }); + it('should be a function', () => { + qb.update_batch.should.be.a('function'); + }); + it('should build a proper batch UPDATE string', () => { + qb.reset_query(); + const sql = qb.update_batch('galaxies', test_data, 'id'); + sql.should.eql(["UPDATE (`galaxies`) SET `name` = CASE WHEN `id` = 3 THEN 'Milky Way' WHEN `id` = 4 THEN 'Andromeda' ELSE `name` END, `type` = CASE WHEN `id` = 3 THEN 'spiral' WHEN `id` = 4 THEN 'spiral' ELSE `type` END WHERE `id` IN (3,4)"]); + }); }); diff --git a/test/mysql/05-tests-multiple-queries.js b/test/mysql/05-tests-multiple-queries.js index 0fcfb4f..c678b70 100755 --- a/test/mysql/05-tests-multiple-queries.js +++ b/test/mysql/05-tests-multiple-queries.js @@ -1,29 +1,29 @@ -const should = require('chai').should(); -const expect = require('chai').expect; -const settings = { - host: '127.0.0.1', - database: 'mock_db', - user: 'travis', - version: '2.5.4', - port: 3306, - debug: false, -}; -const QueryBuilder = require('../../index.js'); -const pool = new QueryBuilder(settings, 'mysql', 'pool'); - -describe('Multiple Queries', () => { - it('should not get confused about table after delete records', done => { - pool.get_connection(qb => { - qb.limit(1).delete('cities', (err, result) => { - qb.select(['city', 'state_code']).from('cities').limit(1).get((err2, result2) => { - qb.release(); - expect(err, 'should not error on delete').to.not.be.instanceof(Error); - expect(result.affectedRows, 'one record should be deleted').to.be.eql(1); - expect(err2, 'should not error on select').to.not.be.instanceof(Error); - expect(result2.length, 'should have one result').to.be.equal(1); - done(); - }); - }); - }); - }); -}); +const should = require('chai').should(); +const expect = require('chai').expect; +const settings = { + host: '127.0.0.1', + database: 'mock_db', + user: 'travis', + version: '2.5.4', + port: 3306, + debug: false, +}; +const QueryBuilder = require('../../index.js'); +const pool = new QueryBuilder(settings, 'mysql', 'pool'); + +describe('MySQL: Multiple Queries', () => { + it('should not get confused about table after delete records', done => { + pool.get_connection(qb => { + qb.limit(1).delete('cities', (err, result) => { + qb.select(['city', 'state_code']).from('cities').limit(1).get((err2, result2) => { + qb.release(); + expect(err, 'should not error on delete').to.not.be.instanceof(Error); + expect(result.affectedRows, 'one record should be deleted').to.be.eql(1); + expect(err2, 'should not error on select').to.not.be.instanceof(Error); + expect(result2.length, 'should have one result').to.be.equal(1); + done(); + }); + }); + }); + }); +});