Make sure models are migrated to avoid conflicts

This commit is contained in:
Raymond Feng 2015-02-20 21:44:49 -08:00
parent a42e3b3d30
commit 9fd3865f32
1 changed files with 83 additions and 42 deletions

View File

@ -447,70 +447,111 @@ describe('manipulation', function () {
person.isNewRecord().should.be.true; person.isNewRecord().should.be.true;
}); });
it('should report current date when using $now as default value for date property', describe('Date $now function', function() {
function (done) { var CustomModel;
var CustomModel = db.define('CustomModel', {
before(function(done) {
CustomModel = db.define('CustomModel1', {
createdAt: { type: Date, default: '$now' } createdAt: { type: Date, default: '$now' }
}); });
db.automigrate('CustomModel1', done);
});
it('should report current date as default value for date property',
function(done) {
var now = Date.now(); var now = Date.now();
var myCustomModel = CustomModel.create(function (err, m) { var myCustomModel = CustomModel.create(function(err, m) {
should.not.exists(err);
m.createdAt.should.be.instanceOf(Date); m.createdAt.should.be.instanceOf(Date);
(m.createdAt >= now).should.be.true; (m.createdAt >= now).should.be.true;
}); });
done(); done();
}); });
it('should report \'$now\' when using $now as default value for string property',
function (done) {
var CustomModel = db.define('CustomModel', {
now: { type: String, default: '$now' }
}); });
var myCustomModel = CustomModel.create(function (err, m) { describe('Date $now function', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel2', {
now: { type: String, default: '$now' }
});
db.automigrate('CustomModel2', done);
});
it('should report \'$now\' as default value for string property',
function(done) {
var myCustomModel = CustomModel.create(function(err, m) {
should.not.exists(err);
m.now.should.be.instanceOf(String); m.now.should.be.instanceOf(String);
m.now.should.equal('$now'); m.now.should.equal('$now');
}); });
done(); done();
}); });
it('should generate a new id when "defaultFn" is "guid"', function (done) {
var CustomModel = db.define('CustomModel', {
guid: { type: String, defaultFn: 'guid' }
}); });
var inst = CustomModel.create(function (err, m) { describe('now defaultFn', function() {
m.guid.should.match(UUID_REGEXP); var CustomModel;
done();
});
});
it('should generate a new id when "defaultfn" is "uuid"', function (done) { before(function(done) {
var CustomModel = db.define('custommodel', { CustomModel = db.define('CustomModel3', {
guid: { type: String, defaultFn: 'uuid' }
});
var inst = CustomModel.create(function (err, m) {
m.guid.should.match(UUID_REGEXP);
done();
});
});
it('should generate current time when "defaultFn" is "now"', function (done) {
var CustomModel = db.define('CustomModel', {
now: { type: Date, defaultFn: 'now' } now: { type: Date, defaultFn: 'now' }
}); });
db.automigrate('CustomModel3', done);
});
it('should generate current time when "defaultFn" is "now"',
function(done) {
var now = Date.now(); var now = Date.now();
var inst = CustomModel.create(function (err, m) { var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.now.should.be.instanceOf(Date); m.now.should.be.instanceOf(Date);
m.now.should.be.within(now, now + 200); m.now.should.be.within(now, now + 200);
done(); done();
}); });
}); });
});
describe('guid defaultFn', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel4', {
guid: { type: String, defaultFn: 'guid' }
});
db.automigrate('CustomModel4', done);
});
it('should generate a new id when "defaultFn" is "guid"', function(done) {
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.guid.should.match(UUID_REGEXP);
done();
});
});
});
describe('uuid defaultFn', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel5', {
guid: { type: String, defaultFn: 'uuid' }
});
db.automigrate('CustomModel5', done);
});
it('should generate a new id when "defaultfn" is "uuid"', function(done) {
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.guid.should.match(UUID_REGEXP);
done();
});
});
});
// it('should work when constructor called as function', function() { // it('should work when constructor called as function', function() {
// var p = Person({name: 'John Resig'}); // var p = Person({name: 'John Resig'});