From 687eb9888b73e6c8bd35c8808a5d69c5c853e2ad Mon Sep 17 00:00:00 2001 From: Fabien Franzen Date: Wed, 23 Jul 2014 11:10:44 +0200 Subject: [PATCH] Added test for belongsTo scope/properties Note: its seems that keyFrom and keyTo were mistakingly reversed in BelongsTo.prototype.create, please double check. The added test cases now pass with pk/fk switched. --- lib/relation-definition.js | 10 ++++++---- test/relations.test.js | 41 +++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 8429fdaa..2aaa70b9 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -853,8 +853,8 @@ RelationDefinition.belongsTo = function (modelFrom, modelTo, params) { BelongsTo.prototype.create = function(targetModelData, cb) { var self = this; var modelTo = this.definition.modelTo; - var fk = this.definition.keyTo; - var pk = this.definition.keyFrom; + var fk = this.definition.keyFrom; + var pk = this.definition.keyTo; var modelInstance = this.modelInstance; if (typeof targetModelData === 'function' && !cb) { @@ -929,7 +929,8 @@ BelongsTo.prototype.related = function (refresh, params) { return cb(null, null); } // Check if the foreign key matches the primary key - if (inst[pk] && inst[pk].toString() === modelInstance[fk].toString()) { + if (inst[pk] && modelInstance[fk] + && inst[pk].toString() === modelInstance[fk].toString()) { self.resetCache(inst); cb(null, inst); } else { @@ -1203,7 +1204,8 @@ HasOne.prototype.related = function (refresh, params) { return cb(null, null); } // Check if the foreign key matches the primary key - if (inst[fk] && inst[fk].toString() === modelInstance[pk].toString()) { + if (inst[fk] && modelInstance[pk] + && inst[fk].toString() === modelInstance[pk].toString()) { self.resetCache(inst); cb(null, inst); } else { diff --git a/test/relations.test.js b/test/relations.test.js index 0691ded8..8218c84a 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -404,7 +404,7 @@ describe('relations', function () { }); }); - describe('hasMany with scope', function () { + describe('hasMany with scope and properties', function () { it('can be declared with properties', function (done) { db = getSchema(); Category = db.define('Category', {name: String, productType: String}); @@ -556,6 +556,45 @@ describe('relations', function () { }); }); + + describe('belongsTo with scope', function () { + var Person, Passport; + + it('can be declared with scope and properties', function (done) { + Person = db.define('Person', {name: String, age: Number}); + Passport = db.define('Passport', {name: String, notes: String}); + Passport.belongsTo(Person, { + properties: { notes: 'passportNotes' }, + scope: { fields: { id: true, name: true } } + }); + db.automigrate(done); + }); + + it('should create record on scope', function (done) { + var p = new Passport({ name: 'Passport', notes: 'Some notes...' }); + p.person.create({ id: 3, name: 'Fred', age: 36 }, function(err, person) { + p.personId.should.equal(person.id); + p.save(function (err, p) { + person.name.should.equal('Fred'); + person.passportNotes.should.equal('Some notes...'); + done(); + }); + }); + }); + + it('should find record on scope', function (done) { + Passport.findOne(function (err, p) { + p.personId.should.equal(3); + p.person(function(err, person) { + person.name.should.equal('Fred'); + person.should.not.have.property('age'); + person.should.not.have.property('passportNotes'); + done(); + }); + }); + }); + + }); describe('hasOne', function () { var Supplier, Account;