Skip to content

Commit

Permalink
mock interface modified. now adding save, fetch and delete mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrockwell committed Sep 21, 2015
1 parent d56b426 commit 9e356fb
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 215 deletions.
6 changes: 6 additions & 0 deletions es5/lib/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
136 changes: 76 additions & 60 deletions es5/lib/modelFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var ModelFinder = (function () {
var query = new ModelQuery(ModelConstructor, {
database: (0, _incognito2["default"])(this).database
});
query.find();
query.find;

return query;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
}
}, {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
}
}]);
Expand Down
74 changes: 0 additions & 74 deletions es5/spec/model/mockResults.spec.js

This file was deleted.

5 changes: 5 additions & 0 deletions es6/lib/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading

0 comments on commit 9e356fb

Please sign in to comment.