Skip to content

Commit f101e98

Browse files
author
Kyle Farris
committed
Added additional tests for the QueryBuilder adapter.
1 parent 43cdf79 commit f101e98

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

drivers/mysql/adapters.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ var Adapters = function(nqb) {
136136
},
137137

138138
release: function() {
139+
if (!pool) throw new Error("You cannot release a non-pooled connection from a connection pool!");
139140
pool.releaseConnection(connection);
140141
}
141142
}, qb, qe);

test/mysql/tests-adapter.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ var check = function(done, f) {
1212
}
1313
};
1414

15-
describe('QueryBuilder() - MySQL', function() {
15+
var connection_released = function(qb) {
16+
var connection = qb.connection();
17+
expect(connection._pool._freeConnections).to.have.length(0);
18+
qb.release();
19+
expect(connection._pool._freeConnections).to.have.length(1);
20+
};
21+
22+
describe('QueryBuilder() - MySQL Adapter', function() {
1623
var on_connect = function(err) {
1724
if (err) { console.error("Not connected!"); return; }
1825
console.log("connected!");
@@ -38,6 +45,21 @@ describe('QueryBuilder() - MySQL', function() {
3845
it('should be a function', function() {
3946
nqb.QueryBuilder.should.be.a('function');
4047
});
48+
it('should have all the QueryBuilder methods', function() {
49+
var qb = nqb.QueryBuilder(_.extend({}, settings), driver);
50+
var children = ['where_array','where_in_array','from_array','join_array','select_array','set_array','order_by_array','group_by_array','having_array','limit_to','offset_val','join_clause','last_query_string','distinct_clause','aliased_tables','reset_query','where','or_where','_where','where_in','or_where_in','where_not_in','or_where_not_in','_where_in','like','not_like','or_like','or_not_like','_like','from','join','select','select_min','select_max','select_avg','select_sum','_min_max_avg_sum','distinct','group_by','having','or_having','_having','order_by','limit','offset','set'];
51+
expect(qb).to.include.keys(children);
52+
});
53+
it('should have all the QueryExec methods', function() {
54+
var qb = nqb.QueryBuilder(_.extend({}, settings), driver);
55+
var children = ['insert','insert_ignore','insert_batch','get','get_where','count','update','update_batch','delete','get_compiled_select','get_compiled_delete','get_compiled_update','get_compiled_insert','compile_select','compile_delete','compile_update','compile_insert'];
56+
expect(qb).to.include.keys(children);
57+
});
58+
it('should have all the miscellaneous methods', function() {
59+
var qb = nqb.QueryBuilder(_.extend({}, settings), driver);
60+
var children = ['last_query','escape','empty_table','truncate'];
61+
expect(qb).to.include.keys(children);
62+
});
4163
it('should establish a single connection given valid connection credentials', function(done) {
4264
var qb = nqb.QueryBuilder(_.extend({}, settings), driver);
4365
expect(qb, 'should have connect property').to.have.property('connect');
@@ -196,4 +218,63 @@ describe('QueryBuilder() - MySQL', function() {
196218
});
197219
});
198220
});
221+
it('should allow us to execute a query', function(done) {
222+
var qb = nqb.QueryBuilder(_.extend({}, settings), driver);
223+
qb.connect(function(err) {
224+
qb.query("select * from `cities` where `city` like 'Z%' and `state_code` = 'FL'", function(err, res) {
225+
check(done, function() {
226+
expect(err).to.not.be.instanceof(Error);
227+
expect(res).to.not.be.empty;
228+
expect(res).to.have.length(3);
229+
});
230+
});
231+
});
232+
});
233+
it('should not be able to release a non-pooled connection', function(done) {
234+
var qb = nqb.QueryBuilder(_.extend({}, settings), driver);
235+
qb.connect(function(err) {
236+
check(done, function() {
237+
expect(function() { qb.release(); }).to.throw(Error);
238+
});
239+
});
240+
});
241+
it('should create a connection pool object if asked', function() {
242+
var pool = nqb.QueryBuilder(_.extend({}, settings), driver, 'pool');
243+
expect(pool).to.be.instanceof.object;
244+
expect(pool).to.include.keys(['pool','get_connection','disconnect']);
245+
pool.pool.should.be.a('function');
246+
pool.get_connection.should.be.a('function');
247+
pool.disconnect.should.be.a('function');
248+
});
249+
it('should create a QueryBuilder adapter when getting a connection from the pool', function(done) {
250+
var qb2 = nqb.QueryBuilder(_.extend({}, settings), driver);
251+
var pool = nqb.QueryBuilder(_.extend({}, settings), driver, 'pool');
252+
pool.get_connection(function(qb) {
253+
check(done, function() {
254+
expect(qb).to.include.keys(Object.keys(qb2));
255+
});
256+
});
257+
});
258+
it('should allow one to release a connection from the pool', function(done) {
259+
var qb2 = nqb.QueryBuilder(_.extend({}, settings), driver);
260+
var pool = nqb.QueryBuilder(_.extend({}, settings), driver, 'pool');
261+
pool.get_connection(function(qb) {
262+
check(done, function() { connection_released(qb); });
263+
});
264+
});
265+
it('should allow one use the same connection pool connection for multiple queries', function(done) {
266+
var pool = nqb.QueryBuilder(_.extend({}, settings), driver, 'pool');
267+
268+
pool.get_connection(function(qb) {
269+
qb.query('select * from `cities` where `city` = "Gainesville"', function(err, res) {
270+
if (res.length > 0) {
271+
qb.query('select * from `cities` where `state_code` = "' + res[0].state_code + '"', function(err, res) {
272+
check(done, function() { connection_released(qb); });
273+
});
274+
} else {
275+
check(done, function() { connection_released(qb); });
276+
}
277+
});
278+
});
279+
});
199280
});

0 commit comments

Comments
 (0)