From 554c2ce4b9ee1255101d4ef6cb52e66f04a51c17 Mon Sep 17 00:00:00 2001 From: auridevil Date: Fri, 4 Nov 2016 17:19:32 +0100 Subject: [PATCH 1/2] exposed mocking --- src/index.js | 55 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/index.js b/src/index.js index e284a12..78c80cb 100644 --- a/src/index.js +++ b/src/index.js @@ -12,15 +12,18 @@ function promisify( context, queryOptions ) { context.step( name, queryOptions ); return when.promise( function( resolve, reject, notify ) { context - .end( resolve ) - .error( reject ) - .on( "data", notify ); + .end( resolve ) + .error( reject ) + .on( "data", notify ); } ); } var seriate = { getTransactionContext: function( connection ) { - var options = { metrics: this.metrics, namespace: this.metricsNamespace }; + var options = { + metrics: this.metrics, + namespace: this.metricsNamespace + }; if ( connection && connection.isolationLevel ) { options.isolationLevel = connection.isolationLevel; delete connection.isolationLevel; @@ -30,7 +33,11 @@ var seriate = { }, getPlainContext: function( connection ) { var conn = connections.get( connection ); - var options = { metrics: this.metrics, namespace: this.metricsNamespace, connection: conn }; + var options = { + metrics: this.metrics, + namespace: this.metricsNamespace, + connection: conn + }; return new SqlContext( options ); }, executeTransaction: function( connection, queryOptions ) { @@ -39,7 +46,11 @@ var seriate = { connection = undefined; } var conn = connections.get( connection ); - var options = { metrics: this.metrics, namespace: this.metricsNamespace, connection: conn }; + var options = { + metrics: this.metrics, + namespace: this.metricsNamespace, + connection: conn + }; return promisify( new TransactionContext( options ), queryOptions ); }, execute: function( connection, queryOptions ) { @@ -48,20 +59,24 @@ var seriate = { connection = undefined; } var conn = connections.get( connection ); - var options = { metrics: this.metrics, namespace: this.metricsNamespace, connection: conn }; + var options = { + metrics: this.metrics, + namespace: this.metricsNamespace, + connection: conn + }; return promisify( new SqlContext( options ), queryOptions ) - .then( function( data ) { - if ( data.__result__ ) { - return data.__result__; - } else { - return data[ queryOptions.procedure || queryOptions.name ]; - } - } ); + .then( function( data ) { + if ( data.__result__ ) { + return data.__result__; + } else { + return data[queryOptions.procedure || queryOptions.name]; + } + } ); }, first: function() { var args = Array.prototype.slice.call( arguments, 0 ); return this.execute.apply( this, args ).then( function( rows ) { - return rows[ 0 ]; + return rows[0]; } ); }, fromFile: utils.fromFile, @@ -85,16 +100,18 @@ var seriate = { useMetrics: function( metrics, namespace ) { this.metrics = metrics; this.metricsNamespace = namespace; - } + }, + SqlContext: SqlContext, + TransactionContext: TransactionContext }; _.each( sql.TYPES, function( val, key ) { - seriate[ key ] = sql.TYPES[ key ]; - seriate[ key.toUpperCase() ] = sql.TYPES[ key ]; + seriate[key] = sql.TYPES[key]; + seriate[key.toUpperCase()] = sql.TYPES[key]; } ); _.each( sql.ISOLATION_LEVEL, function( val, key ) { - seriate[ key ] = sql.ISOLATION_LEVEL[ key ]; + seriate[key] = sql.ISOLATION_LEVEL[key]; } ); seriate.MAX = sql.MAX; From 8b643a422e4435d98d27f72ddd7b8608b96f62bd Mon Sep 17 00:00:00 2001 From: auridevil Date: Mon, 7 Nov 2016 16:06:12 +0100 Subject: [PATCH 2/2] make it working with mock again --- .gitignore | 3 +++ package.json | 4 ++++ src/index.js | 17 +++++++++-------- src/sqlContext.js | 18 +++++++++--------- src/utils.js | 3 ++- 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 5871b6d..8186624 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ node_modules local-config.json .DS_Store + +# webstorm +.idea/ diff --git a/package.json b/package.json index d00c295..6092434 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,10 @@ { "name": "Josh Bush", "url": "https://github.com/digitalbush" + }, + { + "name": "Aureliano Bergese", + "url": "https://github.com/auridevil" } ], "dependencies": { diff --git a/src/index.js b/src/index.js index 78c80cb..080c8c2 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,8 @@ var _ = require( "lodash" ); var Monologue = require( "monologue.js" ).prototype; var sql = require( "mssql" ); var connections = require( "./connections" ); -var SqlContext = require( "./sqlContext" )(); -var TransactionContext = require( "./transactionContext" )( SqlContext ); +var SqlContextG = require( "./sqlContext" )(); +var TransactionContextG = require( "./transactionContext" )( SqlContextG ); var utils = require( "./utils" ); function promisify( context, queryOptions ) { @@ -29,7 +29,7 @@ var seriate = { delete connection.isolationLevel; } options.connection = connections.get( connection ); - return new TransactionContext( options ); + return new this.TransactionContext( options ); }, getPlainContext: function( connection ) { var conn = connections.get( connection ); @@ -38,7 +38,7 @@ var seriate = { namespace: this.metricsNamespace, connection: conn }; - return new SqlContext( options ); + return new this.SqlContext( options ); }, executeTransaction: function( connection, queryOptions ) { if ( arguments.length === 1 ) { @@ -51,7 +51,7 @@ var seriate = { namespace: this.metricsNamespace, connection: conn }; - return promisify( new TransactionContext( options ), queryOptions ); + return promisify( new this.TransactionContext( options ), queryOptions ); }, execute: function( connection, queryOptions ) { if ( arguments.length === 1 ) { @@ -64,7 +64,7 @@ var seriate = { namespace: this.metricsNamespace, connection: conn }; - return promisify( new SqlContext( options ), queryOptions ) + return promisify( new this.SqlContext( options ), queryOptions ) .then( function( data ) { if ( data.__result__ ) { return data.__result__; @@ -79,6 +79,7 @@ var seriate = { return rows[0]; } ); }, + _getFilePath: utils._getFilePath, fromFile: utils.fromFile, addConnection: function( config ) { connections.add( config ); @@ -101,8 +102,8 @@ var seriate = { this.metrics = metrics; this.metricsNamespace = namespace; }, - SqlContext: SqlContext, - TransactionContext: TransactionContext + SqlContext: SqlContextG, + TransactionContext: TransactionContextG }; _.each( sql.TYPES, function( val, key ) { diff --git a/src/sqlContext.js b/src/sqlContext.js index 2400db0..0bac3b9 100644 --- a/src/sqlContext.js +++ b/src/sqlContext.js @@ -169,14 +169,6 @@ function preparedSql( state, name, options ) { } } -function executeSql( state, name, options ) { - if ( options.query || options.procedure ) { - return nonPreparedSql( state, name, options ); - } else { - return preparedSql( state, name, options ); - } -} - function addState( fsm, name, stepAction ) { if ( fsm.states[ name ] ) { throw new Error( "A step by that name already exists: " + fsm.instance ); @@ -187,7 +179,7 @@ function addState( fsm, name, stepAction ) { _onEnter: function() { var promise; var exec = function( options ) { - promise = executeSql( fsm, name, options ); + promise = fsm.executeSql( fsm, name, options ); return promise; }; @@ -340,6 +332,14 @@ module.exports = function() { abort: function() { this.handle( "error", "Operation aborted" ); + }, + + executeSql: function( state, name, options ) { + if ( options.query || options.procedure ) { + return nonPreparedSql( state, name, options ); + } else { + return preparedSql( state, name, options ); + } } } ); diff --git a/src/utils.js b/src/utils.js index 6a860bd..8a76beb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -32,5 +32,6 @@ module.exports = { fileCache[ p ] = content; } return content; - } + }, + _getFilePath: _getFilePath };