From 8e458e7de026ed38fc2fa564cf87e105064f00a9 Mon Sep 17 00:00:00 2001 From: "Jared M. Smith" Date: Fri, 27 Jun 2014 19:50:18 +0000 Subject: [PATCH 1/2] Return cached relation data for hasMany, e.g. user.posts(c), to be consistent with belongsTo behavior. --- lib/scope.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/scope.js b/lib/scope.js index d2639ed3..b555c8af 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -69,6 +69,7 @@ function defineScope(cls, targetClass, name, params, methods) { }); } else { cb(null, this.__cachedRelations[name]); + return this.__cachedRelations[name]; } }; f._scope = typeof params === 'function' ? params.call(this) : params; From 5d17d368f2f42d952c22eedbf470f6f0c51e9cd9 Mon Sep 17 00:00:00 2001 From: "Jared M. Smith" Date: Wed, 9 Jul 2014 22:16:58 +0000 Subject: [PATCH 2/2] Create include async getter sync return tests. --- test/include.test.js | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/test/include.test.js b/test/include.test.js index 23bd1ddb..7e497c7b 100644 --- a/test/include.test.js +++ b/test/include.test.js @@ -132,6 +132,83 @@ describe('include', function() { done(); }); }); + + /* + * Test public async api synchronous return. + * E.g. user.posts(c) should return a list of posts when included. + */ + describe('async getter', function() { + + // Empty callback to pass to async getters. + var c = function() { }; + + it('should return sync Passport - Owner - Posts', function(done) { + Passport.all({include: {owner: 'posts'}}, function(err, passports) { + should.not.exist(err); + should.exist(passports); + passports.length.should.be.ok; + passports.forEach(function(p) { + var user = p.owner(c); + if (!p.ownerId) { + should.not.exist(user); + } else { + should.exist(user); + user.id.should.equal(p.ownerId); + user.should.have.property('posts'); + user.posts(c).forEach(function(pp) { + pp.userId.should.equal(user.id); + }); + } + }); + done(); + }); + }); + + it('should return sync Passports - User - Posts - User', function(done) { + Passport.all({ + include: {owner: {posts: 'author'}} + }, function(err, passports) { + should.not.exist(err); + should.exist(passports); + passports.length.should.be.ok; + passports.forEach(function(p) { + p.should.have.property('owner'); + var user = p.owner(c); + if (!p.ownerId) { + should.not.exist(user); + } else { + should.exist(user); + user.id.should.equal(p.ownerId); + user.should.have.property('posts'); + user.posts(c).forEach(function(pp) { + pp.userId.should.equal(user.id); + pp.should.have.property('author'); + var author = pp.author(c); + author.id.should.equal(user.id); + }); + } + }); + done(); + }); + }); + + it('should return sync User - Posts AND Passports', function(done) { + User.all({include: ['posts', 'passports']}, function(err, users) { + should.not.exist(err); + should.exist(users); + users.length.should.be.ok; + users.forEach(function(user) { + user.posts(c).forEach(function(p) { + p.userId.should.equal(user.id); + }); + user.passports(c).forEach(function(pp) { + pp.ownerId.should.equal(user.id); + }); + }); + done(); + }); + }); + }); }); function setup(done) {