├── .gitattributes ├── test ├── user-database.json ├── my-model-1-database.json ├── my-model-database.json ├── sequelize-mocking-mochaSpec.js └── sequelize-mockingSpec.js ├── .npmrc ├── .travis.yml ├── .eslintignore ├── examples ├── mocha │ ├── test │ │ └── user │ │ │ ├── fake-users-database.json │ │ │ ├── service-with-sequelize-mocking-mochaSpec.js │ │ │ ├── service-with-sequelize-mockingSpec.js │ │ │ ├── serviceSpec.js │ │ │ └── service-with-fixturesSpec.js │ ├── lib │ │ ├── index.js │ │ ├── user │ │ │ ├── service.js │ │ │ └── model.js │ │ └── database │ │ │ ├── _instance.js │ │ │ └── index.js │ └── package.json ├── tape │ ├── test │ │ └── user │ │ │ ├── fake-users-database.json │ │ │ ├── service-with-sequelize-mocking-tapeSpec.js │ │ │ └── serviceSpec.js │ ├── lib │ │ ├── index.js │ │ ├── user │ │ │ ├── service.js │ │ │ └── model.js │ │ └── database │ │ │ ├── _instance.js │ │ │ └── index.js │ └── package.json ├── jasmine │ ├── test │ │ ├── user │ │ │ ├── fake-users-database.json │ │ │ ├── service-with-sequelize-mocking-jasmineSpec.js │ │ │ ├── service-with-sequelize-mockingSpec.js │ │ │ ├── serviceSpec.js │ │ │ └── service-with-fixturesSpec.js │ │ └── index.js │ ├── lib │ │ ├── index.js │ │ ├── user │ │ │ ├── service.js │ │ │ └── model.js │ │ └── database │ │ │ ├── _instance.js │ │ │ └── index.js │ └── package.json └── debug │ ├── issue-007 │ ├── test │ │ ├── user │ │ │ ├── fake-users-database.json │ │ │ └── service-with-sequelize-mocking-mochaSpec.js │ │ └── index.js │ ├── lib │ │ ├── user │ │ │ ├── model.js │ │ │ └── service.js │ │ ├── index.js │ │ └── database │ │ │ ├── _instance.js │ │ │ └── index.js │ └── package.json │ ├── issue-024 │ ├── originals │ │ ├── index.txt │ │ ├── controller.txt │ │ ├── userModel.txt │ │ └── test-spec.txt │ ├── test │ │ ├── user │ │ │ ├── fake-users-database.json │ │ │ └── service-with-sequelize-mocking-jasmineSpec.js │ │ └── index.js │ ├── package.json │ └── lib │ │ ├── database │ │ ├── _instance.js │ │ └── index.js │ │ ├── index.js │ │ └── user │ │ ├── service.js │ │ └── model.js │ └── issue-016 │ ├── test │ ├── user │ │ ├── fake-users-database.json │ │ └── service-with-sequelize-mocking-jasmineSpec.js │ └── index.js │ ├── lib │ ├── index.js │ ├── user │ │ ├── service.js │ │ └── model.js │ └── database │ │ ├── _instance.js │ │ └── index.js │ └── package.json ├── .editorconfig ├── .npmversionrc ├── .gitignore ├── .dockerignore ├── Dockerfile ├── index.js ├── LICENSE ├── lib ├── sequelize-mocking-mocha.js ├── sequelize-mocking-jasmine.js ├── sequelize-mocking-tape.js └── sequelize-mocking.js ├── package.json ├── README.md └── .eslintrc /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /test/user-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "User", 4 | "data": {} 5 | } 6 | ] 7 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | save-exact=true 2 | sign-git-tag=false 3 | strict-ssl=false 4 | engine-strict=true 5 | progress=false 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: required 3 | node_js: 4 | - '11' 5 | - '10' 6 | - '8' 7 | - '6' 8 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | bower_components/**/* 2 | node_modules/**/* 3 | target/**/* 4 | test/**/* 5 | .temp/**/* 6 | .dist/**/* 7 | -------------------------------------------------------------------------------- /test/my-model-1-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "myModel", 4 | "data": { 5 | "id": 2, 6 | "description": null 7 | } 8 | } 9 | ] 10 | -------------------------------------------------------------------------------- /test/my-model-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "myModel", 4 | "data": { 5 | "id": 1, 6 | "description": null 7 | } 8 | } 9 | ] 10 | -------------------------------------------------------------------------------- /examples/mocha/test/user/fake-users-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "user", 4 | "data": { 5 | "id": 1, 6 | "firstName": "John", 7 | "lastName": "Doe", 8 | "age": 25, 9 | "description": null 10 | } 11 | } 12 | ] -------------------------------------------------------------------------------- /examples/tape/test/user/fake-users-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "user", 4 | "data": { 5 | "id": 1, 6 | "firstName": "John", 7 | "lastName": "Doe", 8 | "age": 25, 9 | "description": null 10 | } 11 | } 12 | ] -------------------------------------------------------------------------------- /examples/jasmine/test/user/fake-users-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "user", 4 | "data": { 5 | "id": 1, 6 | "firstName": "John", 7 | "lastName": "Doe", 8 | "age": 25, 9 | "description": null 10 | } 11 | } 12 | ] -------------------------------------------------------------------------------- /examples/debug/issue-007/test/user/fake-users-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "User", 4 | "data": { 5 | "email": "roche.jul@gmail.com", 6 | "password": "5a5fd34f5b3ab16aff01a05dbf570d737e3edec3", 7 | "salt": "abcdef" 8 | } 9 | } 10 | ] 11 | -------------------------------------------------------------------------------- /examples/debug/issue-024/originals/index.txt: -------------------------------------------------------------------------------- 1 | const Sequelize = require('sequelize'); 2 | 3 | const sql = new Sequelize('database', 'username', 'password', { 4 | host: host, 5 | dialect: 'mysql', 6 | logging: false, 7 | operatorsAliases: Sequelize.Op 8 | }); 9 | 10 | module.exports = sql; 11 | -------------------------------------------------------------------------------- /examples/debug/issue-016/test/user/fake-users-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "User", 4 | "data": { 5 | "uuid": "f64f2940-fae4-11e7-8c5f-ef356f279131", 6 | "firstName": "John", 7 | "lastName": "Doe", 8 | "age": 25, 9 | "description": null 10 | } 11 | } 12 | ] 13 | -------------------------------------------------------------------------------- /examples/debug/issue-024/test/user/fake-users-database.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "model": "User", 4 | "data": { 5 | "uuid": "f64f2940-fae4-11e7-8c5f-ef356f279131", 6 | "firstName": "John", 7 | "lastName": "Doe", 8 | "age": 25, 9 | "description": null 10 | } 11 | } 12 | ] 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | indent_style = space 7 | indent_size = 4 8 | end_of_line = lf 9 | charset = utf-8 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | 13 | [*.md] 14 | trim_trailing_whitespace = false 15 | 16 | [{package,bower}.json] 17 | indent_style = space 18 | indent_size = 2 19 | -------------------------------------------------------------------------------- /.npmversionrc: -------------------------------------------------------------------------------- 1 | { 2 | "force-preid": true, 3 | "nogit-commit": false, 4 | "nogit-tag": false, 5 | "git-push": true, 6 | "git-create-branch": false, 7 | "git-branch-message": "releases/%s", 8 | "git-commit-message": "Release version: %s", 9 | "git-remote-name": "origin", 10 | "git-tag-message": "v%s", 11 | "increment": "patch", 12 | "jsonFiles": [ 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /examples/mocha/lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Run the application 3 | * 4 | * @module node-sequelize-with-mocks/index 5 | */ 6 | 7 | 'use strict'; 8 | 9 | // Imports 10 | const UserService = require('./user/service'); 11 | 12 | // Run 13 | console.log('Fetch the users'); 14 | 15 | UserService 16 | .findAll() 17 | .then(function (users) { 18 | console.dir(users); 19 | }) 20 | .catch(function (err) { 21 | console.error(err && err.stack ? err.stack : err); 22 | }); 23 | -------------------------------------------------------------------------------- /examples/tape/lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Run the application 3 | * 4 | * @module node-sequelize-with-mocks/index 5 | */ 6 | 7 | 'use strict'; 8 | 9 | // Imports 10 | const UserService = require('./user/service'); 11 | 12 | // Run 13 | console.log('Fetch the users'); 14 | 15 | UserService 16 | .findAll() 17 | .then(function (users) { 18 | console.dir(users); 19 | }) 20 | .catch(function (err) { 21 | console.error(err && err.stack ? err.stack : err); 22 | }); 23 | -------------------------------------------------------------------------------- /examples/debug/issue-007/lib/user/model.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Database = require('../database'); 4 | 5 | module.exports = Database 6 | .getInstance() 7 | .define( 8 | 'User', 9 | { 10 | 'email': { 11 | 'type': Database.FIELD_TYPE_ENUM.STRING, 12 | 'unique': true 13 | }, 14 | 'password': Database.FIELD_TYPE_ENUM.STRING, 15 | 'salt': Database.FIELD_TYPE_ENUM.STRING 16 | } 17 | ); 18 | -------------------------------------------------------------------------------- /examples/jasmine/lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Run the application 3 | * 4 | * @module node-sequelize-with-mocks/index 5 | */ 6 | 7 | 'use strict'; 8 | 9 | // Imports 10 | const UserService = require('./user/service'); 11 | 12 | // Run 13 | console.log('Fetch the users'); 14 | 15 | UserService 16 | .findAll() 17 | .then(function (users) { 18 | console.dir(users); 19 | }) 20 | .catch(function (err) { 21 | console.error(err && err.stack ? err.stack : err); 22 | }); 23 | -------------------------------------------------------------------------------- /examples/debug/issue-007/lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Run the application 3 | * 4 | * @module node-sequelize-with-mocks/index 5 | */ 6 | 7 | 'use strict'; 8 | 9 | // Imports 10 | const UserService = require('./user/service'); 11 | 12 | // Run 13 | console.log('Fetch the users'); 14 | 15 | UserService 16 | .findAll() 17 | .then(function (users) { 18 | console.dir(users); 19 | }) 20 | .catch(function (err) { 21 | console.error(err && err.stack ? err.stack : err); 22 | }); 23 | -------------------------------------------------------------------------------- /examples/jasmine/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-sequelize-with-mocks", 3 | "version": "1.0.0", 4 | "description": "NodeJs projet to test Sequelize as ORM, but moreover, to see how to make some mocks and tests around Sequelize", 5 | "main": "./lib/index.js", 6 | "scripts": { 7 | "test": "node ./test/index.js" 8 | }, 9 | "license": "ISC", 10 | "engines": { 11 | "node": ">=4.0.0", 12 | "npm": ">=2.0.0" 13 | }, 14 | "dependencies": { 15 | "mysql2": "1.3.5", 16 | "sequelize": "4.3.1" 17 | }, 18 | "devDependencies": { 19 | "jasmine": "2.4.1", 20 | "sequelize-mocking": "../../" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /examples/debug/issue-007/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-sequelize-with-mocks", 3 | "version": "1.0.0", 4 | "description": "NodeJs projet to test Sequelize as ORM, but moreover, to see how to make some mocks and tests around Sequelize", 5 | "main": "./lib/index.js", 6 | "scripts": { 7 | "test": "node ./test/index.js" 8 | }, 9 | "license": "ISC", 10 | "engines": { 11 | "node": ">=4.0.0", 12 | "npm": ">=2.0.0" 13 | }, 14 | "dependencies": { 15 | "mysql2": "1.3.5", 16 | "sequelize": "4.3.1" 17 | }, 18 | "devDependencies": { 19 | "jasmine": "2.4.1", 20 | "sequelize-mocking": "../../../" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /examples/debug/issue-016/lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Run the application 3 | * 4 | * @module node-sequelize-with-mocks/index 5 | */ 6 | 7 | 'use strict'; 8 | 9 | // Imports 10 | const UserService = require('./user/service'); 11 | const Database = require('./database'); 12 | 13 | // Run 14 | console.log('Fetch the users'); 15 | 16 | Database 17 | .getInstance() 18 | .sync() 19 | .then(() => { 20 | return UserService 21 | .findAll() 22 | .then(function (users) { 23 | console.dir(users); 24 | }); 25 | }) 26 | .catch(function (err) { 27 | console.error(err && err.stack ? err.stack : err); 28 | }); 29 | -------------------------------------------------------------------------------- /examples/mocha/lib/user/service.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Service around @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/service 5 | * @exports UserService 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const UserModel = require('./model'); 12 | 13 | 14 | class UserService { 15 | /** 16 | * @returns {Promise.} 17 | */ 18 | static findAll() { 19 | return UserModel.findAll({ 20 | 'raw': true 21 | }); 22 | } 23 | 24 | /** 25 | * @param {number} userId 26 | * @returns {Promise.} 27 | */ 28 | static find(userId) { 29 | return UserModel.findById(userId); 30 | } 31 | } 32 | 33 | module.exports = UserService; 34 | 35 | -------------------------------------------------------------------------------- /examples/tape/lib/user/service.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Service around @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/service 5 | * @exports UserService 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const UserModel = require('./model'); 12 | 13 | 14 | class UserService { 15 | /** 16 | * @returns {Promise.} 17 | */ 18 | static findAll() { 19 | return UserModel.findAll({ 20 | 'raw': true 21 | }); 22 | } 23 | 24 | /** 25 | * @param {number} userId 26 | * @returns {Promise.} 27 | */ 28 | static find(userId) { 29 | return UserModel.findById(userId); 30 | } 31 | } 32 | 33 | module.exports = UserService; 34 | 35 | -------------------------------------------------------------------------------- /examples/jasmine/lib/user/service.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Service around @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/service 5 | * @exports UserService 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const UserModel = require('./model'); 12 | 13 | 14 | class UserService { 15 | /** 16 | * @returns {Promise.} 17 | */ 18 | static findAll() { 19 | return UserModel.findAll({ 20 | 'raw': true 21 | }); 22 | } 23 | 24 | /** 25 | * @param {number} userId 26 | * @returns {Promise.} 27 | */ 28 | static find(userId) { 29 | return UserModel.findById(userId); 30 | } 31 | } 32 | 33 | module.exports = UserService; 34 | 35 | -------------------------------------------------------------------------------- /examples/tape/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-sequelize-with-mocks", 3 | "version": "1.0.0", 4 | "description": "NodeJs projet to test Sequelize as ORM, but moreover, to see how to make some mocks and tests around Sequelize", 5 | "main": "./lib/index.js", 6 | "scripts": { 7 | "tape": "node ./node_modules/tape/bin/tape", 8 | "test": "npm run tape test/**/*Spec.js" 9 | }, 10 | "license": "ISC", 11 | "engines": { 12 | "node": ">=4.0.0", 13 | "npm": ">=2.0.0" 14 | }, 15 | "dependencies": { 16 | "mysql2": "1.3.5", 17 | "sequelize": "4.3.1" 18 | }, 19 | "devDependencies": { 20 | "sequelize-mocking": "../../", 21 | "sinon": "1.17.3", 22 | "tape": "4.6.2" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/debug/issue-016/lib/user/service.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Service around @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/service 5 | * @exports UserService 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const UserModel = require('./model'); 12 | 13 | 14 | class UserService { 15 | /** 16 | * @returns {Promise.} 17 | */ 18 | static findAll() { 19 | return UserModel.findAll({ 20 | 'raw': true 21 | }); 22 | } 23 | 24 | /** 25 | * @param {number} userId 26 | * @returns {Promise.} 27 | */ 28 | static find(userId) { 29 | return UserModel.findById(userId); 30 | } 31 | } 32 | 33 | module.exports = UserService; 34 | 35 | -------------------------------------------------------------------------------- /examples/mocha/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-sequelize-with-mocks", 3 | "version": "1.0.0", 4 | "description": "NodeJs projet to test Sequelize as ORM, but moreover, to see how to make some mocks and tests around Sequelize", 5 | "main": "./lib/index.js", 6 | "scripts": { 7 | "test": "node ./node_modules/mocha/bin/mocha --recursive --ui bdd --colors ./test" 8 | }, 9 | "license": "ISC", 10 | "engines": { 11 | "node": ">=4.0.0", 12 | "npm": ">=2.0.0" 13 | }, 14 | "dependencies": { 15 | "mysql2": "1.3.5", 16 | "sequelize": "4.3.1" 17 | }, 18 | "devDependencies": { 19 | "chai": "3.5.0", 20 | "mocha": "2.4.5", 21 | "sequelize-mocking": "../../", 22 | "sinon": "1.17.3" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | 35 | # Temporary folder 36 | temp 37 | target 38 | 39 | # IDE project folder 40 | .idea 41 | -------------------------------------------------------------------------------- /examples/debug/issue-016/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-sequelize-with-mocks", 3 | "version": "1.0.0", 4 | "description": "NodeJs projet to test Sequelize as ORM, but moreover, to see how to make some mocks and tests around Sequelize", 5 | "main": "./lib/index.js", 6 | "scripts": { 7 | "start": "node ./lib/index.js", 8 | "start-mysql": "node ./lib/index.js --mysql", 9 | "test": "node ./test/index.js", 10 | "test-mysql": "node ./test/index.js --mysql" 11 | }, 12 | "license": "ISC", 13 | "engines": { 14 | "node": ">=4.0.0", 15 | "npm": ">=2.0.0" 16 | }, 17 | "dependencies": { 18 | "sequelize": "4.13.10", 19 | "uuid": "3.2.1" 20 | }, 21 | "devDependencies": { 22 | "jasmine": "2.4.1", 23 | "sequelize-mocking": "../../../" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /examples/debug/issue-024/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-sequelize-with-mocks", 3 | "version": "1.0.0", 4 | "description": "NodeJs projet to test Sequelize as ORM, but moreover, to see how to make some mocks and tests around Sequelize", 5 | "main": "./lib/index.js", 6 | "scripts": { 7 | "start": "node ./lib/index.js", 8 | "start-mysql": "node ./lib/index.js --mysql", 9 | "test": "node ./test/index.js", 10 | "test-mysql": "node ./test/index.js --mysql" 11 | }, 12 | "license": "ISC", 13 | "engines": { 14 | "node": ">=4.0.0", 15 | "npm": ">=2.0.0" 16 | }, 17 | "dependencies": { 18 | "mysql2": "1.3.5", 19 | "sequelize": "4.3.1" 20 | }, 21 | "devDependencies": { 22 | "jasmine": "2.4.1", 23 | "sequelize-mocking": "../../../" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | 30 | # Temporary folders 31 | target 32 | temp 33 | 34 | # IDE folder 35 | .idea 36 | .project 37 | -------------------------------------------------------------------------------- /examples/jasmine/test/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test runner over Jasmine 3 | * 4 | * @module test/index 5 | * @author Julien Roche 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Jasmine = require('jasmine'); 12 | 13 | // Configuration 14 | let jasmine = new Jasmine(); 15 | 16 | jasmine.loadConfig({ 17 | 'projectBaseDir': __dirname, 18 | 'spec_dir': 'test/', 19 | 'spec_files': [ 20 | '**/*Spec.js' 21 | ], 22 | 'random': false 23 | }); 24 | 25 | jasmine.configureDefaultReporter({ 'showColors': false }); 26 | 27 | jasmine.onComplete(function(passed) { 28 | if(passed) { 29 | console.log('All specs have passed'); 30 | } 31 | else { 32 | console.error('At least one spec has failed'); 33 | } 34 | }); 35 | 36 | // Run the tests ! 37 | jasmine.execute(); 38 | -------------------------------------------------------------------------------- /examples/debug/issue-007/test/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test runner over Jasmine 3 | * 4 | * @module test/index 5 | * @author Julien Roche 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Jasmine = require('jasmine'); 12 | 13 | // Configuration 14 | let jasmine = new Jasmine(); 15 | 16 | jasmine.loadConfig({ 17 | 'projectBaseDir': __dirname, 18 | 'spec_dir': 'test/', 19 | 'spec_files': [ 20 | '**/*Spec.js' 21 | ], 22 | 'random': false 23 | }); 24 | 25 | jasmine.configureDefaultReporter({ 'showColors': false }); 26 | 27 | jasmine.onComplete(function(passed) { 28 | if(passed) { 29 | console.log('All specs have passed'); 30 | } 31 | else { 32 | console.error('At least one spec has failed'); 33 | } 34 | }); 35 | 36 | // Run the tests ! 37 | jasmine.execute(); 38 | -------------------------------------------------------------------------------- /examples/debug/issue-016/test/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test runner over Jasmine 3 | * 4 | * @module test/index 5 | * @author Julien Roche 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Jasmine = require('jasmine'); 12 | 13 | // Configuration 14 | let jasmine = new Jasmine(); 15 | 16 | jasmine.loadConfig({ 17 | 'projectBaseDir': __dirname, 18 | 'spec_dir': 'test/', 19 | 'spec_files': [ 20 | '**/*Spec.js' 21 | ], 22 | 'random': false 23 | }); 24 | 25 | jasmine.configureDefaultReporter({ 'showColors': false }); 26 | 27 | jasmine.onComplete(function(passed) { 28 | if(passed) { 29 | console.log('All specs have passed'); 30 | } 31 | else { 32 | console.error('At least one spec has failed'); 33 | } 34 | }); 35 | 36 | // Run the tests ! 37 | jasmine.execute(); 38 | -------------------------------------------------------------------------------- /examples/debug/issue-024/test/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test runner over Jasmine 3 | * 4 | * @module test/index 5 | * @author Julien Roche 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Jasmine = require('jasmine'); 12 | 13 | // Configuration 14 | let jasmine = new Jasmine(); 15 | 16 | jasmine.loadConfig({ 17 | 'projectBaseDir': __dirname, 18 | 'spec_dir': 'test/', 19 | 'spec_files': [ 20 | '**/*Spec.js' 21 | ], 22 | 'random': false 23 | }); 24 | 25 | jasmine.configureDefaultReporter({ 'showColors': false }); 26 | 27 | jasmine.onComplete(function(passed) { 28 | if(passed) { 29 | console.log('All specs have passed'); 30 | } 31 | else { 32 | console.error('At least one spec has failed'); 33 | } 34 | }); 35 | 36 | // Run the tests ! 37 | jasmine.execute(); 38 | -------------------------------------------------------------------------------- /examples/debug/issue-007/lib/user/service.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Service around @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/service 5 | * @exports UserService 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const UserModel = require('./model'); 12 | const Sequelize = require('Sequelize'); 13 | 14 | class UserService { 15 | /** 16 | * @param {string} email 17 | * @param {string} password 18 | * @returns {Promise.} 19 | */ 20 | static login(email, password) { 21 | return UserModel 22 | .findOne({ 23 | 'where': { 24 | email, 25 | 'password': Sequelize.fn('sha1', Sequelize.fn('concat', password, Sequelize.col('salt'))) 26 | } 27 | }); 28 | } 29 | } 30 | 31 | module.exports = UserService; 32 | 33 | -------------------------------------------------------------------------------- /examples/mocha/lib/database/_instance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Internal module to deal with database intances 3 | * 4 | * @module node-sequelize-with-mocks/database/_instance 5 | * @private 6 | * @exports _DatabaseInstance 7 | */ 8 | 9 | 'use strict'; 10 | 11 | // Constants & variables 12 | 13 | /** 14 | * @type {Sequelize} 15 | * @private 16 | */ 17 | let _instance = null; 18 | 19 | /** 20 | * Database instance management 21 | * @private 22 | */ 23 | class _DatabaseInstance { 24 | /** 25 | * @returns {Sequelize} 26 | */ 27 | static getCurrentInstance() { 28 | return _instance; 29 | } 30 | 31 | /** 32 | * @param {Sequelize} instance 33 | * @returns {Sequelize} 34 | */ 35 | static setCurrentInstance(instance) { 36 | _instance = instance; 37 | return _instance; 38 | } 39 | } 40 | 41 | module.exports = _DatabaseInstance; 42 | -------------------------------------------------------------------------------- /examples/tape/lib/database/_instance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Internal module to deal with database intances 3 | * 4 | * @module node-sequelize-with-mocks/database/_instance 5 | * @private 6 | * @exports _DatabaseInstance 7 | */ 8 | 9 | 'use strict'; 10 | 11 | // Constants & variables 12 | 13 | /** 14 | * @type {Sequelize} 15 | * @private 16 | */ 17 | let _instance = null; 18 | 19 | /** 20 | * Database instance management 21 | * @private 22 | */ 23 | class _DatabaseInstance { 24 | /** 25 | * @returns {Sequelize} 26 | */ 27 | static getCurrentInstance() { 28 | return _instance; 29 | } 30 | 31 | /** 32 | * @param {Sequelize} instance 33 | * @returns {Sequelize} 34 | */ 35 | static setCurrentInstance(instance) { 36 | _instance = instance; 37 | return _instance; 38 | } 39 | } 40 | 41 | module.exports = _DatabaseInstance; 42 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # ---------------------------------------------------------------------------------------------------------------------- 2 | # Load 3 | 4 | FROM node:4.3.0 5 | 6 | # ---------------------------------------------------------------------------------------------------------------------- 7 | # Install the NodeJs App 8 | 9 | # Install app dependencies 10 | COPY package.json /sequelize-mocking/package.json 11 | COPY npm-shrinkwrap.json /sequelize-mocking/npm-shrinkwrap.json 12 | RUN cd /sequelize-mocking; npm install 13 | 14 | # Bundle app source (goal: check if the generated ES5 files work) 15 | COPY ./lib-es5 /sequelize-mocking/lib 16 | COPY ./test /sequelize-mocking/test 17 | 18 | # ---------------------------------------------------------------------------------------------------------------------- 19 | # Run the application 20 | 21 | WORKDIR /sequelize-mocking 22 | CMD ["npm", "test"] 23 | -------------------------------------------------------------------------------- /examples/jasmine/lib/database/_instance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Internal module to deal with database intances 3 | * 4 | * @module node-sequelize-with-mocks/database/_instance 5 | * @private 6 | * @exports _DatabaseInstance 7 | */ 8 | 9 | 'use strict'; 10 | 11 | // Constants & variables 12 | 13 | /** 14 | * @type {Sequelize} 15 | * @private 16 | */ 17 | let _instance = null; 18 | 19 | /** 20 | * Database instance management 21 | * @private 22 | */ 23 | class _DatabaseInstance { 24 | /** 25 | * @returns {Sequelize} 26 | */ 27 | static getCurrentInstance() { 28 | return _instance; 29 | } 30 | 31 | /** 32 | * @param {Sequelize} instance 33 | * @returns {Sequelize} 34 | */ 35 | static setCurrentInstance(instance) { 36 | _instance = instance; 37 | return _instance; 38 | } 39 | } 40 | 41 | module.exports = _DatabaseInstance; 42 | -------------------------------------------------------------------------------- /examples/debug/issue-007/lib/database/_instance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Internal module to deal with database intances 3 | * 4 | * @module node-sequelize-with-mocks/database/_instance 5 | * @private 6 | * @exports _DatabaseInstance 7 | */ 8 | 9 | 'use strict'; 10 | 11 | // Constants & variables 12 | 13 | /** 14 | * @type {Sequelize} 15 | * @private 16 | */ 17 | let _instance = null; 18 | 19 | /** 20 | * Database instance management 21 | * @private 22 | */ 23 | class _DatabaseInstance { 24 | /** 25 | * @returns {Sequelize} 26 | */ 27 | static getCurrentInstance() { 28 | return _instance; 29 | } 30 | 31 | /** 32 | * @param {Sequelize} instance 33 | * @returns {Sequelize} 34 | */ 35 | static setCurrentInstance(instance) { 36 | _instance = instance; 37 | return _instance; 38 | } 39 | } 40 | 41 | module.exports = _DatabaseInstance; 42 | -------------------------------------------------------------------------------- /examples/debug/issue-016/lib/database/_instance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Internal module to deal with database intances 3 | * 4 | * @module node-sequelize-with-mocks/database/_instance 5 | * @private 6 | * @exports _DatabaseInstance 7 | */ 8 | 9 | 'use strict'; 10 | 11 | // Constants & variables 12 | 13 | /** 14 | * @type {Sequelize} 15 | * @private 16 | */ 17 | let _instance = null; 18 | 19 | /** 20 | * Database instance management 21 | * @private 22 | */ 23 | class _DatabaseInstance { 24 | /** 25 | * @returns {Sequelize} 26 | */ 27 | static getCurrentInstance() { 28 | return _instance; 29 | } 30 | 31 | /** 32 | * @param {Sequelize} instance 33 | * @returns {Sequelize} 34 | */ 35 | static setCurrentInstance(instance) { 36 | _instance = instance; 37 | return _instance; 38 | } 39 | } 40 | 41 | module.exports = _DatabaseInstance; 42 | -------------------------------------------------------------------------------- /examples/debug/issue-024/lib/database/_instance.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Internal module to deal with database intances 3 | * 4 | * @module node-sequelize-with-mocks/database/_instance 5 | * @private 6 | * @exports _DatabaseInstance 7 | */ 8 | 9 | 'use strict'; 10 | 11 | // Constants & variables 12 | 13 | /** 14 | * @type {Sequelize} 15 | * @private 16 | */ 17 | let _instance = null; 18 | 19 | /** 20 | * Database instance management 21 | * @private 22 | */ 23 | class _DatabaseInstance { 24 | /** 25 | * @returns {Sequelize} 26 | */ 27 | static getCurrentInstance() { 28 | return _instance; 29 | } 30 | 31 | /** 32 | * @param {Sequelize} instance 33 | * @returns {Sequelize} 34 | */ 35 | static setCurrentInstance(instance) { 36 | _instance = instance; 37 | return _instance; 38 | } 39 | } 40 | 41 | module.exports = _DatabaseInstance; 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Access to the SequelizeMocking lib 3 | * 4 | * @module lib/index 5 | * @exports SequelizeMockingLib 6 | * @version 1.0.0 7 | * @since 0.1.0 8 | * @author Julien Roche 9 | */ 10 | 11 | 'use strict'; 12 | 13 | Object.defineProperty(module.exports, 'SequelizeMocking', { 14 | 'get': function() { 15 | return require('./lib/sequelize-mocking'); 16 | } 17 | }); 18 | 19 | Object.defineProperty(module.exports, 'sequelizeMockingMocha', { 20 | 'get': function() { 21 | return require('./lib/sequelize-mocking-mocha'); 22 | } 23 | }); 24 | 25 | Object.defineProperty(module.exports, 'sequelizeMockingJasmine', { 26 | 'get': function() { 27 | return require('./lib/sequelize-mocking-jasmine'); 28 | } 29 | }); 30 | 31 | Object.defineProperty(module.exports, 'sequelizeMockingTape', { 32 | 'get': function() { 33 | return require('./lib/sequelize-mocking-tape'); 34 | } 35 | }); 36 | -------------------------------------------------------------------------------- /examples/debug/issue-024/lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Run the application 3 | * 4 | * @module node-sequelize-with-mocks/index 5 | */ 6 | 7 | 'use strict'; 8 | 9 | // Imports 10 | const UserService = require('./user/service'); 11 | const Database = require('./database'); 12 | 13 | // Run 14 | console.log('Fetch the users'); 15 | 16 | Database 17 | .getInstance() 18 | .sync() 19 | .then(() => { 20 | return UserService.insert({ 21 | 'uuid': 'b00f2027-ac48-4ddc-abcd-d5c7f6fe1671', 22 | 'firstName': 'Jane', 23 | 'lastName': 'Doe', 24 | 'age': 24, 25 | 'description': null 26 | }); 27 | }) 28 | .then(() => { 29 | return UserService 30 | .findAll() 31 | .then(function (users) { 32 | console.dir(users); 33 | }); 34 | }) 35 | .catch(function (err) { 36 | console.error(err && err.stack ? err.stack : err); 37 | }); 38 | -------------------------------------------------------------------------------- /examples/debug/issue-024/lib/user/service.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Service around @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/service 5 | * @exports UserService 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const UserModel = require('./model'); 12 | const Database = require('../database'); 13 | 14 | 15 | class UserService { 16 | /** 17 | * @returns {Promise.} 18 | */ 19 | static findAll() { 20 | return UserModel.findAll({ 21 | 'raw': true 22 | }); 23 | } 24 | 25 | /** 26 | * @param {number} userId 27 | * @returns {Promise.} 28 | */ 29 | static find(userId) { 30 | return UserModel.findById(userId); 31 | } 32 | 33 | /** 34 | * @param {User} user 35 | * @returns {Promise.} 36 | */ 37 | static insert(user) { 38 | return Database 39 | .getInstance() 40 | .transaction(t => UserModel.create(user, { 'transaction': t })); 41 | } 42 | } 43 | 44 | module.exports = UserService; 45 | 46 | -------------------------------------------------------------------------------- /examples/debug/issue-024/originals/controller.txt: -------------------------------------------------------------------------------- 1 | var sql = require('./index'); 2 | var User = require('./userModel'); 3 | 4 | var op = sql.Op; 5 | 6 | function addSomething(data, transaction) { 7 | return sql.Promise.map(data, function (idSmth) { 8 | return otherModel.create(data, transaction); 9 | }).catch(function (err) { 10 | throw new Error('Err'); 11 | }); 12 | } 13 | 14 | exports.insert = function (input, callback) { 15 | var hash = genAccessKey(); 16 | var insert_data = { 17 | 'nome': input.form.nome, 18 | 'docto': input.form.docto, 19 | 'email': input.form.email, 20 | 'telefone': input.form.telefone, 21 | 'senha': hash, 22 | }; 23 | 24 | sql.transaction( function(t) { 25 | return User.create(insert_data, {transaction: t}).then(function (user) { 26 | insert_data.id = user.get('id'); 27 | 28 | return Promise.all([ 29 | addSomething(insert_data, {transaction: t}), 30 | ]); 31 | }); 32 | }).then( function (tr) { 33 | callback(null, tr); 34 | }).catch( function (err) { 35 | callback(err); 36 | }); 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Roche julien 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/debug/issue-016/lib/user/model.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Database = require('../database'); 4 | const uuid = require('uuid/v1'); 5 | 6 | module.exports = Database 7 | .getInstance() 8 | .define( 9 | 'User', 10 | { 11 | 'uuid': { 12 | 'type': Database.FIELD_TYPE_ENUM.UUID, 13 | 'defaultValue': function() { 14 | return uuid(); 15 | }, 16 | 'primaryKey': true 17 | }, 18 | 'firstName': { 19 | 'type': Database.FIELD_TYPE_ENUM.STRING, 20 | 'allowNull': false 21 | }, 22 | 'lastName': { 23 | 'type': Database.FIELD_TYPE_ENUM.STRING, 24 | 'allowNull': false 25 | }, 26 | 'age': { 27 | 'type': Database.FIELD_TYPE_ENUM.INTEGER, 28 | 'defaultValue': 42, 29 | 'validate': { 30 | 'max': 100, 31 | 'min': 18 32 | } 33 | }, 34 | 'description': Database.FIELD_TYPE_ENUM.TEXT 35 | }, 36 | { 37 | 'timestamps': false 38 | } 39 | ); 40 | -------------------------------------------------------------------------------- /examples/debug/issue-024/lib/user/model.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Database = require('../database'); 4 | const uuid = require('uuid/v1'); 5 | 6 | module.exports = Database 7 | .getInstance() 8 | .define( 9 | 'User', 10 | { 11 | 'uuid': { 12 | 'type': Database.FIELD_TYPE_ENUM.UUID, 13 | 'defaultValue': function() { 14 | return uuid(); 15 | }, 16 | 'primaryKey': true 17 | }, 18 | 'firstName': { 19 | 'type': Database.FIELD_TYPE_ENUM.STRING, 20 | 'allowNull': false 21 | }, 22 | 'lastName': { 23 | 'type': Database.FIELD_TYPE_ENUM.STRING, 24 | 'allowNull': false 25 | }, 26 | 'age': { 27 | 'type': Database.FIELD_TYPE_ENUM.INTEGER, 28 | 'defaultValue': 42, 29 | 'validate': { 30 | 'max': 100, 31 | 'min': 18 32 | } 33 | }, 34 | 'description': Database.FIELD_TYPE_ENUM.TEXT 35 | }, 36 | { 37 | 'timestamps': false 38 | } 39 | ); 40 | -------------------------------------------------------------------------------- /examples/debug/issue-024/originals/userModel.txt: -------------------------------------------------------------------------------- 1 | const Sequelize = require('sequelize'); 2 | const sql = require('./index'); 3 | 4 | var user = sql.define('USUARIOS', { 5 | id: { type: Sequelize.INTEGER, primaryKey: true, allowNull: false, autoIncrement: true }, 6 | docto: { 7 | type: Sequelize.STRING, 8 | allowNull: false, 9 | validate: { 10 | is: {args: ["^[0-9]{11}$","g"], msg: 'errMsg'}, 11 | }, 12 | set(val) { 13 | this.setDataValue('docto', val.replace(/(\.|-)/g,'')); 14 | }, 15 | }, 16 | email: { 17 | type: Sequelize.STRING, 18 | allowNull: false, 19 | validate: { 20 | isEmail: {msg: 'errMsg'}, 21 | }, 22 | 23 | set(val) { 24 | this.setDataValue('email', val.toUpperCase()); 25 | }, 26 | }, 27 | nome: { 28 | type: Sequelize.STRING, 29 | allowNull: false, 30 | set(val) { 31 | this.setDataValue('nome', val.toUpperCase()); 32 | }, 33 | }, 34 | senha: { type: Sequelize.STRING, allowNull: true }, 35 | telefone: { 36 | type: Sequelize.STRING(20), 37 | allowNull: true, 38 | set(val) { 39 | this.setDataValue('telefone', val.replace(/(\(|\)|-)/g,'')); 40 | }, 41 | }, 42 | ativo: { type: Sequelize.INTEGER, allowNull: true, defaultValue: 1 }, 43 | }, { 44 | freezeTableName: true, 45 | timestamps: false 46 | }); 47 | 48 | module.exports = usuarios; 49 | -------------------------------------------------------------------------------- /examples/mocha/lib/user/model.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Define the database model around a @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/model 5 | * @exports Sequelize.Model 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Database = require('../database'); 12 | 13 | // Model definition 14 | 15 | /** 16 | * @class User 17 | * @property {number} id 18 | * @property {string} firstName 19 | * @property {string} lastName 20 | * @property {number} [age=42] 21 | * @property {string} [description] 22 | */ 23 | 24 | const UserModel = Database 25 | .getInstance() 26 | .define('user', { 27 | 'id': { 28 | 'type': Database.FIELD_TYPE_ENUM.INTEGER, 29 | 'autoIncrement': true, 30 | 'primaryKey': true 31 | }, 32 | 'firstName': { 33 | 'type': Database.FIELD_TYPE_ENUM.STRING, 34 | 'allowNull': false 35 | }, 36 | 'lastName': { 37 | 'type': Database.FIELD_TYPE_ENUM.STRING, 38 | 'allowNull': false 39 | }, 40 | 'age': { 41 | 'type': Database.FIELD_TYPE_ENUM.INTEGER, 42 | 'defaultValue': 42, 43 | 'validate': { 44 | 'max': 100, 45 | 'min': 18 46 | } 47 | }, 48 | 'description': Database.FIELD_TYPE_ENUM.TEXT 49 | }); 50 | 51 | 52 | module.exports = UserModel; 53 | 54 | -------------------------------------------------------------------------------- /examples/tape/lib/user/model.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Define the database model around a @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/model 5 | * @exports Sequelize.Model 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Database = require('../database'); 12 | 13 | // Model definition 14 | 15 | /** 16 | * @class User 17 | * @property {number} id 18 | * @property {string} firstName 19 | * @property {string} lastName 20 | * @property {number} [age=42] 21 | * @property {string} [description] 22 | */ 23 | 24 | const UserModel = Database 25 | .getInstance() 26 | .define('user', { 27 | 'id': { 28 | 'type': Database.FIELD_TYPE_ENUM.INTEGER, 29 | 'autoIncrement': true, 30 | 'primaryKey': true 31 | }, 32 | 'firstName': { 33 | 'type': Database.FIELD_TYPE_ENUM.STRING, 34 | 'allowNull': false 35 | }, 36 | 'lastName': { 37 | 'type': Database.FIELD_TYPE_ENUM.STRING, 38 | 'allowNull': false 39 | }, 40 | 'age': { 41 | 'type': Database.FIELD_TYPE_ENUM.INTEGER, 42 | 'defaultValue': 42, 43 | 'validate': { 44 | 'max': 100, 45 | 'min': 18 46 | } 47 | }, 48 | 'description': Database.FIELD_TYPE_ENUM.TEXT 49 | }); 50 | 51 | 52 | module.exports = UserModel; 53 | 54 | -------------------------------------------------------------------------------- /examples/jasmine/lib/user/model.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Define the database model around a @{User} 3 | * 4 | * @module node-sequelize-with-mocks/user/model 5 | * @exports Sequelize.Model 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Database = require('../database'); 12 | 13 | // Model definition 14 | 15 | /** 16 | * @class User 17 | * @property {number} id 18 | * @property {string} firstName 19 | * @property {string} lastName 20 | * @property {number} [age=42] 21 | * @property {string} [description] 22 | */ 23 | 24 | const UserModel = Database 25 | .getInstance() 26 | .define('user', { 27 | 'id': { 28 | 'type': Database.FIELD_TYPE_ENUM.INTEGER, 29 | 'autoIncrement': true, 30 | 'primaryKey': true 31 | }, 32 | 'firstName': { 33 | 'type': Database.FIELD_TYPE_ENUM.STRING, 34 | 'allowNull': false 35 | }, 36 | 'lastName': { 37 | 'type': Database.FIELD_TYPE_ENUM.STRING, 38 | 'allowNull': false 39 | }, 40 | 'age': { 41 | 'type': Database.FIELD_TYPE_ENUM.INTEGER, 42 | 'defaultValue': 42, 43 | 'validate': { 44 | 'max': 100, 45 | 'min': 18 46 | } 47 | }, 48 | 'description': Database.FIELD_TYPE_ENUM.TEXT 49 | }); 50 | 51 | 52 | module.exports = UserModel; 53 | 54 | -------------------------------------------------------------------------------- /lib/sequelize-mocking-mocha.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Using SequelizeMocking with mocha easily 3 | * 4 | * @module lib/sequelize-mocking-mocha 5 | * @exports sequelizeMochingMocha 6 | * @version 0.1.0 7 | * @since 0.1.0 8 | * @author Julien Roche 9 | */ 10 | 11 | 'use strict'; 12 | 13 | // Imports 14 | const _ = require('lodash'); 15 | const SequelizeMocking = require('./sequelize-mocking'); 16 | 17 | /** 18 | * @name sequelizeMochingMocha 19 | * @param {Sequelize} originalSequelize 20 | * @param {string} [fixtureFilePath] 21 | * @param {SequelizeMockingOptions} [options] 22 | */ 23 | module.exports = function (originalSequelize, fixtureFilePath, options) { 24 | let beforeEach = global.beforeEach ? global.beforeEach : null; 25 | let afterEach = global.afterEach ? global.afterEach : null; 26 | 27 | if (beforeEach && afterEach) { 28 | let mockedSequelize = null; 29 | 30 | beforeEach(function (done) { 31 | let createFunc = _.partialRight(fixtureFilePath ? SequelizeMocking.createAndLoadFixtureFile : SequelizeMocking.create, options); 32 | 33 | createFunc(originalSequelize, fixtureFilePath) 34 | .then(function (sequelizeInstance) { 35 | mockedSequelize = sequelizeInstance; 36 | done(); 37 | }) 38 | .catch(done); 39 | }); 40 | 41 | afterEach(function (done) { 42 | SequelizeMocking 43 | .restore(mockedSequelize, options) 44 | .then(function () { 45 | done(); 46 | }) 47 | .catch(done); 48 | }); 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /lib/sequelize-mocking-jasmine.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Using SequelizeMocking with jasmine easily 3 | * 4 | * @module lib/sequelize-mocking-jasmine 5 | * @exports sequelizeMochingJasmine 6 | * @version 0.1.0 7 | * @since 0.1.0 8 | * @author Julien Roche 9 | */ 10 | 11 | 'use strict'; 12 | 13 | // Imports 14 | const _ = require('lodash'); 15 | const SequelizeMocking = require('./sequelize-mocking'); 16 | 17 | /** 18 | * @name sequelizeMochingMocha 19 | * @param {Sequelize} originalSequelize 20 | * @param {string} [fixtureFilePath] 21 | * @param {SequelizeMockingOptions} [options] 22 | */ 23 | module.exports = function (originalSequelize, fixtureFilePath, options) { 24 | let beforeEach = global.beforeEach ? global.beforeEach : null; 25 | let afterEach = global.afterEach ? global.afterEach : null; 26 | 27 | if (beforeEach && afterEach) { 28 | let mockedSequelize = null; 29 | 30 | beforeEach(function (done) { 31 | let createFunc = _.partialRight(fixtureFilePath ? SequelizeMocking.createAndLoadFixtureFile : SequelizeMocking.create, options); 32 | 33 | createFunc(originalSequelize, fixtureFilePath) 34 | .then(function (sequelizeInstance) { 35 | mockedSequelize = sequelizeInstance; 36 | done(); 37 | }) 38 | .catch(done.fail); 39 | }); 40 | 41 | afterEach(function (done) { 42 | SequelizeMocking 43 | .restore(mockedSequelize, options) 44 | .then(function () { 45 | done(); 46 | }) 47 | .catch(done.fail); 48 | }); 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /examples/jasmine/lib/database/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Get the current instance instance of the database and more 3 | * 4 | * @module node-sequelize-with-mocks/database/index 5 | * @exports Database 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Sequelize = require('sequelize'); 12 | const _DatabaseInstance = require('./_instance'); 13 | 14 | /** 15 | * Database methods 16 | */ 17 | class Database { 18 | /** 19 | * @returns {Sequelize} 20 | */ 21 | static getInstance() { 22 | let instance = _DatabaseInstance.getCurrentInstance(); 23 | 24 | if (!instance) { 25 | instance = _DatabaseInstance.setCurrentInstance(new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 26 | 'host': 'localhost', 27 | 'dialect': 'mysql', 28 | 'define': { 29 | 'engine': 'MYISAM', 30 | 'timestamps': false, // Don't create for each model the 'createdAt' and 'updatedAt' field 31 | 'paranoid': false // Truly deleted. Not add a 'deletedAt' field 32 | }, 33 | 'pool': { 34 | 'max': 5, 35 | 'min': 0, 36 | 'idle': 10000 37 | }, 38 | 'query': { 39 | 'raw': true 40 | } 41 | })); 42 | } 43 | 44 | return instance; 45 | } 46 | } 47 | 48 | /** 49 | * @enum {string} 50 | */ 51 | Database.FIELD_TYPE_ENUM = { 52 | 'INTEGER': Sequelize.INTEGER, 53 | 'STRING': Sequelize.STRING, 54 | 'TEXT': Sequelize.TEXT 55 | }; 56 | 57 | module.exports = Database; 58 | -------------------------------------------------------------------------------- /examples/mocha/lib/database/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Get the current instance instance of the database and more 3 | * 4 | * @module node-sequelize-with-mocks/database/index 5 | * @exports Database 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Sequelize = require('sequelize'); 12 | const _DatabaseInstance = require('./_instance'); 13 | 14 | /** 15 | * Database methods 16 | */ 17 | class Database { 18 | /** 19 | * @returns {Sequelize} 20 | */ 21 | static getInstance() { 22 | let instance = _DatabaseInstance.getCurrentInstance(); 23 | 24 | if (!instance) { 25 | instance = _DatabaseInstance.setCurrentInstance(new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 26 | 'host': 'localhost', 27 | 'dialect': 'mysql', 28 | 'define': { 29 | 'engine': 'MYISAM', 30 | 'timestamps': false, // Don't create for each model the 'createdAt' and 'updatedAt' field 31 | 'paranoid': false // Truly deleted. Not add a 'deletedAt' field 32 | }, 33 | 'pool': { 34 | 'max': 5, 35 | 'min': 0, 36 | 'idle': 10000 37 | }, 38 | 'query': { 39 | 'raw': true 40 | } 41 | })); 42 | } 43 | 44 | return instance; 45 | } 46 | } 47 | 48 | /** 49 | * @enum {string} 50 | */ 51 | Database.FIELD_TYPE_ENUM = { 52 | 'INTEGER': Sequelize.INTEGER, 53 | 'STRING': Sequelize.STRING, 54 | 'TEXT': Sequelize.TEXT 55 | }; 56 | 57 | module.exports = Database; 58 | -------------------------------------------------------------------------------- /examples/tape/lib/database/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Get the current instance instance of the database and more 3 | * 4 | * @module node-sequelize-with-mocks/database/index 5 | * @exports Database 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Sequelize = require('sequelize'); 12 | const _DatabaseInstance = require('./_instance'); 13 | 14 | /** 15 | * Database methods 16 | */ 17 | class Database { 18 | /** 19 | * @returns {Sequelize} 20 | */ 21 | static getInstance() { 22 | let instance = _DatabaseInstance.getCurrentInstance(); 23 | 24 | if (!instance) { 25 | instance = _DatabaseInstance.setCurrentInstance(new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 26 | 'host': 'localhost', 27 | 'dialect': 'mysql', 28 | 'define': { 29 | 'engine': 'MYISAM', 30 | 'timestamps': false, // Don't create for each model the 'createdAt' and 'updatedAt' field 31 | 'paranoid': false // Truly deleted. Not add a 'deletedAt' field 32 | }, 33 | 'pool': { 34 | 'max': 5, 35 | 'min': 0, 36 | 'idle': 10000 37 | }, 38 | 'query': { 39 | 'raw': true 40 | } 41 | })); 42 | } 43 | 44 | return instance; 45 | } 46 | } 47 | 48 | /** 49 | * @enum {string} 50 | */ 51 | Database.FIELD_TYPE_ENUM = { 52 | 'INTEGER': Sequelize.INTEGER, 53 | 'STRING': Sequelize.STRING, 54 | 'TEXT': Sequelize.TEXT 55 | }; 56 | 57 | module.exports = Database; 58 | -------------------------------------------------------------------------------- /examples/debug/issue-007/lib/database/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Get the current instance instance of the database and more 3 | * 4 | * @module node-sequelize-with-mocks/database/index 5 | * @exports Database 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Sequelize = require('sequelize'); 12 | const _DatabaseInstance = require('./_instance'); 13 | 14 | /** 15 | * Database methods 16 | */ 17 | class Database { 18 | /** 19 | * @returns {Sequelize} 20 | */ 21 | static getInstance() { 22 | let instance = _DatabaseInstance.getCurrentInstance(); 23 | 24 | if (!instance) { 25 | instance = _DatabaseInstance.setCurrentInstance(new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 26 | 'host': 'localhost', 27 | 'dialect': 'mysql', 28 | 'define': { 29 | 'engine': 'MYISAM', 30 | 'timestamps': false, // Don't create for each model the 'createdAt' and 'updatedAt' field 31 | 'paranoid': false // Truly deleted. Not add a 'deletedAt' field 32 | }, 33 | 'pool': { 34 | 'max': 5, 35 | 'min': 0, 36 | 'idle': 10000 37 | }, 38 | 'query': { 39 | 'raw': true 40 | } 41 | })); 42 | } 43 | 44 | return instance; 45 | } 46 | } 47 | 48 | /** 49 | * @enum {string} 50 | */ 51 | Database.FIELD_TYPE_ENUM = { 52 | 'INTEGER': Sequelize.INTEGER, 53 | 'STRING': Sequelize.STRING, 54 | 'TEXT': Sequelize.TEXT 55 | }; 56 | 57 | module.exports = Database; 58 | -------------------------------------------------------------------------------- /examples/debug/issue-024/originals/test-spec.txt: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const chai = require('chai'); 3 | const sinon = require('sinon'); 4 | 5 | const sequelizeMockingMocha = require('sequelize-mocking').sequelizeMockingMocha; 6 | 7 | describe('Users Controller', function () { 8 | const Database = require('./index'); 9 | const User = require('./controller'); 10 | 11 | let sandbox = null; 12 | 13 | const formFields = { 14 | "id": null, 15 | "docto": null, 16 | "nome": null, 17 | "email": null, 18 | "telefone": null, 19 | "ativo": null, 20 | }; 21 | 22 | before(function () { 23 | sandbox = sinon.sandbox.create(); 24 | 25 | formFields.usuario_nome = 'User Test Name'; 26 | formFields.usuario_cpf = '03571283031'; 27 | formFields.usuario_email = 'usuario@email.com'; 28 | formFields.usuario_telefone = '(47) 99999-9999'; 29 | }); 30 | 31 | after(function () { 32 | sandbox && sandbox.restore(); 33 | }); 34 | 35 | sequelizeMockingMocha( 36 | Database, 37 | [ 38 | path.resolve(path.join(__dirname, './../data/user-db.json')), 39 | ], 40 | { 'logging': false } 41 | ); 42 | 43 | it('Controller must exists', function () { 44 | chai.expect(User).to.exist; 45 | }); 46 | 47 | describe('Insert Method.', function () { 48 | it('Must Exists', function () { 49 | chai.expect(User.insert).to.exist; 50 | }); 51 | 52 | it('Should create a new register.', function (done) { 53 | // var spy = sandbox.spy(User, 'genAccessKey'); 54 | // chai.expect(spy.called).to.be.true; 55 | // chai.expect(spy.calledOnce).to.be.true; 56 | // chai.expect(spy.calledWith()).to.be.true; 57 | 58 | User.insert({ 'form': formFields }, function (err, u) { 59 | chai.expect(err).to.be.null; 60 | done(); 61 | }); 62 | }); 63 | }); 64 | }); 65 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sequelize-mocking", 3 | "version": "2.0.1", 4 | "description": "A Sequelize extension to deal with mocking for tests", 5 | "main": "index.js", 6 | "files": [ 7 | "index.js", 8 | "lib", 9 | "lib-es5" 10 | ], 11 | "scripts": { 12 | "delete": "node node_modules/rimraf/bin.js", 13 | "checkstyle": "npm run delete ./target/eslint-reporter-checkstyle.xml && node node_modules/eslint/bin/eslint.js --config=.eslintrc --output-file ./target/eslint-reporter-checkstyle.xml --format checkstyle ./lib", 14 | "generate-shrinkwrap": "npm prune && npm shrinkwrap --only=prod", 15 | "test": "node ./node_modules/mocha/bin/mocha --recursive --ui bdd --colors ./test", 16 | "bumping": "node ./node_modules/npmversion/bin/npmversion", 17 | "bump-release": "npm test && npm run bumping -- --unpreid --git-create-branch", 18 | "bump-major": "npm test && npm run bumping -- --git-create-branch --increment major", 19 | "bump-minor": "npm test && npm run bumping -- --git-create-branch --increment minor", 20 | "bump-patch": "npm test && npm run bumping -- --git-create-branch --increment patch", 21 | "bump-major-beta": "npm run bumping -- --increment major --preid beta", 22 | "bump-minor-beta": "npm run bumping -- --increment minor --preid beta", 23 | "bump-patch-beta": "npm run bumping -- --increment patch --preid beta" 24 | }, 25 | "repository": { 26 | "type": "git", 27 | "url": "git+https://github.com/rochejul/sequelize-mocking.git" 28 | }, 29 | "keywords": [ 30 | "Sequelize", 31 | "mocking", 32 | "fixtures", 33 | "test", 34 | "mocha" 35 | ], 36 | "author": "Julien Roche", 37 | "license": "MIT", 38 | "bugs": { 39 | "url": "https://github.com/rochejul/sequelize-mocking/issues" 40 | }, 41 | "homepage": "https://github.com/rochejul/sequelize-mocking#readme", 42 | "dependencies": { 43 | "lodash": "4.17.13", 44 | "sequelize-fixtures": "0.10.0", 45 | "sqlite3": "4.0.8" 46 | }, 47 | "engines": { 48 | "node": ">=6.0.0", 49 | "npm": ">=3.0.0" 50 | }, 51 | "devDependencies": { 52 | "chai": "4.0.2", 53 | "eslint": "4.7.1", 54 | "mocha": "5.2.0", 55 | "mysql2": "1.3.5", 56 | "npmversion": "1.7.0", 57 | "rimraf": "2.6.1", 58 | "sequelize": "5.8.7", 59 | "sinon": "2.3.7" 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /examples/debug/issue-016/lib/database/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Get the current instance instance of the database and more 3 | * 4 | * @module node-sequelize-with-mocks/database/index 5 | * @exports Database 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Sequelize = require('sequelize'); 12 | const _DatabaseInstance = require('./_instance'); 13 | 14 | const useMysql = process.argv.find(arg => arg.startsWith('--mysql')); 15 | 16 | /** 17 | * Database methods 18 | */ 19 | class Database { 20 | /** 21 | * @returns {Sequelize} 22 | */ 23 | static getInstance() { 24 | let instance = _DatabaseInstance.getCurrentInstance(); 25 | 26 | if (!instance) { 27 | if (useMysql) { 28 | instance = _DatabaseInstance.setCurrentInstance(new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 29 | 'host': 'localhost', 30 | 'dialect': 'mysql', 31 | 'define': { 32 | 'engine': 'MYISAM', 33 | 'timestamps': false, // Don't create for each model the 'createdAt' and 'updatedAt' field 34 | 'paranoid': false // Truly deleted. Not add a 'deletedAt' field 35 | }, 36 | 'pool': { 37 | 'max': 5, 38 | 'min': 0, 39 | 'idle': 10000 40 | }, 41 | 'query': { 42 | 'raw': true 43 | } 44 | })); 45 | } else { 46 | instance = _DatabaseInstance.setCurrentInstance(new Sequelize('null', 'null', 'null', { 47 | 'host': 'localhost', 48 | 'dialect': 'sqlite', 49 | 'storage': ':memory:', 50 | 'pool': { 51 | 'max': 5, 52 | 'min': 0, 53 | 'idle': 10000 54 | }, 55 | 'query': { 56 | 'raw': true 57 | } 58 | })); 59 | } 60 | } 61 | 62 | return instance; 63 | } 64 | } 65 | 66 | /** 67 | * @enum {string} 68 | */ 69 | Database.FIELD_TYPE_ENUM = { 70 | 'INTEGER': Sequelize.DataTypes.INTEGER, 71 | 'STRING': Sequelize.DataTypes.STRING, 72 | 'UUID': Sequelize.DataTypes.UUID, 73 | 'TEXT': Sequelize.DataTypes.TEXT 74 | }; 75 | 76 | module.exports = Database; 77 | -------------------------------------------------------------------------------- /examples/debug/issue-024/lib/database/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Get the current instance instance of the database and more 3 | * 4 | * @module node-sequelize-with-mocks/database/index 5 | * @exports Database 6 | */ 7 | 8 | 'use strict'; 9 | 10 | // Imports 11 | const Sequelize = require('sequelize'); 12 | const _DatabaseInstance = require('./_instance'); 13 | 14 | const useMysql = process.argv.find(arg => arg.startsWith('--mysql')) || !!process.env.USE_MYSQL; 15 | 16 | /** 17 | * Database methods 18 | */ 19 | class Database { 20 | /** 21 | * @returns {Sequelize} 22 | */ 23 | static getInstance() { 24 | let instance = _DatabaseInstance.getCurrentInstance(); 25 | 26 | if (!instance) { 27 | if (useMysql) { 28 | instance = _DatabaseInstance.setCurrentInstance(new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 29 | 'host': 'localhost', 30 | 'dialect': 'mysql', 31 | 'define': { 32 | 'engine': 'MYISAM', 33 | 'timestamps': false, // Don't create for each model the 'createdAt' and 'updatedAt' field 34 | 'paranoid': false // Truly deleted. Not add a 'deletedAt' field 35 | }, 36 | 'pool': { 37 | 'max': 5, 38 | 'min': 0, 39 | 'idle': 10000 40 | }, 41 | 'query': { 42 | 'raw': true 43 | } 44 | })); 45 | } else { 46 | instance = _DatabaseInstance.setCurrentInstance(new Sequelize('null', 'null', 'null', { 47 | 'host': 'localhost', 48 | 'dialect': 'sqlite', 49 | 'storage': ':memory:', 50 | 'pool': { 51 | 'max': 5, 52 | 'min': 0, 53 | 'idle': 10000 54 | }, 55 | 'query': { 56 | 'raw': true 57 | } 58 | })); 59 | } 60 | } 61 | 62 | return instance; 63 | } 64 | } 65 | 66 | /** 67 | * @enum {string} 68 | */ 69 | Database.FIELD_TYPE_ENUM = { 70 | 'INTEGER': Sequelize.DataTypes.INTEGER, 71 | 'STRING': Sequelize.DataTypes.STRING, 72 | 'UUID': Sequelize.DataTypes.UUID, 73 | 'TEXT': Sequelize.DataTypes.TEXT 74 | }; 75 | 76 | module.exports = Database; 77 | -------------------------------------------------------------------------------- /examples/debug/issue-007/test/user/service-with-sequelize-mocking-mochaSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const sequelizeMockingMocha = require('sequelize-mocking').sequelizeMockingMocha; 13 | 14 | describe('User - UserService (using sequelizeMockingMocha) - ', function () { 15 | const Database = require('../../lib/database'); 16 | const UserService = require('../../lib/user/service'); 17 | const UserModel = require('../../lib/user/model'); 18 | 19 | // Basic configuration: create a sinon sandbox for testing 20 | let sandbox = null; 21 | 22 | beforeEach(function () { 23 | sandbox = sinon.sandbox.create(); 24 | }); 25 | 26 | afterEach(function () { 27 | sandbox && sandbox.restore(); 28 | }); 29 | 30 | // Load fake data for the users 31 | sequelizeMockingMocha( 32 | Database.getInstance(), 33 | path.resolve(path.join(__dirname, './fake-users-database.json')), 34 | { 'logging': false } 35 | ); 36 | 37 | it('the service shall exist', function () { 38 | chai.expect(UserService).to.exist; 39 | }); 40 | 41 | describe('and the method login shall ', function () { 42 | it('exist', function () { 43 | chai.expect(UserService.login).to.exist; 44 | }); 45 | 46 | it('return an user if we can', function () { 47 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 48 | 49 | return UserService 50 | .login('roche.jul@gmail.com', 'myPassword') 51 | .then(function (user) { 52 | chai.expect(findByIdSpy.called).to.be.true; 53 | chai.expect(findByIdSpy.calledOnce).to.be.true; 54 | chai.expect(findByIdSpy.calledWith('roche.jul@gmail.com', 'myPassword')).to.be.true; 55 | 56 | chai.expect(user).deep.equals({ 57 | 'email': 'roche.jul@gmail.com', 58 | 'password': '5a5fd34f5b3ab16aff01a05dbf570d737e3edec3', 59 | 'salt': 'abcdef' 60 | }); 61 | }) 62 | .catch(function (err) { 63 | throw err; 64 | }); 65 | }); 66 | 67 | it('return null if not found', function () { 68 | return UserService 69 | .login('roche.jul@gmail.com', 'badPassword') 70 | .then(function (user) { 71 | chai.expect(user).to.be.null; 72 | }); 73 | }); 74 | }); 75 | }); 76 | -------------------------------------------------------------------------------- /lib/sequelize-mocking-tape.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Using SequelizeMocking with tape easily 3 | * 4 | * @module lib/sequelize-mocking-tape 5 | * @exports sequelizeMochingTape 6 | * @version 0.1.0 7 | * @since 0.1.0 8 | * @author Julien Roche 9 | */ 10 | 11 | 'use strict'; 12 | 13 | // Imports 14 | const test = require('tape'); 15 | 16 | const _ = require('lodash'); 17 | const SequelizeMocking = require('./sequelize-mocking'); 18 | 19 | /** 20 | * @method 21 | * @private 22 | * @param {Test} test 23 | * @param {Function} handler 24 | * @returns {Function} 25 | */ 26 | function beforeEach(test, handler) { 27 | return function (name, listener) { 28 | test(name, function (assert) { 29 | let _end = assert.end; 30 | 31 | assert.end = function () { 32 | assert.end = _end; 33 | listener(assert); 34 | }; 35 | 36 | handler(assert); 37 | }); 38 | } 39 | } 40 | 41 | /** 42 | * @method 43 | * @private 44 | * @param {Test} test 45 | * @param {Function} handler 46 | * @returns {Function} 47 | */ 48 | function afterEach(test, handler) { 49 | return function (name, listener) { 50 | test(name, function (assert) { 51 | let _end = assert.end; 52 | 53 | assert.end = function () { 54 | assert.end = _end; 55 | handler(assert); 56 | }; 57 | 58 | listener(assert); 59 | }); 60 | } 61 | } 62 | 63 | 64 | /** 65 | * @name sequelizeMochingMocha 66 | * @param {Sequelize} originalSequelize 67 | * @param {string} [fixtureFilePath] 68 | * @param {SequelizeMockingOptions} [options] 69 | * @param {Test} [currentTestInstance] 70 | */ 71 | function sequelizeMochingTape(originalSequelize, fixtureFilePath, options, currentTestInstance) { 72 | let mockedSequelize = null; 73 | let newTest = null; 74 | 75 | newTest = beforeEach(currentTestInstance ? currentTestInstance : test, function (assert) { 76 | let createFunc = _.partialRight(fixtureFilePath ? SequelizeMocking.createAndLoadFixtureFile : SequelizeMocking.create, options); 77 | 78 | createFunc(originalSequelize, fixtureFilePath) 79 | .then(function (sequelizeInstance) { 80 | mockedSequelize = sequelizeInstance; 81 | assert.end(); 82 | }) 83 | .catch(assert.end); 84 | }); 85 | 86 | newTest = afterEach(newTest, function (assert) { 87 | SequelizeMocking 88 | .restore(mockedSequelize, options) 89 | .then(function () { 90 | assert.end(); 91 | }) 92 | .catch(assert.end); 93 | }); 94 | 95 | return newTest; 96 | }; 97 | 98 | sequelizeMochingTape.beforeEach = beforeEach; 99 | sequelizeMochingTape.afterEach = afterEach; 100 | 101 | module.exports = sequelizeMochingTape; 102 | -------------------------------------------------------------------------------- /examples/mocha/test/user/service-with-sequelize-mocking-mochaSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const sequelizeMockingMocha = require('sequelize-mocking').sequelizeMockingMocha; 13 | 14 | describe('User - UserService (using sequelizeMockingMocha) - ', function () { 15 | const Database = require('../../lib/database'); 16 | const UserService = require('../../lib/user/service'); 17 | const UserModel = require('../../lib/user/model'); 18 | 19 | // Basic configuration: create a sinon sandbox for testing 20 | let sandbox = null; 21 | 22 | beforeEach(function () { 23 | sandbox = sinon.sandbox.create(); 24 | }); 25 | 26 | afterEach(function () { 27 | sandbox && sandbox.restore(); 28 | }); 29 | 30 | // Load fake data for the users 31 | sequelizeMockingMocha( 32 | Database.getInstance(), 33 | path.resolve(path.join(__dirname, './fake-users-database.json')), 34 | { 'logging': false } 35 | ); 36 | 37 | it('the service shall exist', function () { 38 | chai.expect(UserService).to.exist; 39 | }); 40 | 41 | describe('and the method findAll shall ', function () { 42 | it('exist', function () { 43 | chai.expect(UserService.findAll).to.exist; 44 | }); 45 | 46 | it('shall returns an array of user', function () { 47 | return UserService 48 | .findAll() 49 | .then(function (users) { 50 | chai.expect(users).deep.equals([{ 51 | 'id': 1, 52 | 'firstName': 'John', 53 | 'lastName': 'Doe', 54 | 'age': 25, 55 | 'description': null 56 | }]); 57 | }); 58 | }); 59 | }); 60 | 61 | describe('and the method find shall ', function () { 62 | it('exist', function () { 63 | chai.expect(UserService.find).to.exist; 64 | }); 65 | 66 | it('shall return an user if we can', function () { 67 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 68 | 69 | return UserService 70 | .find(1) 71 | .then(function (user) { 72 | chai.expect(findByIdSpy.called).to.be.true; 73 | chai.expect(findByIdSpy.calledOnce).to.be.true; 74 | chai.expect(findByIdSpy.calledWith(1)).to.be.true; 75 | 76 | chai.expect(user).deep.equals({ 77 | 'id': 1, 78 | 'firstName': 'John', 79 | 'lastName': 'Doe', 80 | 'age': 25, 81 | 'description': null 82 | }); 83 | }); 84 | }); 85 | 86 | it('shall return null if not found', function () { 87 | return UserService 88 | .find(-1) 89 | .then(function (user) { 90 | chai.expect(user).to.be.null; 91 | }); 92 | }); 93 | }); 94 | }); 95 | -------------------------------------------------------------------------------- /examples/tape/test/user/service-with-sequelize-mocking-tapeSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const sinon = require('sinon'); 10 | const path = require('path'); 11 | const sequelizeMockingTape = require('sequelize-mocking').sequelizeMockingTape; 12 | let test = require('tape'); 13 | 14 | 15 | // Basic configuration: create a sinon sandbox for testing 16 | let sandbox = null; 17 | 18 | test = sequelizeMockingTape.beforeEach(test, function (t) { 19 | sandbox = sinon.sandbox.create(); 20 | t.end(); 21 | }); 22 | 23 | test = sequelizeMockingTape.afterEach(test, function (t) { 24 | sandbox && sandbox.restore(); 25 | t.end(); 26 | }); 27 | 28 | const Database = require('../../lib/database'); 29 | const UserService = require('../../lib/user/service'); 30 | const UserModel = require('../../lib/user/model'); 31 | 32 | // Load fake data for the users 33 | test = sequelizeMockingTape( 34 | Database.getInstance(), 35 | path.resolve(path.join(__dirname, './fake-users-database.json')), 36 | { 'logging': false }, 37 | test 38 | ); 39 | 40 | test('User - UserService (classical way) - the service shall exist', function (t) { 41 | t.true(!!UserService); 42 | t.end(); 43 | }); 44 | 45 | test('User - UserService (classical way) - and the method findAll shall exist', function (t) { 46 | t.true(!!UserService.findAll); 47 | t.end(); 48 | }); 49 | 50 | test('User - UserService (classical way) - and the method findAll shall returns an array of user', function (t) { 51 | let findAllStub = sandbox.spy(UserModel, 'findAll'); 52 | 53 | UserService 54 | .findAll() 55 | .then(function (users) { 56 | t.ok(findAllStub.called); 57 | t.ok(findAllStub.calledOnce); 58 | t.ok(findAllStub.calledWith()); 59 | 60 | t.deepEqual(users, [{ 61 | 'id': 1, 62 | 'firstName': 'John', 63 | 'lastName': 'Doe', 64 | 'age': 25, 65 | 'description': null 66 | }]); 67 | 68 | t.end(); 69 | }) 70 | .catch(t.end); 71 | }); 72 | 73 | test('User - UserService (classical way) - and the method find shall exist', function (t) { 74 | t.true(!!UserService.find); 75 | t.end(); 76 | }); 77 | 78 | test('User - UserService (classical way) - and the method find shall return an user', function (t) { 79 | let findByIdStub = sandbox.spy(UserModel, 'findById'); 80 | 81 | UserService 82 | .find(1) 83 | .then(function (users) { 84 | t.ok(findByIdStub.called); 85 | t.ok(findByIdStub.calledOnce); 86 | t.ok(findByIdStub.calledWith(1)); 87 | 88 | t.deepEqual(users, { 89 | 'id': 1, 90 | 'firstName': 'John', 91 | 'lastName': 'Doe', 92 | 'age': 25, 93 | 'description': null 94 | }); 95 | 96 | t.end(); 97 | }) 98 | .catch(t.end); 99 | }); 100 | 101 | test('User - UserService (classical way) - and the method find shall return null if not found', function (t) { 102 | return UserService 103 | .find(-1) 104 | .then(function (user) { 105 | t.ok(user === null); 106 | t.end(); 107 | }) 108 | .catch(t.end); 109 | }); 110 | -------------------------------------------------------------------------------- /examples/jasmine/test/user/service-with-sequelize-mocking-jasmineSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const sequelizeMockingJasmine = require('sequelize-mocking').sequelizeMockingJasmine; 13 | 14 | describe('User - UserService (using sequelizeMockingJasmine) - ', function () { 15 | const Database = require('../../lib/database'); 16 | const UserService = require('../../lib/user/service'); 17 | const UserModel = require('../../lib/user/model'); 18 | 19 | // Basic configuration: create a sinon sandbox for testing 20 | let sandbox = null; 21 | 22 | beforeEach(function () { 23 | sandbox = sinon.sandbox.create(); 24 | }); 25 | 26 | afterEach(function () { 27 | sandbox && sandbox.restore(); 28 | }); 29 | 30 | // Load fake data for the users 31 | sequelizeMockingJasmine( 32 | Database.getInstance(), 33 | path.resolve(path.join(__dirname, './fake-users-database.json')), 34 | { 'logging': false } 35 | ); 36 | 37 | it('the service shall exist', function () { 38 | chai.expect(UserService).to.exist; 39 | }); 40 | 41 | describe('and the method findAll shall ', function () { 42 | it('exist', function () { 43 | chai.expect(UserService.findAll).to.exist; 44 | }); 45 | 46 | it('shall returns an array of user', function (done) { 47 | UserService 48 | .findAll() 49 | .then(function (users) { 50 | chai.expect(users).deep.equals([{ 51 | 'id': 1, 52 | 'firstName': 'John', 53 | 'lastName': 'Doe', 54 | 'age': 25, 55 | 'description': null 56 | }]); 57 | done(); 58 | }) 59 | .catch(done.fail); 60 | }); 61 | }); 62 | 63 | describe('and the method find shall ', function () { 64 | it('exist', function () { 65 | chai.expect(UserService.find).to.exist; 66 | }); 67 | 68 | it('shall return an user if we can', function (done) { 69 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 70 | 71 | UserService 72 | .find(1) 73 | .then(function (user) { 74 | chai.expect(findByIdSpy.called).to.be.true; 75 | chai.expect(findByIdSpy.calledOnce).to.be.true; 76 | chai.expect(findByIdSpy.calledWith(1)).to.be.true; 77 | 78 | chai.expect(user).deep.equals({ 79 | 'id': 1, 80 | 'firstName': 'John', 81 | 'lastName': 'Doe', 82 | 'age': 25, 83 | 'description': null 84 | }); 85 | done(); 86 | }) 87 | .catch(done.fail); 88 | }); 89 | 90 | it('shall return null if not found', function (done) { 91 | UserService 92 | .find(-1) 93 | .then(function (user) { 94 | chai.expect(user).to.be.null; 95 | done(); 96 | }) 97 | .catch(done.fail); 98 | }); 99 | }); 100 | }); 101 | -------------------------------------------------------------------------------- /examples/debug/issue-016/test/user/service-with-sequelize-mocking-jasmineSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const sequelizeMockingJasmine = require('sequelize-mocking').sequelizeMockingJasmine; 13 | 14 | describe('User - UserService (using sequelizeMockingJasmine) - ', function () { 15 | const Database = require('../../lib/database'); 16 | const UserService = require('../../lib/user/service'); 17 | const UserModel = require('../../lib/user/model'); 18 | 19 | // Basic configuration: create a sinon sandbox for testing 20 | let sandbox = null; 21 | 22 | beforeEach(function () { 23 | sandbox = sinon.sandbox.create(); 24 | }); 25 | 26 | afterEach(function () { 27 | sandbox && sandbox.restore(); 28 | }); 29 | 30 | // Load fake data for the users 31 | sequelizeMockingJasmine( 32 | Database.getInstance(), 33 | path.resolve(path.join(__dirname, './fake-users-database.json')), 34 | { 'logging': false } 35 | ); 36 | 37 | it('the service shall exist', function () { 38 | chai.expect(UserService).to.exist; 39 | }); 40 | 41 | describe('and the method findAll shall ', function () { 42 | it('exist', function () { 43 | chai.expect(UserService.findAll).to.exist; 44 | }); 45 | 46 | it('shall returns an array of user', function (done) { 47 | UserService 48 | .findAll() 49 | .then(function (users) { 50 | chai.expect(users).deep.equals([{ 51 | 'uuid': 'f64f2940-fae4-11e7-8c5f-ef356f279131', 52 | 'firstName': 'John', 53 | 'lastName': 'Doe', 54 | 'age': 25, 55 | 'description': null 56 | }]); 57 | done(); 58 | }) 59 | .catch(done.fail); 60 | }); 61 | }); 62 | 63 | describe('and the method find shall ', function () { 64 | it('exist', function () { 65 | chai.expect(UserService.find).to.exist; 66 | }); 67 | 68 | it('shall return an user if we can', function (done) { 69 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 70 | 71 | UserService 72 | .find('f64f2940-fae4-11e7-8c5f-ef356f279131') 73 | .then(function (user) { 74 | chai.expect(findByIdSpy.called).to.be.true; 75 | chai.expect(findByIdSpy.calledOnce).to.be.true; 76 | chai.expect(findByIdSpy.calledWith('f64f2940-fae4-11e7-8c5f-ef356f279131')).to.be.true; 77 | 78 | chai.expect(user).deep.equals({ 79 | 'uuid': 'f64f2940-fae4-11e7-8c5f-ef356f279131', 80 | 'firstName': 'John', 81 | 'lastName': 'Doe', 82 | 'age': 25, 83 | 'description': null 84 | }); 85 | done(); 86 | }) 87 | .catch(done.fail); 88 | }); 89 | 90 | it('shall return null if not found', function (done) { 91 | UserService 92 | .find('f64f2940-fae4-11e7-8c5f-ef356f279135') 93 | .then(function (user) { 94 | chai.expect(user).to.be.null; 95 | done(); 96 | }) 97 | .catch(done.fail); 98 | }); 99 | }); 100 | }); 101 | -------------------------------------------------------------------------------- /examples/mocha/test/user/service-with-sequelize-mockingSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const SequelizeMocking = require('sequelize-mocking').SequelizeMocking; 13 | 14 | describe('User - UserService (using SequelizeMocking) - ', function () { 15 | const Database = require('../../lib/database'); 16 | const UserService = require('../../lib/user/service'); 17 | const UserModel = require('../../lib/user/model'); 18 | 19 | // Basic configuration: create a sinon sandbox for testing 20 | let sandbox = null; 21 | 22 | beforeEach(function () { 23 | sandbox = sinon.sandbox.create(); 24 | }); 25 | 26 | afterEach(function () { 27 | sandbox && sandbox.restore(); 28 | }); 29 | 30 | // Load fake data for the users 31 | let sequelizeInstance = null; 32 | 33 | beforeEach(function (done) { 34 | SequelizeMocking 35 | .createAndLoadFixtureFile(Database.getInstance(), path.resolve(path.join(__dirname, './fake-users-database.json'))) 36 | .then(function (mockedSequelizeInstance) { 37 | sequelizeInstance = mockedSequelizeInstance; 38 | done(); 39 | }) 40 | .catch(done); 41 | }); 42 | 43 | afterEach(function (done) { 44 | SequelizeMocking 45 | .restore(sequelizeInstance) 46 | .then(function () { 47 | done(); 48 | }) 49 | .catch(done); 50 | }); 51 | 52 | it('the service shall exist', function () { 53 | chai.expect(UserService).to.exist; 54 | }); 55 | 56 | describe('and the method findAll shall ', function () { 57 | it('exist', function () { 58 | chai.expect(UserService.findAll).to.exist; 59 | }); 60 | 61 | it('shall returns an array of user', function () { 62 | return UserService 63 | .findAll() 64 | .then(function (users) { 65 | chai.expect(users).deep.equals([{ 66 | 'id': 1, 67 | 'firstName': 'John', 68 | 'lastName': 'Doe', 69 | 'age': 25, 70 | 'description': null 71 | }]); 72 | }); 73 | }); 74 | }); 75 | 76 | describe('and the method find shall ', function () { 77 | it('exist', function () { 78 | chai.expect(UserService.find).to.exist; 79 | }); 80 | 81 | it('shall return an user if we can', function () { 82 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 83 | 84 | return UserService 85 | .find(1) 86 | .then(function (user) { 87 | chai.expect(findByIdSpy.called).to.be.true; 88 | chai.expect(findByIdSpy.calledOnce).to.be.true; 89 | chai.expect(findByIdSpy.calledWith(1)).to.be.true; 90 | 91 | chai.expect(user).deep.equals({ 92 | 'id': 1, 93 | 'firstName': 'John', 94 | 'lastName': 'Doe', 95 | 'age': 25, 96 | 'description': null 97 | }); 98 | }); 99 | }); 100 | 101 | it('shall return null if not found', function () { 102 | return UserService 103 | .find(-1) 104 | .then(function (user) { 105 | chai.expect(user).to.be.null; 106 | }); 107 | }); 108 | }); 109 | }); 110 | -------------------------------------------------------------------------------- /examples/mocha/test/user/serviceSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | 12 | describe('User - UserService (classical way) - ', function () { 13 | const UserService = require('../../lib/user/service'); 14 | let UserModel = require('../../lib/user/model'); 15 | 16 | // Basic configuration: create a sinon sandbox for testing 17 | let sandbox = null; 18 | 19 | beforeEach(function () { 20 | sandbox = sinon.sandbox.create(); 21 | }); 22 | 23 | afterEach(function () { 24 | sandbox && sandbox.restore(); 25 | }); 26 | 27 | it('the service shall exist', function () { 28 | chai.expect(UserService).to.exist; 29 | }); 30 | 31 | describe('and the method findAll shall ', function () { 32 | it('exist', function () { 33 | chai.expect(UserService.findAll).to.exist; 34 | }); 35 | 36 | it('shall returns an array of user', function () { 37 | let findAllStub = sandbox.stub(UserModel, 'findAll', function () { 38 | return Promise.resolve([{ 39 | 'id': 1, 40 | 'firstName': 'John', 41 | 'lastName': 'Doe', 42 | 'age': 25, 43 | 'description': null 44 | }]); 45 | }); 46 | 47 | return UserService 48 | .findAll() 49 | .then(function (users) { 50 | chai.expect(findAllStub.called).to.be.true; 51 | chai.expect(findAllStub.calledOnce).to.be.true; 52 | chai.expect(findAllStub.calledWith()).to.be.true; 53 | 54 | chai.expect(users).deep.equals([{ 55 | 'id': 1, 56 | 'firstName': 'John', 57 | 'lastName': 'Doe', 58 | 'age': 25, 59 | 'description': null 60 | }]); 61 | }); 62 | }); 63 | }); 64 | 65 | describe('and the method find shall ', function () { 66 | it('exist', function () { 67 | chai.expect(UserService.find).to.exist; 68 | }); 69 | 70 | it('shall return an user', function () { 71 | let findByIdStub = sandbox.stub(UserModel, 'findById', function () { 72 | return Promise.resolve({ 73 | 'id': 1, 74 | 'firstName': 'John', 75 | 'lastName': 'Doe', 76 | 'age': 25, 77 | 'description': null 78 | }); 79 | }); 80 | 81 | return UserService 82 | .find(1) 83 | .then(function (users) { 84 | chai.expect(findByIdStub.called).to.be.true; 85 | chai.expect(findByIdStub.calledOnce).to.be.true; 86 | chai.expect(findByIdStub.calledWith(1)).to.be.true; 87 | 88 | chai.expect(users).deep.equals({ 89 | 'id': 1, 90 | 'firstName': 'John', 91 | 'lastName': 'Doe', 92 | 'age': 25, 93 | 'description': null 94 | }); 95 | }); 96 | }); 97 | 98 | it('shall return null if not found', function () { 99 | sandbox.stub(UserModel, 'findById', function () { 100 | return Promise.resolve(null); 101 | }); 102 | 103 | return UserService 104 | .find(-1) 105 | .then(function (user) { 106 | chai.expect(user).to.be.null; 107 | }); 108 | }); 109 | }); 110 | }); 111 | -------------------------------------------------------------------------------- /examples/jasmine/test/user/service-with-sequelize-mockingSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const SequelizeMocking = require('sequelize-mocking').SequelizeMocking; 13 | 14 | describe('User - UserService (using SequelizeMocking) - ', function () { 15 | const Database = require('../../lib/database'); 16 | const UserService = require('../../lib/user/service'); 17 | const UserModel = require('../../lib/user/model'); 18 | 19 | // Basic configuration: create a sinon sandbox for testing 20 | let sandbox = null; 21 | 22 | beforeEach(function () { 23 | sandbox = sinon.sandbox.create(); 24 | }); 25 | 26 | afterEach(function () { 27 | sandbox && sandbox.restore(); 28 | }); 29 | 30 | // Load fake data for the users 31 | let sequelizeInstance = null; 32 | 33 | beforeEach(function (done) { 34 | SequelizeMocking 35 | .createAndLoadFixtureFile(Database.getInstance(), path.resolve(path.join(__dirname, './fake-users-database.json'))) 36 | .then(function (mockedSequelizeInstance) { 37 | sequelizeInstance = mockedSequelizeInstance; 38 | done(); 39 | }) 40 | .catch(done.fail); 41 | }); 42 | 43 | afterEach(function (done) { 44 | SequelizeMocking 45 | .restore(sequelizeInstance) 46 | .then(function () { 47 | done(); 48 | }) 49 | .catch(done.fail); 50 | }); 51 | 52 | it('the service shall exist', function () { 53 | chai.expect(UserService).to.exist; 54 | }); 55 | 56 | describe('and the method findAll shall ', function () { 57 | it('exist', function () { 58 | chai.expect(UserService.findAll).to.exist; 59 | }); 60 | 61 | it('shall returns an array of user', function (done) { 62 | UserService 63 | .findAll() 64 | .then(function (users) { 65 | chai.expect(users).deep.equals([{ 66 | 'id': 1, 67 | 'firstName': 'John', 68 | 'lastName': 'Doe', 69 | 'age': 25, 70 | 'description': null 71 | }]); 72 | done(); 73 | }) 74 | .catch(done.fail); 75 | }); 76 | }); 77 | 78 | describe('and the method find shall ', function () { 79 | it('exist', function () { 80 | chai.expect(UserService.find).to.exist; 81 | }); 82 | 83 | it('shall return an user if we can', function (done) { 84 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 85 | 86 | UserService 87 | .find(1) 88 | .then(function (user) { 89 | chai.expect(findByIdSpy.called).to.be.true; 90 | chai.expect(findByIdSpy.calledOnce).to.be.true; 91 | chai.expect(findByIdSpy.calledWith(1)).to.be.true; 92 | 93 | chai.expect(user).deep.equals({ 94 | 'id': 1, 95 | 'firstName': 'John', 96 | 'lastName': 'Doe', 97 | 'age': 25, 98 | 'description': null 99 | }); 100 | done(); 101 | }) 102 | .catch(done.fail); 103 | }); 104 | 105 | it('shall return null if not found', function (done) { 106 | UserService 107 | .find(-1) 108 | .then(function (user) { 109 | chai.expect(user).to.be.null; 110 | done(); 111 | }) 112 | .catch(done.fail); 113 | }); 114 | }); 115 | }); 116 | -------------------------------------------------------------------------------- /examples/jasmine/test/user/serviceSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | 12 | describe('User - UserService (classical way) - ', function () { 13 | const UserService = require('../../lib/user/service'); 14 | let UserModel = require('../../lib/user/model'); 15 | 16 | // Basic configuration: create a sinon sandbox for testing 17 | let sandbox = null; 18 | 19 | beforeEach(function () { 20 | sandbox = sinon.sandbox.create(); 21 | }); 22 | 23 | afterEach(function () { 24 | sandbox && sandbox.restore(); 25 | }); 26 | 27 | it('the service shall exist', function () { 28 | chai.expect(UserService).to.exist; 29 | }); 30 | 31 | describe('and the method findAll shall ', function () { 32 | it('exist', function () { 33 | chai.expect(UserService.findAll).to.exist; 34 | }); 35 | 36 | it('shall returns an array of user', function (done) { 37 | let findAllStub = sandbox.stub(UserModel, 'findAll', function () { 38 | return Promise.resolve([{ 39 | 'id': 1, 40 | 'firstName': 'John', 41 | 'lastName': 'Doe', 42 | 'age': 25, 43 | 'description': null 44 | }]); 45 | }); 46 | 47 | UserService 48 | .findAll() 49 | .then(function (users) { 50 | chai.expect(findAllStub.called).to.be.true; 51 | chai.expect(findAllStub.calledOnce).to.be.true; 52 | chai.expect(findAllStub.calledWith()).to.be.true; 53 | 54 | chai.expect(users).deep.equals([{ 55 | 'id': 1, 56 | 'firstName': 'John', 57 | 'lastName': 'Doe', 58 | 'age': 25, 59 | 'description': null 60 | }]); 61 | done(); 62 | }) 63 | .catch(done.fail); 64 | }); 65 | }); 66 | 67 | describe('and the method find shall ', function () { 68 | it('exist', function () { 69 | chai.expect(UserService.find).to.exist; 70 | }); 71 | 72 | it('shall return an user', function (done) { 73 | let findByIdStub = sandbox.stub(UserModel, 'findById', function () { 74 | return Promise.resolve({ 75 | 'id': 1, 76 | 'firstName': 'John', 77 | 'lastName': 'Doe', 78 | 'age': 25, 79 | 'description': null 80 | }); 81 | }); 82 | 83 | UserService 84 | .find(1) 85 | .then(function (users) { 86 | chai.expect(findByIdStub.called).to.be.true; 87 | chai.expect(findByIdStub.calledOnce).to.be.true; 88 | chai.expect(findByIdStub.calledWith(1)).to.be.true; 89 | 90 | chai.expect(users).deep.equals({ 91 | 'id': 1, 92 | 'firstName': 'John', 93 | 'lastName': 'Doe', 94 | 'age': 25, 95 | 'description': null 96 | }); 97 | done(); 98 | }) 99 | .catch(done.fail); 100 | }); 101 | 102 | it('shall return null if not found', function (done) { 103 | sandbox.stub(UserModel, 'findById', function () { 104 | return Promise.resolve(null); 105 | }); 106 | 107 | UserService 108 | .find(-1) 109 | .then(function (user) { 110 | chai.expect(user).to.be.null; 111 | done(); 112 | }) 113 | .catch(done.fail); 114 | }); 115 | }); 116 | }); 117 | -------------------------------------------------------------------------------- /examples/tape/test/user/serviceSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const sinon = require('sinon'); 10 | let test = require('tape'); 11 | 12 | /** 13 | * @method 14 | * @private 15 | * @param {Test} test 16 | * @param {Function} handler 17 | * @returns {Function} 18 | */ 19 | function beforeEach(test, handler) { 20 | return function (name, listener) { 21 | test(name, function (assert) { 22 | let _end = assert.end; 23 | 24 | assert.end = function () { 25 | assert.end = _end; 26 | listener(assert); 27 | }; 28 | 29 | handler(assert); 30 | }); 31 | } 32 | } 33 | 34 | /** 35 | * @method 36 | * @private 37 | * @param {Test} test 38 | * @param {Function} handler 39 | * @returns {Function} 40 | */ 41 | function afterEach(test, handler) { 42 | return function (name, listener) { 43 | test(name, function (assert) { 44 | let _end = assert.end; 45 | 46 | assert.end = function () { 47 | assert.end = _end; 48 | handler(assert); 49 | }; 50 | 51 | listener(assert); 52 | }); 53 | }; 54 | } 55 | 56 | // Basic configuration: create a sinon sandbox for testing 57 | let sandbox = null; 58 | 59 | test = beforeEach(test, function (t) { 60 | sandbox = sinon.sandbox.create(); 61 | t.end(); 62 | }); 63 | 64 | test = afterEach(test, function (t) { 65 | sandbox && sandbox.restore(); 66 | t.end(); 67 | }); 68 | 69 | const UserService = require('../../lib/user/service'); 70 | let UserModel = require('../../lib/user/model'); 71 | 72 | test('User - UserService (classical way) - the service shall exist', function (t) { 73 | t.true(!!UserService); 74 | t.end(); 75 | }); 76 | 77 | test('User - UserService (classical way) - and the method findAll shall exist', function (t) { 78 | t.true(!!UserService.findAll); 79 | t.end(); 80 | }); 81 | 82 | test('User - UserService (classical way) - and the method findAll shall returns an array of user', function (t) { 83 | let findAllStub = sandbox.stub(UserModel, 'findAll', function () { 84 | return Promise.resolve([{ 85 | 'id': 1, 86 | 'firstName': 'John', 87 | 'lastName': 'Doe', 88 | 'age': 25, 89 | 'description': null 90 | }]); 91 | }); 92 | 93 | UserService 94 | .findAll() 95 | .then(function (users) { 96 | t.ok(findAllStub.called); 97 | t.ok(findAllStub.calledOnce); 98 | t.ok(findAllStub.calledWith()); 99 | 100 | t.deepEqual(users, [{ 101 | 'id': 1, 102 | 'firstName': 'John', 103 | 'lastName': 'Doe', 104 | 'age': 25, 105 | 'description': null 106 | }]); 107 | 108 | t.end(); 109 | }) 110 | .catch(t.end); 111 | }); 112 | 113 | test('User - UserService (classical way) - and the method find shall exist', function (t) { 114 | t.true(!!UserService.find); 115 | t.end(); 116 | }); 117 | 118 | test('User - UserService (classical way) - and the method find shall return an user', function (t) { 119 | let findByIdStub = sandbox.stub(UserModel, 'findById', function () { 120 | return Promise.resolve({ 121 | 'id': 1, 122 | 'firstName': 'John', 123 | 'lastName': 'Doe', 124 | 'age': 25, 125 | 'description': null 126 | }); 127 | }); 128 | 129 | UserService 130 | .find(1) 131 | .then(function (users) { 132 | t.ok(findByIdStub.called); 133 | t.ok(findByIdStub.calledOnce); 134 | t.ok(findByIdStub.calledWith(1)); 135 | 136 | t.deepEqual(users, { 137 | 'id': 1, 138 | 'firstName': 'John', 139 | 'lastName': 'Doe', 140 | 'age': 25, 141 | 'description': null 142 | }); 143 | 144 | t.end(); 145 | }) 146 | .catch(t.end); 147 | }); 148 | 149 | test('User - UserService (classical way) - and the method find shall return null if not found', function (t) { 150 | sandbox.stub(UserModel, 'findById', function () { 151 | return Promise.resolve(null); 152 | }); 153 | 154 | return UserService 155 | .find(-1) 156 | .then(function (user) { 157 | t.ok(user === null); 158 | t.end(); 159 | }) 160 | .catch(t.end); 161 | }); 162 | -------------------------------------------------------------------------------- /examples/mocha/test/user/service-with-fixturesSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const Sequelize = require('sequelize'); 13 | const sequelizeFixtures = require('sequelize-fixtures'); 14 | 15 | describe('User - UserService (using sequelize-fixtures) - ', function () { 16 | const DatabaseInstance = require('../../lib/database/_instance'); 17 | const UserService = require('../../lib/user/service'); 18 | const UserModel = require('../../lib/user/model'); 19 | 20 | // Basic configuration: create a sinon sandbox for testing 21 | let sandbox = null; 22 | let realSequelizeInstance = null; 23 | 24 | beforeEach(function () { 25 | sandbox = sinon.sandbox.create(); 26 | }); 27 | 28 | afterEach(function () { 29 | sandbox && sandbox.restore(); 30 | }); 31 | 32 | // Load fake data for the users 33 | beforeEach(function (done) { 34 | realSequelizeInstance = DatabaseInstance.getCurrentInstance(); 35 | 36 | let sequelizeInstance = new Sequelize('test-database', null, null, { 37 | 'host': 'localhost', 38 | 'dialect': 'sqlite', 39 | 'storage': ':memory:', 40 | 'define': { 41 | 'timestamps': false, // Don't create for each model the 'createdAt' and 'updatedAt' field 42 | 'paranoid': false // Truly deleted. Not add a 'deletedAt' field 43 | }, 44 | 'pool': { 45 | 'max': 5, 46 | 'min': 0, 47 | 'idle': 10000 48 | } 49 | }); 50 | 51 | sandbox.stub(DatabaseInstance, 'getCurrentInstance', function () { 52 | return sequelizeInstance; 53 | }); 54 | 55 | realSequelizeInstance.modelManager.all.forEach(function (model) { 56 | sequelizeInstance.modelManager.addModel(model); 57 | model.sequelize = sequelizeInstance; 58 | }); 59 | 60 | sequelizeInstance 61 | .sync() 62 | .then(function () { 63 | done(); 64 | }) 65 | .catch(done); 66 | }); 67 | 68 | beforeEach(function (done) { 69 | sequelizeFixtures 70 | .loadFile(path.resolve(path.join(__dirname, './fake-users-database.json')), { 'user': UserModel }) 71 | .then(function () { 72 | done(); 73 | }) 74 | .catch(done); 75 | }); 76 | 77 | afterEach(function () { 78 | realSequelizeInstance.modelManager.all.forEach(function (model) { 79 | model.sequelize = realSequelizeInstance; 80 | }); 81 | }); 82 | 83 | it('the service shall exist', function () { 84 | chai.expect(UserService).to.exist; 85 | }); 86 | 87 | //describe('and the method findAll shall ', function () { 88 | // it('exist', function () { 89 | // chai.expect(UserService.findAll).to.exist; 90 | // }); 91 | // 92 | // it('shall returns an array of user', function () { 93 | // return UserService 94 | // .findAll() 95 | // .then(function (users) { 96 | // chai.expect(users).deep.equals([{ 97 | // 'id': 1, 98 | // 'firstName': 'John', 99 | // 'lastName': 'Doe', 100 | // 'age': 25, 101 | // 'description': null 102 | // }]); 103 | // }); 104 | // }); 105 | //}); 106 | // 107 | //describe('and the method find shall ', function () { 108 | // it('exist', function () { 109 | // chai.expect(UserService.find).to.exist; 110 | // }); 111 | // 112 | // it('shall return an user if we can', function () { 113 | // let findByIdSpy = sandbox.spy(UserModel, 'findById'); 114 | // 115 | // return UserService 116 | // .find(1) 117 | // .then(function (user) { 118 | // chai.expect(findByIdSpy.called).to.be.true; 119 | // chai.expect(findByIdSpy.calledOnce).to.be.true; 120 | // chai.expect(findByIdSpy.calledWith(1)).to.be.true; 121 | // 122 | // chai.expect(user).deep.equals({ 123 | // 'id': 1, 124 | // 'firstName': 'John', 125 | // 'lastName': 'Doe', 126 | // 'age': 25, 127 | // 'description': null 128 | // }); 129 | // }); 130 | // }); 131 | // 132 | // it('shall return null if not found', function () { 133 | // return UserService 134 | // .find(-1) 135 | // .then(function (user) { 136 | // chai.expect(user).to.be.null; 137 | // }); 138 | // }); 139 | //}); 140 | }); 141 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sequelize-mocking 2 | 3 | [![Build Status](https://travis-ci.org/rochejul/sequelize-mocking.svg?branch=master)](https://travis-ci.org/rochejul/sequelize-mocking)[![Dependency Status](https://david-dm.org/rochejul/sequelize-mocking.svg)](https://david-dm.org/rochejul/sequelize-mocking) 4 | [![devDependency Status](https://david-dm.org/rochejul/sequelize-mocking/dev-status.svg)](https://david-dm.org/rochejul/sequelize-mocking#info=devDependencies) 5 | 6 | [![Known Vulnerabilities](https://snyk.io/test/github/rochejul/sequelize-mocking/badge.svg)](https://snyk.io/test/github/rochejul/sequelize-mocking) 7 | 8 | [![NPM](https://nodei.co/npm/sequelize-mocking.png?downloads=true&downloadRank=true)](https://nodei.co/npm/sequelize-mocking/) 9 | [![NPM](https://nodei.co/npm-dl/sequelize-mocking.png?&months=6&height=3)](https://nodei.co/npm/sequelize-mocking/) 10 | 11 | 12 | Sequelize extension to deal with data-mocking for testing 13 | 14 | - it was tested with Sequelize 3.19.3 before 1.0.0 15 | - it was tested with Sequelize 4.3.1 since 1.0.0. 16 | - it was tested with Sequelize 5.3.0 since 2.0.0 17 | 18 | **And you have to declare in your package.json the expected sequelize version** 19 | 20 | It will use the sqlite database for mocked database, will recreate it for database. 21 | 22 | Can be integrated with Mocha and Jasmine. 23 | 24 | A sample of use: 25 | 26 | ````js 27 | /** 28 | * Test around the @{UserService} 29 | * 30 | * @module test/user/service 31 | */ 32 | 33 | 'use strict'; 34 | 35 | const chai = require('chai'); 36 | const sinon = require('sinon'); 37 | const path = require('path'); 38 | const sequelizeMockingMocha = require('sequelize-mocking').sequelizeMockingMocha; 39 | 40 | describe('User - UserService (using sequelizeMockingMocha) - ', function () { 41 | const Database = require('../../lib/database'); 42 | const UserService = require('../../lib/user/service'); 43 | const UserModel = require('../../lib/user/model'); 44 | 45 | // Basic configuration: create a sinon sandbox for testing 46 | let sandbox = null; 47 | 48 | beforeEach(function () { 49 | sandbox = sinon.sandbox.create(); 50 | }); 51 | 52 | afterEach(function () { 53 | sandbox && sandbox.restore(); 54 | }); 55 | 56 | // Load fake data for the users 57 | sequelizeMockingMocha( 58 | Database.getInstance(), 59 | path.resolve(path.join(__dirname, './fake-users-database.json')), 60 | /* Or load array of files 61 | [ 62 | path.resolve(path.join(__dirname, './fake-users-database.json')), 63 | path.resolve(path.join(__dirname, './fake-candy-database.json')), 64 | ] 65 | */ 66 | { 'logging': false } 67 | ); 68 | 69 | it('the service shall exist', function () { 70 | chai.expect(UserService).to.exist; 71 | }); 72 | 73 | describe('and the method findAll shall ', function () { 74 | it('exist', function () { 75 | chai.expect(UserService.findAll).to.exist; 76 | }); 77 | 78 | it('shall returns an array of user', function () { 79 | return UserService 80 | .findAll() 81 | .then(function (users) { 82 | chai.expect(users).deep.equals([{ 83 | 'id': 1, 84 | 'firstName': 'John', 85 | 'lastName': 'Doe', 86 | 'age': 25, 87 | 'description': null 88 | }]); 89 | }); 90 | }); 91 | }); 92 | 93 | describe('and the method find shall ', function () { 94 | it('exist', function () { 95 | chai.expect(UserService.find).to.exist; 96 | }); 97 | 98 | it('shall return a user if we can', function () { 99 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 100 | 101 | return UserService 102 | .find(1) 103 | .then(function (user) { 104 | chai.expect(findByIdSpy.called).to.be.true; 105 | chai.expect(findByIdSpy.calledOnce).to.be.true; 106 | chai.expect(findByIdSpy.calledWith(1)).to.be.true; 107 | 108 | chai.expect(user).deep.equals({ 109 | 'id': 1, 110 | 'firstName': 'John', 111 | 'lastName': 'Doe', 112 | 'age': 25, 113 | 'description': null 114 | }); 115 | }); 116 | }); 117 | 118 | it('shall return null if not found', function () { 119 | return UserService 120 | .find(-1) 121 | .then(function (user) { 122 | chai.expect(user).to.be.null; 123 | }); 124 | }); 125 | }); 126 | }); 127 | ```` 128 | 129 | And the mocked data from the JSON file: 130 | 131 | ````JSON 132 | [ 133 | { 134 | "model": "user", 135 | "data": { 136 | "id": 1, 137 | "firstName": "John", 138 | "lastName": "Doe", 139 | "age": 25, 140 | "description": null 141 | } 142 | } 143 | ] 144 | ```` 145 | -------------------------------------------------------------------------------- /examples/jasmine/test/user/service-with-fixturesSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const Sequelize = require('sequelize'); 13 | const sequelizeFixtures = require('sequelize-fixtures'); 14 | 15 | describe('User - UserService (using sequelize-fixtures) - ', function () { 16 | const DatabaseInstance = require('../../lib/database/_instance'); 17 | const UserService = require('../../lib/user/service'); 18 | const UserModel = require('../../lib/user/model'); 19 | 20 | // Basic configuration: create a sinon sandbox for testing 21 | let sandbox = null; 22 | let realSequelizeInstance = null; 23 | 24 | beforeEach(function () { 25 | sandbox = sinon.sandbox.create(); 26 | }); 27 | 28 | afterEach(function () { 29 | sandbox && sandbox.restore(); 30 | }); 31 | 32 | // Load fake data for the users 33 | beforeEach(function (done) { 34 | realSequelizeInstance = DatabaseInstance.getCurrentInstance(); 35 | 36 | let sequelizeInstance = new Sequelize('test-database', null, null, { 37 | 'host': 'localhost', 38 | 'dialect': 'sqlite', 39 | 'storage': ':memory:', 40 | 'define': { 41 | 'timestamps': false, // Don't create for each model the 'createdAt' and 'updatedAt' field 42 | 'paranoid': false // Truly deleted. Not add a 'deletedAt' field 43 | }, 44 | 'pool': { 45 | 'max': 5, 46 | 'min': 0, 47 | 'idle': 10000 48 | } 49 | }); 50 | 51 | sandbox.stub(DatabaseInstance, 'getCurrentInstance', function () { 52 | return sequelizeInstance; 53 | }); 54 | 55 | realSequelizeInstance.modelManager.all.forEach(function (model) { 56 | sequelizeInstance.modelManager.addModel(model); 57 | model.sequelize = sequelizeInstance; 58 | }); 59 | 60 | sequelizeInstance 61 | .sync() 62 | .then(function () { 63 | done(); 64 | }) 65 | .catch(done.fail); 66 | }); 67 | 68 | beforeEach(function (done) { 69 | sequelizeFixtures 70 | .loadFile(path.resolve(path.join(__dirname, './fake-users-database.json')), { 'user': UserModel }) 71 | .then(function () { 72 | done(); 73 | }) 74 | .catch(done.fail); 75 | }); 76 | 77 | afterEach(function () { 78 | realSequelizeInstance.modelManager.all.forEach(function (model) { 79 | model.sequelize = realSequelizeInstance; 80 | }); 81 | }); 82 | 83 | it('the service shall exist', function () { 84 | chai.expect(UserService).to.exist; 85 | }); 86 | 87 | describe('and the method findAll shall ', function () { 88 | it('exist', function () { 89 | chai.expect(UserService.findAll).to.exist; 90 | }); 91 | 92 | it('shall returns an array of user', function (done) { 93 | return UserService 94 | .findAll() 95 | .then(function (users) { 96 | chai.expect(users).deep.equals([{ 97 | 'id': 1, 98 | 'firstName': 'John', 99 | 'lastName': 'Doe', 100 | 'age': 25, 101 | 'description': null 102 | }]); 103 | done(); 104 | }) 105 | .catch(done.fail); 106 | }); 107 | }); 108 | 109 | describe('and the method find shall ', function () { 110 | it('exist', function () { 111 | chai.expect(UserService.find).to.exist; 112 | }); 113 | 114 | it('shall return an user if we can', function (done) { 115 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 116 | 117 | UserService 118 | .find(1) 119 | .then(function (result) { 120 | chai.expect(findByIdSpy.called).to.be.true; 121 | chai.expect(findByIdSpy.calledOnce).to.be.true; 122 | chai.expect(findByIdSpy.calledWith(1)).to.be.true; 123 | 124 | chai.expect(result.dataValues).deep.equals({ 125 | 'id': 1, 126 | 'firstName': 'John', 127 | 'lastName': 'Doe', 128 | 'age': 25, 129 | 'description': null 130 | }); 131 | done(); 132 | }) 133 | .catch(done.fail); 134 | }); 135 | 136 | it('shall return null if not found', function (done) { 137 | UserService 138 | .find(-1) 139 | .then(function (user) { 140 | chai.expect(user).to.be.null; 141 | done(); 142 | }) 143 | .catch(done.fail); 144 | }); 145 | }); 146 | }); 147 | -------------------------------------------------------------------------------- /examples/debug/issue-024/test/user/service-with-sequelize-mocking-jasmineSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Test around the @{UserService} 3 | * 4 | * @module test/user/service 5 | */ 6 | 7 | 'use strict'; 8 | 9 | const chai = require('chai'); 10 | const sinon = require('sinon'); 11 | const path = require('path'); 12 | const sequelizeMockingJasmine = require('sequelize-mocking').sequelizeMockingJasmine; 13 | 14 | describe('User - UserService (using sequelizeMockingJasmine) - ', function () { 15 | const Database = require('../../lib/database'); 16 | const UserService = require('../../lib/user/service'); 17 | const UserModel = require('../../lib/user/model'); 18 | 19 | // Basic configuration: create a sinon sandbox for testing 20 | let sandbox = null; 21 | 22 | beforeEach(function () { 23 | sandbox = sinon.sandbox.create(); 24 | }); 25 | 26 | afterEach(function () { 27 | sandbox && sandbox.restore(); 28 | }); 29 | 30 | // Load fake data for the users 31 | sequelizeMockingJasmine( 32 | Database.getInstance(), 33 | path.resolve(path.join(__dirname, './fake-users-database.json')), 34 | { 'logging': false } 35 | ); 36 | 37 | it('the service shall exist', function () { 38 | chai.expect(UserService).to.exist; 39 | }); 40 | 41 | describe('and the method findAll shall ', function () { 42 | it('exist', function () { 43 | chai.expect(UserService.findAll).to.exist; 44 | }); 45 | 46 | it('shall returns an array of user', function (done) { 47 | UserService 48 | .findAll() 49 | .then(function (users) { 50 | chai.expect(users).deep.equals([{ 51 | 'uuid': 'f64f2940-fae4-11e7-8c5f-ef356f279131', 52 | 'firstName': 'John', 53 | 'lastName': 'Doe', 54 | 'age': 25, 55 | 'description': null 56 | }]); 57 | done(); 58 | }) 59 | .catch(done.fail); 60 | }); 61 | }); 62 | 63 | describe('and the method insert shall ', function () { 64 | it('exist', function () { 65 | chai.expect(UserService.insert).to.exist; 66 | }); 67 | 68 | it('shall insert a new user', function (done) { 69 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 70 | 71 | UserService 72 | .insert({ 73 | 'uuid': '9e126f71-a6b4-4ad2-b370-37f9f3971005', 74 | 'firstName': 'Smith', 75 | 'lastName': 'Wesson', 76 | 'age': 42, 77 | 'description': null 78 | }) 79 | .then(() => UserService.findAll()) 80 | .then(function (users) { 81 | chai.expect(users.length).equals(2); 82 | chai.expect(users).deep.equals([ 83 | { 84 | 'uuid': 'f64f2940-fae4-11e7-8c5f-ef356f279131', 85 | 'firstName': 'John', 86 | 'lastName': 'Doe', 87 | 'age': 25, 88 | 'description': null 89 | }, 90 | { 91 | 'uuid': '9e126f71-a6b4-4ad2-b370-37f9f3971005', 92 | 'firstName': 'Smith', 93 | 'lastName': 'Wesson', 94 | 'age': 42, 95 | 'description': null 96 | } 97 | ]); 98 | done(); 99 | }) 100 | .catch(done.fail); 101 | }); 102 | }); 103 | 104 | describe('and the method find shall ', function () { 105 | it('exist', function () { 106 | chai.expect(UserService.find).to.exist; 107 | }); 108 | 109 | it('shall return an user if we can', function (done) { 110 | let findByIdSpy = sandbox.spy(UserModel, 'findById'); 111 | 112 | UserService 113 | .find('f64f2940-fae4-11e7-8c5f-ef356f279131') 114 | .then(function (user) { 115 | chai.expect(findByIdSpy.called).to.be.true; 116 | chai.expect(findByIdSpy.calledOnce).to.be.true; 117 | chai.expect(findByIdSpy.calledWith('f64f2940-fae4-11e7-8c5f-ef356f279131')).to.be.true; 118 | 119 | chai.expect(user).deep.equals({ 120 | 'uuid': 'f64f2940-fae4-11e7-8c5f-ef356f279131', 121 | 'firstName': 'John', 122 | 'lastName': 'Doe', 123 | 'age': 25, 124 | 'description': null 125 | }); 126 | done(); 127 | }) 128 | .catch(done.fail); 129 | }); 130 | 131 | it('shall return null if not found', function (done) { 132 | UserService 133 | .find('f64f2940-fae4-11e7-8c5f-ef356f279135') 134 | .then(function (user) { 135 | chai.expect(user).to.be.null; 136 | done(); 137 | }) 138 | .catch(done.fail); 139 | }); 140 | }); 141 | }); 142 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "espree", 3 | "env": { 4 | "node": true, 5 | "es6": true 6 | }, 7 | "globals": { 8 | "__dirname": true, 9 | "module": true, 10 | "exports": true, 11 | "require": true, 12 | "process": true, 13 | "Promise": true 14 | }, 15 | "rules": { 16 | "no-alert": 1, 17 | "no-array-constructor": 2, 18 | "no-bitwise": 0, 19 | "no-caller": 2, 20 | "no-catch-shadow": 2, 21 | "no-comma-dangle": 0, 22 | "no-cond-assign": 1, 23 | "no-console": 1, 24 | "no-constant-condition": 2, 25 | "no-control-regex": 2, 26 | "no-debugger": 1, 27 | "no-delete-var": 2, 28 | "no-div-regex": 0, 29 | "no-dupe-keys": 2, 30 | "no-dupe-args": 2, 31 | "no-duplicate-case": 2, 32 | "no-else-return": 0, 33 | "no-empty-character-class": 2, 34 | "no-empty-label": 2, 35 | "no-eq-null": 0, 36 | "no-eval": 2, 37 | "no-ex-assign": 2, 38 | "no-extend-native": 2, 39 | "no-extra-bind": 2, 40 | "no-extra-parens": 2, 41 | "no-extra-boolean-cast": 2, 42 | "no-extra-semi": 2, 43 | "no-extra-strict": 0, 44 | "no-fallthrough": 2, 45 | "no-floating-decimal": 0, 46 | "no-func-assign": 2, 47 | "no-implied-eval": 2, 48 | "no-inline-comments": 0, 49 | "no-inner-declarations": [1, "functions"], 50 | "no-invalid-regexp": 2, 51 | "no-irregular-whitespace": 2, 52 | "no-iterator": 0, 53 | "no-label-var": 2, 54 | "no-labels": 2, 55 | "no-lone-blocks": 2, 56 | "no-lonely-if": 0, 57 | "no-loop-func": 2, 58 | "no-mixed-requires": [0, false], 59 | "no-mixed-spaces-and-tabs": [2, false], 60 | "no-multi-spaces": 2, 61 | "no-multi-str": 2, 62 | "no-multiple-empty-lines": [0, {"max": 2}], 63 | "no-native-reassign": 2, 64 | "no-negated-in-lhs": 2, 65 | "no-nested-ternary": 0, 66 | "no-new": 2, 67 | "no-new-func": 2, 68 | "no-new-object": 2, 69 | "no-new-require": 0, 70 | "no-new-wrappers": 2, 71 | "no-obj-calls": 2, 72 | "no-octal": 2, 73 | "no-octal-escape": 2, 74 | "no-param-reassign": 0, 75 | "no-path-concat": 0, 76 | "no-plusplus": 0, 77 | "no-process-env": 0, 78 | "no-process-exit": 2, 79 | "no-proto": 2, 80 | "no-redeclare": 2, 81 | "no-regex-spaces": 2, 82 | "no-reserved-keys": 0, 83 | "no-restricted-modules": 0, 84 | "no-return-assign": 2, 85 | "no-script-url": 2, 86 | "no-self-compare": 0, 87 | "no-sequences": 2, 88 | "no-shadow": 2, 89 | "no-shadow-restricted-names": 2, 90 | "no-space-before-semi": 0, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-sync": 0, 94 | "no-ternary": 0, 95 | "no-trailing-spaces": 2, 96 | "no-throw-literal": 0, 97 | "no-undef": 2, 98 | "no-undef-init": 2, 99 | "no-undefined": 0, 100 | "no-underscore-dangle": 1, 101 | "no-unreachable": 2, 102 | "no-unused-expressions": 0, 103 | "no-unused-vars": [2, {"vars": "all", "args": "after-used"}], 104 | "no-use-before-define": 2, 105 | "no-void": 0, 106 | "no-var": 0, 107 | "no-warning-comments": [0, { "terms": ["todo", "fixme", "xxx"], "location": "start" }], 108 | "no-with": 2, 109 | 110 | "block-scoped-var": 0, 111 | "brace-style": [0, "1tbs"], 112 | "camelcase": 2, 113 | "comma-dangle": [2, "never"], 114 | "comma-spacing": 2, 115 | "comma-style": 0, 116 | "complexity": [0, 11], 117 | "consistent-return": 2, 118 | "consistent-this": [0, "that"], 119 | "curly": 2, 120 | "default-case": 0, 121 | "dot-notation": [2, { "allowKeywords": true }], 122 | "eol-last": 2, 123 | "eqeqeq": 2, 124 | "func-names": 0, 125 | "func-style": [0, "declaration"], 126 | "generator-star": 0, 127 | "generator-star-spacing": 1, 128 | "guard-for-in": 0, 129 | "handle-callback-err": 0, 130 | "indent": ["warn", 4, { "SwitchCase": 1, "MemberExpression": 1, "ArrayExpression": 1, "ObjectExpression": 1 }], 131 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 132 | "max-depth": [0, 4], 133 | "max-len": [0, 80, 4], 134 | "max-nested-callbacks": [0, 2], 135 | "max-params": [0, 3], 136 | "max-statements": [0, 10], 137 | "new-cap": 2, 138 | "new-parens": 2, 139 | "newline-after-var": 0, 140 | "one-var": 0, 141 | "operator-assignment": [0, "always"], 142 | "padded-blocks": 0, 143 | "quote-props": 0, 144 | "quotes": [2, "single"], 145 | "radix": 0, 146 | "semi": 2, 147 | "semi-spacing": [2, {"before": false, "after": true}], 148 | "sort-vars": 0, 149 | "space-after-function-name": [0, "never"], 150 | "space-after-keywords": [0, "always"], 151 | "space-before-blocks": [0, "always"], 152 | "space-before-function-paren": [0, "always"], 153 | "space-before-function-parentheses": [0, "always"], 154 | "space-in-brackets": [0, "never"], 155 | "space-in-parens": [0, "never"], 156 | "space-infix-ops": 2, 157 | "space-return-throw-case": 2, 158 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 159 | "spaced-line-comment": [0, "always"], 160 | "strict": [2, "global"], 161 | "use-isnan": 2, 162 | "valid-jsdoc": 0, 163 | "valid-typeof": 2, 164 | "vars-on-top": 0, 165 | "wrap-iife": 0, 166 | "wrap-regex": 0, 167 | "yoda": [2, "never"] 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /lib/sequelize-mocking.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Base service for mocking with Sequelize 3 | * 4 | * @module lib/sequelize-mocking 5 | * @exports SequelizeMocking 6 | * @version 0.1.0 7 | * @since 0.1.0 8 | * @author Julien Roche 9 | */ 10 | 11 | 'use strict'; 12 | 13 | // Imports 14 | const Sequelize = require('sequelize'); 15 | const _ = require('lodash'); 16 | const sequelizeFixtures = require('sequelize-fixtures'); 17 | 18 | // Constants and variables 19 | const FAKE_DATABASE_NAME = 'sqlite://test-database'; 20 | const AFTER_DEFINE_EVENT = 'afterDefine'; 21 | const AFTER_DEFINE_EVENT_NAME = 'sequelizeMockAfterDefine'; 22 | const SQLITE_SEQUELIZE_OPTIONS = { 23 | 'dialect': 'sqlite', 24 | 'storage': ':memory:' 25 | }; 26 | 27 | /** 28 | * @class SequelizeMockingOptions 29 | * @property {boolean} [logging=true] 30 | * @property {Function} [transformFixtureDataFn] Allow an external caller to do some transforms to the data. See https://github.com/domasx2/sequelize-fixtures/commit/cffbfb1c67c8e05d5099b4455b99ac3aadd0089d 31 | */ 32 | 33 | /** 34 | * Sequelize mocking service 35 | */ 36 | class SequelizeMocking { 37 | /** 38 | * @param {Sequelize} sequelizeInstance 39 | * @param {SequelizeMockingOptions} [options] 40 | * @returns {Sequelize} Mocked Sequelize object 41 | */ 42 | static adaptSequelizeOptions(sequelizeInstance, options) { 43 | let optionsExtended = _.merge( 44 | { }, 45 | sequelizeInstance.options, 46 | SQLITE_SEQUELIZE_OPTIONS, 47 | { 'logging': options && !options.logging ? false : console.log } 48 | ); 49 | 50 | return optionsExtended; 51 | } 52 | 53 | /** 54 | * @param {Sequelize} sequelizeInstance 55 | * @param {Sequelize.Model} model 56 | * @returns {Sequelize.Model} 57 | */ 58 | static copyModel(sequelizeInstance, model) { 59 | class TempModel extends Sequelize.Model { 60 | } 61 | 62 | let newModel = TempModel.init( 63 | _.merge({ }, model.rawAttributes), 64 | _.merge({ }, model.options, { 'sequelize': sequelizeInstance, 'modelName': model.name }) 65 | ); 66 | 67 | // Let's recreate a new instance of the datatype 68 | for (let att of _.values(newModel.rawAttributes)) { 69 | att.type = new Sequelize.DataTypes[att.type.key](); 70 | } 71 | 72 | return newModel; 73 | } 74 | 75 | /** 76 | * @param {Sequelize} originalSequelize 77 | * @param {Sequelize} mockedSequelize 78 | * @returns {Sequelize} Mocked Sequelize object 79 | */ 80 | static copyCurrentModels(originalSequelize, mockedSequelize) { 81 | originalSequelize.modelManager.all.forEach(function (model) { 82 | SequelizeMocking.copyModel(mockedSequelize, model); 83 | }); 84 | 85 | return mockedSequelize; 86 | } 87 | 88 | /** 89 | * @param {Sequelize} originalSequelize 90 | * @param {SequelizeMockingOptions} [options] 91 | * @returns {Promise.} 92 | */ 93 | static create(originalSequelize, options) { 94 | let logging = !options || options.logging; 95 | let mockedSequelize = new Sequelize(FAKE_DATABASE_NAME, null, null, SequelizeMocking.adaptSequelizeOptions(originalSequelize, options)); 96 | 97 | mockedSequelize.__originalSequelize = originalSequelize; 98 | 99 | SequelizeMocking.copyCurrentModels(originalSequelize, mockedSequelize); 100 | SequelizeMocking.modifyConnection(originalSequelize, mockedSequelize); 101 | SequelizeMocking.modifyModelReferences(originalSequelize, mockedSequelize); 102 | SequelizeMocking.hookNewModel(originalSequelize, mockedSequelize, options); 103 | 104 | logging && console.log('SequelizeMocking - Mock the context'); 105 | return mockedSequelize 106 | .sync() 107 | .then(function () { 108 | logging && console.log('SequelizeMocking - Database construction done'); 109 | return mockedSequelize; 110 | }); 111 | } 112 | 113 | /** 114 | * @param {Sequelize} originalSequelize 115 | * @param {string | Array.} fixtureFilePath 116 | * @param {SequelizeMockingOptions} [options] 117 | * @returns {Promise.} 118 | */ 119 | static createAndLoadFixtureFile(originalSequelize, fixtureFilePath, options) { 120 | let logging = !options || options.logging; 121 | 122 | return SequelizeMocking 123 | .create(originalSequelize, options) 124 | .then(function (mockedSequelize) { 125 | return SequelizeMocking 126 | .loadFixtureFile(mockedSequelize, fixtureFilePath, options) 127 | .then(function () { 128 | logging && console.log('SequelizeMocking - Mocked data injected'); 129 | return mockedSequelize; 130 | }); 131 | }); 132 | } 133 | 134 | /** 135 | * @param {Sequelize} originalSequelize 136 | * @param {Sequelize} mockedSequelize 137 | * @param {SequelizeMockingOptions} [options] 138 | */ 139 | static hookNewModel(originalSequelize, mockedSequelize, options) { 140 | let logging = !options || options.logging; 141 | 142 | originalSequelize.addHook(AFTER_DEFINE_EVENT, AFTER_DEFINE_EVENT_NAME, function (newModel) { 143 | SequelizeMocking 144 | .modifyModelReference( 145 | mockedSequelize, 146 | SequelizeMocking.copyModel(mockedSequelize, newModel) 147 | ) 148 | .sync({ 'hooks': true }) 149 | .then(function () { 150 | logging && console.log(`Model ${newModel.name} was declared into the database`); 151 | }) 152 | .catch(function (err) { 153 | logging && console.error(`An error occured when initializing the model ${newModel.name}`); 154 | console.error(err && err.stack ? err.stack : err); 155 | process.exit(1); 156 | }); 157 | }); 158 | } 159 | 160 | /** 161 | * @param {Sequelize} sequelize 162 | * @param {string | Array.} fixtureFilePath 163 | * @param {SequelizeMockingOptions} [options] 164 | * @returns {Promise.} 165 | */ 166 | static loadFixtureFile(sequelize, fixtureFilePath, options) { 167 | let logging = !options || options.logging; 168 | let transformFixtureDataFn = !options || options.transformFixtureDataFn; 169 | let loadFixturesOptions = { 170 | 'log': logging ? null : _.noop 171 | }; 172 | 173 | if (_.isFunction(transformFixtureDataFn)) { 174 | loadFixturesOptions.transformFixtureDataFn = transformFixtureDataFn; 175 | } 176 | 177 | return sequelizeFixtures[Array.isArray(fixtureFilePath) ? 'loadFiles' : 'loadFile'](fixtureFilePath, SequelizeMocking.mapModels(sequelize), loadFixturesOptions) 178 | .then(function(){ 179 | return sequelize; 180 | }); 181 | 182 | } 183 | 184 | /** 185 | * @param sequelize 186 | * @returns {Object} 187 | */ 188 | static mapModels(sequelize) { 189 | let map = { }; 190 | 191 | sequelize.modelManager.all.forEach(function (model) { 192 | map[model.name] = model; 193 | }); 194 | 195 | return map; 196 | } 197 | 198 | /** 199 | * @param {Sequelize} originalSequelize 200 | * @param {Sequelize} newSequelizeToUse 201 | * @returns {Sequelize} The new Sequelize object 202 | */ 203 | static modifyConnection(originalSequelize, newSequelizeToUse) { 204 | originalSequelize.__connectionManager = originalSequelize.connectionManager; 205 | originalSequelize.connectionManager = newSequelizeToUse.connectionManager; 206 | 207 | originalSequelize.__dialect = originalSequelize.dialect; 208 | originalSequelize.dialect = newSequelizeToUse.dialect; 209 | 210 | originalSequelize.__queryInterface = originalSequelize.queryInterface; 211 | originalSequelize.queryInterface = newSequelizeToUse.queryInterface; 212 | 213 | return newSequelizeToUse; 214 | } 215 | 216 | /** 217 | * Goal: the instanciate model shall use another instance of @{Sequelize} than the one used to create the model 218 | * 219 | * @param {Sequelize} newSequelizeToUse 220 | * @param {Sequelize.Model} model 221 | * @returns {Sequelize.Model} 222 | */ 223 | static modifyModelReference (newSequelizeToUse, model) { 224 | model.sequelize = newSequelizeToUse; 225 | return model; 226 | } 227 | 228 | /** 229 | * @param {Sequelize} originalSequelize 230 | * @param {Sequelize} newSequelizeToUse 231 | * @returns {Sequelize} The new Sequelize object 232 | */ 233 | static modifyModelReferences(originalSequelize, newSequelizeToUse) { 234 | originalSequelize.modelManager.all.forEach(function (model) { 235 | SequelizeMocking.modifyModelReference(newSequelizeToUse, model); 236 | }); 237 | 238 | return newSequelizeToUse; 239 | } 240 | 241 | /** 242 | * @param {Sequelize} mockedSequelize 243 | * @param {SequelizeMockingOptions} [options] 244 | * @returns {Promise} 245 | */ 246 | static restore(mockedSequelize, options) { 247 | let logging = !options || options.logging; 248 | 249 | SequelizeMocking.unhookNewModel(mockedSequelize); 250 | 251 | if (mockedSequelize.__originalSequelize) { 252 | SequelizeMocking.modifyModelReferences(mockedSequelize, mockedSequelize.__originalSequelize); 253 | } 254 | 255 | if (mockedSequelize.__dialect && mockedSequelize.__connectionManager) { 256 | SequelizeMocking.modifyConnection(mockedSequelize, mockedSequelize.__originalSequelize); 257 | } 258 | 259 | delete mockedSequelize.__originalSequelize; 260 | delete mockedSequelize.__dialect; 261 | delete mockedSequelize.__queryInterface; 262 | delete mockedSequelize.__connectionManager; 263 | 264 | logging && console.log('SequelizeMocking - restore the context'); 265 | return mockedSequelize 266 | .getQueryInterface() 267 | .dropAllTables({ 'logging': logging }) 268 | .then(function () { 269 | logging && console.log('SequelizeMocking - Context is restored'); 270 | }); 271 | } 272 | 273 | /** 274 | * @param {Sequelize} mockedSequelize 275 | */ 276 | static unhookNewModel(mockedSequelize) { 277 | if (mockedSequelize.__originalSequelize) { 278 | mockedSequelize.__originalSequelize.removeHook(AFTER_DEFINE_EVENT, AFTER_DEFINE_EVENT_NAME); 279 | } 280 | } 281 | } 282 | 283 | module.exports = SequelizeMocking; 284 | -------------------------------------------------------------------------------- /test/sequelize-mocking-mochaSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing around the @{sequelizeMochingMocha} function to ease testing with Mocha or Jasmine 3 | * 4 | * @module test/sequelize-mocking 5 | * @version 0.1.0 6 | * @since 0.1.0 7 | * @author Julien Roche 8 | */ 9 | 10 | 'use strict'; 11 | 12 | describe('sequelizeMochingMocha - ', function () { 13 | const expect = require('chai').expect; 14 | const sinon = require('sinon'); 15 | 16 | const path = require('path'); 17 | const _ = require('lodash'); 18 | const Sequelize = require('sequelize'); 19 | 20 | const SequelizeMocking = require('../lib/sequelize-mocking'); 21 | const sequelizeMochingMocha = require('../lib/sequelize-mocking-mocha'); 22 | 23 | it('shall exist', function () { 24 | expect(sequelizeMochingMocha).to.exist; 25 | expect(_.isPlainObject(sequelizeMochingMocha)).to.be.false; 26 | }); 27 | 28 | it('should be a function', function () { 29 | expect(sequelizeMochingMocha).to.exist; 30 | expect(sequelizeMochingMocha).to.be.a('function'); 31 | }); 32 | 33 | let sinonSandbox; 34 | 35 | beforeEach(function () { 36 | sinonSandbox = sinon.sandbox.create(); 37 | }); 38 | 39 | afterEach(function () { 40 | sinonSandbox.restore(); 41 | }); 42 | 43 | describe('should detect Mocha and Jasmine functions, and ', function () { 44 | beforeEach(function () { 45 | global.__beforeEach = global.beforeEach; 46 | global.beforeEach = null; 47 | 48 | global.__afterEach = global.afterEach; 49 | global.afterEach = null; 50 | }); 51 | 52 | afterEach(function () { 53 | global.beforeEach = global.__beforeEach; 54 | global.afterEach = global.__afterEach; 55 | 56 | delete global.__beforeEach; 57 | delete global.__afterEach; 58 | }); 59 | 60 | it('should do nothing if only beforeEach is available', function () { 61 | global.beforeEach = _.noop; 62 | 63 | let spyBefore = sinonSandbox.spy(global, 'beforeEach'); 64 | 65 | let sequelizeInstance = new Sequelize('mocked-database', null, null, { 66 | 'host': 'localhost', 67 | 'dialect': 'sqlite', 68 | 'storage': ':memory:' 69 | }); 70 | 71 | sequelizeMochingMocha(sequelizeInstance, 'a/path/to/fixture'); 72 | 73 | expect(spyBefore.called).to.be.false; 74 | }); 75 | 76 | it('should do nothing if only afterEach is available', function () { 77 | global.afterEach = _.noop; 78 | 79 | let spyAfter = sinonSandbox.spy(global, 'afterEach'); 80 | 81 | let sequelizeInstance = new Sequelize('mocked-database', null, null, { 82 | 'host': 'localhost', 83 | 'dialect': 'sqlite', 84 | 'storage': ':memory:' 85 | }); 86 | 87 | sequelizeMochingMocha(sequelizeInstance, 'a/path/to/fixture'); 88 | 89 | expect(spyAfter.called).to.be.false; 90 | }); 91 | 92 | it('should do something if both afterEach and beforeEach functions are available', function () { 93 | global.beforeEach = _.noop; 94 | global.afterEach = _.noop; 95 | 96 | let spyBefore = sinonSandbox.spy(global, 'beforeEach'); 97 | let spyAfter = sinonSandbox.spy(global, 'afterEach'); 98 | 99 | let sequelizeInstance = new Sequelize('mocked-database', null, null, { 100 | 'host': 'localhost', 101 | 'dialect': 'sqlite', 102 | 'storage': ':memory:' 103 | }); 104 | 105 | sequelizeMochingMocha(sequelizeInstance, 'a/path/to/fixture'); 106 | 107 | expect(spyBefore.called).to.be.true; 108 | expect(spyBefore.calledOnce).to.be.true; 109 | expect(spyBefore.calledWith(sinon.match.func)).to.be.true; 110 | 111 | expect(spyAfter.called).to.be.true; 112 | expect(spyAfter.calledOnce).to.be.true; 113 | expect(spyAfter.calledWith(sinon.match.func)).to.be.true; 114 | }); 115 | }); 116 | 117 | describe('should on the afterEach method ', function () { 118 | beforeEach(function () { 119 | global.__afterEach = global.afterEach; 120 | global.__beforeEach = global.beforeEach; 121 | global.beforeEach = _.noop; 122 | }); 123 | 124 | afterEach(function () { 125 | global.afterEach = global.__afterEach; 126 | global.beforeEach = global.__beforeEach; 127 | 128 | delete global.__afterEach; 129 | delete global.__beforeEach; 130 | }); 131 | 132 | it('call the restore method', function () { 133 | let count = 0; 134 | let doneFunc = () => count++; 135 | 136 | global.afterEach = function (func) { 137 | func(doneFunc); 138 | }; 139 | 140 | sinonSandbox.stub(SequelizeMocking, 'restore').callsFake(() => Promise.resolve()); 141 | 142 | return new Promise(function(resolve, reject) { 143 | let sequelizeInstance = new Sequelize('mocked-database', null, null, { 144 | 'host': 'localhost', 145 | 'dialect': 'sqlite', 146 | 'storage': ':memory:' 147 | }); 148 | 149 | sequelizeMochingMocha(sequelizeInstance, 'a/path/to/fixture'); 150 | 151 | setTimeout(function () { 152 | try { 153 | expect(count).equals(1); 154 | resolve(); 155 | 156 | } catch (ex) { 157 | reject(ex); 158 | } 159 | }, 150); 160 | }); 161 | }); 162 | 163 | it('call the afterEach done parameter even if an error occured', function () { 164 | let count = 0; 165 | let doneFunc = () => count++; 166 | 167 | global.afterEach = function (func) { 168 | func(doneFunc); 169 | }; 170 | 171 | sinonSandbox.stub(SequelizeMocking, 'restore').callsFake(() => Promise.reject()); 172 | 173 | return new Promise(function(resolve, reject) { 174 | let sequelizeInstance = new Sequelize('mocked-database', null, null, { 175 | 'host': 'localhost', 176 | 'dialect': 'sqlite', 177 | 'storage': ':memory:' 178 | }); 179 | 180 | sequelizeMochingMocha(sequelizeInstance, 'a/path/to/fixture'); 181 | 182 | setTimeout(function () { 183 | try { 184 | expect(count).equals(1); 185 | resolve(); 186 | 187 | } catch (ex) { 188 | reject(ex); 189 | } 190 | }, 150); 191 | }); 192 | }); 193 | }); 194 | 195 | describe('should on the beforeEach method ', function () { 196 | beforeEach(function () { 197 | global.__afterEach = global.afterEach; 198 | global.__beforeEach = global.beforeEach; 199 | global.afterEach = _.noop; 200 | }); 201 | 202 | afterEach(function () { 203 | global.afterEach = global.__afterEach; 204 | global.beforeEach = global.__beforeEach; 205 | 206 | delete global.__afterEach; 207 | delete global.__beforeEach; 208 | }); 209 | 210 | it('call the create method if no fixture path is set', function () { 211 | let count = 0; 212 | let doneFunc = () => count++; 213 | 214 | global.beforeEach = function (func) { 215 | func(doneFunc); 216 | }; 217 | 218 | sinonSandbox.stub(SequelizeMocking, 'create').callsFake(() => Promise.resolve()); 219 | 220 | return new Promise(function(resolve, reject) { 221 | let sequelizeInstance = new Sequelize('mocked-database', null, null, { 222 | 'host': 'localhost', 223 | 'dialect': 'sqlite', 224 | 'storage': ':memory:' 225 | }); 226 | 227 | sequelizeMochingMocha(sequelizeInstance); 228 | 229 | setTimeout(function () { 230 | try { 231 | expect(count).equals(1); 232 | resolve(); 233 | 234 | } catch (ex) { 235 | reject(ex); 236 | } 237 | }, 150); 238 | }); 239 | }); 240 | 241 | it('call the createAndLoadFixtureFile method if a fixture path is set', function () { 242 | let count = 0; 243 | let doneFunc = () => count++; 244 | 245 | global.beforeEach = function (func) { 246 | func(doneFunc); 247 | }; 248 | 249 | sinonSandbox.stub(SequelizeMocking, 'createAndLoadFixtureFile').callsFake(() => Promise.resolve()); 250 | 251 | return new Promise(function(resolve, reject) { 252 | let sequelizeInstance = new Sequelize('mocked-database', null, null, { 253 | 'host': 'localhost', 254 | 'dialect': 'sqlite', 255 | 'storage': ':memory:' 256 | }); 257 | 258 | sequelizeMochingMocha(sequelizeInstance, 'a/path/to/fixture'); 259 | 260 | setTimeout(function () { 261 | try { 262 | expect(count).equals(1); 263 | resolve(); 264 | 265 | } catch (ex) { 266 | reject(ex); 267 | } 268 | }, 150); 269 | }); 270 | }); 271 | 272 | it('call the beforeEach done parameter even if an error occured', function () { 273 | let count = 0; 274 | let doneFunc = () => count++; 275 | 276 | global.beforeEach = function (func) { 277 | func(doneFunc); 278 | }; 279 | 280 | sinonSandbox.stub(SequelizeMocking, 'createAndLoadFixtureFile').callsFake(() => Promise.reject()); 281 | 282 | return new Promise(function(resolve, reject) { 283 | let sequelizeInstance = new Sequelize('mocked-database', null, null, { 284 | 'host': 'localhost', 285 | 'dialect': 'sqlite', 286 | 'storage': ':memory:' 287 | }); 288 | 289 | sequelizeMochingMocha(sequelizeInstance, 'a/path/to/fixture'); 290 | 291 | setTimeout(function () { 292 | try { 293 | expect(count).equals(1); 294 | resolve(); 295 | 296 | } catch (ex) { 297 | reject(ex); 298 | } 299 | }, 150); 300 | }); 301 | }); 302 | }); 303 | 304 | describe('should deal with multiple models', function () { 305 | const sequelize = new Sequelize('mysql://user:xyzzy@localhost:3306/'); 306 | const User = sequelize.define('User', { }); 307 | const OtherObject = sequelize.define('OtherObject', { }); 308 | 309 | sequelizeMochingMocha( 310 | sequelize, 311 | path.resolve(path.join(__dirname, './user-database.json')), 312 | { 'logging': false } 313 | ); 314 | 315 | it('', function () { 316 | expect(User).to.exist; 317 | }); 318 | }); 319 | }); 320 | -------------------------------------------------------------------------------- /test/sequelize-mockingSpec.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing around @{SequelizeMocking} 3 | * 4 | * @module test/sequelize-mocking 5 | * @version 0.1.0 6 | * @since 0.1.0 7 | * @author Julien Roche 8 | */ 9 | 10 | 'use strict'; 11 | 12 | describe('SequelizeMocking - ', function () { 13 | const expect = require('chai').expect; 14 | const sinon = require('sinon'); 15 | 16 | const path = require('path'); 17 | const EventEmitter = require('events').EventEmitter; 18 | const _ = require('lodash'); 19 | 20 | const Sequelize = require('sequelize'); 21 | const sequelizeFixtures = require('sequelize-fixtures'); 22 | const SequelizeMocking = require('../lib/sequelize-mocking'); 23 | 24 | const defaultMaxListeners = EventEmitter.defaultMaxListeners; 25 | 26 | it('shall exist', function () { 27 | expect(SequelizeMocking).to.exist; 28 | expect(_.isPlainObject(SequelizeMocking)).to.be.false; 29 | }); 30 | 31 | let sinonSandbox; 32 | 33 | beforeEach(function () { 34 | sinonSandbox = sinon.sandbox.create(); 35 | EventEmitter.defaultMaxListeners = 100; // Due to an error when we instanciate too many times fastly some dialects, like the MySql one 36 | }); 37 | 38 | afterEach(function () { 39 | sinonSandbox.restore(); 40 | EventEmitter.defaultMaxListeners = defaultMaxListeners; 41 | }); 42 | 43 | describe('and the method "adaptSequelizeOptions" should ', function () { 44 | it('exist', function () { 45 | expect(SequelizeMocking.adaptSequelizeOptions).to.exist; 46 | }); 47 | 48 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 49 | 'host': 'localhost', 50 | 'dialect': 'mysql', 51 | 'define': { 52 | 'engine': 'MYISAM', 53 | 'timestamps': false, 54 | 'paranoid': false 55 | }, 56 | 'pool': { 57 | 'max': 5, 58 | 'min': 0, 59 | 'idle': 10000 60 | } 61 | }); 62 | let sequelizeInstanceOptions = _.cloneDeep(sequelizeInstance.options); 63 | 64 | it('returns an extended sequelize configuration', function () { 65 | expect(SequelizeMocking.adaptSequelizeOptions(sequelizeInstance)) 66 | .deep 67 | .equals({ 68 | 'benchmark': false, 69 | 'clientMinMessages': 'warning', 70 | 'databaseVersion': 0, 71 | 'define': { 72 | 'engine': 'MYISAM', 73 | 'paranoid': false, 74 | 'timestamps': false 75 | }, 76 | 'dialect': 'sqlite', 77 | 'dialectModule': null, 78 | 'dialectModulePath': null, 79 | 'hooks': {}, 80 | 'host': 'localhost', 81 | 'isolationLevel': null, 82 | 'logging': console.log, 83 | 'native': false, 84 | 'omitNull': false, 85 | 'pool': { 86 | 'idle': 10000, 87 | 'max': 5, 88 | 'min': 0, 89 | }, 90 | 'protocol': 'tcp', 91 | 'query': {}, 92 | 'quoteIdentifiers': true, 93 | 'replication': false, 94 | 'retry': { 95 | 'match': [ 96 | 'SQLITE_BUSY: database is locked' 97 | ], 98 | 'max': 5 99 | }, 100 | 'ssl': undefined, 101 | 'standardConformingStrings': true, 102 | 'storage': ':memory:', 103 | 'sync': {}, 104 | 'timezone': '+00:00', 105 | 'transactionType': 'DEFERRED', 106 | 'typeValidation': false 107 | }); 108 | }); 109 | 110 | it('does not affect the options of the sequelize instance passed as parameter', function () { 111 | let adaptedSequelizeOptions = SequelizeMocking.adaptSequelizeOptions(sequelizeInstance); 112 | expect(sequelizeInstance.options).deep.equals(sequelizeInstanceOptions); 113 | }); 114 | 115 | describe('returns, based on options, ', function () { 116 | it('a sequelize options which allows logging', function () { 117 | let adaptedSequelizeOptions = SequelizeMocking.adaptSequelizeOptions(sequelizeInstance, { 'logging': true }); 118 | expect(adaptedSequelizeOptions.logging).equals(console.log); 119 | }); 120 | 121 | it('a sequelize options which disables logging', function () { 122 | let adaptedSequelizeOptions = SequelizeMocking.adaptSequelizeOptions(sequelizeInstance, { 'logging': false }); 123 | expect(adaptedSequelizeOptions.logging).to.be.false; 124 | }); 125 | }); 126 | }); 127 | 128 | describe('and the method "copyModel" should ', function () { 129 | it('exist', function () { 130 | expect(SequelizeMocking.copyModel).to.exist; 131 | }); 132 | 133 | it('duplicate a model with the same options', function () { 134 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 135 | 'host': 'localhost', 136 | 'dialect': 'sqlite', 137 | 'storage': ':memory:' 138 | }); 139 | 140 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 141 | 'host': 'localhost', 142 | 'dialect': 'mysql', 143 | 'define': { 144 | 'engine': 'MYISAM', 145 | 'timestamps': false, 146 | 'paranoid': false 147 | }, 148 | 'pool': { 149 | 'max': 5, 150 | 'min': 0, 151 | 'idle': 10000 152 | } 153 | }); 154 | 155 | let MyModel = sequelizeInstance.define('myModel', { 156 | 'id': { 157 | 'type': Sequelize.INTEGER, 158 | 'autoIncrement': true, 159 | 'primaryKey': true 160 | }, 161 | 'description': Sequelize.TEXT 162 | }); 163 | 164 | let DuplicatedMyModel = SequelizeMocking.copyModel(mockedSequelizeInstance, MyModel); 165 | expect(DuplicatedMyModel.name).equals(MyModel.name); 166 | expect(_.omit(DuplicatedMyModel.options, 'sequelize')).deep.equals(_.omit(MyModel.options, 'sequelize')); 167 | }); 168 | 169 | it('duplicate a model without keeping the references', function () { 170 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 171 | 'host': 'localhost', 172 | 'dialect': 'sqlite', 173 | 'storage': ':memory:' 174 | }); 175 | 176 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 177 | 'host': 'localhost', 178 | 'dialect': 'mysql', 179 | 'define': { 180 | 'engine': 'MYISAM', 181 | 'timestamps': false, 182 | 'paranoid': false 183 | }, 184 | 'pool': { 185 | 'max': 5, 186 | 'min': 0, 187 | 'idle': 10000 188 | } 189 | }); 190 | 191 | let MyModel = sequelizeInstance.define('myModel', { 192 | 'id': { 193 | 'type': Sequelize.INTEGER, 194 | 'autoIncrement': true, 195 | 'primaryKey': true 196 | }, 197 | 'description': Sequelize.TEXT 198 | }); 199 | 200 | let DuplicatedMyModel = SequelizeMocking.copyModel(mockedSequelizeInstance, MyModel); 201 | expect(DuplicatedMyModel).not.equals(MyModel); 202 | expect(DuplicatedMyModel.options).not.equals(MyModel.options); 203 | expect(DuplicatedMyModel.rawAttributes).not.equals(MyModel.rawAttributes); 204 | }); 205 | 206 | it('duplicate a model with upgrading the modelManager of the Sequelize instance', function () { 207 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 208 | 'host': 'localhost', 209 | 'dialect': 'sqlite', 210 | 'storage': ':memory:' 211 | }); 212 | 213 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 214 | 'host': 'localhost', 215 | 'dialect': 'mysql', 216 | 'define': { 217 | 'engine': 'MYISAM', 218 | 'timestamps': false, 219 | 'paranoid': false 220 | }, 221 | 'pool': { 222 | 'max': 5, 223 | 'min': 0, 224 | 'idle': 10000 225 | } 226 | }); 227 | 228 | let MyModel = sequelizeInstance.define('myModel', { 229 | 'id': { 230 | 'type': Sequelize.INTEGER, 231 | 'autoIncrement': true, 232 | 'primaryKey': true 233 | }, 234 | 'description': Sequelize.TEXT 235 | }); 236 | 237 | expect(mockedSequelizeInstance.modelManager.all.length).equals(0); 238 | 239 | let DuplicatedMyModel = SequelizeMocking.copyModel(mockedSequelizeInstance, MyModel); 240 | expect(MyModel.options.sequelize).equals(sequelizeInstance); 241 | expect(DuplicatedMyModel.options.sequelize).equals(mockedSequelizeInstance); 242 | 243 | expect(sequelizeInstance.modelManager.all.length).equals(1); 244 | expect(mockedSequelizeInstance.modelManager.all.length).equals(1); 245 | }); 246 | }); 247 | 248 | describe('and the method "create" should ', function () { 249 | it('exist', function () { 250 | expect(SequelizeMocking.create).to.exist; 251 | }); 252 | 253 | it('should use the copyCurrentModels, modifyModelReferences, modifyConnection and hookNewModel methods', function () { 254 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 255 | 'host': 'localhost', 256 | 'dialect': 'sqlite', 257 | 'storage': ':memory:', 258 | 'define': { 259 | 'timestamps': false, 260 | 'paranoid': false 261 | } 262 | }); 263 | 264 | let stubCopy = sinonSandbox.stub(SequelizeMocking, 'copyCurrentModels').callsFake(_.noop); 265 | let stubModifyModelReferences = sinonSandbox.stub(SequelizeMocking, 'modifyModelReferences').callsFake(_.noop); 266 | let stubModifyConnection = sinonSandbox.stub(SequelizeMocking, 'modifyConnection').callsFake(_.noop); 267 | let stubHook = sinonSandbox.stub(SequelizeMocking, 'hookNewModel').callsFake(_.noop); 268 | 269 | SequelizeMocking.create(sequelizeInstance); 270 | 271 | expect(stubCopy.called).to.be.true; 272 | expect(stubCopy.calledOnce).to.be.true; 273 | expect(stubCopy.calledWith(sequelizeInstance, sinon.match.instanceOf(Sequelize))).to.be.true; 274 | 275 | expect(stubModifyModelReferences.called).to.be.true; 276 | expect(stubModifyModelReferences.calledOnce).to.be.true; 277 | expect(stubModifyModelReferences.calledWith(sequelizeInstance, sinon.match.instanceOf(Sequelize))).to.be.true; 278 | 279 | expect(stubModifyConnection.called).to.be.true; 280 | expect(stubModifyConnection.calledOnce).to.be.true; 281 | expect(stubModifyConnection.calledWith(sequelizeInstance, sinon.match.instanceOf(Sequelize))).to.be.true; 282 | 283 | expect(stubHook.called).to.be.true; 284 | expect(stubHook.calledOnce).to.be.true; 285 | expect(stubHook.calledWith(sequelizeInstance, sinon.match.instanceOf(Sequelize))).to.be.true; 286 | }); 287 | 288 | it('should return a "mocked" sequelize instance', function () { 289 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 290 | 'host': 'localhost', 291 | 'dialect': 'sqlite', 292 | 'storage': ':memory:', 293 | 'define': { 294 | 'timestamps': false, 295 | 'paranoid': false 296 | } 297 | }); 298 | 299 | let stubCopy = sinonSandbox.stub(SequelizeMocking, 'copyCurrentModels').callsFake(_.noop); 300 | let stubModify = sinonSandbox.stub(SequelizeMocking, 'modifyModelReferences').callsFake(_.noop); 301 | let stubHook = sinonSandbox.stub(SequelizeMocking, 'hookNewModel').callsFake(_.noop); 302 | 303 | return SequelizeMocking 304 | .create(sequelizeInstance) 305 | .then(function (mockedSequelize) { 306 | expect(mockedSequelize).to.be.instanceof(Sequelize); 307 | expect(mockedSequelize).not.equals(sequelizeInstance); 308 | }); 309 | }); 310 | 311 | it('should associate onto the "mocked" sequelize instance the original one', function () { 312 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 313 | 'host': 'localhost', 314 | 'dialect': 'sqlite', 315 | 'storage': ':memory:', 316 | 'define': { 317 | 'timestamps': false, 318 | 'paranoid': false 319 | } 320 | }); 321 | 322 | let stubCopy = sinonSandbox.stub(SequelizeMocking, 'copyCurrentModels').callsFake(_.noop); 323 | let stubModify = sinonSandbox.stub(SequelizeMocking, 'modifyModelReferences').callsFake(_.noop); 324 | let stubHook = sinonSandbox.stub(SequelizeMocking, 'hookNewModel').callsFake(_.noop); 325 | 326 | return SequelizeMocking 327 | .create(sequelizeInstance) 328 | .then(function (mockedSequelize) { 329 | expect(mockedSequelize.__originalSequelize).not.to.be.undefined; 330 | expect(mockedSequelize.__originalSequelize).to.be.instanceof(Sequelize); 331 | expect(mockedSequelize.__originalSequelize).equals(sequelizeInstance); 332 | }); 333 | }); 334 | 335 | it('should pass through the options', function () { 336 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 337 | 'host': 'localhost', 338 | 'dialect': 'sqlite', 339 | 'storage': ':memory:', 340 | 'define': { 341 | 'timestamps': false, 342 | 'paranoid': false 343 | } 344 | }); 345 | 346 | let stubCopy = sinonSandbox.stub(SequelizeMocking, 'copyCurrentModels').callsFake(_.noop); 347 | let stubModify = sinonSandbox.stub(SequelizeMocking, 'modifyModelReferences').callsFake(_.noop); 348 | let stubHook = sinonSandbox.stub(SequelizeMocking, 'hookNewModel').callsFake(_.noop); 349 | 350 | return SequelizeMocking 351 | .create(sequelizeInstance, { 'logging': false }) 352 | .then(function (mockedSequelize) { 353 | expect(stubHook.called).to.be.true; 354 | expect(stubHook.calledOnce).to.be.true; 355 | expect(stubHook.calledWith(sequelizeInstance, sinon.match.instanceOf(Sequelize), { 'logging': false })).to.be.true; 356 | }); 357 | }); 358 | }); 359 | 360 | describe('and the method "createAndLoadFixtureFile" should ', function () { 361 | it('exist', function () { 362 | expect(SequelizeMocking.createAndLoadFixtureFile).to.exist; 363 | }); 364 | 365 | it('call the "create" function', function () { 366 | let stub = sinonSandbox.stub(SequelizeMocking, 'create').callsFake(() => Promise.reject()); 367 | 368 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 369 | 'host': 'localhost', 370 | 'dialect': 'sqlite', 371 | 'storage': ':memory:', 372 | 'define': { 373 | 'timestamps': false, 374 | 'paranoid': false 375 | } 376 | }); 377 | 378 | SequelizeMocking.createAndLoadFixtureFile(sequelizeInstance, 'a/path', { 'logging': false }); 379 | expect(stub.called).to.be.true; 380 | expect(stub.calledOnce).to.be.true; 381 | expect(stub.calledWith(sequelizeInstance, { 'logging': false })).to.be.true; 382 | }); 383 | 384 | it('call the "loadFixtureFile" function for the created mocked sequelize instance', function () { 385 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 386 | 'host': 'localhost', 387 | 'dialect': 'sqlite', 388 | 'storage': ':memory:', 389 | 'define': { 390 | 'timestamps': false, 391 | 'paranoid': false 392 | } 393 | }); 394 | 395 | let mockedSequelizeInstance = new Sequelize('mocked-database', 'mysqlUserName', 'mysqlUserPassword', { 396 | 'host': 'localhost', 397 | 'dialect': 'sqlite', 398 | 'storage': ':memory:', 399 | 'define': { 400 | 'timestamps': false, 401 | 'paranoid': false 402 | } 403 | }); 404 | 405 | let stub = sinonSandbox.stub(SequelizeMocking, 'create').callsFake(() => Promise.resolve(mockedSequelizeInstance)); 406 | let stub2 = sinonSandbox.stub(SequelizeMocking, 'loadFixtureFile').callsFake(() => Promise.resolve()); 407 | 408 | return SequelizeMocking 409 | .createAndLoadFixtureFile(sequelizeInstance, 'a/path', { 'logging': false }) 410 | .then(function () { 411 | expect(stub2.called).to.be.true; 412 | expect(stub2.calledOnce).to.be.true; 413 | expect(stub2.calledWith(mockedSequelizeInstance, 'a/path', { 'logging': false })).to.be.true; 414 | }); 415 | }); 416 | 417 | it('return a Promise with the mocked sequelize instance', function () { 418 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 419 | 'host': 'localhost', 420 | 'dialect': 'sqlite', 421 | 'storage': ':memory:', 422 | 'define': { 423 | 'timestamps': false, 424 | 'paranoid': false 425 | } 426 | }); 427 | 428 | let mockedSequelizeInstance = new Sequelize('mocked-database', 'mysqlUserName', 'mysqlUserPassword', { 429 | 'host': 'localhost', 430 | 'dialect': 'sqlite', 431 | 'storage': ':memory:', 432 | 'define': { 433 | 'timestamps': false, 434 | 'paranoid': false 435 | } 436 | }); 437 | 438 | let stub = sinonSandbox.stub(SequelizeMocking, 'create').callsFake(() => Promise.resolve(mockedSequelizeInstance)); 439 | let stub2 = sinonSandbox.stub(SequelizeMocking, 'loadFixtureFile').callsFake(() => Promise.resolve()); 440 | 441 | return SequelizeMocking 442 | .createAndLoadFixtureFile(sequelizeInstance, 'a/path', { 'logging': false }) 443 | .then(function (mockedSequelize) { 444 | expect(mockedSequelize).equals(mockedSequelizeInstance); 445 | }); 446 | }); 447 | }); 448 | 449 | describe('and the method "copyCurrentModels" should ', function (){ 450 | it('exist', function () { 451 | expect(SequelizeMocking.copyCurrentModels).to.exist; 452 | }); 453 | 454 | it('copy the models of the first sequelize instance into the second one', function () { 455 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 456 | 'host': 'localhost', 457 | 'dialect': 'sqlite', 458 | 'storage': ':memory:' 459 | }); 460 | 461 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 462 | 'host': 'localhost', 463 | 'dialect': 'mysql', 464 | 'define': { 465 | 'engine': 'MYISAM', 466 | 'timestamps': false, 467 | 'paranoid': false 468 | }, 469 | 'pool': { 470 | 'max': 5, 471 | 'min': 0, 472 | 'idle': 10000 473 | } 474 | }); 475 | 476 | sequelizeInstance.define('myModel', { 477 | 'id': { 478 | 'type': Sequelize.INTEGER, 479 | 'autoIncrement': true, 480 | 'primaryKey': true 481 | }, 482 | 'description': Sequelize.TEXT 483 | }); 484 | 485 | expect(sequelizeInstance.modelManager.all.length).equals(1); 486 | expect(mockedSequelizeInstance.modelManager.all.length).equals(0); 487 | 488 | SequelizeMocking.copyCurrentModels(sequelizeInstance, mockedSequelizeInstance); 489 | 490 | expect(sequelizeInstance.modelManager.all.length).equals(1); 491 | expect(mockedSequelizeInstance.modelManager.all.length).equals(1); 492 | expect(sequelizeInstance.modelManager.all[0]).not.equals(mockedSequelizeInstance.modelManager.all[0]); 493 | }); 494 | 495 | it('use the "copyModel" function', function () { 496 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 497 | 'host': 'localhost', 498 | 'dialect': 'sqlite', 499 | 'storage': ':memory:' 500 | }); 501 | 502 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 503 | 'host': 'localhost', 504 | 'dialect': 'mysql', 505 | 'define': { 506 | 'engine': 'MYISAM', 507 | 'timestamps': false, 508 | 'paranoid': false 509 | }, 510 | 'pool': { 511 | 'max': 5, 512 | 'min': 0, 513 | 'idle': 10000 514 | } 515 | }); 516 | 517 | let MyModel = sequelizeInstance.define('myModel', { 518 | 'id': { 519 | 'type': Sequelize.INTEGER, 520 | 'autoIncrement': true, 521 | 'primaryKey': true 522 | }, 523 | 'description': Sequelize.TEXT 524 | }); 525 | 526 | 527 | let spyCopyModel = sinonSandbox.spy(SequelizeMocking, 'copyModel'); 528 | SequelizeMocking.copyCurrentModels(sequelizeInstance, mockedSequelizeInstance); 529 | 530 | spyCopyModel.restore(); 531 | expect(spyCopyModel.called).to.be.true; 532 | expect(spyCopyModel.calledOnce).to.be.true; 533 | expect(spyCopyModel.calledWith(mockedSequelizeInstance, MyModel)).to.be.true; 534 | }); 535 | }); 536 | 537 | describe('and the method "hookNewModel" should ', function () { 538 | it('exist', function () { 539 | expect(SequelizeMocking.hookNewModel).to.exist; 540 | }); 541 | 542 | it('listen the "afterDefine" event', function () { 543 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 544 | 'host': 'localhost', 545 | 'dialect': 'sqlite', 546 | 'storage': ':memory:', 547 | 'define': { 548 | 'timestamps': false, 549 | 'paranoid': false 550 | } 551 | }); 552 | 553 | let mockedSequelizeInstance = new Sequelize('mocked-database', 'mysqlUserName', 'mysqlUserPassword', { 554 | 'host': 'localhost', 555 | 'dialect': 'sqlite', 556 | 'storage': ':memory:', 557 | 'define': { 558 | 'timestamps': false, 559 | 'paranoid': false 560 | } 561 | }); 562 | 563 | let spy = sinonSandbox.spy(sequelizeInstance, 'addHook'); 564 | SequelizeMocking.hookNewModel(sequelizeInstance, mockedSequelizeInstance); 565 | expect(spy.called).to.be.true; 566 | expect(spy.calledOnce).to.be.true; 567 | expect(spy.calledWith('afterDefine', 'sequelizeMockAfterDefine', sinon.match.func)).to.be.true; 568 | }); 569 | 570 | it('should call "copyModel" when a new model is added', function () { 571 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 572 | 'host': 'localhost', 573 | 'dialect': 'sqlite', 574 | 'storage': ':memory:', 575 | 'define': { 576 | 'timestamps': false, 577 | 'paranoid': false 578 | } 579 | }); 580 | 581 | let mockedSequelizeInstance = new Sequelize('mocked-database', 'mysqlUserName', 'mysqlUserPassword', { 582 | 'host': 'localhost', 583 | 'dialect': 'sqlite', 584 | 'storage': ':memory:', 585 | 'define': { 586 | 'timestamps': false, 587 | 'paranoid': false 588 | } 589 | }); 590 | 591 | let spy = sinonSandbox.spy(SequelizeMocking, 'copyModel'); 592 | SequelizeMocking.hookNewModel(sequelizeInstance, mockedSequelizeInstance); 593 | 594 | let MyModel = sequelizeInstance.define('myModel', { 595 | 'id': { 596 | 'type': Sequelize.INTEGER, 597 | 'autoIncrement': true, 598 | 'primaryKey': true 599 | }, 600 | 'description': Sequelize.TEXT 601 | }); 602 | 603 | expect(spy.called).to.be.true; 604 | expect(spy.calledOnce).to.be.true; 605 | expect(spy.calledWith(mockedSequelizeInstance, MyModel)).to.be.true; 606 | }); 607 | 608 | it('should call "modifyModelReference" when a new model is added', function () { 609 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 610 | 'host': 'localhost', 611 | 'dialect': 'sqlite', 612 | 'storage': ':memory:', 613 | 'define': { 614 | 'timestamps': false, 615 | 'paranoid': false 616 | } 617 | }); 618 | 619 | let mockedSequelizeInstance = new Sequelize('mocked-database', 'mysqlUserName', 'mysqlUserPassword', { 620 | 'host': 'localhost', 621 | 'dialect': 'sqlite', 622 | 'storage': ':memory:', 623 | 'define': { 624 | 'timestamps': false, 625 | 'paranoid': false 626 | } 627 | }); 628 | 629 | let spy = sinonSandbox.spy(SequelizeMocking, 'modifyModelReference'); 630 | SequelizeMocking.hookNewModel(sequelizeInstance, mockedSequelizeInstance); 631 | 632 | sequelizeInstance.define('myModel', { 633 | 'id': { 634 | 'type': Sequelize.INTEGER, 635 | 'autoIncrement': true, 636 | 'primaryKey': true 637 | }, 638 | 'description': Sequelize.TEXT 639 | }); 640 | 641 | expect(spy.called).to.be.true; 642 | expect(spy.calledOnce).to.be.true; 643 | expect(spy.calledWith(mockedSequelizeInstance, sinon.match.any)).to.be.true; 644 | }); 645 | 646 | it('should use the "logging" option', function () { 647 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 648 | 'host': 'localhost', 649 | 'dialect': 'sqlite', 650 | 'storage': ':memory:', 651 | 'define': { 652 | 'timestamps': false, 653 | 'paranoid': false 654 | } 655 | }); 656 | 657 | let mockedSequelizeInstance = new Sequelize('mocked-database', 'mysqlUserName', 'mysqlUserPassword', { 658 | 'host': 'localhost', 659 | 'dialect': 'sqlite', 660 | 'storage': ':memory:', 661 | 'define': { 662 | 'timestamps': false, 663 | 'paranoid': false 664 | } 665 | }); 666 | 667 | let fakeObject = { 668 | 'sync': () => Promise.resolve() 669 | }; 670 | let stub = sinonSandbox.stub(SequelizeMocking, 'modifyModelReference').callsFake(() => fakeObject); 671 | let spy = sinonSandbox.stub(console, 'log'); 672 | 673 | SequelizeMocking.hookNewModel(sequelizeInstance, mockedSequelizeInstance, { 'logging': false }); 674 | 675 | sequelizeInstance.define('myModel', { 676 | 'id': { 677 | 'type': Sequelize.INTEGER, 678 | 'autoIncrement': true, 679 | 'primaryKey': true 680 | }, 681 | 'description': Sequelize.TEXT 682 | }); 683 | 684 | expect(spy.called).to.be.false; 685 | }); 686 | }); 687 | 688 | describe('and the method "loadFixtureFile" should ', function () { 689 | it('exist', function () { 690 | expect(SequelizeMocking.loadFixtureFile).to.exist; 691 | }); 692 | 693 | it('call the map models function', function () { 694 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 695 | 'host': 'localhost', 696 | 'dialect': 'mysql', 697 | 'define': { 698 | 'engine': 'MYISAM', 699 | 'timestamps': false, 700 | 'paranoid': false 701 | }, 702 | 'pool': { 703 | 'max': 5, 704 | 'min': 0, 705 | 'idle': 10000 706 | } 707 | }); 708 | 709 | sequelizeInstance.define('myModel', { 710 | 'id': { 711 | 'type': Sequelize.INTEGER, 712 | 'autoIncrement': true, 713 | 'primaryKey': true 714 | }, 715 | 'description': Sequelize.TEXT 716 | }); 717 | 718 | let stub = sinonSandbox.stub(sequelizeFixtures, 'loadFile').callsFake(() => Promise.resolve()); 719 | let spy = sinonSandbox.spy(SequelizeMocking, 'mapModels'); 720 | 721 | SequelizeMocking.loadFixtureFile(sequelizeInstance, '/a/path/for/json/file'); 722 | expect(spy.called).to.be.true; 723 | expect(spy.calledOnce).to.be.true; 724 | expect(spy.calledWith(sequelizeInstance)).to.be.true; 725 | }); 726 | 727 | it('load the fixture models file and return into the Promise the sequelize instance', function () { 728 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 729 | 'host': 'localhost', 730 | 'dialect': 'sqlite', 731 | 'storage': ':memory:', 732 | 'define': { 733 | 'timestamps': false, 734 | 'paranoid': false 735 | } 736 | }); 737 | 738 | sequelizeInstance.define('myModel', { 739 | 'id': { 740 | 'type': Sequelize.INTEGER, 741 | 'autoIncrement': true, 742 | 'primaryKey': true 743 | }, 744 | 'description': Sequelize.TEXT 745 | }); 746 | 747 | return sequelizeInstance 748 | .sync() 749 | .then(function () { 750 | return SequelizeMocking 751 | .loadFixtureFile(sequelizeInstance, path.resolve(path.join(__dirname, './my-model-database.json'))); 752 | }) 753 | .then(function (sequelize) { 754 | expect(sequelize).equals(sequelizeInstance); 755 | }); 756 | }); 757 | 758 | it('Should detect load the fixture models files from array and return into the Promise the sequelize instance', function () { 759 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 760 | 'host': 'localhost', 761 | 'dialect': 'sqlite', 762 | 'storage': ':memory:', 763 | 'define': { 764 | 'timestamps': false, 765 | 'paranoid': false 766 | } 767 | }); 768 | 769 | sequelizeInstance.define('myModel', { 770 | 'id': { 771 | 'type': Sequelize.INTEGER, 772 | 'autoIncrement': true, 773 | 'primaryKey': true 774 | }, 775 | 'description': Sequelize.TEXT 776 | }); 777 | 778 | return sequelizeInstance 779 | .sync() 780 | .then(function () { 781 | return SequelizeMocking 782 | .loadFixtureFile(sequelizeInstance, [ 783 | path.resolve(path.join(__dirname, './my-model-database.json')), 784 | path.resolve(path.join(__dirname, './my-model-1-database.json')) 785 | ]); 786 | }) 787 | .then(function (sequelize) { 788 | expect(sequelize).equals(sequelizeInstance); 789 | }); 790 | }); 791 | 792 | it('should not log if the logging option is false', function () { 793 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 794 | 'host': 'localhost', 795 | 'dialect': 'sqlite', 796 | 'storage': ':memory:', 797 | 'define': { 798 | 'timestamps': false, 799 | 'paranoid': false 800 | } 801 | }); 802 | 803 | sequelizeInstance.define('myModel', { 804 | 'id': { 805 | 'type': Sequelize.INTEGER, 806 | 'autoIncrement': true, 807 | 'primaryKey': true 808 | }, 809 | 'description': Sequelize.TEXT 810 | }); 811 | 812 | let spy = sinonSandbox.spy(sequelizeFixtures, 'loadFile'); 813 | let filePath = path.resolve(path.join(__dirname, './my-model-database.json')); 814 | 815 | return sequelizeInstance 816 | .sync() 817 | .then(function () { 818 | return SequelizeMocking 819 | .loadFixtureFile(sequelizeInstance, filePath, { 'logging': false }); 820 | }) 821 | .then(function () { 822 | expect(spy.firstCall.args).deep.equals([ 823 | filePath, 824 | { 825 | 'myModel': sequelizeInstance.modelManager.all[0] 826 | }, 827 | { 828 | 'encoding': 'utf8', 829 | 'log': _.noop 830 | } 831 | ]); 832 | }); 833 | }); 834 | 835 | it('should allow transform the data if specified', function () { 836 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 837 | 'host': 'localhost', 838 | 'dialect': 'sqlite', 839 | 'storage': ':memory:', 840 | 'define': { 841 | 'timestamps': false, 842 | 'paranoid': false 843 | } 844 | }); 845 | 846 | sequelizeInstance.define('myModel', { 847 | 'id': { 848 | 'type': Sequelize.INTEGER, 849 | 'autoIncrement': true, 850 | 'primaryKey': true 851 | }, 852 | 'description': Sequelize.TEXT 853 | }); 854 | 855 | let spy = sinonSandbox.spy(sequelizeFixtures, 'loadFile'); 856 | let filePath = path.resolve(path.join(__dirname, './my-model-database.json')); 857 | 858 | function transformFixtureDataFn(data) { 859 | // Fixtures with negative numbers allow creating data objects 860 | // relative to the time of the import. 861 | if(data.createdAt 862 | && data.createdAt < 0) { 863 | data.createdAt = new Date((new Date()).getTime() + parseFloat(data.createdAt) * 1000 * 60); 864 | } 865 | return data; 866 | } 867 | 868 | return sequelizeInstance 869 | .sync() 870 | .then(function () { 871 | return SequelizeMocking 872 | .loadFixtureFile(sequelizeInstance, filePath, { 'logging': false, 'transformFixtureDataFn': transformFixtureDataFn }); 873 | }) 874 | .then(function () { 875 | expect(spy.firstCall.args).deep.equals([ 876 | filePath, 877 | { 878 | 'myModel': sequelizeInstance.modelManager.all[0] 879 | }, 880 | { 881 | 'encoding': 'utf8', 882 | 'log': _.noop, 883 | 'transformFixtureDataFn': transformFixtureDataFn 884 | } 885 | ]); 886 | }); 887 | }); 888 | }); 889 | 890 | describe('and the method "mapModels" should ', function () { 891 | it('exist', function () { 892 | expect(SequelizeMocking.mapModels).to.exist; 893 | }); 894 | 895 | it('return an empty map if no Sequelize models were defined', function () { 896 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 897 | 'host': 'localhost', 898 | 'dialect': 'mysql', 899 | 'define': { 900 | 'engine': 'MYISAM', 901 | 'timestamps': false, 902 | 'paranoid': false 903 | }, 904 | 'pool': { 905 | 'max': 5, 906 | 'min': 0, 907 | 'idle': 10000 908 | } 909 | }); 910 | 911 | let mapModels = SequelizeMocking.mapModels(sequelizeInstance); 912 | expect(mapModels).not.to.be.undefined; 913 | expect(mapModels).to.be.empty; 914 | }); 915 | 916 | it('return a map with the defined Sequelize model', function () { 917 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 918 | 'host': 'localhost', 919 | 'dialect': 'mysql', 920 | 'define': { 921 | 'engine': 'MYISAM', 922 | 'timestamps': false, 923 | 'paranoid': false 924 | }, 925 | 'pool': { 926 | 'max': 5, 927 | 'min': 0, 928 | 'idle': 10000 929 | } 930 | }); 931 | 932 | sequelizeInstance.define('myModel', { 933 | 'id': { 934 | 'type': Sequelize.INTEGER, 935 | 'autoIncrement': true, 936 | 'primaryKey': true 937 | }, 938 | 'description': Sequelize.TEXT 939 | }); 940 | 941 | let mapModels = SequelizeMocking.mapModels(sequelizeInstance); 942 | expect(mapModels).not.to.be.undefined; 943 | expect(mapModels).deep.equals({ 944 | 'myModel': sequelizeInstance.modelManager.all[0] 945 | }); 946 | }); 947 | 948 | it('return a map with the defined Sequelize models', function () { 949 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 950 | 'host': 'localhost', 951 | 'dialect': 'mysql', 952 | 'define': { 953 | 'engine': 'MYISAM', 954 | 'timestamps': false, 955 | 'paranoid': false 956 | }, 957 | 'pool': { 958 | 'max': 5, 959 | 'min': 0, 960 | 'idle': 10000 961 | } 962 | }); 963 | 964 | sequelizeInstance.define('myModel1', { 965 | 'id': { 966 | 'type': Sequelize.INTEGER, 967 | 'autoIncrement': true, 968 | 'primaryKey': true 969 | }, 970 | 'description': Sequelize.TEXT 971 | }); 972 | 973 | sequelizeInstance.define('myModel2', { 974 | 'id': { 975 | 'type': Sequelize.INTEGER, 976 | 'autoIncrement': true, 977 | 'primaryKey': true 978 | }, 979 | 'description': Sequelize.TEXT 980 | }); 981 | 982 | let mapModels = SequelizeMocking.mapModels(sequelizeInstance); 983 | expect(mapModels).not.to.be.undefined; 984 | expect(mapModels).deep.equals({ 985 | 'myModel1': sequelizeInstance.modelManager.all[0], 986 | 'myModel2': sequelizeInstance.modelManager.all[1] 987 | }); 988 | }); 989 | }); 990 | 991 | describe('and the method "modifyConnection" should ', function () { 992 | it('exist', function () { 993 | expect(SequelizeMocking.modifyConnection).to.exist; 994 | }); 995 | 996 | it('should override the dialect and the connectionManafer', function () { 997 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 998 | 'host': 'localhost', 999 | 'dialect': 'mysql', 1000 | 'define': { 1001 | 'engine': 'MYISAM', 1002 | 'timestamps': false, 1003 | 'paranoid': false 1004 | }, 1005 | 'pool': { 1006 | 'max': 5, 1007 | 'min': 0, 1008 | 'idle': 10000 1009 | } 1010 | }); 1011 | 1012 | let usedDialect = sequelizeInstance.dialect; 1013 | let usedQueryInterface = sequelizeInstance.queryInterface; 1014 | let usedConnectionManager = sequelizeInstance.connectionManager; 1015 | 1016 | let sequelizeInstance2 = new Sequelize('my-database2', 'mysqlUserName', 'mysqlUserPassword', { 1017 | 'host': 'localhost', 1018 | 'dialect': 'sqlite', 1019 | 'storage': ':memory:', 1020 | 'define': { 1021 | 'timestamps': false, 1022 | 'paranoid': false 1023 | } 1024 | }); 1025 | 1026 | SequelizeMocking.modifyConnection(sequelizeInstance, sequelizeInstance2); 1027 | expect(sequelizeInstance.__dialect).to.exist; 1028 | expect(sequelizeInstance.__dialect).equals(usedDialect); 1029 | 1030 | expect(sequelizeInstance.__queryInterface).to.exist; 1031 | expect(sequelizeInstance.__queryInterface).equals(usedQueryInterface); 1032 | 1033 | expect(sequelizeInstance.__connectionManager).to.exist; 1034 | expect(sequelizeInstance.__connectionManager).equals(usedConnectionManager); 1035 | 1036 | expect(sequelizeInstance.dialect === sequelizeInstance2.dialect).to.be.true; 1037 | expect(sequelizeInstance.queryInterface === sequelizeInstance2.queryInterface).to.be.true; 1038 | expect(sequelizeInstance.connectionManager === sequelizeInstance2.connectionManager).to.be.true; 1039 | }); 1040 | }); 1041 | 1042 | describe('and the method "modifyModelReference" should ', function () { 1043 | it('exist', function () { 1044 | expect(SequelizeMocking.modifyModelReference).to.exist; 1045 | }); 1046 | 1047 | it('should override the sequelize property of the specified model with the specified sequelize instance', function () { 1048 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 1049 | 'host': 'localhost', 1050 | 'dialect': 'sqlite', 1051 | 'storage': ':memory:', 1052 | 'define': { 1053 | 'timestamps': false, 1054 | 'paranoid': false 1055 | } 1056 | }); 1057 | 1058 | let sequelizeInstance2 = new Sequelize('my-database2', 'mysqlUserName', 'mysqlUserPassword', { 1059 | 'host': 'localhost', 1060 | 'dialect': 'sqlite', 1061 | 'storage': ':memory:', 1062 | 'define': { 1063 | 'timestamps': false, 1064 | 'paranoid': false 1065 | } 1066 | }); 1067 | 1068 | let MyModel = sequelizeInstance.define('myModel', { 1069 | 'id': { 1070 | 'type': Sequelize.INTEGER, 1071 | 'autoIncrement': true, 1072 | 'primaryKey': true 1073 | }, 1074 | 'description': Sequelize.TEXT 1075 | }); 1076 | 1077 | expect(MyModel.sequelize).equals(sequelizeInstance); 1078 | SequelizeMocking.modifyModelReference(sequelizeInstance2, MyModel); 1079 | expect(MyModel.sequelize).equals(sequelizeInstance2); 1080 | }); 1081 | 1082 | it('should override the model manager based on the specified sequelize instance', function () { 1083 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 1084 | 'host': 'localhost', 1085 | 'dialect': 'sqlite', 1086 | 'storage': ':memory:', 1087 | 'define': { 1088 | 'timestamps': false, 1089 | 'paranoid': false 1090 | } 1091 | }); 1092 | 1093 | let sequelizeInstance2 = new Sequelize('my-database2', 'mysqlUserName', 'mysqlUserPassword', { 1094 | 'host': 'localhost', 1095 | 'dialect': 'sqlite', 1096 | 'storage': ':memory:', 1097 | 'define': { 1098 | 'timestamps': false, 1099 | 'paranoid': false 1100 | } 1101 | }); 1102 | 1103 | let MyModel = sequelizeInstance.define('myModel', { 1104 | 'id': { 1105 | 'type': Sequelize.INTEGER, 1106 | 'autoIncrement': true, 1107 | 'primaryKey': true 1108 | }, 1109 | 'description': Sequelize.TEXT 1110 | }); 1111 | 1112 | expect(MyModel.sequelize).equals(sequelizeInstance); 1113 | SequelizeMocking.modifyModelReference(sequelizeInstance2, MyModel); 1114 | expect(MyModel.sequelize).equals(sequelizeInstance2); 1115 | }); 1116 | }); 1117 | 1118 | describe('and the method "modifyModelReferences" should ', function (){ 1119 | it('exist', function () { 1120 | expect(SequelizeMocking.modifyModelReferences).to.exist; 1121 | }); 1122 | 1123 | it('override the models of the first sequelize instance', function () { 1124 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1125 | 'host': 'localhost', 1126 | 'dialect': 'sqlite', 1127 | 'storage': ':memory:' 1128 | }); 1129 | 1130 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 1131 | 'host': 'localhost', 1132 | 'dialect': 'mysql', 1133 | 'define': { 1134 | 'engine': 'MYISAM', 1135 | 'timestamps': false, 1136 | 'paranoid': false 1137 | }, 1138 | 'pool': { 1139 | 'max': 5, 1140 | 'min': 0, 1141 | 'idle': 10000 1142 | } 1143 | }); 1144 | 1145 | let MyModel = sequelizeInstance.define('myModel', { 1146 | 'id': { 1147 | 'type': Sequelize.INTEGER, 1148 | 'autoIncrement': true, 1149 | 'primaryKey': true 1150 | }, 1151 | 'description': Sequelize.TEXT 1152 | }); 1153 | 1154 | SequelizeMocking.modifyModelReferences(sequelizeInstance, mockedSequelizeInstance); 1155 | expect(MyModel.sequelize).equals(mockedSequelizeInstance); 1156 | }); 1157 | 1158 | it('use the "modifyModelReference" function', function () { 1159 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1160 | 'host': 'localhost', 1161 | 'dialect': 'sqlite', 1162 | 'storage': ':memory:' 1163 | }); 1164 | 1165 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 1166 | 'host': 'localhost', 1167 | 'dialect': 'sqlite', 1168 | 'storage': ':memory:', 1169 | 'define': { 1170 | 'timestamps': false, 1171 | 'paranoid': false 1172 | }, 1173 | 'pool': { 1174 | 'max': 5, 1175 | 'min': 0, 1176 | 'idle': 10000 1177 | } 1178 | }); 1179 | 1180 | let MyModel = sequelizeInstance.define('myModel', { 1181 | 'id': { 1182 | 'type': Sequelize.INTEGER, 1183 | 'autoIncrement': true, 1184 | 'primaryKey': true 1185 | }, 1186 | 'description': Sequelize.TEXT 1187 | }); 1188 | 1189 | 1190 | let spyCopyModel = sinonSandbox.spy(SequelizeMocking, 'modifyModelReference'); 1191 | SequelizeMocking.modifyModelReferences(sequelizeInstance, mockedSequelizeInstance); 1192 | 1193 | expect(spyCopyModel.called).to.be.true; 1194 | expect(spyCopyModel.calledOnce).to.be.true; 1195 | expect(spyCopyModel.calledWith(mockedSequelizeInstance, MyModel)).to.be.true; 1196 | }); 1197 | }); 1198 | 1199 | describe('and the method "restore" should ', function () { 1200 | it('exist', function () { 1201 | expect(SequelizeMocking.restore).to.exist; 1202 | }); 1203 | 1204 | it('should call "unhookNewModel" method', function () { 1205 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1206 | 'host': 'localhost', 1207 | 'dialect': 'sqlite', 1208 | 'storage': ':memory:' 1209 | }); 1210 | 1211 | let spy = sinonSandbox.spy(SequelizeMocking, 'unhookNewModel'); 1212 | SequelizeMocking.restore(mockedSequelizeInstance); 1213 | expect(spy.called).to.be.true; 1214 | expect(spy.calledOnce).to.be.true; 1215 | expect(spy.calledWith(mockedSequelizeInstance)).to.be.true; 1216 | }); 1217 | 1218 | it('should call "modifyModelReferences" method if the sequelize instance is a mocked one', function () { 1219 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1220 | 'host': 'localhost', 1221 | 'dialect': 'sqlite', 1222 | 'storage': ':memory:' 1223 | }); 1224 | 1225 | let sequelizeInstance = new Sequelize('my-database', null, null, { 1226 | 'host': 'localhost', 1227 | 'dialect': 'sqlite', 1228 | 'storage': ':memory:' 1229 | }); 1230 | 1231 | mockedSequelizeInstance.__originalSequelize = sequelizeInstance; 1232 | 1233 | let spy = sinonSandbox.spy(SequelizeMocking, 'modifyModelReferences'); 1234 | SequelizeMocking.restore(mockedSequelizeInstance); 1235 | expect(spy.called).to.be.true; 1236 | expect(spy.calledOnce).to.be.true; 1237 | expect(spy.calledWith(mockedSequelizeInstance, sequelizeInstance)).to.be.true; 1238 | }); 1239 | 1240 | it('should call "modifyConnection" method if the sequelize instance is a mocked one', function () { 1241 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1242 | 'host': 'localhost', 1243 | 'dialect': 'sqlite', 1244 | 'storage': ':memory:' 1245 | }); 1246 | 1247 | let sequelizeInstance = new Sequelize('my-database', null, null, { 1248 | 'host': 'localhost', 1249 | 'dialect': 'sqlite', 1250 | 'storage': ':memory:' 1251 | }); 1252 | 1253 | mockedSequelizeInstance.__originalSequelize = sequelizeInstance; 1254 | mockedSequelizeInstance.__dialect = sequelizeInstance.dialect; 1255 | mockedSequelizeInstance.__queryInterface = sequelizeInstance.queryInterface; 1256 | mockedSequelizeInstance.__connectionManager = sequelizeInstance.connectionManager; 1257 | 1258 | let spy = sinonSandbox.spy(SequelizeMocking, 'modifyConnection'); 1259 | SequelizeMocking.restore(mockedSequelizeInstance); 1260 | expect(spy.called).to.be.true; 1261 | expect(spy.calledOnce).to.be.true; 1262 | expect(spy.calledWith(mockedSequelizeInstance, sequelizeInstance)).to.be.true; 1263 | }); 1264 | 1265 | it('should remove "__originalSequelize" property', function () { 1266 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1267 | 'host': 'localhost', 1268 | 'dialect': 'sqlite', 1269 | 'storage': ':memory:' 1270 | }); 1271 | 1272 | mockedSequelizeInstance.__originalSequelize = new Sequelize('my-database', null, null, { 1273 | 'host': 'localhost', 1274 | 'dialect': 'sqlite', 1275 | 'storage': ':memory:' 1276 | }); 1277 | 1278 | SequelizeMocking.restore(mockedSequelizeInstance); 1279 | expect(mockedSequelizeInstance.__originalSequelize).not.to.exist; 1280 | }); 1281 | 1282 | it('should remove "__dialect" and "__connectionManager" properties', function () { 1283 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1284 | 'host': 'localhost', 1285 | 'dialect': 'sqlite', 1286 | 'storage': ':memory:' 1287 | }); 1288 | 1289 | mockedSequelizeInstance.__originalSequelize = new Sequelize('my-database', null, null, { 1290 | 'host': 'localhost', 1291 | 'dialect': 'sqlite', 1292 | 'storage': ':memory:' 1293 | }); 1294 | 1295 | SequelizeMocking.restore(mockedSequelizeInstance); 1296 | expect(mockedSequelizeInstance.__dialect).not.to.exist; 1297 | expect(mockedSequelizeInstance.__connectionManager).not.to.exist; 1298 | }); 1299 | 1300 | it('should flush the mocked sequelize database', function () { 1301 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1302 | 'host': 'localhost', 1303 | 'dialect': 'sqlite', 1304 | 'storage': ':memory:' 1305 | }); 1306 | 1307 | let spyGetQueryInterface = sinonSandbox.spy(mockedSequelizeInstance, 'getQueryInterface'); 1308 | let spyDropAllTables = sinonSandbox.spy(mockedSequelizeInstance.getQueryInterface(), 'dropAllTables'); 1309 | 1310 | return SequelizeMocking 1311 | .restore(mockedSequelizeInstance) 1312 | .then(function () { 1313 | expect(spyGetQueryInterface.called).to.be.true; 1314 | expect(spyDropAllTables.called).to.be.true; 1315 | expect(spyDropAllTables.calledWith({ 'logging': true })).to.be.true; 1316 | }); 1317 | }); 1318 | 1319 | it('should use the "logging" option', function () { 1320 | let mockedSequelizeInstance = new Sequelize('mocked-database', null, null, { 1321 | 'host': 'localhost', 1322 | 'dialect': 'sqlite', 1323 | 'storage': ':memory:' 1324 | }); 1325 | 1326 | let spyDropAllTables = sinonSandbox.spy(mockedSequelizeInstance.getQueryInterface(), 'dropAllTables'); 1327 | 1328 | return SequelizeMocking 1329 | .restore(mockedSequelizeInstance, { 'logging': false }) 1330 | .then(function () { 1331 | expect(spyDropAllTables.called).to.be.true; 1332 | expect(spyDropAllTables.calledOnce).to.be.true; 1333 | expect(spyDropAllTables.calledWith({ 'logging': false })).to.be.true; 1334 | }); 1335 | }); 1336 | }); 1337 | 1338 | describe('and the method "unhookNewModel" should ', function () { 1339 | it('exist', function () { 1340 | expect(SequelizeMocking.unhookNewModel).to.exist; 1341 | }); 1342 | 1343 | it('do nothing if the sequelize was not mocked', function () { 1344 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 1345 | 'host': 'localhost', 1346 | 'dialect': 'sqlite', 1347 | 'storage': ':memory:', 1348 | 'define': { 1349 | 'timestamps': false, 1350 | 'paranoid': false 1351 | } 1352 | }); 1353 | 1354 | expect(function () { 1355 | SequelizeMocking.unhookNewModel(sequelizeInstance); 1356 | }).not.to.throw; 1357 | }); 1358 | 1359 | it('remove the hook on the original sequelize on the mocked sequelize', function () { 1360 | let sequelizeInstance = new Sequelize('my-database', 'mysqlUserName', 'mysqlUserPassword', { 1361 | 'host': 'localhost', 1362 | 'dialect': 'sqlite', 1363 | 'storage': ':memory:', 1364 | 'define': { 1365 | 'timestamps': false, 1366 | 'paranoid': false 1367 | } 1368 | }); 1369 | 1370 | sequelizeInstance.__originalSequelize = { 1371 | 'removeHook': function (eventName) { 1372 | 1373 | } 1374 | }; 1375 | 1376 | let spy = sinonSandbox.spy(sequelizeInstance.__originalSequelize, 'removeHook'); 1377 | 1378 | SequelizeMocking.unhookNewModel(sequelizeInstance); 1379 | expect(spy.called).to.be.true; 1380 | expect(spy.calledOnce).to.be.true; 1381 | expect(spy.calledWith('afterDefine')).to.be.true; 1382 | }); 1383 | }); 1384 | }); 1385 | --------------------------------------------------------------------------------