Merge pull request #288 from fabien/fix/relation-tests

Test sync (cached) relation getters
This commit is contained in:
Raymond Feng 2014-09-12 10:29:42 -07:00
commit 6903253471
3 changed files with 50 additions and 9 deletions

View File

@ -1713,6 +1713,7 @@ RelationDefinition.embedsOne = function (modelFrom, modelTo, params) {
relationMethod.build = relation.build.bind(relation);
relationMethod.update = relation.update.bind(relation);
relationMethod.destroy = relation.destroy.bind(relation);
relationMethod.value = relation.embeddedValue.bind(relation);
relationMethod._targetClass = definition.modelTo.modelName;
return relationMethod;
}
@ -1761,6 +1762,11 @@ EmbedsOne.prototype.related = function (refresh, params) {
}
};
EmbedsOne.prototype.embeddedValue = function(modelInstance) {
modelInstance = modelInstance || this.modelInstance;
return modelInstance[this.definition.keyFrom];
};
EmbedsOne.prototype.create = function (targetModelData, cb) {
var modelTo = this.definition.modelTo;
var propertyName = this.definition.keyFrom;
@ -1922,7 +1928,8 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
get: scopeMethod(definition, 'get'),
set: scopeMethod(definition, 'set'),
unset: scopeMethod(definition, 'unset'),
at: scopeMethod(definition, 'at')
at: scopeMethod(definition, 'at'),
value: scopeMethod(definition, 'embeddedValue')
};
var findByIdFunc = scopeMethods.findById;
@ -2002,7 +2009,8 @@ EmbedsMany.prototype.prepareEmbeddedInstance = function(inst) {
}
};
EmbedsMany.prototype.embeddedList = function(modelInstance) {
EmbedsMany.prototype.embeddedList =
EmbedsMany.prototype.embeddedValue = function(modelInstance) {
modelInstance = modelInstance || this.modelInstance;
var embeddedList = modelInstance[this.definition.keyFrom] || [];
embeddedList.forEach(this.prepareEmbeddedInstance.bind(this));
@ -2030,7 +2038,7 @@ EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, cb
var params = mergeQuery(actualCond, scopeParams);
if (params.where) { // TODO [fabien] Support order/sorting
if (params.where && Object.keys(params.where).length > 0) { // TODO [fabien] Support order/sorting
embeddedList = embeddedList ? embeddedList.filter(applyFilter(params)) : embeddedList;
}

View File

@ -55,13 +55,13 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
} else {
throw new Error('Method can be only called with one or two arguments');
}
if (!self.__cachedRelations || self.__cachedRelations[name] === undefined
|| actualRefresh) {
// It either doesn't hit the cache or refresh is required
var params = mergeQuery(actualCond, scopeParams);
var targetModel = this.targetModel(receiver);
return targetModel.find(params, function (err, data) {
targetModel.find(params, function (err, data) {
if (!err && saveOnCache) {
defineCachedRelations(self);
self.__cachedRelations[name] = data;
@ -149,10 +149,16 @@ function defineScope(cls, targetClass, name, params, methods, options) {
var self = this;
var f = function(condOrRefresh, cb) {
if(arguments.length === 1) {
definition.related(self, f._scope, condOrRefresh);
if (arguments.length === 0) {
if (typeof f.value === 'function') {
return f.value(self);
} else if (self.__cachedRelations) {
return self.__cachedRelations[name];
}
} else if (arguments.length === 1) {
return definition.related(self, f._scope, condOrRefresh);
} else {
definition.related(self, f._scope, condOrRefresh, cb);
return definition.related(self, f._scope, condOrRefresh, cb);
}
};

View File

@ -91,10 +91,14 @@ describe('relations', function () {
should.not.exist(err);
should.exist(ch);
ch.should.have.lengthOf(3);
var chapters = book.chapters();
chapters.should.eql(ch);
book.chapters({order: 'name DESC'}, function (e, c) {
should.not.exist(e);
should.exist(c);
c.shift().name.should.equal('z');
c.pop().name.should.equal('a');
done();
@ -298,6 +302,10 @@ describe('relations', function () {
});
function verify(physician) {
physician.patients(function (err, ch) {
var patients = physician.patients();
patients.should.eql(ch);
should.not.exist(err);
should.exist(ch);
ch.should.have.lengthOf(3);
@ -842,6 +850,10 @@ describe('relations', function () {
Author.findOne(function (err, author) {
author.avatar(function (err, p) {
should.not.exist(err);
var avatar = author.avatar();
avatar.should.equal(p);
p.name.should.equal('Avatar');
p.imageableId.should.eql(author.id);
p.imageableType.should.equal('Author');
@ -971,6 +983,10 @@ describe('relations', function () {
Author.findOne(function (err, author) {
author.pictures(function (err, pics) {
should.not.exist(err);
var pictures = author.pictures();
pictures.should.eql(pics);
pics.should.have.length(1);
pics[0].name.should.equal('Author Pic');
done();
@ -1638,6 +1654,9 @@ describe('relations', function () {
article.tags(function (e, tags) {
should.not.exist(e);
should.exist(tags);
article.tags().should.eql(tags);
done();
});
});
@ -1756,6 +1775,7 @@ describe('relations', function () {
passport.toObject().should.eql({name: 'Fredric'});
passport.should.be.an.instanceOf(Passport);
passport.should.equal(p.passport);
passport.should.equal(p.passportItem.value());
done();
});
});
@ -1891,6 +1911,13 @@ describe('relations', function () {
Person.findOne(function(err, p) {
p.addressList(function(err, addresses) {
should.not.exist(err);
var list = p.addressList();
list.should.equal(addresses);
list.should.equal(p.addresses);
p.addressList.value().should.equal(list);
addresses.should.have.length(2);
addresses[0].id.should.eql(address1.id);
addresses[0].street.should.equal('Street 1');