Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
67c8039
Merge pull request #27 from BlinkUX/docs-patch
LoveAndCoding Jul 31, 2017
919b36e
Implement Sequelize#authenticate
vFederer Sep 3, 2017
07a11c3
Merge pull request #29 from vFederer/feature-authenticate
LoveAndCoding Sep 5, 2017
350059a
Fix misspelling
localstatic Sep 8, 2017
0e949e0
Merge pull request #30 from localstatic/patch-1
LoveAndCoding Sep 11, 2017
1990cd7
Add `sequelize#authenticate()` to documentation
LoveAndCoding Sep 21, 2017
e37dbf9
Add recent changes to Changelog
LoveAndCoding Sep 21, 2017
3aeaa05
0.9.1
LoveAndCoding Sep 21, 2017
edd97de
Add v0.9.1 Commit Hash to Changelog
LoveAndCoding Sep 21, 2017
8d262a3
Add FindAndCountAll method and add tests
TerryMooreII Oct 20, 2017
0c65238
Merge pull request #32 from TerryMooreII/findAndCountAll
LoveAndCoding Oct 23, 2017
8bc80c2
Add `findAndCountAll` to changelog
LoveAndCoding Oct 23, 2017
b728ac9
Add support for `findAndCount` alias for `findAndCountAll`
LoveAndCoding Oct 23, 2017
17cadc9
Add `findAndCount` alias to Model test
LoveAndCoding Oct 23, 2017
b69eef2
Add `Utils.stack` and Tests
LoveAndCoding Oct 24, 2017
96fddc0
Add `sequelize.import` and `sequelize.$overrideImport` support
LoveAndCoding Oct 24, 2017
82ca223
Add import notes to changelog
LoveAndCoding Oct 24, 2017
47c5420
Add `import` to Getting Started Documentation
LoveAndCoding Oct 24, 2017
cec23a6
Add `.stack()` to Utils API Docs
LoveAndCoding Oct 24, 2017
9912a55
Add `import` and `$overrideImport` to Sequelize API Docs
LoveAndCoding Oct 24, 2017
1c2be61
Merge pull request #34 from BlinkUX/import
LoveAndCoding Oct 24, 2017
daee2c0
Add `findAndCount` to Model API Docs
LoveAndCoding Oct 24, 2017
0a7f270
0.10.0
LoveAndCoding Oct 24, 2017
edac5af
Add v0.10.0 Commit Hash to Changelog
LoveAndCoding Oct 24, 2017
0a184bb
Fix `Util.stack` functionality
LoveAndCoding Nov 20, 2017
b815684
Fix expected return from `Util.stack` in `import`
LoveAndCoding Nov 20, 2017
43cc668
0.10.1
LoveAndCoding Nov 20, 2017
fe656c6
Add v0.10.1 Commit Hash to Changelog
LoveAndCoding Nov 20, 2017
286e644
Fix issue related to mimicking sequelize.import
robpre Nov 22, 2017
70ae87e
Create .editorconfig
LoveAndCoding Nov 22, 2017
594b4e2
Merge pull request #40 from BlinkUX/editorconfig
LoveAndCoding Nov 22, 2017
dd877f8
Add editorconfig override for files needing spaces
LoveAndCoding Nov 22, 2017
a835177
Merge pull request #41 from BlinkUX/editorconfig
LoveAndCoding Nov 22, 2017
07d8f98
Add editorconfig note to changelog
LoveAndCoding Nov 22, 2017
c3e573b
Merge pull request #39 from RpprRoger/patch-1
LoveAndCoding Nov 27, 2017
aec4d0b
Add DataType Fix to Changelog
LoveAndCoding Dec 4, 2017
8ed75d2
0.10.2
LoveAndCoding Dec 4, 2017
2f13295
Add v0.10.2 Commit Hash to Changelog
LoveAndCoding Dec 4, 2017
04c9e38
Add support for mocking .addScope()
toxuin Jan 18, 2018
cc93c78
Enable check for addScope in model test
toxuin Jan 18, 2018
93c5d33
Merge pull request #47 from toxuin/master
LoveAndCoding Jan 22, 2018
4975151
Add getDataValue and setDataValue
Jan 22, 2018
6478e15
Merge pull request #48 from exos/feature/get-set-value-support
LoveAndCoding Jan 23, 2018
2cc4dbd
#36: Adding ES6 style module support
TigerC10 Aug 17, 2018
2db72e5
Merge pull request #61 from TigerC10/master
litopia Oct 17, 2018
76c684d
Update mock-queries.md
officialkevinbrian Jul 21, 2023
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
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2
16 changes: 16 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

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

v0.10.1 - 43cc668 - Nov 20th 2017
* 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
* 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)
* 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)
Expand Down
40 changes: 40 additions & 0 deletions docs/api/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,46 @@ Name | Type | Description



<a name="findAndCount"></a>
## findAndCount([options]) -> Promise.&#60;Object&#62;

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`. <br>**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.<Object>`: result returned by the mock query



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

Expand Down
60 changes: 60 additions & 0 deletions docs/api/sequelize.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Options passed into the Sequelize initialization



<a name="importCache"></a>
### .importCache

Used to cache and override model imports for easy mock model importing



<a name="models"></a>
### .models

Expand Down Expand Up @@ -151,6 +158,25 @@ Clears any queued results from `$queueResult` or `$queueFailure` <br>**Alias** $



<a name="overrideImport"></a>
## $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




<a name="getDialect"></a>
## getDialect() -> String

Expand Down Expand Up @@ -240,6 +266,30 @@ name | String | Name of the model



<a name="import"></a>
## 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



<a name="model"></a>
## model(name) -> Model

Expand Down Expand Up @@ -303,3 +353,13 @@ arg | Any | Value to return
### Return
`Any`: value passed in



<a name="authenticate"></a>
## authenticate() -> Promise

Always returns a resolved promise

### Return
`Promise`: will always resolve as a successful authentication

7 changes: 7 additions & 0 deletions docs/api/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ str | String | Word to convert to its plural form



<a name="stack"></a>
## stack()

Gets the current stack frame



<a name="lodash"></a>
## lodash

Expand Down
23 changes: 22 additions & 1 deletion docs/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions docs/docs/mock-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sequelize-mock",
"version": "0.9.0",
"version": "0.10.2",
"description": "A simple mock interface specifically for testing code relying on Sequelize models",
"main": "src/index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,6 @@ module.exports = function (Sequelize) {
Sequelize.ARRAY = ARRAY;
Sequelize.GEOMETRY = GEOMETRY;
Sequelize.GEOGRAPHY = GEOGRAPHY;

return Sequelize;
};
18 changes: 18 additions & 0 deletions src/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
57 changes: 57 additions & 0 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -277,6 +285,55 @@ 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
* @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<Object>} result returned by the mock query
**/
fakeModel.prototype.findAndCount =
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
Expand Down
Loading