@@ -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