From 919b36eb9ad29bd4f9be27f54fc724ee5c7fab69 Mon Sep 17 00:00:00 2001 From: vFederer Date: Sun, 3 Sep 2017 14:23:08 +0200 Subject: [PATCH 01/35] Implement Sequelize#authenticate --- src/sequelize.js | 11 +++++++++++ test/sequelize.spec.js | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/src/sequelize.js b/src/sequelize.js index 36be0d2..70f1dcc 100644 --- a/src/sequelize.js +++ b/src/sequelize.js @@ -346,4 +346,15 @@ Sequelize.prototype.literal = function (arg) { return arg; }; +/** + * Simply returns a resolving promise + * + * @return {Promise} + */ +Sequelize.prototype.authenticate = function() { + return new bluebird(function (resolve) { + return resolve(); + }); +}; + module.exports = Sequelize; diff --git a/test/sequelize.spec.js b/test/sequelize.spec.js index 2b0acce..5b0f8c0 100644 --- a/test/sequelize.spec.js +++ b/test/sequelize.spec.js @@ -257,4 +257,10 @@ describe('Sequelize', function () { }); }); + describe('#authenticate', function () { + it('should simply return a resolving promise', (done) => { + var seq = new Sequelize(); + seq.authenticate().then(done).catch(done); + }) + }); }); From 350059a4fbc20f0e6d143c58ab51717cf2e1beb4 Mon Sep 17 00:00:00 2001 From: Morgan Terry Date: Fri, 8 Sep 2017 13:45:40 -0600 Subject: [PATCH 02/35] Fix misspelling --- docs/docs/mock-queries.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/mock-queries.md b/docs/docs/mock-queries.md index 4ad65b6..f6aa926 100644 --- a/docs/docs/mock-queries.md +++ b/docs/docs/mock-queries.md @@ -4,7 +4,7 @@ Sequelize Mock utilizes a special `QueryInterface` to be utilized for testing. T When you call into a function that would run a DB query in standard Sequelize, this function instead calls into our special test `QueryInterface` object to get the results for the query. Query results can be either manually queued up based on what your code may expect next, automatically filled in from the `Model`'s defined default values, or dynamically generated. -Each query will return the first availble result from the list. +Each query will return the first available result from the list. 1. The value generated by a query handler 2. If not available, the next result queued for the object the query is being run on From 1990cd798e24965a66f325c7ce70adf3900250f2 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Thu, 21 Sep 2017 14:14:05 -0700 Subject: [PATCH 03/35] Add `sequelize#authenticate()` to documentation Also tweaked the documentation it generates just a bit --- docs/api/sequelize.md | 10 ++++++++++ src/sequelize.js | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/docs/api/sequelize.md b/docs/api/sequelize.md index 684f495..f0e1918 100644 --- a/docs/api/sequelize.md +++ b/docs/api/sequelize.md @@ -303,3 +303,13 @@ arg | Any | Value to return ### Return `Any`: value passed in + + + +## authenticate() -> Promise + +Always returns a resolved promise + +### Return +`Promise`: will always resolve as a successful authentication + diff --git a/src/sequelize.js b/src/sequelize.js index 70f1dcc..9f90725 100644 --- a/src/sequelize.js +++ b/src/sequelize.js @@ -347,9 +347,9 @@ Sequelize.prototype.literal = function (arg) { }; /** - * Simply returns a resolving promise + * Always returns a resolved promise * - * @return {Promise} + * @return {Promise} will always resolve as a successful authentication */ Sequelize.prototype.authenticate = function() { return new bluebird(function (resolve) { From e37dbf94a9eb3e6e2782986ac13f1516b0109972 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Thu, 21 Sep 2017 14:14:44 -0700 Subject: [PATCH 04/35] Add recent changes to Changelog In prep for a v0.9.1 release --- changelog.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/changelog.md b/changelog.md index 92f7f7d..8dab2b3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # Changelog +vNext + * Add `authenticate()` to `sequelize` which always resolves (thanks to @vFederer) + * Fix a few documentation issues + v0.9.0 - c75d75e - Jul 28th 2017 * Add DataType mock objects for use with any DataType funcitonality * Add support for conditional query result handling (thanks to @scinos) From 3aeaa0529e737d91e9567b4e06b810f0f07192da Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Thu, 21 Sep 2017 14:17:36 -0700 Subject: [PATCH 05/35] 0.9.1 --- changelog.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 8dab2b3..39a5a8f 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -vNext +v0.9.1 - Sep 21st 2017 * Add `authenticate()` to `sequelize` which always resolves (thanks to @vFederer) * Fix a few documentation issues diff --git a/package.json b/package.json index ead4daa..dea2e8f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sequelize-mock", - "version": "0.9.0", + "version": "0.9.1", "description": "A simple mock interface specifically for testing code relying on Sequelize models", "main": "src/index.js", "scripts": { From edd97de14f526692060dfb8bc893461fcba8041e Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Thu, 21 Sep 2017 14:24:13 -0700 Subject: [PATCH 06/35] Add v0.9.1 Commit Hash to Changelog --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 39a5a8f..7b9a488 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -v0.9.1 - Sep 21st 2017 +v0.9.1 - 3aeaa05 - Sep 21st 2017 * Add `authenticate()` to `sequelize` which always resolves (thanks to @vFederer) * Fix a few documentation issues From 8d262a33c0632b1874f50c799b1b21328f3a8d70 Mon Sep 17 00:00:00 2001 From: Terry Moore Date: Fri, 20 Oct 2017 18:49:55 -0400 Subject: [PATCH 07/35] Add FindAndCountAll method and add tests --- src/model.js | 46 ++++++++++++++++++++++++++++++++++++++++++ test/model.spec.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/src/model.js b/src/model.js index 3e2f3c5..e78b5dc 100644 --- a/src/model.js +++ b/src/model.js @@ -277,6 +277,52 @@ fakeModel.prototype.findAll = function (options) { }); }; + +/** + * Executes a mock query to find all of the instances with any provided options and also return + * the count. Without any other configuration, the default behavior when no queueud query result + * is present is to create an array of a single result based on the where query in the options and + * wraps it in a promise. + * + * To turn off this behavior, the `$autoQueryFallback` option on the model should be set + * to `false`. + * + * @example + * // This is an example of the default behavior with no queued results + * // If there is a queued result or failure, that will be returned instead + * User.findAndCountAll({ + * where: { + * email: 'myEmail@example.com', + * }, + * }).then(function (results) { + * // results is an array of 1 + * results.count = 1 + * results.rows[0].get('email') === 'myEmail@example.com'; // true + * }); + * + * @instance + * @param {Object} [options] Options for the findAll query + * @param {Object} [options.where] Values that any automatically created Instances should have + * @return {Promise} result returned by the mock query + **/ +fakeModel.prototype.findAndCountAll = function (options) { + var self = this; + + return this.$query({ + query: "findAndCountAll", + queryOptions: arguments, + fallbackFn: !this.options.autoQueryFallback ? null : function () { + return Promise.resolve([ self.build(options ? options.where : {}) ]) + .then(function(result){ + return Promise.resolve({ + count: result.length, + rows: result + }); + }); + }, + }); +}; + /** * Executes a mock query to find an instance with the given ID value. Without any other * configuration, the default behavior when no queueud query result is present is to diff --git a/test/model.spec.js b/test/model.spec.js index e91a8ad..05ebb7d 100644 --- a/test/model.spec.js +++ b/test/model.spec.js @@ -166,6 +166,7 @@ describe('Model', function () { mdl.should.have.property('scope').which.is.a.Function(); mdl.should.have.property('find').which.is.a.Function(); mdl.should.have.property('findAll').which.is.a.Function(); + mdl.should.have.property('findAndCountAll').which.is.a.Function(); mdl.should.have.property('findById').which.is.a.Function(); mdl.should.have.property('findOne').which.is.a.Function(); // mdl.should.have.property('aggregate').which.is.a.Function(); @@ -605,6 +606,55 @@ describe('Model', function () { }); }); + describe('#findAndCountAll', function () { + var mdl; + beforeEach(function () { + mdl = new Model('foo'); + }); + + it('should pass along where value to Instance creation', function (done) { + var options = { + where: { + 'foo': 'bar', + }, + }; + + mdl.findAndCountAll(options) + .fallbackFn().then(function (result) { + result.rows.length.should.equal(1); + result.count.should.equal(1); + result.rows[0]._args[0].should.have.property('foo').which.is.exactly('bar'); + done(); + }).catch(done); + }); + + it('should still find results if there is not options', function (done) { + mdl.findAndCountAll() + .fallbackFn().then(function (result) { + result.count.should.equal(1); + result.rows.length.should.equal(1); + done(); + }).catch(done); + }); + + it('should not pass along a fallback function if auto fallback is turned off', function () { + mdl.options.autoQueryFallback = false; + should.not.exist(mdl.findAndCountAll().fallbackFn); + }); + + it('should pass query info to the QueryInterface instance', function(done) { + var queryOptions = {}; + + mdl.$query = function(options) { + options.query.should.equal('findAndCountAll'); + options.queryOptions.length.should.equal(1); + options.queryOptions[0].should.equal(queryOptions); + done(); + } + mdl.findAndCountAll(queryOptions) + }); + }); + describe('#destroy', function () { var mdl; beforeEach(function () { From 8bc80c29ebdaa6ddde5b4aafa355372dfe8f6823 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 23 Oct 2017 11:09:05 -0700 Subject: [PATCH 08/35] Add `findAndCountAll` to changelog --- changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/changelog.md b/changelog.md index 7b9a488..5c20caf 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +vNext + * Add `Model.findAndCountAll()` support (thanks to @TerryMooreII) + v0.9.1 - 3aeaa05 - Sep 21st 2017 * Add `authenticate()` to `sequelize` which always resolves (thanks to @vFederer) * Fix a few documentation issues From b728ac985bcadf63e2a5465ab389e8e3a100bae3 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 23 Oct 2017 11:11:45 -0700 Subject: [PATCH 09/35] Add support for `findAndCount` alias for `findAndCountAll` Also tweaked changelog entry for that --- changelog.md | 2 +- src/model.js | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 5c20caf..40309ca 100644 --- a/changelog.md +++ b/changelog.md @@ -1,7 +1,7 @@ # Changelog vNext - * Add `Model.findAndCountAll()` support (thanks to @TerryMooreII) + * Add support for `Model.findAndCount()` and it's alias `Model.findAndCountAll()` (thanks to @TerryMooreII) v0.9.1 - 3aeaa05 - Sep 21st 2017 * Add `authenticate()` to `sequelize` which always resolves (thanks to @vFederer) diff --git a/src/model.js b/src/model.js index e78b5dc..8c9e533 100644 --- a/src/model.js +++ b/src/model.js @@ -301,10 +301,13 @@ fakeModel.prototype.findAll = function (options) { * }); * * @instance + * @method findAndCount + * @alias findAndCountAll * @param {Object} [options] Options for the findAll query * @param {Object} [options.where] Values that any automatically created Instances should have - * @return {Promise} result returned by the mock query + * @return {Promise} result returned by the mock query **/ +fakeModel.prototype.findAndCount = fakeModel.prototype.findAndCountAll = function (options) { var self = this; From 17cadc9e6418c62908535b1361fd916bf688a884 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 23 Oct 2017 11:27:31 -0700 Subject: [PATCH 10/35] Add `findAndCount` alias to Model test --- test/model.spec.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/model.spec.js b/test/model.spec.js index 05ebb7d..900778e 100644 --- a/test/model.spec.js +++ b/test/model.spec.js @@ -166,6 +166,7 @@ describe('Model', function () { mdl.should.have.property('scope').which.is.a.Function(); mdl.should.have.property('find').which.is.a.Function(); mdl.should.have.property('findAll').which.is.a.Function(); + mdl.should.have.property('findAndCount').which.is.a.Function(); mdl.should.have.property('findAndCountAll').which.is.a.Function(); mdl.should.have.property('findById').which.is.a.Function(); mdl.should.have.property('findOne').which.is.a.Function(); From b69eef2e5f8f552657263d9c9f4fc2cec5e71a9b Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 12:57:25 -0700 Subject: [PATCH 11/35] Add `Utils.stack` and Tests Used for imports to get the relative file locations --- src/utils.js | 14 ++++++++++++++ test/utils.spec.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/utils.js b/src/utils.js index 8eccf59..fb45253 100644 --- a/src/utils.js +++ b/src/utils.js @@ -51,6 +51,20 @@ exports.pluralize = function(str) { return inflection.pluralize(str); }; +/** + * Gets the current stack frame + * + **/ +exports.stack = function () { + // Stash original stack prep + var prepareStackTrace = Error.prepareStackTrace; + Error.prepareStackTrace = function (_, s) { return s; }; + var curr = {}; + Error.captureStackTrace(curr, exports.stack); + Error.prepareStackTrace = prepareStackTrace; + return curr.stack; +}; + /** * Exposed version of the lodash library * diff --git a/test/utils.spec.js b/test/utils.spec.js index cfdb629..30ed83b 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -80,4 +80,35 @@ describe('Utils', function () { }); }); + describe('#stack', function () { + var captureStack; + beforeEach(function () { + captureStack = Error.captureStackTrace; + }); + + afterEach(function () { + Error.captureStackTrace = captureStack; + }); + + it('should capture and return the stack trace', function () { + var arg1, arg2; + Error.captureStackTrace = function (obj, fn) { + arg1 = obj; + arg2 = fn; + obj.stack = 'bar'; + }; + + var ret = Utils.stack(); + + // We need to restore nomality here so that the asserts and things can work properly + Error.captureStackTrace = captureStack; + + should(arg1).be.Object(); + should(arg2).be.Function(); + + ret.should.equal('bar'); + }); + + }); + }); From 96fddc0f4412060c790090648ebe272c3bbe75c5 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 13:07:00 -0700 Subject: [PATCH 12/35] Add `sequelize.import` and `sequelize.$overrideImport` support --- src/sequelize.js | 55 ++++++++++++++++++++++++- test/sequelize.spec.js | 91 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 141 insertions(+), 5 deletions(-) diff --git a/src/sequelize.js b/src/sequelize.js index 9f90725..42e732e 100644 --- a/src/sequelize.js +++ b/src/sequelize.js @@ -7,12 +7,14 @@ * @fileOverview Mock class for the base Sequelize class */ -var _ = require('lodash'), +var path = require('path'), + _ = require('lodash'), bluebird = require('bluebird'), Model = require('./model'), Instance = require('./instance'), Utils = require('./utils'), Errors = require('./errors'), + DataTypes = require('./data-types')({}), QueryInterface = require('./queryinterface'); /** @@ -51,6 +53,14 @@ function Sequelize(database, username, password, options) { dialect: 'mock', }, options || {}); + /** + * Used to cache and override model imports for easy mock model importing + * + * @member Sequelize + * @property + **/ + this.importCache = {}; + /** * Models that have been defined in this Sequelize Mock instances * @@ -213,6 +223,17 @@ Sequelize.prototype.$queueQueryClear = Sequelize.prototype.$cqq = Sequelize.prototype.$qqc = Sequelize.prototype.$clearQueue; +/** + * Overrides a path used for import + * + * @see {@link import} + * @param {String} importPath The original path that import will be called with + * @param {String} overridePath The path that should actually be used for resolving. If this path is relative, it will be relative to the file calling the import function + **/ +Sequelize.prototype.$overrideImport = function (realPath, mockPath) { + this.importCache[realPath] = mockPath; +}; + /* Mock Functionality * */ @@ -288,6 +309,38 @@ Sequelize.prototype.isDefined = function (name) { return name in this.models && typeof this.models[name] !== 'undefined'; }; +/** + * Imports a given model from the provided file path. Files that are imported should + * export a function that accepts two parameters, this sequelize instance, and an object + * with all of the available datatypes + * + * Before importing any modules, it will remap any paths that were overridden using the + * `$overrideImport` test function. This method is most helpful when used to make the + * SequelizeMock framework import your mock models instead of the real ones in your test + * code. + * + * @param {String} path Path of the model to import. Can be relative or absolute + * @return {Any} The result of evaluating the imported file's function + **/ +Sequelize.prototype.import = function (importPath) { + if(typeof this.importCache[importPath] === 'string') { + importPath = this.importCache[importPath]; + } + + if(path.normalize(importPath) !== path.resolve(importPath)) { + // We're relative, and need the calling files location + var callLoc = path.dirname(Utils.stack().getFileName()); + + importPath = path.resolve(callLoc, importPath); + } + + if(this.importCache[importPath] === 'string' || !this.importCache[importPath]) { + this.importCache[importPath] = require(importPath)(this, DataTypes); + } + + return this.importCache[importPath]; +}; + /** * Fetch a Model which is already defined. * diff --git a/test/sequelize.spec.js b/test/sequelize.spec.js index 5b0f8c0..95471cf 100644 --- a/test/sequelize.spec.js +++ b/test/sequelize.spec.js @@ -5,7 +5,16 @@ var bluebird = require('bluebird'); var proxyquire = require('proxyquire').noCallThru(); var ModelMock = function () {}; -var UtilsMock = {}; +var PathMock = { + normalize: function (p) { return p }, + resolve: function (p) { return p }, + dirname: function (p) { return p }, +}; + +var UtilsMock = { + stack: function () { return {}; }, +}; + var PackageMock = { version: 'test', }; @@ -15,13 +24,21 @@ var ErrorMock = { }; var QueryInterfaceMock = function () {}; +var lastImportTestCall; +function importTestFunc() { + lastImportTestCall = arguments; +} + var Sequelize = proxyquire('../src/sequelize', { + 'path' : PathMock, './model' : ModelMock, './utils' : UtilsMock, './errors' : ErrorMock, './queryinterface' : QueryInterfaceMock, '../package.json' : PackageMock, - './data-types' : function () {} + './data-types' : function () {}, + + 'import-test' : importTestFunc, }); describe('Sequelize', function () { @@ -141,7 +158,7 @@ describe('Sequelize', function () { seq = new Sequelize(); }); - it('should queue a result against the QueryInterface', function () { + it('should clear queue of results in the QueryInterface', function () { var run = 0; seq.queryInterface = { $clearQueue: function (res) { @@ -153,6 +170,19 @@ describe('Sequelize', function () { }); }); + describe('#$overrideImport', function () { + var seq; + beforeEach(function () { + seq = new Sequelize(); + }); + + it('should override an import path in the importCache', function () { + seq.importCache = {}; + seq.$overrideImport('foo', 'bar'); + seq.importCache.should.have.property('foo').which.is.exactly('bar'); + }); + }); + describe('#getDialect', function () { it('should return the dialect set during initialization', function () { var seq = new Sequelize({ @@ -190,7 +220,60 @@ describe('Sequelize', function () { seq.isDefined('test').should.be.false() }); }); - + + describe('#import', function () { + var seq, resolve, stack; + beforeEach(function () { + seq = new Sequelize(); + lastImportTestCall = null; + resolve = PathMock.resolve; + stack = UtilsMock.stack; + }); + + afterEach(function () { + PathMock.resolve = resolve; + UtilsMock.stack = stack; + }); + + it('should return an already imported model', function () { + var findItem = {}; + seq.importCache = { + 'foo': findItem, + }; + seq.import('foo').should.be.exactly(findItem); + }); + + it('should import a model from the given path', function () { + seq.import('import-test'); + should(lastImportTestCall).not.be.Null(); + lastImportTestCall[0].should.be.exactly(seq); + }); + + it('should turn a relative path into an absolute path', function () { + var pathRun = 0; + var stackRun = 0; + PathMock.resolve = function () { pathRun++; return './bar'; }; + UtilsMock.stack = function () { stackRun++; return { getFileName: function () { return 'baz'; }}; }; + var findItem = {}; + + seq.importCache = { + './bar': findItem, + }; + seq.import('./foo').should.be.exactly(findItem); + pathRun.should.be.exactly(2); + stackRun.should.be.exactly(1); + }); + + it('should import a replaced model from an overridden import', function () { + var findItem = {}; + seq.importCache = { + 'foo': 'bar', + 'bar': findItem, + }; + seq.import('foo').should.be.exactly(findItem); + }); + }); + describe('#model', function() { it('should return a previously defined Mock Model referenced its name', function() { var seq = new Sequelize(); From 82ca223eedb55a467f4e9767307d202c8d385703 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 13:09:26 -0700 Subject: [PATCH 13/35] Add import notes to changelog --- changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/changelog.md b/changelog.md index 40309ca..3b277bc 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,8 @@ # Changelog vNext + * Add `sequelize.import` support + * Add `sequelize.$overrideImport` test functionality to allow overriding imported module paths * Add support for `Model.findAndCount()` and it's alias `Model.findAndCountAll()` (thanks to @TerryMooreII) v0.9.1 - 3aeaa05 - Sep 21st 2017 From 47c54204999876f377e7e567171fa74097f71dc1 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 13:22:28 -0700 Subject: [PATCH 14/35] Add `import` to Getting Started Documentation --- docs/docs/getting-started.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/docs/getting-started.md b/docs/docs/getting-started.md index d2eade6..bedfe53 100644 --- a/docs/docs/getting-started.md +++ b/docs/docs/getting-started.md @@ -38,6 +38,27 @@ var UserMock = dbMock.define('user', { ## Swapping Model for Mocks +### Using `Sequelize.import()` + +In SequelizeMock, we provide the same `import()` functionality that Sequelize provides, with the added ability to override any given imported path with your mock paths. + +Using the `$overrideImport` method, you can simply define a mapping between your model and your mock object. + +```javascript +// If your Sequelize code looks like this +sequelize.import('./users/model.js'); + +// Your test code can simply override the import so your code will function as expected +sequelize.$overrideImport('./users/model.js', './users/mock.js'); + +// Now an import for your users model will actually import your user mock file instead +sequelize.import('./users/model.js'); // Will load './users/mock.js' instead +``` + +**Note that relative paths are relative to the file calling the `import` function, and not to your test code.** + +### Using `require()` + There are a number of libraries out there that can be used to replace `require()` dependencies with mock objects. You can simply use one of these libraries to replace the Sequelize Mock object into your code and it should run exactly as you would expect. Here is an example of doing so with [proxyquire](https://www.npmjs.com/package/proxyquire) @@ -54,7 +75,7 @@ var myModule = proxyquire('user.controller', { }); ``` -### Some Mock Injection Libraries +#### Some Mock Injection Libraries * [proxyquire](https://www.npmjs.com/package/proxyquire) * [mockery](https://www.npmjs.com/package/mockery) * [mock-require](https://www.npmjs.com/package/mock-require) From cec23a6cc9be8dccf793c43413538b2edd88694e Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 13:23:13 -0700 Subject: [PATCH 15/35] Add `.stack()` to Utils API Docs --- docs/api/utils.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/api/utils.md b/docs/api/utils.md index e261fc9..b613553 100644 --- a/docs/api/utils.md +++ b/docs/api/utils.md @@ -74,6 +74,13 @@ str | String | Word to convert to its plural form + +## stack() + +Gets the current stack frame + + + ## lodash From 9912a5526092e909d1be9fd2cd95238fb3673168 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 13:23:45 -0700 Subject: [PATCH 16/35] Add `import` and `$overrideImport` to Sequelize API Docs --- docs/api/sequelize.md | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/docs/api/sequelize.md b/docs/api/sequelize.md index f0e1918..9f934c4 100644 --- a/docs/api/sequelize.md +++ b/docs/api/sequelize.md @@ -33,6 +33,13 @@ Options passed into the Sequelize initialization + +### .importCache + +Used to cache and override model imports for easy mock model importing + + + ### .models @@ -151,6 +158,25 @@ Clears any queued results from `$queueResult` or `$queueFailure`
**Alias** $ + +## $overrideImport(importPath, overridePath) + +Overrides a path used for import + +**See** + + - [import](#import) + +### Parameters + +Name | Type | Description +--- | --- | --- +importPath | String | The original path that import will be called with +overridePath | String | The path that should actually be used for resolving. If this path is relative, it will be relative to the file calling the import function + + + + ## getDialect() -> String @@ -240,6 +266,30 @@ name | String | Name of the model + +## import(path) -> Any + +Imports a given model from the provided file path. Files that are imported should +export a function that accepts two parameters, this sequelize instance, and an object +with all of the available datatypes + +Before importing any modules, it will remap any paths that were overridden using the +`$overrideImport` test function. This method is most helpful when used to make the +SequelizeMock framework import your mock models instead of the real ones in your test +code. + +### Parameters + +Name | Type | Description +--- | --- | --- +path | String | Path of the model to import. Can be relative or absolute + + +### Return +`Any`: The result of evaluating the imported file's function + + + ## model(name) -> Model From daee2c0c617fc5258d26dda9f9267710e2114574 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 13:34:57 -0700 Subject: [PATCH 17/35] Add `findAndCount` to Model API Docs --- docs/api/model.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/api/model.md b/docs/api/model.md index 7eb8271..421aaba 100644 --- a/docs/api/model.md +++ b/docs/api/model.md @@ -266,6 +266,46 @@ Name | Type | Description + +## findAndCount([options]) -> Promise.<Object> + +Executes a mock query to find all of the instances with any provided options and also return +the count. Without any other configuration, the default behavior when no queueud query result +is present is to create an array of a single result based on the where query in the options and +wraps it in a promise. + +To turn off this behavior, the `$autoQueryFallback` option on the model should be set +to `false`.
**Alias** findAndCountAll + +**Example** + +```javascript +// This is an example of the default behavior with no queued results +// If there is a queued result or failure, that will be returned instead +User.findAndCountAll({ + where: { + email: 'myEmail@example.com', + }, +}).then(function (results) { + // results is an array of 1 + results.count = 1 + results.rows[0].get('email') === 'myEmail@example.com'; // true +}); +``` + +### Parameters + +Name | Type | Description +--- | --- | --- +[options] | Object | Options for the findAll query +[options.where] | Object | Values that any automatically created Instances should have + + +### Return +`Promise.`: result returned by the mock query + + + ## findById(id) -> Promise.<Instance> From 0a7f270f8d921cdc51e627737d14863d933440ec Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 14:28:10 -0700 Subject: [PATCH 18/35] 0.10.0 --- changelog.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 3b277bc..34dec99 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -vNext +v0.10.0 - Oct 24th 2017 * Add `sequelize.import` support * Add `sequelize.$overrideImport` test functionality to allow overriding imported module paths * Add support for `Model.findAndCount()` and it's alias `Model.findAndCountAll()` (thanks to @TerryMooreII) diff --git a/package.json b/package.json index dea2e8f..d3f01a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sequelize-mock", - "version": "0.9.1", + "version": "0.10.0", "description": "A simple mock interface specifically for testing code relying on Sequelize models", "main": "src/index.js", "scripts": { From edac5af69d5363d3a84b62bff2366a1c9fa316d5 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Tue, 24 Oct 2017 14:30:04 -0700 Subject: [PATCH 19/35] Add v0.10.0 Commit Hash to Changelog --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 34dec99..f5a69fb 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -v0.10.0 - Oct 24th 2017 +v0.10.0 - 0a7f270f - Oct 24th 2017 * Add `sequelize.import` support * Add `sequelize.$overrideImport` test functionality to allow overriding imported module paths * Add support for `Model.findAndCount()` and it's alias `Model.findAndCountAll()` (thanks to @TerryMooreII) From 0a184bb2e0f015ff45235257f80d89eb6486aeb6 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 20 Nov 2017 10:37:57 -0800 Subject: [PATCH 20/35] Fix `Util.stack` functionality Stack looks like it is a getter which is where `prepareStackTrace` is actually called. Order matters for getting that and resetting the `prepareStackTrace` function. TODO: Test has been converted to a bit more of a integration test than unit test to make sure we get coverage of this. Unit test code remains in place but commented out and if/when we get an integration or end-to-end test suite, we should transfer this test over to one of those suites and uncomment the unit test code here. --- src/utils.js | 3 ++- test/utils.spec.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index fb45253..1c9d514 100644 --- a/src/utils.js +++ b/src/utils.js @@ -61,8 +61,9 @@ exports.stack = function () { Error.prepareStackTrace = function (_, s) { return s; }; var curr = {}; Error.captureStackTrace(curr, exports.stack); + var stack = curr.stack; Error.prepareStackTrace = prepareStackTrace; - return curr.stack; + return stack; }; /** diff --git a/test/utils.spec.js b/test/utils.spec.js index 30ed83b..96b9124 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -90,16 +90,21 @@ describe('Utils', function () { Error.captureStackTrace = captureStack; }); + // TODO: This test is more integration than unit. Need to move this + // code over to a different test suite once one exists. it('should capture and return the stack trace', function () { + /* var arg1, arg2; Error.captureStackTrace = function (obj, fn) { arg1 = obj; arg2 = fn; obj.stack = 'bar'; }; + */ var ret = Utils.stack(); + /* // We need to restore nomality here so that the asserts and things can work properly Error.captureStackTrace = captureStack; @@ -107,6 +112,10 @@ describe('Utils', function () { should(arg2).be.Function(); ret.should.equal('bar'); + */ + + ret.should.be.an.Array(); + should(ret[0].getFileName).be.a.Function(); }); }); From b81568432239f95ef5f4a866b356143fe7ba4ff7 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 20 Nov 2017 10:40:52 -0800 Subject: [PATCH 21/35] Fix expected return from `Util.stack` in `import` Missed array access in the call to lookup the filename for the relative file paths. Fix test as well. Fixes #35 --- changelog.md | 3 +++ src/sequelize.js | 2 +- test/sequelize.spec.js | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index f5a69fb..1621a00 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +vNext + * Fix for relative file paths in the `sequelize.import` function + v0.10.0 - 0a7f270f - Oct 24th 2017 * Add `sequelize.import` support * Add `sequelize.$overrideImport` test functionality to allow overriding imported module paths diff --git a/src/sequelize.js b/src/sequelize.js index 42e732e..f2ba06a 100644 --- a/src/sequelize.js +++ b/src/sequelize.js @@ -329,7 +329,7 @@ Sequelize.prototype.import = function (importPath) { if(path.normalize(importPath) !== path.resolve(importPath)) { // We're relative, and need the calling files location - var callLoc = path.dirname(Utils.stack().getFileName()); + var callLoc = path.dirname(Utils.stack()[1].getFileName()); importPath = path.resolve(callLoc, importPath); } diff --git a/test/sequelize.spec.js b/test/sequelize.spec.js index 95471cf..ace2fe4 100644 --- a/test/sequelize.spec.js +++ b/test/sequelize.spec.js @@ -253,7 +253,7 @@ describe('Sequelize', function () { var pathRun = 0; var stackRun = 0; PathMock.resolve = function () { pathRun++; return './bar'; }; - UtilsMock.stack = function () { stackRun++; return { getFileName: function () { return 'baz'; }}; }; + UtilsMock.stack = function () { stackRun++; return [0, { getFileName: function () { return 'baz'; } } ] }; var findItem = {}; seq.importCache = { From 43cc66876fa56914d6ce028603eee551395363ab Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 20 Nov 2017 10:45:01 -0800 Subject: [PATCH 22/35] 0.10.1 --- changelog.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 1621a00..2ed0de9 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -vNext +v0.10.1 - Nov 20th 2017 * Fix for relative file paths in the `sequelize.import` function v0.10.0 - 0a7f270f - Oct 24th 2017 diff --git a/package.json b/package.json index d3f01a3..60bcd77 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sequelize-mock", - "version": "0.10.0", + "version": "0.10.1", "description": "A simple mock interface specifically for testing code relying on Sequelize models", "main": "src/index.js", "scripts": { From fe656c6aea363dff80dc108cfbaa054c0dcf9364 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 20 Nov 2017 10:47:21 -0800 Subject: [PATCH 23/35] Add v0.10.1 Commit Hash to Changelog --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 2ed0de9..2f19c88 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -v0.10.1 - Nov 20th 2017 +v0.10.1 - 43cc668 - Nov 20th 2017 * Fix for relative file paths in the `sequelize.import` function v0.10.0 - 0a7f270f - Oct 24th 2017 From 286e64430ee3f8ae2ae14d662cb7deab003f0899 Mon Sep 17 00:00:00 2001 From: Robert Preus-MacLaren Date: Wed, 22 Nov 2017 16:26:53 +0200 Subject: [PATCH 24/35] Fix issue related to mimicking sequelize.import We need to return the object used to store the datatypes, otherwise sequelizeInstance.import is broken. --- src/data-types.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/data-types.js b/src/data-types.js index 0b62155..818ac5f 100644 --- a/src/data-types.js +++ b/src/data-types.js @@ -546,4 +546,6 @@ module.exports = function (Sequelize) { Sequelize.ARRAY = ARRAY; Sequelize.GEOMETRY = GEOMETRY; Sequelize.GEOGRAPHY = GEOGRAPHY; + + return Sequelize; }; From 70ae87e6eb7384a8c5f329262e170a5cfdcb1107 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Wed, 22 Nov 2017 08:34:52 -0800 Subject: [PATCH 25/35] Create .editorconfig Adding an editorconfig so things are normalized a bit during editing and we don't get a bunch of whitespace changes --- .editorconfig | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..82e12eb --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = true From dd877f82af40b21dcd88a6f537c7fdd7b8964296 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Wed, 22 Nov 2017 08:47:36 -0800 Subject: [PATCH 26/35] Add editorconfig override for files needing spaces --- .editorconfig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.editorconfig b/.editorconfig index 82e12eb..78b2aca 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,3 +6,7 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = false insert_final_newline = true + +[{package.json,*.yml}] +indent_style = space +indent_size = 2 From 07d8f98a0719928e1f08a5f9448f0b10a0966ca4 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Wed, 22 Nov 2017 08:50:39 -0800 Subject: [PATCH 27/35] Add editorconfig note to changelog --- changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/changelog.md b/changelog.md index 2f19c88..ad53872 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +vNext + * *DEV* Added .editorconfig file to normalize editors and minimize whitespace changes + v0.10.1 - 43cc668 - Nov 20th 2017 * Fix for relative file paths in the `sequelize.import` function From aec4d0b27b7713ca8ff0ec8bec0c447471512706 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 4 Dec 2017 11:36:49 -0800 Subject: [PATCH 28/35] Add DataType Fix to Changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index ad53872..12e6217 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,7 @@ # Changelog vNext + * Fix for DataTypes not being exposed during `sequelize.import` calls * *DEV* Added .editorconfig file to normalize editors and minimize whitespace changes v0.10.1 - 43cc668 - Nov 20th 2017 From 8ed75d2bba44e0c61a92a20677c5376f83f4f95d Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 4 Dec 2017 11:38:32 -0800 Subject: [PATCH 29/35] 0.10.2 --- changelog.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 12e6217..cbf9cd4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -vNext +v0.10.2 - Dec 4th 2017 * Fix for DataTypes not being exposed during `sequelize.import` calls * *DEV* Added .editorconfig file to normalize editors and minimize whitespace changes diff --git a/package.json b/package.json index 60bcd77..a2974c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sequelize-mock", - "version": "0.10.1", + "version": "0.10.2", "description": "A simple mock interface specifically for testing code relying on Sequelize models", "main": "src/index.js", "scripts": { From 2f132959b9db9de785420ea169139fe1e3890e89 Mon Sep 17 00:00:00 2001 From: Andrew Hushbeck Date: Mon, 4 Dec 2017 11:40:16 -0800 Subject: [PATCH 30/35] Add v0.10.2 Commit Hash to Changelog --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index cbf9cd4..b917dbd 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,6 @@ # Changelog -v0.10.2 - Dec 4th 2017 +v0.10.2 - 8ed75d2 - Dec 4th 2017 * Fix for DataTypes not being exposed during `sequelize.import` calls * *DEV* Added .editorconfig file to normalize editors and minimize whitespace changes From 04c9e38f287433f1427edf4c1d973620622d985e Mon Sep 17 00:00:00 2001 From: toxuin Date: Thu, 18 Jan 2018 09:42:19 -0700 Subject: [PATCH 31/35] Add support for mocking .addScope() --- src/model.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/model.js b/src/model.js index 8c9e533..313f447 100644 --- a/src/model.js +++ b/src/model.js @@ -239,6 +239,14 @@ fakeModel.prototype.scope = function () { return this; }; +/** + * No-op that returns a void. + * + * @instance + * @return {undefined} + **/ +fakeModel.prototype.addScope = function () {}; + /** * Executes a mock query to find all of the instances with any provided options. Without * any other configuration, the default behavior when no queueud query result is present From cc93c78a8d78725600b31c6f20f7fe9d8dd11dc8 Mon Sep 17 00:00:00 2001 From: toxuin Date: Thu, 18 Jan 2018 09:45:36 -0700 Subject: [PATCH 32/35] Enable check for addScope in model test --- test/model.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model.spec.js b/test/model.spec.js index 900778e..02067cf 100644 --- a/test/model.spec.js +++ b/test/model.spec.js @@ -162,7 +162,7 @@ describe('Model', function () { // mdl.should.have.property('schema').which.is.a.Function(); mdl.should.have.property('getTableName').which.is.a.Function(); mdl.should.have.property('unscoped').which.is.a.Function(); - // mdl.should.have.property('addScope').which.is.a.Function(); + mdl.should.have.property('addScope').which.is.a.Function(); mdl.should.have.property('scope').which.is.a.Function(); mdl.should.have.property('find').which.is.a.Function(); mdl.should.have.property('findAll').which.is.a.Function(); From 4975151b8e36da7aeb5a2050c0a3264247589a0e Mon Sep 17 00:00:00 2001 From: Oscar Gentilezza Date: Mon, 22 Jan 2018 19:09:04 -0300 Subject: [PATCH 33/35] Add getDataValue and setDataValue --- src/instance.js | 18 ++++++++++++++++++ test/instance.spec.js | 20 +++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/instance.js b/src/instance.js index b35df2d..a2cb256 100644 --- a/src/instance.js +++ b/src/instance.js @@ -205,6 +205,24 @@ fakeModelInstance.prototype.get = function (key) { } }; +/** + * Get plain value + * @param {String} key Key yo get the value for + * @return {Any} + */ +fakeModelInstance.prototype.getDataValue = function (key) { + return this._values[key]; +}; + +/** + * Set plain value + * @param {String} key Key yo get the value for + * @param {Any} value + */ +fakeModelInstance.prototype.setDataValue = function (key, value) { + this._values[key] = value; +}; + /** * Triggers validation. If there are errors added through `$addValidationError` they will * be returned and the queue of validation errors will be cleared. diff --git a/test/instance.spec.js b/test/instance.spec.js index 916c71b..e45c145 100644 --- a/test/instance.spec.js +++ b/test/instance.spec.js @@ -161,7 +161,25 @@ describe('Instance', function () { inst.get().should.be.eql({foo: 'bar'}); }); }); - + + describe('#getDataValue', function () { + it('should get the value of a property on the object', function () { + var inst = new Instance(); + inst._values.foo = 'bar'; + + inst.getDataValue('foo').should.be.exactly('bar'); + }); + }); + + describe('#setDataValue', function () { + it('should set the value of a property on the object', function () { + var inst = new Instance(); + inst._values.foo = 'bar'; + inst.setDataValue('foo', 'baz'); + inst._values.foo.should.be.exactly('baz'); + }); + }); + describe('#validate', function () { it('should return no validation errors if no errors are set on the instances', function (done) { var inst = new Instance(); From 2cc4dbddf8b36f0ba6ff149cac3f33a84494c4ba Mon Sep 17 00:00:00 2001 From: Ian Cervantez Date: Thu, 16 Aug 2018 21:03:07 -0500 Subject: [PATCH 34/35] #36: Adding ES6 style module support --- src/sequelize.js | 9 +++++++-- test/sequelize.spec.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/sequelize.js b/src/sequelize.js index f2ba06a..485b4d2 100644 --- a/src/sequelize.js +++ b/src/sequelize.js @@ -334,8 +334,13 @@ Sequelize.prototype.import = function (importPath) { importPath = path.resolve(callLoc, importPath); } - if(this.importCache[importPath] === 'string' || !this.importCache[importPath]) { - this.importCache[importPath] = require(importPath)(this, DataTypes); + if(this.importCache[importPath] === 'string' || !this.importCache[importPath]) { + var defineCall = arguments.length > 1 ? arguments[1] : require(importPath); + if(typeof defineCall === 'object') { + // ES6 module compatibility + defineCall = defineCall.default; + } + this.importCache[importPath] = defineCall(this, DataTypes); } return this.importCache[importPath]; diff --git a/test/sequelize.spec.js b/test/sequelize.spec.js index ace2fe4..5072d48 100644 --- a/test/sequelize.spec.js +++ b/test/sequelize.spec.js @@ -39,6 +39,7 @@ var Sequelize = proxyquire('../src/sequelize', { './data-types' : function () {}, 'import-test' : importTestFunc, + 'import-test-es6' : { default: importTestFunc }, }); describe('Sequelize', function () { @@ -272,6 +273,18 @@ describe('Sequelize', function () { }; seq.import('foo').should.be.exactly(findItem); }); + + it('should import an es6 model from the given path', function () { + seq.import('import-test-es6'); + should(lastImportTestCall).not.be.Null(); + lastImportTestCall[0].should.be.exactly(seq); + }); + + it('should import a model function as the second argument (for meteor compatibility)', function () { + seq.import('import-test', importTestFunc); + should(lastImportTestCall).not.be.Null(); + lastImportTestCall[0].should.be.exactly(seq); + }); }); describe('#model', function() { From 76c684d9804663262023cc2e579d77e3df8647d3 Mon Sep 17 00:00:00 2001 From: Kevin Brian <116806209+kevinbrian-cortex@users.noreply.github.com> Date: Fri, 21 Jul 2023 11:54:54 -0700 Subject: [PATCH 35/35] Update mock-queries.md I have updated the documentation of this error, that too many people were complaining User.$useHandler(function(query, queryOptions, done) { if (query === 'findOne') { if (queryOptions[0].where.id === 42) { // Result found, return it return User.build({ id: 42, name: 'foo' }); } else { // No results return null; } } }); --- docs/docs/mock-queries.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/docs/mock-queries.md b/docs/docs/mock-queries.md index f6aa926..620b1eb 100644 --- a/docs/docs/mock-queries.md +++ b/docs/docs/mock-queries.md @@ -39,7 +39,7 @@ Option | Type | Description ## Query handlers -Query results can be generated using query handlers. When multiple handlers are added to the QueryInterface, they will be called in order until one of them returns a valud result. If no handler returns a result, then QueryInterface will get the value from the list of queued results. +Query results can be generated using query handlers. When multiple handlers are added to the QueryInterface, they will be called in order until one of them returns a valid result. If no handler returns a result, then QueryInterface will get the value from the list of queued results. The handler will receive two arguments @@ -49,9 +49,12 @@ The handler will receive two arguments Those arguments can be used to filter the results. For example: ```javascript -User.$useHandler(function(query, queryOptions, done) { +User.$queryInterface.$useHandler(function(query, queryOptions, done) { + //this will return + //query: 'findOne', + //queryOptions: [Arguments] { '0': { [field_your_are_querying]: 'value' } if (query === 'findOne') { - if (queryOptions[0].where.id === 42) { + if (queryOptions[0].field === value_you_desire_to_find) { // Result found, return it return User.build({ id: 42, name: 'foo' }); } else {