Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating #97

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

v0.10.2 - 8ed75d2 - Dec 4th 2017
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

Expand Down
6 changes: 3 additions & 3 deletions docs/api/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ UserMock.findOne().then(function (result) {
UserMock.$queueResult(UserMock.build(), { wasCreated: false });
UserMock.findOrCreate({
// ...
}).spread(function (user, created) {
}).then(function ([user, created]) {
// created == false
});
```
Expand Down Expand Up @@ -306,8 +306,8 @@ Name | Type | Description



<a name="findById"></a>
## findById(id) -> Promise.&#60;Instance&#62;
<a name="findByPk"></a>
## findBfindByPkyId(id) -> Promise.&#60;Instance&#62;

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
Expand Down
2 changes: 1 addition & 1 deletion docs/api/sequelize.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Reference to the Util functions
<a name="Promise"></a>
## Promise

Reference to the bluebird promise library
Reference to the Promise library



Expand Down
6 changes: 3 additions & 3 deletions docs/docs/mock-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ User.$useHandler(function(query, queryOptions, done) {
if (query === "findOne") return User.build({id: 1});
});
User.$useHandler(function(query, queryOptions, done) {
if (query === "findById") return User.build({id: queryOptions[0]});
if (query === "findByPk") return User.build({id: queryOptions[0]});
});
User.$useHandler(function(query, queryOptions, done) {
if (query === "findOrCreate") return User.build({id:1000});
Expand All @@ -107,7 +107,7 @@ User.$useHandler(function(query, queryOptions, done) {
User.findOne().then(function (user) {
user.get('id'); // === 1
});
User.findById(123).then(function (user) {
User.findByPk(123).then(function (user) {
user.get('id'); // === 123
});
User.findOrCreate().then(function (user) {
Expand Down Expand Up @@ -158,7 +158,7 @@ Some functions require additional parameters or configuration. You can specify b
```javascript
User.$queueResult( User.build(), { wasCreated: true } );

User.findOrCreate().spread(function (user, wasCreated) {
User.findOrCreate().then(function ([user, wasCreated]) {
wasCreated; // === true
});
```
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
},
"homepage": "https://github.com/BlinkUX/sequelize-mock#readme",
"dependencies": {
"bluebird": "^3.4.6",
"inflection": "^1.10.0",
"lodash": "^4.16.4"
},
Expand Down
27 changes: 4 additions & 23 deletions src/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
* @fileOverview Instances of Models created by Model function calls.
*/

var bluebird = require('bluebird'),
_ = require('lodash'),
var _ = require('lodash'),
Errors = require('./errors');

var id = 0;
Expand Down Expand Up @@ -205,24 +204,6 @@ 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.
Expand Down Expand Up @@ -250,7 +231,7 @@ fakeModelInstance.prototype.validate = function () {
this.$clearValidationErrors();
}

return bluebird.resolve(validationError);
return Promise.resolve(validationError);
};

/**
Expand Down Expand Up @@ -279,7 +260,7 @@ fakeModelInstance.prototype.save = function () {
**/
fakeModelInstance.prototype.destroy = function () {
this._values.deletedAt = new Date();
return bluebird.resolve();
return Promise.resolve();
};

/**
Expand All @@ -289,7 +270,7 @@ fakeModelInstance.prototype.destroy = function () {
* @return {Promise<Instance>} will always resolve with the current instance
**/
fakeModelInstance.prototype.reload = function () {
return bluebird.resolve(this);
return Promise.resolve(this);
};

/**
Expand Down
66 changes: 52 additions & 14 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
* @fileOverview The base mock Model object for use in tests
*/

var Promise = require('bluebird'),
_ = require('lodash'),
var _ = require('lodash'),
nodeutil = require('util'),
Utils = require('./utils'),
Instance = require('./instance'),
Expand Down Expand Up @@ -127,7 +126,7 @@ function fakeModel (name, defaults, opts) {
* UserMock.$queueResult(UserMock.build(), { wasCreated: false });
* UserMock.findOrCreate({
* // ...
* }).spread(function (user, created) {
* }).then(function ([user, created]) {
* // created == false
* });
*
Expand Down Expand Up @@ -239,14 +238,6 @@ 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
Expand Down Expand Up @@ -346,11 +337,11 @@ fakeModel.prototype.findAndCountAll = function (options) {
* @param {Integer} id ID of the instance
* @return {Promise<Instance>} Promise that resolves with an instance with the given ID
**/
fakeModel.prototype.findById = function (id) {
fakeModel.prototype.findByPk = function (id) {
var self = this;

return this.$query({
query: "findById",
query: "findByPk",
queryOptions: arguments,
fallbackFn: !this.options.autoQueryFallback ? null : function () {
return Promise.resolve( self.build({ id: id }) );
Expand Down Expand Up @@ -395,6 +386,53 @@ fakeModel.prototype.findOne = function (obj) {
});
};


/**
* Executes a mock query to count all of the instances with any provided options.
* Without any other configuration, the default behavior when no queueud query result
* is present is to create result with the value 1 wrapped in 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.count({
* where: {
* email: 'myEmail@example.com',
* },
* }).then(function (count) {
* // count returns the actual value
* count == 1; // true
* });
*
* @instance
* @method count
* @param {Object} [options] Options for the count query
* @param {Object} [options.where] Values that any automatically created Instances should have
* @return {Promise<Object>} result returned by the mock query
**/
fakeModel.prototype.count = function(options) {
var self = this;

return this.$query({
query: "count",
queryOptions: arguments,
fallbackFn: !this.options.autoQueryFallback
? null
: function() {
return Promise.resolve([
self.build(options ? options.where : {})
]).then(function(result) {
return Promise.resolve(result.length);
});
}
});
};



/**
* Executes a mock query to find the max value of a field. Without any other
* configuration, the default behavior when no queueud query result is present
Expand Down Expand Up @@ -525,7 +563,7 @@ fakeModel.prototype.upsert = function (values) {
query: "upsert",
queryOptions: arguments,
fallbackFn: !this.options.autoQueryFallback ? null : function () {
return self.build(values).save().return(self.options.createdDefault);
return self.build(values).save().then((()=>self.options.createdDefault))
},
});
}
Expand Down
13 changes: 6 additions & 7 deletions src/queryinterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
* @fileOverview The mock QueryInterface base that is used for returning results from queries for tests
**/

var bluebird = require('bluebird'),
_ = require('lodash'),
var _ = require('lodash'),
Errors = require('./errors');

/**
Expand Down Expand Up @@ -180,7 +179,7 @@ function resultsQueueHandler(qi, options) {
}

if(result.type == 'Failure') {
return bluebird.reject(result.content);
return Promise.reject(result.content);
}

if(options.includeCreated) {
Expand All @@ -189,17 +188,17 @@ function resultsQueueHandler(qi, options) {
created = !!result.options.wasCreated;
}

return bluebird.resolve([result.content, created]);
return Promise.resolve([result.content, created]);
}
if (options.includeAffectedRows) {
var affectedRows = [];
if(result.options.affectedRows instanceof Array) {
affectedRows = result.options.affectedRows;
}

return bluebird.resolve([result.content, affectedRows]);
return Promise.resolve([result.content, affectedRows]);
}
return bluebird.resolve(result.content);
return Promise.resolve(result.content);
}
}

Expand Down Expand Up @@ -256,7 +255,7 @@ QueryInterface.prototype.$query = function (options) {
processHandler(handlers.shift());

// Always convert the result to a promise. If the promise was rejected, this method will return a rejected promise.
return bluebird.resolve(result);
return Promise.resolve(result);
};

module.exports = QueryInterface;
Loading