diff --git a/package.json b/package.json index 835278d..04b4a16 100644 --- a/package.json +++ b/package.json @@ -1,32 +1,35 @@ { - "name": "seriate.mock", - "version": "0.0.1", - "description": "A mocking add-on for seriate", - "main": "src/index.js", - "author": "LeanKit", - "homepage": "http://github.com/leankit-labs/seriate.mock", - "license": "MIT License - http://opensource.org/licenses/MIT", - "contributors": [{ - "name": "Brian Edgerton", - "email": "brian.edgerton@leankit.com" - }, { - "name": "Jim Cowart", - "email": "jim.cowart@leankit.com", - "url": "http://ifandelse.com" - }], - "dependencies": { - "sinon": "^1.10.2", - "lodash": "~2.4.1", - "when": "3.2.3" + "name": "seriate.mock", + "version": "0.0.1", + "description": "A mocking add-on for seriate", + "main": "src/index.js", + "author": "LeanKit", + "homepage": "http://github.com/leankit-labs/seriate.mock", + "license": "MIT License - http://opensource.org/licenses/MIT", + "contributors": [ + { + "name": "Brian Edgerton", + "email": "brian.edgerton@leankit.com" }, - "devDependencies": { - "gulp": "~3.8.0", - "gulp-mocha": "~0.4.1", - "gulp-watch": "~0.6.6-1", - "expect.js": "~0.2.0", - "mocha": "~1.20.1", - "sinon": "^1.10.2", - "seriate": "git://github.com/ifandelse/seriate.git" - }, - "scripts": {} + { + "name": "Jim Cowart", + "email": "jim.cowart@leankit.com", + "url": "http://ifandelse.com" + } + ], + "dependencies": { + "sinon": "^1.10.2", + "lodash": "~2.4.1", + "when": "3.2.3" + }, + "devDependencies": { + "expect.js": "~0.2.0", + "gulp": "~3.9.1", + "gulp-mocha": "~0.4.1", + "gulp-watch": "~0.6.6-1", + "mocha": "~1.20.1", + "seriate": "git://github.com/auridevil/seriate.git", + "sinon": "^1.10.2" + }, + "scripts": {} } diff --git a/spec/executeBehavior.spec.js b/spec/executeBehavior.spec.js index c9dd639..068aeba 100644 --- a/spec/executeBehavior.spec.js +++ b/spec/executeBehavior.spec.js @@ -22,7 +22,8 @@ describe( "When mocking one-time queries", function() { it( "should support mocking by query", function( done ) { sql.addMock( "SELECT * FROM books", records ); - sql.execute( { + + sql.execute({}, { query: "SELECT * FROM books" } ) .then( function( sets ) { @@ -49,7 +50,7 @@ describe( "When mocking one-time queries", function() { it( "should support mocking by query", function( done ) { sql.addMock( "SELECT * FROM books", records ); - sql.first( { + sql.first({}, { query: "SELECT * FROM books" } ) .then( function( sets ) { @@ -61,7 +62,7 @@ describe( "When mocking one-time queries", function() { it( "should support mocking by preparedSql", function( done ) { sql.addMock( "SELECT * FROM books", records ); - sql.first( { + sql.first({}, { preparedSql: "SELECT * FROM books" } ) .then( function( sets ) { diff --git a/src/mock.js b/src/mock.js index 9850a71..bd92886 100644 --- a/src/mock.js +++ b/src/mock.js @@ -1,6 +1,6 @@ -var _ = require( "lodash" ); -var when = require( "when" ); -var mockMethods = require( "./methods" ); +var _ = require("lodash"); +var when = require("when"); +var mockMethods = require("./methods"); /* Example mock object structure @@ -15,140 +15,163 @@ var mockMethods = require( "./methods" ); */ -function executeSqlFn( sql, fallbackFn ) { - return function( options ) { - var stepName = this.state; - var mock = sql.resolveMock( stepName, options ); - - if ( !_.isUndefined( mock ) ) { - return when.promise( function( resolve, reject ) { - var results = mock.mockResults.call( this, stepName, options ); - var timeout = mock.waitTime || 0; - - setTimeout( function() { - if ( mock.isError ) { - reject( results ); - } else { - resolve( results ); - } - }, timeout ); - - }.bind( this ) ); - } +function executeSqlFn(sql, fallbackFn) { + return function(step,stepName,options) { + var mock = sql.resolveMock(stepName, options); + + if (!_.isUndefined(mock)) { + return when.promise(function(resolve, reject) { + var results = mock.mockResults.call(this, stepName, options); + var timeout = mock.waitTime || 0; + + setTimeout(function() { + if (mock.isError) { + reject(results); + } else { + resolve(results); + } + }, timeout); - return fallbackFn.call( this, options ); - }; + }.bind(this)); + } + + return fallbackFn.call(this, options); + }; } module.exports = { - setup: function( sql, options ) { - options = options || {}; + setup: function(sql, options) { + options = options || {}; - sql.mockCache = {}; - sql.mockConfig = {}; + sql.mockCache = {}; + sql.mockConfig = {}; - _.extend( sql, mockMethods ); + _.extend(sql, mockMethods); - var defaultConfig = { - ignoreFailedConnections: true - }; + var defaultConfig = { + ignoreFailedConnections: true + }; - sql.setMockConfig( _.merge( defaultConfig, options ) ); + sql.setMockConfig(_.merge(defaultConfig, options)); - this.patchSeriate( sql ); + this.patchSeriate(sql); - this.patchSqlContext( sql ); + this.patchSqlContext(sql); - this.patchTransactionContext( sql ); + this.patchTransactionContext(sql); - }, + }, - patchSeriate: function( sql ) { - var _fromFile = sql.fromFile; + patchSeriate: function(sql) { + var _fromFile = sql.fromFile; - sql.fromFile = function( p ) { - p = this._getFilePath( p ); + sql.fromFile = function(p) { + p = this._getFilePath(p); - var mockExists = !_.isUndefined( this.getMock( p, { cacheKey: "file" } ) ); + var mockExists = !_.isUndefined(this.getMock(p, { + cacheKey: "file" + })); - if ( mockExists ) { - return "file://" + p; - } else { - return _fromFile.call( this, p ); - } + if (mockExists) { + return "file://" + p; + } else { + return _fromFile.call(this, p); + } - }; - }, + }; + }, - patchSqlContext: function( sql ) { - var origContext = sql.SqlContext; - var _executeSql = origContext.prototype.executeSql; - var _errorState = origContext.prototype.states.connecting.error; + patchSqlContext: function(sql) { + var origContext = sql.SqlContext; + var _executeSql = origContext.prototype.executeSql; + var _errorState = origContext.prototype.states.connecting.error; - sql.SqlContext = origContext.extend( { - executeSql: executeSqlFn( sql, _executeSql ), - states: { - connecting: { - error: function( err ) { - if ( sql.getMockConfig( "ignoreFailedConnections" ) ) { - return this.states.connecting.success.call( this ); - } - return _errorState.call( this, err ); - } - } - } - } ); - }, - - patchTransactionContext: function( sql ) { - var origContext = sql.TransactionContext; - var _executeSql = origContext.prototype.executeSql; - var _errorState = origContext.prototype.states.connecting.error; - - sql.TransactionContext = origContext.extend( { - executeSql: executeSqlFn( sql, _executeSql ), - states: { - connecting: { - error: function( err ) { - if ( sql.getMockConfig( "ignoreFailedConnections" ) ) { - var success = this.states.connecting.success; - if ( _.isString( success ) ) { - return this.transition( success ); - } else { - return success.call( this ); - } - } - return _errorState.call( this, err ); - } - }, - startingTransaction: { - _onEnter: function() { - return this.states.startingTransaction.success.call( this ); - } - }, - done: { - _onEnter: function() { - var self = this; - self.emit( "end", { - sets: self.results, - transaction: { - commit: function() { - return when.promise( function( resolve ) { - resolve(); - } ); - }, - rollback: function() { - return when.promise( function( resolve ) { - resolve(); - } ); - } - } - } ); - } + // var md5sum = require('crypto').createHash('md5'); + // md5sum.update(require('util').inspect(sql.SqlContext,showHidden=true, depth=5)); + // console.log('PRE ',md5sum.digest('hex')); + + sql.SqlContext = origContext.extend({ + executeSql: executeSqlFn(sql, _executeSql), + states: { + connecting: { + error: function(err) { + if (sql.getMockConfig("ignoreFailedConnections")) { + return this.states.connecting.success.call(this); + } + return _errorState.call(this, err); + } + } + } + }); + + if(!sql.SqlContext.prototype.executeSql.isSinonProxy) { + require('sinon').stub(sql.SqlContext.prototype, 'executeSql', executeSqlFn(sql, _executeSql)); + } + if(!sql.SqlContext.prototype.states.connecting.error.isSinonProxy) { + require('sinon').stub(sql.SqlContext.prototype.states.connecting, 'error', function (err) { + if (sql.getMockConfig("ignoreFailedConnections")) { + return this.states.connecting.success.call(this); } - } - } ); - } + return _errorState.call(this, err); + }); + } + + sql.SqlContext['mockedSym'] = Symbol('mock'); + // + // var md5sum2 = require('crypto').createHash('md5'); + // md5sum2.update(require('util').inspect(sql.SqlContext,showHidden=true, depth=5)); + // console.log('POST',md5sum2.digest('hex')); + }, + + patchTransactionContext: function(sql) { + var origContext = sql.TransactionContext; + var _executeSql = origContext.prototype.executeSql; + var _errorState = origContext.prototype.states.connecting.error; + + sql.TransactionContext = origContext.extend({ + executeSql: executeSqlFn(sql, _executeSql), + states: { + connecting: { + error: function(err) { + if (sql.getMockConfig("ignoreFailedConnections")) { + var success = this.states.connecting.success; + if (_.isString(success)) { + return this.transition(success); + } else { + return success.call(this); + } + } + return _errorState.call(this, err); + } + }, + startingTransaction: { + _onEnter: function() { + return this.states.startingTransaction.success.call(this); + } + }, + done: { + _onEnter: function() { + var self = this; + self.emit("end", { + sets: self.results, + transaction: { + commit: function() { + return when.promise(function(resolve) { + resolve(); + }); + }, + rollback: function() { + return when.promise(function(resolve) { + resolve(); + }); + } + } + }); + } + } + } + }); + } };