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,68 +447,109 @@ 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);
});
var now = Date.now(); it('should report current date as default value for date property',
function(done) {
var now = Date.now();
var myCustomModel = CustomModel.create(function (err, m) { var myCustomModel = CustomModel.create(function(err, m) {
m.createdAt.should.be.instanceOf(Date); should.not.exists(err);
(m.createdAt >= now).should.be.true; m.createdAt.should.be.instanceOf(Date);
(m.createdAt >= now).should.be.true;
});
done();
}); });
done();
}); });
it('should report \'$now\' when using $now as default value for string property', describe('Date $now function', function() {
function (done) { var CustomModel;
var CustomModel = db.define('CustomModel', {
before(function(done) {
CustomModel = db.define('CustomModel2', {
now: { type: String, default: '$now' } now: { type: String, default: '$now' }
}); });
db.automigrate('CustomModel2', done);
});
var myCustomModel = CustomModel.create(function (err, m) { it('should report \'$now\' as default value for string property',
m.now.should.be.instanceOf(String); function(done) {
m.now.should.equal('$now'); var myCustomModel = CustomModel.create(function(err, m) {
should.not.exists(err);
m.now.should.be.instanceOf(String);
m.now.should.equal('$now');
});
done();
}); });
done();
}); });
it('should generate a new id when "defaultFn" is "guid"', function (done) { describe('now defaultFn', function() {
var CustomModel = db.define('CustomModel', { var CustomModel;
guid: { type: String, defaultFn: 'guid' }
before(function(done) {
CustomModel = db.define('CustomModel3', {
now: { type: Date, defaultFn: 'now' }
});
db.automigrate('CustomModel3', done);
}); });
var inst = CustomModel.create(function (err, m) { it('should generate current time when "defaultFn" is "now"',
m.guid.should.match(UUID_REGEXP); function(done) {
done(); var now = Date.now();
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.now.should.be.instanceOf(Date);
m.now.should.be.within(now, now + 200);
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();
});
}); });
}); });
it('should generate a new id when "defaultfn" is "uuid"', function (done) { describe('uuid defaultFn', function() {
var CustomModel = db.define('custommodel', { var CustomModel;
guid: { type: String, defaultFn: 'uuid' }
before(function(done) {
CustomModel = db.define('CustomModel5', {
guid: { type: String, defaultFn: 'uuid' }
});
db.automigrate('CustomModel5', done);
}); });
var inst = CustomModel.create(function (err, m) { it('should generate a new id when "defaultfn" is "uuid"', function(done) {
m.guid.should.match(UUID_REGEXP); var inst = CustomModel.create(function(err, m) {
done(); should.not.exists(err);
}); 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' }
});
var now = Date.now();
var inst = CustomModel.create(function (err, m) {
m.now.should.be.instanceOf(Date);
m.now.should.be.within(now, now + 200);
done();
}); });
}); });