From 9e356fbbbc7db24ee59af036e5b47b3322c095dd Mon Sep 17 00:00:00 2001 From: "D. C. Rockwell" Date: Mon, 21 Sep 2015 14:49:20 -0600 Subject: [PATCH] mock interface modified. now adding save, fetch and delete mocks --- es5/lib/model/index.js | 6 ++ es5/lib/modelFinder.js | 136 ++++++++++++++++------------- es5/spec/model/mockResults.spec.js | 74 ---------------- es6/lib/model/index.js | 5 ++ es6/lib/modelFinder.js | 73 ++++++++++------ es6/spec/model/mockResults.spec.js | 52 ----------- 6 files changed, 131 insertions(+), 215 deletions(-) delete mode 100644 es5/spec/model/mockResults.spec.js delete mode 100644 es6/spec/model/mockResults.spec.js diff --git a/es5/lib/model/index.js b/es5/lib/model/index.js index f5d3e18..d55d02b 100644 --- a/es5/lib/model/index.js +++ b/es5/lib/model/index.js @@ -514,6 +514,12 @@ var Model = (function () { var modelQuery = new _modelFinderJs2["default"](this.database); return modelQuery.count(this); } + }, { + key: "mock", + get: function get() { + var modelQuery = new _modelFinderJs2["default"](this.database); + return modelQuery.mock(this); + } }]); return Model; diff --git a/es5/lib/modelFinder.js b/es5/lib/modelFinder.js index 277e55f..2cd83ba 100644 --- a/es5/lib/modelFinder.js +++ b/es5/lib/modelFinder.js @@ -42,7 +42,7 @@ var ModelFinder = (function () { var query = new ModelQuery(ModelConstructor, { database: (0, _incognito2["default"])(this).database }); - query.find(); + query.find; return query; } @@ -52,7 +52,18 @@ var ModelFinder = (function () { var query = new ModelQuery(ModelConstructor, { database: (0, _incognito2["default"])(this).database }); - query.count(); + query.count; + + return query; + } + }, { + key: "mock", + value: function mock(ModelConstructor) { + var query = new ModelQuery(ModelConstructor, { + database: (0, _incognito2["default"])(this).database + }); + + query.mock; return query; } @@ -104,18 +115,6 @@ var ModelQuery = (function () { return chainString; } - }, { - key: "mockResults", - value: function mockResults(mockValue) { - var _ = (0, _incognito2["default"])(this); - - _.ModelConstructor.mocks[this.toString()] = { - query: this, - value: mockValue - }; - - return this; - } }, { key: "equalTo", value: function equalTo(query) { @@ -151,38 +150,6 @@ var ModelQuery = (function () { return isEqual; } - }, { - key: "find", - value: function find() { - var _ = (0, _incognito2["default"])(this); - - var tempModel = new _.ModelConstructor(); - - if (_.database) { - _.query = _.database.select("*").from(tempModel.tableName); - } - - this[addChain](".find"); - - return this; - } - }, { - key: "count", - value: function count() { - var _ = (0, _incognito2["default"])(this); - - this.countResults = true; - - var tempModel = new _.ModelConstructor(); - - if (_.database) { - _.query = _.database.select(null).count("* AS rowCount").from(tempModel.tableName); - } - - this[addChain](".count"); - - return this; - } }, { key: "where", value: function where() { @@ -288,25 +255,36 @@ var ModelQuery = (function () { } }, { key: "results", - value: function results(callback) { + value: function results(callbackOrMockValue) { var _ = (0, _incognito2["default"])(this); - var useMock = false; - var mockValue = undefined; + if (_.isMockDefinition) { + var mockValue = callbackOrMockValue; - for (var chainString in _.ModelConstructor.mocks) { - var mock = _.ModelConstructor.mocks[chainString]; - if (this.equalTo(mock.query)) { - useMock = true; - mockValue = mock.value; - break; + _.ModelConstructor.mocks[this.toString()] = { + query: this, + value: mockValue + }; + } else { + var callback = callbackOrMockValue; + + var useMock = false; + var mockValue = undefined; + + for (var chainString in _.ModelConstructor.mocks) { + var mock = _.ModelConstructor.mocks[chainString]; + if (this.equalTo(mock.query)) { + useMock = true; + mockValue = mock.value; + break; + } } - } - if (useMock) { - callback(null, mockValue); - } else { - this[callDatabase](callback); + if (useMock) { + callback(null, mockValue); + } else { + this[callDatabase](callback); + } } } }, { @@ -420,6 +398,12 @@ var ModelQuery = (function () { } } } + }, { + key: "mock", + get: function get() { + (0, _incognito2["default"])(this).isMockDefinition = true; + return this; + } }, { key: "chain", get: function get() { @@ -450,6 +434,38 @@ var ModelQuery = (function () { get: function get() { this[addChain](".all"); + return this; + } + }, { + key: "find", + get: function get() { + var _ = (0, _incognito2["default"])(this); + + var tempModel = new _.ModelConstructor(); + + if (_.database) { + _.query = _.database.select("*").from(tempModel.tableName); + } + + this[addChain](".find"); + + return this; + } + }, { + key: "count", + get: function get() { + var _ = (0, _incognito2["default"])(this); + + this.countResults = true; + + var tempModel = new _.ModelConstructor(); + + if (_.database) { + _.query = _.database.select(null).count("* AS rowCount").from(tempModel.tableName); + } + + this[addChain](".count"); + return this; } }]); diff --git a/es5/spec/model/mockResults.spec.js b/es5/spec/model/mockResults.spec.js deleted file mode 100644 index 7a17dad..0000000 --- a/es5/spec/model/mockResults.spec.js +++ /dev/null @@ -1,74 +0,0 @@ -"use strict"; - -var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } - -var _ = require("../../../"); - -var _2 = _interopRequireDefault(_); - -describe(".mockResults(callback)", function () { - - var mockedValue = undefined; - - var User = (function (_Model) { - _inherits(User, _Model); - - function User() { - _classCallCheck(this, User); - - _get(Object.getPrototypeOf(User.prototype), "constructor", this).apply(this, arguments); - } - - return User; - })(_2["default"]); - - beforeEach(function () { - mockedValue = { id: 1, name: "Bob" }; - User.find.one.where("id", 1).mockResults(mockedValue); - }); - - it("should return itself so that chaining is possible", function () { - var query = User.find; - query.mockResults().should.eql(query); - }); - - it("should return the mocked value on subsequent calls of the same type", function (done) { - User.find.one.where("id", 1).results(function (error, data) { - data.should.eql(mockedValue); - done(); - }); - }); - - it("should return the mocked value when using regex values", function (done) { - User.find.one.where("createdAt", /.*/).mockResults(mockedValue); - - User.find.one.where("createdAt", "2014-10-08 10:16:34").results(function (error, data) { - data.should.eql(mockedValue); - done(); - }); - }); - - it("should not matter which order the chain is called in", function () { - User.find.where("id", 1).one.results(function (error, data) { - data.should.eql(mockedValue); - }); - }); - - it("should not affect calls with different parameters", function () { - (function () { - User.find.one.where("id", 2).results(function () {}); - }).should["throw"]("Cannot find models without a database set."); - }); - - it("should not affect calls with a different chain", function () { - (function () { - User.find.all.where("id", 1).results(function () {}); - }).should["throw"]("Cannot find models without a database set."); - }); -}); \ No newline at end of file diff --git a/es6/lib/model/index.js b/es6/lib/model/index.js index 01c3130..cdf6008 100644 --- a/es6/lib/model/index.js +++ b/es6/lib/model/index.js @@ -115,6 +115,11 @@ export default class Model { return modelQuery.count(this); } + static get mock() { + let modelQuery = new ModelFinder(this.database); + return modelQuery.mock(this); + } + /** * INSTANCE INTERFACE */ diff --git a/es6/lib/modelFinder.js b/es6/lib/modelFinder.js index e1e25d5..3acb73a 100644 --- a/es6/lib/modelFinder.js +++ b/es6/lib/modelFinder.js @@ -14,7 +14,7 @@ export default class ModelFinder { const query = new ModelQuery(ModelConstructor, { database: privateData(this).database }); - query.find(); + query.find; return query; } @@ -23,7 +23,17 @@ export default class ModelFinder { const query = new ModelQuery(ModelConstructor, { database: privateData(this).database }); - query.count(); + query.count; + + return query; + } + + mock(ModelConstructor) { + const query = new ModelQuery(ModelConstructor, { + database: privateData(this).database + }); + + query.mock; return query; } @@ -46,6 +56,11 @@ export class ModelQuery { _.ModelConstructor.mocks = _.ModelConstructor.mocks || {}; } + get mock() { + privateData(this).isMockDefinition = true; + return this; + } + toString() { const _ = privateData(this); @@ -76,17 +91,6 @@ export class ModelQuery { return chainString; } - mockResults(mockValue) { - const _ = privateData(this); - - _.ModelConstructor.mocks[this.toString()] = { - query: this, - value: mockValue - }; - - return this; - } - equalTo(query) { const ourChain = this.chain; const theirChain = query.chain; @@ -153,7 +157,7 @@ export class ModelQuery { return this; } - find() { + get find() { const _ = privateData(this); const tempModel = new _.ModelConstructor(); @@ -169,7 +173,7 @@ export class ModelQuery { return this; } - count() { + get count() { const _ = privateData(this); this.countResults = true; @@ -248,25 +252,36 @@ export class ModelQuery { return this; } - results(callback) { + results(callbackOrMockValue) { const _ = privateData(this); - let useMock = false; - let mockValue; + if (_.isMockDefinition) { + const mockValue = callbackOrMockValue; + + _.ModelConstructor.mocks[this.toString()] = { + query: this, + value: mockValue + }; + } else { + const callback = callbackOrMockValue; + + let useMock = false; + let mockValue; - for (let chainString in _.ModelConstructor.mocks) { - const mock = _.ModelConstructor.mocks[chainString]; - if (this.equalTo(mock.query)) { - useMock = true; - mockValue = mock.value; - break; + for (let chainString in _.ModelConstructor.mocks) { + const mock = _.ModelConstructor.mocks[chainString]; + if (this.equalTo(mock.query)) { + useMock = true; + mockValue = mock.value; + break; + } } - } - if (useMock) { - callback(null, mockValue); - } else { - this[callDatabase](callback); + if (useMock) { + callback(null, mockValue); + } else { + this[callDatabase](callback); + } } } diff --git a/es6/spec/model/mockResults.spec.js b/es6/spec/model/mockResults.spec.js deleted file mode 100644 index 4dfa95e..0000000 --- a/es6/spec/model/mockResults.spec.js +++ /dev/null @@ -1,52 +0,0 @@ -import Model from "../../../"; - -describe(".mockResults(callback)", () => { - - let mockedValue; - - class User extends Model {} - - beforeEach(() => { - mockedValue = {id: 1, name: "Bob"}; - User.find.one.where("id", 1).mockResults(mockedValue); - }); - - it("should return itself so that chaining is possible", () => { - const query = User.find; - query.mockResults().should.eql(query); - }); - - it("should return the mocked value on subsequent calls of the same type", done => { - User.find.one.where("id", 1).results((error, data) => { - data.should.eql(mockedValue); - done(); - }); - }); - - it("should return the mocked value when using regex values", done => { - User.find.one.where("createdAt", /.*/).mockResults(mockedValue); - - User.find.one.where("createdAt", "2014-10-08 10:16:34").results((error, data) => { - data.should.eql(mockedValue); - done(); - }); - }); - - it("should not matter which order the chain is called in", () => { - User.find.where("id", 1).one.results((error, data) => { - data.should.eql(mockedValue); - }); - }); - - it("should not affect calls with different parameters", () => { - () => { - User.find.one.where("id", 2).results(() => {}); - }.should.throw("Cannot find models without a database set."); - }); - - it("should not affect calls with a different chain", () => { - () => { - User.find.all.where("id", 1).results(() => {}); - }.should.throw("Cannot find models without a database set."); - }); -});