Test belongsTo
This commit is contained in:
parent
bc36089eb1
commit
7f073a4622
|
@ -490,6 +490,8 @@ AbstractClass.hasMany = function (anotherClass, params) {
|
|||
AbstractClass.belongsTo = function (anotherClass, params) {
|
||||
var methodName = params.as;
|
||||
var fk = params.foreignKey;
|
||||
|
||||
this.schema.defineForeignKey(this.modelName, fk);
|
||||
this.prototype['__finders__'] = this.prototype['__finders__'] || {}
|
||||
|
||||
this.prototype['__finders__'][methodName] = function (id, cb) {
|
||||
|
@ -508,12 +510,14 @@ AbstractClass.belongsTo = function (anotherClass, params) {
|
|||
this[fk] = p.id;
|
||||
this.cachedRelations[methodName] = p;
|
||||
} else if (typeof p === 'function') { // acts as async getter
|
||||
this.__finders__[methodName](this[fk],p);
|
||||
} else if (!p) { // acts as sync getter
|
||||
this.__finders__[methodName](this[fk], p);
|
||||
return this[fk];
|
||||
} else if (typeof p === 'undefined') { // acts as sync getter
|
||||
return this[fk];
|
||||
} else { // setter
|
||||
this[fk] = p;
|
||||
}
|
||||
};
|
||||
console.log(this.prototype);
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ Object.keys(schemas).forEach(function (schemaName) {
|
|||
|
||||
function testOrm(schema) {
|
||||
|
||||
var Post, User;
|
||||
var Post, User, Passport;
|
||||
var start = Date.now();
|
||||
|
||||
it('should define class', function (test) {
|
||||
|
@ -92,6 +92,12 @@ function testOrm(schema) {
|
|||
// post.author() -- sync getter when called without params
|
||||
// post.author(user) -- setter when called with object
|
||||
|
||||
Passport = schema.define('Passport', {
|
||||
number: String
|
||||
});
|
||||
|
||||
Passport.belongsTo(User, {as: 'owner', foreignKey: 'ownerId'});
|
||||
|
||||
var user = new User;
|
||||
|
||||
test.ok(User instanceof Function);
|
||||
|
@ -598,6 +604,16 @@ function testOrm(schema) {
|
|||
});
|
||||
});
|
||||
|
||||
it('should handle belongsTo correctly', function (test) {
|
||||
var passport = new Passport({ownerId: 16});
|
||||
// sync getter
|
||||
test.equal(passport.owner(), 16);
|
||||
// sync setter
|
||||
passport.owner(18);
|
||||
test.equal(passport.owner(), 18);
|
||||
test.done();
|
||||
});
|
||||
|
||||
it('all tests done', function (test) {
|
||||
test.done();
|
||||
process.nextTick(allTestsDone);
|
||||
|
|
Loading…
Reference in New Issue