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.
This commit is contained in:
parent
bc4076f35e
commit
687eb9888b
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue