Merge pull request #288 from fabien/fix/relation-tests
Test sync (cached) relation getters
This commit is contained in:
commit
6903253471
|
@ -1713,6 +1713,7 @@ RelationDefinition.embedsOne = function (modelFrom, modelTo, params) {
|
||||||
relationMethod.build = relation.build.bind(relation);
|
relationMethod.build = relation.build.bind(relation);
|
||||||
relationMethod.update = relation.update.bind(relation);
|
relationMethod.update = relation.update.bind(relation);
|
||||||
relationMethod.destroy = relation.destroy.bind(relation);
|
relationMethod.destroy = relation.destroy.bind(relation);
|
||||||
|
relationMethod.value = relation.embeddedValue.bind(relation);
|
||||||
relationMethod._targetClass = definition.modelTo.modelName;
|
relationMethod._targetClass = definition.modelTo.modelName;
|
||||||
return relationMethod;
|
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) {
|
EmbedsOne.prototype.create = function (targetModelData, cb) {
|
||||||
var modelTo = this.definition.modelTo;
|
var modelTo = this.definition.modelTo;
|
||||||
var propertyName = this.definition.keyFrom;
|
var propertyName = this.definition.keyFrom;
|
||||||
|
@ -1922,7 +1928,8 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
||||||
get: scopeMethod(definition, 'get'),
|
get: scopeMethod(definition, 'get'),
|
||||||
set: scopeMethod(definition, 'set'),
|
set: scopeMethod(definition, 'set'),
|
||||||
unset: scopeMethod(definition, 'unset'),
|
unset: scopeMethod(definition, 'unset'),
|
||||||
at: scopeMethod(definition, 'at')
|
at: scopeMethod(definition, 'at'),
|
||||||
|
value: scopeMethod(definition, 'embeddedValue')
|
||||||
};
|
};
|
||||||
|
|
||||||
var findByIdFunc = scopeMethods.findById;
|
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;
|
modelInstance = modelInstance || this.modelInstance;
|
||||||
var embeddedList = modelInstance[this.definition.keyFrom] || [];
|
var embeddedList = modelInstance[this.definition.keyFrom] || [];
|
||||||
embeddedList.forEach(this.prepareEmbeddedInstance.bind(this));
|
embeddedList.forEach(this.prepareEmbeddedInstance.bind(this));
|
||||||
|
@ -2030,7 +2038,7 @@ EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, cb
|
||||||
|
|
||||||
var params = mergeQuery(actualCond, scopeParams);
|
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;
|
embeddedList = embeddedList ? embeddedList.filter(applyFilter(params)) : embeddedList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
16
lib/scope.js
16
lib/scope.js
|
@ -55,13 +55,13 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Method can be only called with one or two arguments');
|
throw new Error('Method can be only called with one or two arguments');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!self.__cachedRelations || self.__cachedRelations[name] === undefined
|
if (!self.__cachedRelations || self.__cachedRelations[name] === undefined
|
||||||
|| actualRefresh) {
|
|| actualRefresh) {
|
||||||
// It either doesn't hit the cache or refresh is required
|
// It either doesn't hit the cache or refresh is required
|
||||||
var params = mergeQuery(actualCond, scopeParams);
|
var params = mergeQuery(actualCond, scopeParams);
|
||||||
var targetModel = this.targetModel(receiver);
|
var targetModel = this.targetModel(receiver);
|
||||||
return targetModel.find(params, function (err, data) {
|
targetModel.find(params, function (err, data) {
|
||||||
if (!err && saveOnCache) {
|
if (!err && saveOnCache) {
|
||||||
defineCachedRelations(self);
|
defineCachedRelations(self);
|
||||||
self.__cachedRelations[name] = data;
|
self.__cachedRelations[name] = data;
|
||||||
|
@ -149,10 +149,16 @@ function defineScope(cls, targetClass, name, params, methods, options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
var f = function(condOrRefresh, cb) {
|
var f = function(condOrRefresh, cb) {
|
||||||
if(arguments.length === 1) {
|
if (arguments.length === 0) {
|
||||||
definition.related(self, f._scope, condOrRefresh);
|
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 {
|
} else {
|
||||||
definition.related(self, f._scope, condOrRefresh, cb);
|
return definition.related(self, f._scope, condOrRefresh, cb);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -91,10 +91,14 @@ describe('relations', function () {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(ch);
|
should.exist(ch);
|
||||||
ch.should.have.lengthOf(3);
|
ch.should.have.lengthOf(3);
|
||||||
|
|
||||||
|
var chapters = book.chapters();
|
||||||
|
chapters.should.eql(ch);
|
||||||
|
|
||||||
book.chapters({order: 'name DESC'}, function (e, c) {
|
book.chapters({order: 'name DESC'}, function (e, c) {
|
||||||
should.not.exist(e);
|
should.not.exist(e);
|
||||||
should.exist(c);
|
should.exist(c);
|
||||||
|
|
||||||
c.shift().name.should.equal('z');
|
c.shift().name.should.equal('z');
|
||||||
c.pop().name.should.equal('a');
|
c.pop().name.should.equal('a');
|
||||||
done();
|
done();
|
||||||
|
@ -298,6 +302,10 @@ describe('relations', function () {
|
||||||
});
|
});
|
||||||
function verify(physician) {
|
function verify(physician) {
|
||||||
physician.patients(function (err, ch) {
|
physician.patients(function (err, ch) {
|
||||||
|
|
||||||
|
var patients = physician.patients();
|
||||||
|
patients.should.eql(ch);
|
||||||
|
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
should.exist(ch);
|
should.exist(ch);
|
||||||
ch.should.have.lengthOf(3);
|
ch.should.have.lengthOf(3);
|
||||||
|
@ -842,6 +850,10 @@ describe('relations', function () {
|
||||||
Author.findOne(function (err, author) {
|
Author.findOne(function (err, author) {
|
||||||
author.avatar(function (err, p) {
|
author.avatar(function (err, p) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
|
|
||||||
|
var avatar = author.avatar();
|
||||||
|
avatar.should.equal(p);
|
||||||
|
|
||||||
p.name.should.equal('Avatar');
|
p.name.should.equal('Avatar');
|
||||||
p.imageableId.should.eql(author.id);
|
p.imageableId.should.eql(author.id);
|
||||||
p.imageableType.should.equal('Author');
|
p.imageableType.should.equal('Author');
|
||||||
|
@ -971,6 +983,10 @@ describe('relations', function () {
|
||||||
Author.findOne(function (err, author) {
|
Author.findOne(function (err, author) {
|
||||||
author.pictures(function (err, pics) {
|
author.pictures(function (err, pics) {
|
||||||
should.not.exist(err);
|
should.not.exist(err);
|
||||||
|
|
||||||
|
var pictures = author.pictures();
|
||||||
|
pictures.should.eql(pics);
|
||||||
|
|
||||||
pics.should.have.length(1);
|
pics.should.have.length(1);
|
||||||
pics[0].name.should.equal('Author Pic');
|
pics[0].name.should.equal('Author Pic');
|
||||||
done();
|
done();
|
||||||
|
@ -1638,6 +1654,9 @@ describe('relations', function () {
|
||||||
article.tags(function (e, tags) {
|
article.tags(function (e, tags) {
|
||||||
should.not.exist(e);
|
should.not.exist(e);
|
||||||
should.exist(tags);
|
should.exist(tags);
|
||||||
|
|
||||||
|
article.tags().should.eql(tags);
|
||||||
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1756,6 +1775,7 @@ describe('relations', function () {
|
||||||
passport.toObject().should.eql({name: 'Fredric'});
|
passport.toObject().should.eql({name: 'Fredric'});
|
||||||
passport.should.be.an.instanceOf(Passport);
|
passport.should.be.an.instanceOf(Passport);
|
||||||
passport.should.equal(p.passport);
|
passport.should.equal(p.passport);
|
||||||
|
passport.should.equal(p.passportItem.value());
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1891,6 +1911,13 @@ describe('relations', function () {
|
||||||
Person.findOne(function(err, p) {
|
Person.findOne(function(err, p) {
|
||||||
p.addressList(function(err, addresses) {
|
p.addressList(function(err, addresses) {
|
||||||
should.not.exist(err);
|
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.should.have.length(2);
|
||||||
addresses[0].id.should.eql(address1.id);
|
addresses[0].id.should.eql(address1.id);
|
||||||
addresses[0].street.should.equal('Street 1');
|
addresses[0].street.should.equal('Street 1');
|
||||||
|
|
Loading…
Reference in New Issue