Test belongsTo

This commit is contained in:
Anatoliy Chakkaev 2012-03-01 21:26:52 +04:00
parent bc36089eb1
commit 7f073a4622
2 changed files with 24 additions and 4 deletions

View File

@ -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) {
@ -509,11 +511,13 @@ AbstractClass.belongsTo = function (anotherClass, params) {
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
return this[fk];
} else if (typeof p === 'undefined') { // acts as sync getter
return this[fk];
} else { // setter
this[fk] = p;
}
};
console.log(this.prototype);
};

View File

@ -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);