Skip to content

Commit

Permalink
Mocking fully works
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrockwell committed Sep 22, 2015
1 parent 9e356fb commit 51973c6
Show file tree
Hide file tree
Showing 19 changed files with 723 additions and 479 deletions.
535 changes: 401 additions & 134 deletions README.md

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions es5/lib/model/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ var _jargon2 = _interopRequireDefault(_jargon);
function deleteSelf(callback) {
var _this = this;

if (!this[_symbols2["default"].getDatabase]()) {
throw new Error("Cannot delete without Model.database set.");
}

_flowsync2["default"].series([function (done) {
_this.beforeDelete(done);
}, function (done) {
Expand All @@ -55,10 +51,24 @@ function deleteSelf(callback) {
}

function performDelete(callback) {
if ((0, _incognito2["default"])(this)._softDelete) {
softDelete.call(this, callback);
var _ = (0, _incognito2["default"])(this);
if (_.mockDelete) {
if (_._softDelete) {
this.deletedAt = new _fleming2["default"]();
callback();
} else {
callback();
}
} else {
hardDelete.call(this, callback);
if (this[_symbols2["default"].getDatabase]()) {
if (_._softDelete) {
softDelete.call(this, callback);
} else {
hardDelete.call(this, callback);
}
} else {
callback(new Error("Cannot delete without Model.database set."));
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions es5/lib/model/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ function fetchByBelongsTo(associationName, associations, callback) {
}

function fetchBy() {
var fields = arguments.length <= 0 || arguments[0] === undefined ? [this.primaryKey] : arguments[0];
var callback = arguments.length <= 1 || arguments[1] === undefined ? undefined : arguments[1];

var _ = (0, _incognito2["default"])(this);
if (_.mockFetchRecord) {
for (var attributeName in _.mockFetchRecord) {
var mockValue = _.mockFetchRecord[attributeName];
this[attributeName] = mockValue;
}
callback();
} else {
fetchFromDatabase.call(this, fields, callback);
}
}

function fetchFromDatabase() {
var _this5 = this;

var fields = arguments.length <= 0 || arguments[0] === undefined ? [this.primaryKey] : arguments[0];
Expand Down
47 changes: 47 additions & 0 deletions es5/lib/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,16 @@ var Model = (function () {

return fieldAttributes;
}
}, {
key: "mock",

/**
* INSTANCE MOCKING
*/

get: function get() {
return new InstanceMock(this);
}
}], [{
key: "database",
get: function get() {
Expand Down Expand Up @@ -527,6 +537,43 @@ var Model = (function () {

exports["default"] = Model;

var InstanceMock = (function () {
function InstanceMock(instance) {
_classCallCheck(this, InstanceMock);

(0, _incognito2["default"])(this).instance = instance;
}

_createClass(InstanceMock, [{
key: "save",
value: function save(mockNewId) {
var instance = (0, _incognito2["default"])(this).instance;
(0, _incognito2["default"])(instance).mockNewId = mockNewId;
}
}, {
key: "fetch",
value: function fetch(mockRecord) {
var instance = (0, _incognito2["default"])(this).instance;
(0, _incognito2["default"])(instance).mockFetchRecord = mockRecord;
}
}, {
key: "delete",
value: function _delete() {
var instance = (0, _incognito2["default"])(this).instance;
(0, _incognito2["default"])(instance).mockDelete = true;
}
}, {
key: "record",
value: function record(mockRecord) {
this.save(mockRecord.id);
this.fetch(mockRecord);
this["delete"]();
}
}]);

return InstanceMock;
})();

Object.assign(Model.prototype, _defineProperty({
"fetch": require("./fetch.js"),
"save": require("./save.js"),
Expand Down
56 changes: 32 additions & 24 deletions es5/lib/model/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,42 @@ function saveOrUpdate(callback) {
var _this = this;

var now = new _fleming2["default"]();
if (this.isNew) {
this.createdAt = now.toDate();
var fieldAttributes = this[_symbols2["default"].getFieldAttributes]();

this[_symbols2["default"].getDatabase]().insert(fieldAttributes).into(this.tableName).results(function (error, ids) {
if (error) {
callback(error);
} else {
_this[_this.primaryKey] = ids[0];
callback();
}
});
var _ = (0, _incognito2["default"])(this);

if (_.mockNewId) {
this[this.primaryKey] = _.mockNewId;
callback(undefined, _.mockNewId);
} else {
this.updatedAt = now.toDate();
var attributes = this[_symbols2["default"].getFieldAttributes]();
var updateAttributes = {};
if (!this[_symbols2["default"].getDatabase]()) {
callback(new Error("Cannot save without Model.database set."));
} else {
if (this.isNew) {
this.createdAt = now.toDate();
var fieldAttributes = this[_symbols2["default"].getFieldAttributes]();

this[_symbols2["default"].getDatabase]().insert(fieldAttributes).into(this.tableName).results(function (error, ids) {
if (error) {
callback(error);
} else {
_this[_this.primaryKey] = ids[0];
callback();
}
});
} else {
this.updatedAt = now.toDate();
var attributes = this[_symbols2["default"].getFieldAttributes]();
var updateAttributes = {};

for (var attributeName in attributes) {
if (attributeName !== this.primaryKey) {
updateAttributes[attributeName] = attributes[attributeName];
for (var attributeName in attributes) {
if (attributeName !== this.primaryKey) {
updateAttributes[attributeName] = attributes[attributeName];
}
}

this[_symbols2["default"].getDatabase]().update(updateAttributes).into(this.tableName).where(this.primaryKey, "=", this[this.primaryKey]).results(callback);
}
}

this[_symbols2["default"].getDatabase]().update(updateAttributes).into(this.tableName).where(this.primaryKey, "=", this[this.primaryKey]).results(callback);
}
}

Expand Down Expand Up @@ -114,10 +126,6 @@ function validate(callback) {
function save(callback) {
var _this3 = this;

if (!this[_symbols2["default"].getDatabase]()) {
throw new Error("Cannot save without Model.database set.");
}

_flowsync2["default"].series([function (next) {
_this3.beforeValidation(next);
}, function (next) {
Expand All @@ -134,7 +142,7 @@ function save(callback) {
if (errors) {
callback(errors);
} else {
callback(undefined, _this3);
callback(undefined, _this3[_this3.primaryKey]);
}
});
}
Expand Down
38 changes: 24 additions & 14 deletions es5/lib/modelFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var _collectionJs = require("./collection.js");

var _collectionJs2 = _interopRequireDefault(_collectionJs);

var attributesToColumns = Symbol();
var attributesToColumns = Symbol(),
newQuery = Symbol();

var ModelFinder = (function () {
function ModelFinder(database) {
Expand All @@ -39,34 +40,37 @@ var ModelFinder = (function () {
_createClass(ModelFinder, [{
key: "find",
value: function find(ModelConstructor) {
var query = new ModelQuery(ModelConstructor, {
database: (0, _incognito2["default"])(this).database
});
var query = this[newQuery](ModelConstructor);

query.find;

return query;
}
}, {
key: "count",
value: function count(ModelConstructor) {
var query = new ModelQuery(ModelConstructor, {
database: (0, _incognito2["default"])(this).database
});
var query = this[newQuery](ModelConstructor);

query.count;

return query;
}
}, {
key: "mock",
value: function mock(ModelConstructor) {
var query = new ModelQuery(ModelConstructor, {
database: (0, _incognito2["default"])(this).database
});
var query = this[newQuery](ModelConstructor);

query.mock;

return query;
}
}, {
key: newQuery,
value: function value(ModelConstructor) {
return new ModelQuery(ModelConstructor, {
database: (0, _incognito2["default"])(this).database
});
}
}]);

return ModelFinder;
Expand All @@ -75,6 +79,7 @@ var ModelFinder = (function () {
exports["default"] = ModelFinder;

var addChain = Symbol(),
addMock = Symbol(),
validateDependencies = Symbol(),
argumentString = Symbol(),
callDatabase = Symbol(),
Expand Down Expand Up @@ -261,10 +266,7 @@ var ModelQuery = (function () {
if (_.isMockDefinition) {
var mockValue = callbackOrMockValue;

_.ModelConstructor.mocks[this.toString()] = {
query: this,
value: mockValue
};
this[addMock](this.toString(), mockValue);
} else {
var callback = callbackOrMockValue;

Expand All @@ -287,6 +289,14 @@ var ModelQuery = (function () {
}
}
}
}, {
key: addMock,
value: function value(mockIdentifier, mockValue) {
(0, _incognito2["default"])(this).ModelConstructor.mocks[mockIdentifier] = {
query: this,
value: mockValue
};
}
}, {
key: validateDependencies,
value: function value() {
Expand Down
57 changes: 13 additions & 44 deletions es5/lib/validation/isPresent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,27 @@ var _modelFinderJs = require("../modelFinder.js");

var _modelFinderJs2 = _interopRequireDefault(_modelFinderJs);

var _incognito = require("incognito");

var _incognito2 = _interopRequireDefault(_incognito);

function countAssociationHasMany(model, associationModel, association, resultDetails, callback) {
var modelFinder = new _modelFinderJs2["default"](model.constructor.database);
var collection = associationModel;
if (collection.length > 0) {
resultDetails.result = true;
callback(undefined, resultDetails);
} else {
if (model.isNew) {
//it does not have an id to look for
resultDetails.result = false;
callback(null, resultDetails);
} else {
modelFinder.count(association.constructor).where(association.foreignKey, "=", model.id).results(function (error, count) {
if (error) {
resultDetails.result = false;
callback(error, resultDetails);
} else {
var modelsFound = count > 0;

if (modelsFound) {
resultDetails.result = true;
callback(null, resultDetails);
} else {
resultDetails.result = false;
callback(null, resultDetails);
}
}
});
}
resultDetails.result = false;
callback(undefined, resultDetails);
}
}

function isPresent(associationName, callback) {
var model = this;
var defaultErrorMessage = "must be present on " + model.constructor.name;

if (!model.constructor.database) {
throw new Error("Cannot check isPresent without a database set.");
}

//apiKey
var association = model.associations[associationName];
var modelFinder = new _modelFinderJs2["default"](model.constructor.database);

var associationModel = model[associationName]; //this["apiKey"], this["apiKeyId"]

Expand Down Expand Up @@ -100,22 +78,13 @@ function isPresent(associationName, callback) {
switch (association.type) {
case "hasOne":
case "hasMany":
modelFinder.count(association.constructor).where(association.foreignKey, "=", model.id).results(function (error, count) {
if (error) {
resultDetails.result = false;
callback(error, resultDetails);
} else {
var modelsFound = count > 0;

if (modelsFound) {
resultDetails.result = true;
callback(null, resultDetails);
} else {
resultDetails.result = false;
callback(null, resultDetails);
}
}
});
if (model[foreignId]) {
resultDetails.result = true;
callback(null, resultDetails);
} else {
resultDetails.result = false;
callback(null, resultDetails);
}
break;
case "belongsTo":

Expand Down
6 changes: 3 additions & 3 deletions es5/spec/model/delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ describe("Model(attributes, options)", function () {
});

it("should throw an error", function () {
(function () {
post["delete"]();
}).should["throw"]("Cannot delete without Model.database set.");
post["delete"](function (error) {
error.message.should.eql("Cannot delete without Model.database set.");
});
});
});
});
Expand Down
Loading

0 comments on commit 51973c6

Please sign in to comment.