2016-04-01 22:25:16 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2013-04-06 10:57:12 +00:00
|
|
|
// This test written in mocha+should.js
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-12-05 14:14:09 +00:00
|
|
|
/* global getSchema:false */
|
2018-12-07 16:13:48 +00:00
|
|
|
const should = require('./init.js');
|
2013-04-06 10:57:12 +00:00
|
|
|
|
2019-04-16 14:51:44 +00:00
|
|
|
let db, Model, modelWithDecimalArray, dateArrayModel, numArrayModel;
|
2013-04-06 10:34:16 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('datatypes', function() {
|
|
|
|
before(function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
db = getSchema();
|
2018-12-07 16:13:48 +00:00
|
|
|
const Nested = db.define('Nested', {});
|
|
|
|
const modelTableSchema = {
|
2014-01-24 17:09:53 +00:00
|
|
|
str: String,
|
|
|
|
date: Date,
|
|
|
|
num: Number,
|
|
|
|
bool: Boolean,
|
2016-08-19 17:46:59 +00:00
|
|
|
list: {type: [String]},
|
2015-01-08 14:34:04 +00:00
|
|
|
arr: Array,
|
2016-04-01 11:48:17 +00:00
|
|
|
nested: Nested,
|
2017-04-06 20:04:16 +00:00
|
|
|
};
|
|
|
|
Model = db.define('Model', modelTableSchema);
|
2019-04-16 14:51:44 +00:00
|
|
|
modelWithDecimalArray = db.define('modelWithDecimalArray', {
|
|
|
|
randomReview: {
|
|
|
|
type: [String],
|
|
|
|
mongodb: {
|
|
|
|
dataType: 'Decimal128',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
dateArrayModel = db.define('dateArrayModel', {
|
|
|
|
bunchOfDates: [Date],
|
|
|
|
bunchOfOtherDates: {
|
|
|
|
type: [Date],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
numArrayModel = db.define('numArrayModel', {
|
|
|
|
bunchOfNums: [Number],
|
|
|
|
});
|
|
|
|
db.automigrate(['Model', 'modelWithDecimalArray', 'dateArrayModel', 'numArrayModel'], done);
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-06 10:34:16 +00:00
|
|
|
|
2019-03-07 11:20:29 +00:00
|
|
|
it('should resolve top-level "type" property correctly', function() {
|
|
|
|
const Account = db.define('Account', {
|
|
|
|
type: String,
|
|
|
|
id: String,
|
|
|
|
});
|
|
|
|
Account.definition.properties.type.type.should.equal(String);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve "type" sub-property correctly', function() {
|
|
|
|
const Account = db.define('Account', {
|
|
|
|
item: {type: {
|
|
|
|
itemname: {type: String},
|
|
|
|
type: {type: String},
|
|
|
|
}},
|
|
|
|
});
|
|
|
|
Account.definition.properties.item.type.should.not.equal(String);
|
|
|
|
});
|
2019-04-16 14:50:54 +00:00
|
|
|
it('should resolve array prop with connector specific metadata', function() {
|
2019-04-16 14:51:44 +00:00
|
|
|
const props = modelWithDecimalArray.definition.properties;
|
|
|
|
props.randomReview.type.should.deepEqual(Array(String));
|
|
|
|
props.randomReview.mongodb.should.deepEqual({dataType: 'Decimal128'});
|
2019-04-16 14:50:54 +00:00
|
|
|
});
|
|
|
|
|
2019-04-16 14:51:44 +00:00
|
|
|
it('should coerce array of dates from string', function() {
|
2019-04-16 14:50:54 +00:00
|
|
|
const dateVal = new Date('2019-02-21T12:00:00').toISOString();
|
2019-04-16 14:51:44 +00:00
|
|
|
return dateArrayModel.create({
|
2019-04-16 14:50:54 +00:00
|
|
|
bunchOfDates: [dateVal,
|
|
|
|
dateVal,
|
|
|
|
dateVal],
|
|
|
|
bunchOfOtherDates: [dateVal,
|
|
|
|
dateVal,
|
|
|
|
dateVal],
|
2019-04-16 14:51:44 +00:00
|
|
|
}).then((created) => {
|
|
|
|
created.bunchOfDates[0].should.be.an.instanceOf(Date);
|
|
|
|
created.bunchOfDates[0].should.deepEqual(new Date(dateVal));
|
|
|
|
created.bunchOfOtherDates[0].should.be.an.instanceOf(Date);
|
|
|
|
created.bunchOfOtherDates[0].should.deepEqual(new Date(dateVal));
|
2019-04-16 14:50:54 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-04-16 14:51:44 +00:00
|
|
|
it('should coerce array of numbers from string', function() {
|
2019-04-16 14:50:54 +00:00
|
|
|
const dateVal = new Date('2019-02-21T12:00:00').toISOString();
|
2019-04-16 14:51:44 +00:00
|
|
|
return numArrayModel.create({
|
2019-04-16 14:50:54 +00:00
|
|
|
bunchOfNums: ['1',
|
|
|
|
'2',
|
|
|
|
'3'],
|
2019-04-16 14:51:44 +00:00
|
|
|
})
|
|
|
|
.then((created) => {
|
|
|
|
created.bunchOfNums[0].should.be.an.instanceOf(Number);
|
|
|
|
created.bunchOfNums[0].should.equal(1);
|
|
|
|
});
|
2019-04-16 14:50:54 +00:00
|
|
|
});
|
2019-03-07 11:20:29 +00:00
|
|
|
|
2015-02-05 11:28:42 +00:00
|
|
|
it('should return 400 when property of type array is set to string value',
|
2016-04-01 11:48:17 +00:00
|
|
|
function(done) {
|
2018-12-07 16:13:48 +00:00
|
|
|
const myModel = db.define('myModel', {
|
2016-08-19 17:46:59 +00:00
|
|
|
list: {type: ['object']},
|
2015-02-05 11:28:42 +00:00
|
|
|
});
|
|
|
|
|
2017-07-24 04:58:35 +00:00
|
|
|
myModel.create({list: 'This string will crash the server'}, function(err) {
|
|
|
|
(err.statusCode).should.equal(400);
|
|
|
|
done();
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-02-05 11:28:42 +00:00
|
|
|
|
|
|
|
it('should return 400 when property of type array is set to object value',
|
2016-04-01 11:48:17 +00:00
|
|
|
function(done) {
|
2018-12-07 16:13:48 +00:00
|
|
|
const myModel = db.define('myModel', {
|
2016-08-19 17:46:59 +00:00
|
|
|
list: {type: ['object']},
|
2015-02-05 11:28:42 +00:00
|
|
|
});
|
|
|
|
|
2017-07-24 04:58:35 +00:00
|
|
|
myModel.create({list: {key: 'This string will crash the server'}}, function(err) {
|
|
|
|
(err.statusCode).should.equal(400);
|
|
|
|
done();
|
|
|
|
});
|
2016-04-01 11:48:17 +00:00
|
|
|
});
|
2015-02-05 11:28:42 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should keep types when get read data from db', function(done) {
|
2018-12-07 15:22:36 +00:00
|
|
|
const d = new Date('2015-01-01T12:00:00');
|
|
|
|
let id;
|
2015-12-23 23:41:16 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
Model.create({
|
2016-04-01 11:48:17 +00:00
|
|
|
str: 'hello', date: d, num: '3', bool: 1, list: ['test'], arr: [1, 'str'],
|
|
|
|
}, 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-12-23 23:41:16 +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) {
|
2016-04-01 11:48:17 +00:00
|
|
|
Model.findById(id, function(err, m) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(m);
|
2015-12-23 23:41:16 +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() {
|
2016-04-01 11:48:17 +00:00
|
|
|
Model.findOne(function(err, m) {
|
2014-01-24 17:09:53 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(m);
|
2015-12-23 23:41:16 +00:00
|
|
|
m.str.should.be.type('string');
|
|
|
|
m.num.should.be.type('number');
|
|
|
|
m.bool.should.be.type('boolean');
|
|
|
|
m.date.should.be.an.instanceOf(Date);
|
|
|
|
m.date.toString().should.equal(d.toString(), 'Time must match');
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-04-06 10:34:16 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should respect data types when updating attributes', function(done) {
|
2018-12-07 15:22:36 +00:00
|
|
|
const d = new Date;
|
|
|
|
let id;
|
2014-02-07 12:50:35 +00:00
|
|
|
|
|
|
|
Model.create({
|
2016-08-19 17:46:59 +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-12-23 23:41:16 +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;
|
2016-04-01 11:48:17 +00:00
|
|
|
testDataInDB(function() {
|
2014-02-07 12:50:35 +00:00
|
|
|
testUpdate(function() {
|
|
|
|
testDataInDB(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function testUpdate(done) {
|
|
|
|
Model.findById(id, function(err, m) {
|
|
|
|
should.not.exist(err);
|
|
|
|
// update using updateAttributes
|
|
|
|
m.updateAttributes({
|
2017-04-06 20:04:16 +00:00
|
|
|
id: m.id, num: 10,
|
2016-04-01 11:48:17 +00:00
|
|
|
}, function(err, m) {
|
2014-02-07 12:50:35 +00:00
|
|
|
should.not.exist(err);
|
2015-12-23 23:41:16 +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
|
2015-05-13 16:36:29 +00:00
|
|
|
function cb(err, data) {
|
2014-02-07 12:50:35 +00:00
|
|
|
should.exist(data);
|
2015-12-23 23:41:16 +00:00
|
|
|
data.num.should.be.type('number');
|
2014-02-07 12:50:35 +00:00
|
|
|
done();
|
2015-05-13 16:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (db.connector.find.length === 4) {
|
|
|
|
db.connector.find(Model.modelName, id, {}, cb);
|
|
|
|
} else {
|
|
|
|
db.connector.find(Model.modelName, id, cb);
|
|
|
|
}
|
2014-02-07 12:50:35 +00:00
|
|
|
}
|
|
|
|
});
|
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() {
|
2018-12-07 16:13:48 +00:00
|
|
|
const coerced = Model._coerce({nested: {foo: 'bar'}});
|
2016-04-01 11:48:17 +00:00
|
|
|
coerced.nested.constructor.name.should.equal('Object');
|
2015-01-08 14:34:04 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2015-04-22 17:57:48 +00:00
|
|
|
it('rejects array value converted to NaN for a required property',
|
2018-06-12 07:13:32 +00:00
|
|
|
function(done) {
|
|
|
|
db = getSchema();
|
|
|
|
Model = db.define('RequiredNumber', {
|
|
|
|
num: {type: Number, required: true},
|
|
|
|
});
|
|
|
|
db.automigrate(['Model'], function() {
|
|
|
|
Model.create({num: [1, 2, 3]}, function(err, inst) {
|
|
|
|
should.exist(err);
|
|
|
|
err.should.have.property('name').equal('ValidationError');
|
|
|
|
done();
|
|
|
|
});
|
2015-04-22 17:57:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-09-28 00:12:30 +00:00
|
|
|
it('handles null data', (done) => {
|
|
|
|
db = getSchema();
|
2017-10-16 20:07:01 +00:00
|
|
|
Model = db.define('HandleNullModel', {
|
2017-09-28 00:12:30 +00:00
|
|
|
data: {type: 'string'},
|
|
|
|
});
|
2017-10-16 20:07:01 +00:00
|
|
|
db.automigrate(['HandleNullModel'], function() {
|
2018-12-07 16:13:48 +00:00
|
|
|
const a = new Model(null);
|
2017-09-28 00:12:30 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-03-27 09:45:14 +00:00
|
|
|
describe('model option persistUndefinedAsNull', function() {
|
2018-12-07 16:13:48 +00:00
|
|
|
let TestModel, isStrict;
|
2015-03-27 09:45:14 +00:00
|
|
|
before(function(done) {
|
2017-07-11 18:56:39 +00:00
|
|
|
db = getSchema();
|
2015-03-27 09:45:14 +00:00
|
|
|
TestModel = db.define(
|
|
|
|
'TestModel',
|
|
|
|
{
|
2016-09-07 18:24:48 +00:00
|
|
|
name: {type: String, required: false},
|
2016-08-19 17:46:59 +00:00
|
|
|
desc: {type: String, required: false},
|
|
|
|
stars: {type: Number, required: false},
|
2015-03-27 09:45:14 +00:00
|
|
|
},
|
|
|
|
{
|
2016-04-01 11:48:17 +00:00
|
|
|
persistUndefinedAsNull: true,
|
2018-07-16 06:46:25 +00:00
|
|
|
}
|
|
|
|
);
|
2015-03-27 09:45:14 +00:00
|
|
|
|
2015-03-30 08:45:55 +00:00
|
|
|
isStrict = TestModel.definition.settings.strict;
|
|
|
|
|
2015-08-27 22:59:58 +00:00
|
|
|
db.automigrate(['TestModel'], done);
|
2015-03-27 09:45:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should set missing optional properties to null', function(done) {
|
2018-12-07 16:13:48 +00:00
|
|
|
const EXPECTED = {desc: null, stars: null};
|
2016-08-19 17:46:59 +00:00
|
|
|
TestModel.create({name: 'a-test-name'}, function(err, created) {
|
2015-03-27 09:45:14 +00:00
|
|
|
if (err) return done(err);
|
2015-12-23 23:41:16 +00:00
|
|
|
created.should.have.properties(EXPECTED);
|
2015-03-27 09:45:14 +00:00
|
|
|
|
|
|
|
TestModel.findById(created.id, function(err, found) {
|
|
|
|
if (err) return done(err);
|
2015-12-23 23:41:16 +00:00
|
|
|
found.should.have.properties(EXPECTED);
|
2015-03-27 09:45:14 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-12-23 23:41:16 +00:00
|
|
|
it('should convert property value undefined to null', function(done) {
|
2018-12-07 16:13:48 +00:00
|
|
|
const EXPECTED = {desc: null, extra: null};
|
|
|
|
const data = {desc: undefined, extra: undefined};
|
2015-03-30 08:45:55 +00:00
|
|
|
if (isStrict) {
|
|
|
|
// SQL-based connectors don't support dynamic properties
|
|
|
|
delete EXPECTED.extra;
|
2016-09-07 18:24:48 +00:00
|
|
|
delete data.extra;
|
2015-03-30 08:45:55 +00:00
|
|
|
}
|
2015-03-27 09:45:14 +00:00
|
|
|
TestModel.create(data, function(err, created) {
|
|
|
|
if (err) return done(err);
|
2015-03-30 08:45:55 +00:00
|
|
|
|
2015-12-23 23:41:16 +00:00
|
|
|
created.should.have.properties(EXPECTED);
|
2015-03-27 09:45:14 +00:00
|
|
|
|
|
|
|
TestModel.findById(created.id, function(err, found) {
|
|
|
|
if (err) return done(err);
|
2015-12-23 23:41:16 +00:00
|
|
|
found.should.have.properties(EXPECTED);
|
2015-03-27 09:45:14 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should convert undefined to null in the setter', function() {
|
2018-12-07 16:13:48 +00:00
|
|
|
const inst = new TestModel();
|
2015-03-27 09:45:14 +00:00
|
|
|
inst.desc = undefined;
|
|
|
|
inst.should.have.property('desc', null);
|
|
|
|
inst.toObject().should.have.property('desc', null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should use null in unsetAttribute()', function() {
|
2018-12-07 16:13:48 +00:00
|
|
|
const inst = new TestModel();
|
2015-03-27 09:45:14 +00:00
|
|
|
inst.unsetAttribute('stars');
|
|
|
|
inst.should.have.property('stars', null);
|
|
|
|
inst.toObject().should.have.property('stars', null);
|
|
|
|
});
|
|
|
|
|
2015-12-23 23:41:16 +00:00
|
|
|
it('should convert undefined to null on save', function(done) {
|
2018-12-07 16:13:48 +00:00
|
|
|
const EXPECTED = {desc: null, stars: null, extra: null, dx: null};
|
2015-03-30 08:45:55 +00:00
|
|
|
if (isStrict) {
|
|
|
|
// SQL-based connectors don't support dynamic properties
|
|
|
|
delete EXPECTED.extra;
|
|
|
|
delete EXPECTED.dx;
|
|
|
|
}
|
|
|
|
|
2015-03-27 09:45:14 +00:00
|
|
|
TestModel.create({}, function(err, created) {
|
|
|
|
if (err) return done(err);
|
|
|
|
created.desc = undefined; // Note: this is may be a no-op
|
|
|
|
created.unsetAttribute('stars');
|
|
|
|
created.extra = undefined;
|
|
|
|
created.__data.dx = undefined;
|
|
|
|
|
|
|
|
created.save(function(err, saved) {
|
|
|
|
if (err) return done(err);
|
2015-03-30 08:45:55 +00:00
|
|
|
|
2015-12-23 23:41:16 +00:00
|
|
|
created.should.have.properties(EXPECTED);
|
|
|
|
saved.should.have.properties(EXPECTED);
|
2015-03-27 09:45:14 +00:00
|
|
|
|
2015-05-13 16:36:29 +00:00
|
|
|
function cb(err, found) {
|
|
|
|
if (err) return done(err);
|
|
|
|
should.exist(found[0]);
|
|
|
|
found[0].should.have.properties(EXPECTED);
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TestModel.dataSource.connector.all.length === 4) {
|
|
|
|
TestModel.dataSource.connector.all(
|
|
|
|
TestModel.modelName,
|
2016-08-19 17:46:59 +00:00
|
|
|
{where: {id: created.id}},
|
2015-05-13 16:36:29 +00:00
|
|
|
{},
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
TestModel.dataSource.connector.all(
|
|
|
|
TestModel.modelName,
|
2016-08-19 17:46:59 +00:00
|
|
|
{where: {id: created.id}},
|
2015-05-13 16:36:29 +00:00
|
|
|
cb
|
|
|
|
);
|
|
|
|
}
|
2015-03-27 09:45:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should convert undefined to null in toObject()', function() {
|
2018-12-07 16:13:48 +00:00
|
|
|
const inst = new TestModel();
|
2015-03-27 09:45:14 +00:00
|
|
|
inst.desc = undefined; // Note: this may be a no-op
|
|
|
|
inst.unsetAttribute('stars');
|
|
|
|
inst.extra = undefined;
|
|
|
|
inst.__data.dx = undefined;
|
|
|
|
|
2015-12-23 23:41:16 +00:00
|
|
|
inst.toObject(false).should.have.properties({
|
2016-04-01 11:48:17 +00:00
|
|
|
desc: null, stars: null, extra: null, dx: null,
|
2015-12-23 23:41:16 +00:00
|
|
|
});
|
2015-03-27 09:45:14 +00:00
|
|
|
});
|
|
|
|
});
|
2013-04-06 10:34:16 +00:00
|
|
|
});
|