2013-04-06 10:57:12 +00:00
|
|
|
// This test written in mocha+should.js
|
|
|
|
var should = require('./init.js');
|
|
|
|
|
2013-04-06 10:34:16 +00:00
|
|
|
var db, Model;
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('datatypes', function () {
|
2013-04-06 10:34:16 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
2015-01-08 14:34:04 +00:00
|
|
|
Nested = db.define('Nested', {});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
Model = db.define('Model', {
|
|
|
|
str: String,
|
|
|
|
date: Date,
|
|
|
|
num: Number,
|
|
|
|
bool: Boolean,
|
2014-02-13 06:49:54 +00:00
|
|
|
list: {type: [String]},
|
2015-01-08 14:34:04 +00:00
|
|
|
arr: Array,
|
|
|
|
nested: Nested
|
2013-04-06 10:34:16 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
db.automigrate(function () {
|
|
|
|
Model.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
2013-04-06 10:34:16 +00:00
|
|
|
|
2015-02-05 11:28:42 +00:00
|
|
|
it('should return 400 when property of type array is set to string value',
|
|
|
|
function (done) {
|
|
|
|
var myModel = db.define('myModel', {
|
|
|
|
list: { type: ['object'] }
|
|
|
|
});
|
|
|
|
|
|
|
|
(function(){
|
|
|
|
myModel.create({ list: 'This string will crash the server' });
|
|
|
|
}).should.throw({ statusCode: 400 });
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return 400 when property of type array is set to object value',
|
|
|
|
function (done) {
|
|
|
|
var myModel = db.define('myModel', {
|
|
|
|
list: { type: ['object'] }
|
|
|
|
});
|
|
|
|
|
|
|
|
(function(){
|
|
|
|
myModel.create({ list: { key: 'This string will crash the server' } });
|
|
|
|
}).should.throw({ statusCode: 400 });
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should keep types when get read data from db', function (done) {
|
|
|
|
var d = new Date, id;
|
2013-04-07 13:59:24 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
Model.create({
|
2014-03-03 23:52:49 +00:00
|
|
|
str: 'hello', date: d, num: '3', bool: 1, list: ['test'], arr: [1, 'str']
|
2014-01-24 17:09:53 +00:00
|
|
|
}, function (err, m) {
|
2015-02-03 10:37:43 +00:00
|
|
|
should.not.exists(err);
|
2014-01-24 17:09:53 +00:00
|
|
|
should.exist(m && m.id);
|
2015-02-03 10:37:43 +00:00
|
|
|
m.str.should.be.type('string');
|
|
|
|
m.num.should.be.type('number');
|
|
|
|
m.bool.should.be.type('boolean');
|
2014-03-03 23:52:49 +00:00
|
|
|
m.list[0].should.be.equal('test');
|
|
|
|
m.arr[0].should.be.equal(1);
|
|
|
|
m.arr[1].should.be.equal('str');
|
2014-01-24 17:09:53 +00:00
|
|
|
id = m.id;
|
|
|
|
testFind(testAll);
|
|
|
|
});
|
2013-04-06 10:34:16 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
function testFind(next) {
|
|
|
|
Model.findById(id, function (err, m) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(m);
|
2015-02-03 10:37:43 +00:00
|
|
|
m.str.should.be.type('string');
|
|
|
|
m.num.should.be.type('number');
|
|
|
|
m.bool.should.be.type('boolean');
|
2014-03-03 23:52:49 +00:00
|
|
|
m.list[0].should.be.equal('test');
|
|
|
|
m.arr[0].should.be.equal(1);
|
|
|
|
m.arr[1].should.be.equal('str');
|
2014-01-24 17:09:53 +00:00
|
|
|
m.date.should.be.an.instanceOf(Date);
|
|
|
|
m.date.toString().should.equal(d.toString(), 'Time must match');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
2013-04-06 10:34:16 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
function testAll() {
|
|
|
|
Model.findOne(function (err, m) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(m);
|
2015-02-03 10:37:43 +00:00
|
|
|
m.str.should.be.type('string');
|
|
|
|
m.num.should.be.type('number');
|
|
|
|
m.bool.should.be.type('boolean');
|
2014-01-24 17:09:53 +00:00
|
|
|
m.date.should.be.an.instanceOf(Date);
|
|
|
|
m.date.toString().should.equal(d.toString(), 'Time must match');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
2013-04-07 13:59:24 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-06 10:34:16 +00:00
|
|
|
|
2014-02-07 12:50:35 +00:00
|
|
|
it('should respect data types when updating attributes', function (done) {
|
|
|
|
var d = new Date, id;
|
|
|
|
|
|
|
|
Model.create({
|
2014-02-13 06:49:54 +00:00
|
|
|
str: 'hello', date: d, num: '3', bool: 1}, function(err, m) {
|
2014-02-07 12:50:35 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(m && m.id);
|
|
|
|
|
|
|
|
// sanity check initial types
|
2015-02-03 10:37:43 +00:00
|
|
|
m.str.should.be.type('string');
|
|
|
|
m.num.should.be.type('number');
|
|
|
|
m.bool.should.be.type('boolean');
|
2014-02-07 12:50:35 +00:00
|
|
|
id = m.id;
|
|
|
|
testDataInDB(function () {
|
|
|
|
testUpdate(function() {
|
|
|
|
testDataInDB(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function testUpdate(done) {
|
|
|
|
Model.findById(id, function(err, m) {
|
|
|
|
should.not.exist(err);
|
|
|
|
|
|
|
|
// update using updateAttributes
|
|
|
|
m.updateAttributes({
|
2015-03-16 16:25:38 +00:00
|
|
|
id: m.id, num: '10'
|
2014-02-07 12:50:35 +00:00
|
|
|
}, function (err, m) {
|
|
|
|
should.not.exist(err);
|
2015-02-03 10:37:43 +00:00
|
|
|
m.num.should.be.type('number');
|
2014-02-07 12:50:35 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDataInDB(done) {
|
|
|
|
|
|
|
|
// verify that the value stored in the db is still an object
|
|
|
|
db.connector.find(Model.modelName, id, function (err, data) {
|
|
|
|
should.exist(data);
|
2015-02-03 10:37:43 +00:00
|
|
|
data.num.should.be.type('number');
|
2014-02-07 12:50:35 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-01-08 14:34:04 +00:00
|
|
|
it('should not coerce nested objects into ModelConstructor types', function() {
|
|
|
|
var coerced = Model._coerce({ nested: { foo: 'bar' } });
|
|
|
|
coerced.nested.constructor.name.should.equal('Object');
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2013-04-06 10:34:16 +00:00
|
|
|
});
|