Adding more tests

This commit is contained in:
Raymond Feng 2013-05-28 13:50:59 -07:00
parent dd8512bc1f
commit 465a963d2d
5 changed files with 175 additions and 7 deletions

View File

@ -18,7 +18,7 @@ var User = modelBuilder.define('User', {
age: Number age: Number
}); });
var Group = modelBuilder.define('Group', {name: String}); var Group = modelBuilder.define('Group', {group: String});
// define any custom method // define any custom method
User.prototype.getNameAndAge = function () { User.prototype.getNameAndAge = function () {
@ -31,6 +31,10 @@ console.log(user);
console.log(modelBuilder.models); console.log(modelBuilder.models);
console.log(modelBuilder.definitions); console.log(modelBuilder.definitions);
User.mixin(Group);
var user = new User({name: 'Ray', group: 'Admin'});
console.log(user);

View File

@ -23,13 +23,14 @@ exports.mixin = function (newClass, baseClass, options) {
options = options || { options = options || {
staticProperties: true, staticProperties: true,
instanceProperties: true, instanceProperties: true,
override: true override: false
}; };
if (options.staticProperties) { if (options.staticProperties) {
Object.keys(baseClass).forEach(function (classProp) { Object.keys(baseClass).forEach(function (classProp) {
if (classProp !== 'super_' && (!newClass.hasOwnProperty(classProp) || options.override)) { if (classProp !== 'super_' && (!newClass.hasOwnProperty(classProp) || options.override)) {
newClass[classProp] = baseClass[classProp]; var pd = Object.getOwnPropertyDescriptor(baseClass, classProp);
Object.defineProperty(newClass, classProp, pd);
} }
}); });
} }
@ -37,7 +38,10 @@ exports.mixin = function (newClass, baseClass, options) {
if (options.instanceProperties) { if (options.instanceProperties) {
if (baseClass.prototype) { if (baseClass.prototype) {
Object.keys(baseClass.prototype).forEach(function (instanceProp) { Object.keys(baseClass.prototype).forEach(function (instanceProp) {
newClass.prototype[instanceProp] = baseClass.prototype[instanceProp]; if (!newClass.hasOwnProperty(instanceProp) || options.override) {
var pd = Object.getOwnPropertyDescriptor(baseClass.prototype, instanceProp);
Object.defineProperty(newClass.prototype, instanceProp, pd);
}
}); });
} }
} }
@ -47,5 +51,7 @@ exports.mixin = function (newClass, baseClass, options) {
} else { } else {
newClass._mixins = [baseClass]; newClass._mixins = [baseClass];
} }
return newClass;
}; };

View File

@ -193,13 +193,16 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
if (ModelClass.getter[attr]) { if (ModelClass.getter[attr]) {
return ModelClass.getter[attr].call(this); return ModelClass.getter[attr].call(this);
} else { } else {
return this.__data[attr]; return this.__data && this.__data[attr];
} }
}, },
set: function (value) { set: function (value) {
if (ModelClass.setter[attr]) { if (ModelClass.setter[attr]) {
ModelClass.setter[attr].call(this, value); ModelClass.setter[attr].call(this, value);
} else { } else {
if (!this.__data) {
this.__data = {};
}
if (value === null || value === undefined) { if (value === null || value === undefined) {
this.__data[attr] = value; this.__data[attr] = value;
} else { } else {
@ -212,14 +215,17 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
}); });
ModelClass.prototype.__defineGetter__(attr + '_was', function () { ModelClass.prototype.__defineGetter__(attr + '_was', function () {
return this.__dataWas[attr]; return this.__dataWas && this.__dataWas[attr];
}); });
Object.defineProperty(ModelClass.prototype, '_' + attr, { Object.defineProperty(ModelClass.prototype, '_' + attr, {
get: function () { get: function () {
return this.__data[attr]; return this.__data && this.__data[attr];
}, },
set: function (value) { set: function (value) {
if (!this.__data) {
this.__data = {};
}
this.__data[attr] = value; this.__data[attr] = value;
}, },
configurable: true, configurable: true,

View File

@ -243,5 +243,9 @@ function isdef(s) {
return s !== undef; return s !== undef;
} }
ModelBaseClass.mixin = function(anotherClass, options) {
return jutil.mixin(this, anotherClass, options);
}
jutil.mixin(ModelBaseClass, Hookable); jutil.mixin(ModelBaseClass, Hookable);

148
test/adl.test.js Normal file
View File

@ -0,0 +1,148 @@
// This test written in mocha+should.js
var should = require('./init.js');
var jdb = require('../');
var ModelBuilder = jdb.ModelBuilder;
var DataSource = jdb.DataSource;
describe('ModelBuilder define model', function () {
it('should be able to define plain models', function (done) {
var modelBuilder = new ModelBuilder();
// simplier way to describe model
var User = modelBuilder.define('User', {
name: String,
bio: ModelBuilder.Text,
approved: Boolean,
joinedAt: Date,
age: Number
});
// define any custom method
User.prototype.getNameAndAge = function () {
return this.name + ', ' + this.age;
};
modelBuilder.models.should.be.a('object').and.have.property('User', User);
modelBuilder.definitions.should.be.a('object').and.have.property('User');
var user = new User({name: 'Joe', age: 20});
console.log(user);
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
done(null, User);
});
});
describe('DataSource define model', function () {
it('should be able to define plain models', function () {
var ds = new DataSource('memory');
// define models
var Post = ds.define('Post', {
title: { type: String, length: 255 },
content: { type: DataSource.Text },
date: { type: Date, default: function () {
return new Date;
} },
timestamp: { type: Number, default: Date.now },
published: { type: Boolean, default: false, index: true }
});
// simplier way to describe model
var User = ds.define('User', {
name: String,
bio: DataSource.Text,
approved: Boolean,
joinedAt: Date,
age: Number
});
var Group = ds.define('Group', {name: String});
// define any custom method
User.prototype.getNameAndAge = function () {
return this.name + ', ' + this.age;
};
var user = new User({name: 'Joe'});
console.log(user);
// setup relationships
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
User.hasAndBelongsToMany('groups');
var user2 = new User({name: 'Smith'});
user2.save(function (err) {
console.log(user2);
var post = user2.posts.build({title: 'Hello world'});
post.save(function (err, data) {
console.log(err ? err : data);
});
});
Post.findOne({where: {published: false}, order: 'date DESC'}, function (err, data) {
console.log(data);
});
User.create({name: 'Jeff'}, function (err, data) {
if (err) {
console.log(err);
return;
}
console.log(data);
var post = data.posts.build({title: 'My Post'});
console.log(post);
});
User.create({name: 'Ray'}, function (err, data) {
console.log(data);
});
var Article = ds.define('Article', {title: String});
var Tag = ds.define('Tag', {name: String});
Article.hasAndBelongsToMany('tags');
Article.create(function (e, article) {
article.tags.create({name: 'popular'}, function (err, data) {
Article.findOne(function (e, article) {
article.tags(function (e, tags) {
console.log(tags);
});
});
});
});
// should be able to attach a data source to an existing model
var modelBuilder = new ModelBuilder();
var Color = modelBuilder.define('Color', {
name: String
});
Color.should.not.have.property('create');
// attach
ds.attach(Color);
Color.should.have.property('create');
Color.create({name: 'red'});
Color.create({name: 'green'});
Color.create({name: 'blue'});
Color.all(function (err, colors) {
colors.should.have.lengthOf(3);
});
});
});