Merge branch 'release/2.16.0' into production

This commit is contained in:
Miroslav Bajtoš 2015-02-05 12:47:35 +01:00
commit 169e270925
12 changed files with 494 additions and 402 deletions

View File

@ -1,3 +1,21 @@
2015-02-05, Version 2.16.0
==========================
* Return 400 when client provides an incorrect value (Pradnya Baviskar)
* Relax "id" checks in test/manipulation.test.js (Miroslav Bajtoš)
* Fix typo (Raymond Feng)
* test: undefined property values are preserved (Miroslav Bajtoš)
* Remove undefined properties for create (Raymond Feng)
* Update to `should` to the latest version 4.6.3 (Miroslav Bajtoš)
* Relax the id equality test for mongodb object ids (Raymond Feng)
2015-02-02, Version 2.15.0
==========================
@ -944,6 +962,14 @@
* JSDoc improvements (Rand McKinney)
* validations: include more details in `err.message` (Miroslav Bajtoš)
2014-05-27, Version 1.5.4
=========================
* Bump version (Raymond Feng)
* Keep undefined/null values for the array type (Raymond Feng)
* Remove JSDocs for scopeMethods.add(acInst) and scopeMethods.remove(acInst) (crandmck)
@ -979,24 +1005,6 @@
* Remove remoting metadata (Ritchie Martori)
2014-05-27, Version 1.5.4
=========================
* Bump version (Raymond Feng)
* Keep undefined/null values for the array type (Raymond Feng)
* Remove JSDocs for scopeMethods.add(acInst) and scopeMethods.remove(acInst) (crandmck)
* Copy info from api-model.md to JSDoc (crandmck)
* validations: include more details in `err.message` (Miroslav Bajtoš)
* Add a path to show customer.orders(query, cb) (Raymond Feng)
* Add support for logical operator (AND/OR) (Raymond Feng)
2014-05-20, Version 1.5.2
=========================

View File

@ -231,7 +231,8 @@ DataAccessObject.create = function (data, callback) {
obj.trigger('save', function (saveDone) {
var _idName = idName(Model);
var modelName = Model.modelName;
this._adapter().create(modelName, this.constructor._forDB(obj.toObject(true)), function (err, id, rev) {
var val = removeUndefined(obj.toObject(true));
this._adapter().create(modelName, this.constructor._forDB(val), function (err, id, rev) {
if (id) {
obj.__data[_idName] = id;
defineReadonlyProp(obj, _idName, id);
@ -1022,7 +1023,7 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
}
function done(err, data) {
if (err) return cb(er);
if (err) return cb(err);
if (options.notify === false) {
return cb(err, data);

View File

@ -13,7 +13,9 @@ function List(items, itemType, parent) {
try {
items = JSON.parse(items);
} catch (e) {
throw new Error(util.format('could not create List from JSON string: %j', items));
var err = new Error(util.format('could not create List from JSON string: %j', items));
err.statusCode = 400;
throw err;
}
}
@ -22,7 +24,9 @@ function List(items, itemType, parent) {
items = items || [];
if (!Array.isArray(items)) {
throw new Error(util.format('Items must be an array: %j', items));
var err = new Error(util.format('Items must be an array: %j', items));
err.statusCode = 400;
throw err;
}
if(!itemType) {

View File

@ -1,6 +1,6 @@
{
"name": "loopback-datasource-juggler",
"version": "2.15.0",
"version": "2.16.0",
"description": "LoopBack DataSoure Juggler",
"keywords": [
"StrongLoop",
@ -24,7 +24,7 @@
],
"devDependencies": {
"mocha": "~1.20.1",
"should": "^1.3.0"
"should": "^4.6.3"
},
"dependencies": {
"async": "~0.9.0",

View File

@ -1,4 +1,5 @@
require('./datatype.test.js');
require('./basic-querying.test.js');
require('./manipulation.test.js');
require('./hooks.test.js');
require('./relations.test.js');

View File

@ -23,17 +23,43 @@ describe('datatypes', function () {
});
});
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();
});
it('should keep types when get read data from db', function (done) {
var d = new Date, id;
Model.create({
str: 'hello', date: d, num: '3', bool: 1, list: ['test'], arr: [1, 'str']
}, function (err, m) {
should.not.exist(err);
should.not.exists(err);
should.exist(m && m.id);
m.str.should.be.a('string');
m.num.should.be.a('number');
m.bool.should.be.a('boolean');
m.str.should.be.type('string');
m.num.should.be.type('number');
m.bool.should.be.type('boolean');
m.list[0].should.be.equal('test');
m.arr[0].should.be.equal(1);
m.arr[1].should.be.equal('str');
@ -45,9 +71,9 @@ describe('datatypes', function () {
Model.findById(id, function (err, m) {
should.not.exist(err);
should.exist(m);
m.str.should.be.a('string');
m.num.should.be.a('number');
m.bool.should.be.a('boolean');
m.str.should.be.type('string');
m.num.should.be.type('number');
m.bool.should.be.type('boolean');
m.list[0].should.be.equal('test');
m.arr[0].should.be.equal(1);
m.arr[1].should.be.equal('str');
@ -61,9 +87,9 @@ describe('datatypes', function () {
Model.findOne(function (err, m) {
should.not.exist(err);
should.exist(m);
m.str.should.be.a('string');
m.num.should.be.a('number');
m.bool.should.be.a('boolean');
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');
done();
@ -81,9 +107,9 @@ describe('datatypes', function () {
should.exist(m && m.id);
// sanity check initial types
m.str.should.be.a('string');
m.num.should.be.a('number');
m.bool.should.be.a('boolean');
m.str.should.be.type('string');
m.num.should.be.type('number');
m.bool.should.be.type('boolean');
id = m.id;
testDataInDB(function () {
testUpdate(function() {
@ -101,7 +127,7 @@ describe('datatypes', function () {
id: id, num: '10'
}, function (err, m) {
should.not.exist(err);
m.num.should.be.a('number');
m.num.should.be.type('number');
done();
});
});
@ -112,7 +138,7 @@ describe('datatypes', function () {
// verify that the value stored in the db is still an object
db.connector.find(Model.modelName, id, function (err, data) {
should.exist(data);
data.num.should.be.a('number');
data.num.should.be.type('number');
done();
});
}

View File

@ -43,7 +43,7 @@ describe('defaults', function () {
Server.find({ fields: ['host'] }, function (err, servers) {
servers[0].host.should.equal('localhost');
servers[0].should.have.property('host');
servers[0].should.not.have.property('port');
servers[0].should.have.property('port', undefined);
done();
});
});

View File

@ -142,7 +142,7 @@ describe('include', function () {
posts.should.have.length(3);
posts[0].title.should.equal('Post C');
posts[0].should.not.have.property('id'); // omitted
posts[0].should.have.property('id', undefined); // omitted
posts[0].author().should.be.instanceOf(User);
posts[0].author().name.should.equal('User A');
@ -167,7 +167,7 @@ describe('include', function () {
var author = posts[0].author();
author.name.should.equal('User A');
author.should.have.property('id');
author.should.not.have.property('age');
author.should.have.property('age', undefined);
done();
});

View File

@ -25,17 +25,17 @@ describe('ModelBuilder define model', 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');
modelBuilder.models.should.be.type('object').and.have.property('User').exactly(User);
modelBuilder.definitions.should.be.type('object').and.have.property('User');
var user = new User({name: 'Joe', age: 20, xyz: false});
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.have.property('xyz', false);
user.should.not.have.property('bio');
user.should.have.property('bio', undefined);
done(null, User);
});
@ -47,12 +47,12 @@ describe('ModelBuilder define model', function () {
var user = new User({name: 'Joe', age: 20});
User.modelName.should.equal('User');
user.should.be.a('object');
user.should.be.type('object');
user.should.have.property('name', 'Joe');
user.should.not.have.property('age');
user.toObject().should.not.have.property('age');
user.toObject(true).should.not.have.property('age');
user.should.not.have.property('bio');
user.should.have.property('bio', undefined);
done(null, User);
});
@ -102,7 +102,7 @@ describe('ModelBuilder define model', function () {
var user = new User({name: 'Joe', age: 20});
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
@ -142,7 +142,7 @@ describe('ModelBuilder define model', function () {
var user = new User({name: 'Joe', age: 20});
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
@ -180,8 +180,8 @@ describe('ModelBuilder define model', 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');
modelBuilder.models.should.be.type('object').and.have.property('User', User);
modelBuilder.definitions.should.be.type('object').and.have.property('User');
var user = new User({
name: 'Joe', age: 20,
@ -193,10 +193,10 @@ describe('ModelBuilder define model', function () {
});
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
user.should.have.property('bio', undefined);
user.should.have.property('address');
user.address.should.have.property('city', 'San Jose');
user.address.should.have.property('state', 'CA');
@ -236,7 +236,7 @@ describe('ModelBuilder define model', function () {
User.modelName.should.equal('User');
User.definition.properties.address.should.have.property('type', Address);
user.should.be.a('object');
user.should.be.type('object');
assert(user.name === 'Joe');
user.address.should.have.property('city', 'San Jose');
user.address.should.have.property('state', 'CA');
@ -432,7 +432,7 @@ describe('DataSource define model', function () {
User.create({name: 'Joe', age: 20}, function (err, user) {
User.modelName.should.equal('User');
user.should.be.a('object');
user.should.be.type('object');
assert(user.name === 'Joe');
assert(user.age === undefined);
assert(user.toObject().age === undefined);
@ -464,13 +464,13 @@ describe('DataSource define model', function () {
User.create({name: 'Joe', age: 20}, function (err, user) {
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
User.findById(user.id, function (err, user) {
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
@ -487,7 +487,7 @@ describe('DataSource define model', function () {
User.create({name: 'Joe', age: 20}, function (err, user) {
User.modelName.should.equal('User');
user.should.be.a('object').and.have.property('name', 'Joe');
user.should.be.type('object').and.have.property('name', 'Joe');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.should.not.have.property('bio');
@ -504,7 +504,7 @@ describe('DataSource define model', function () {
var user = new User({name: 'Joe', age: 20});
User.modelName.should.equal('User');
user.should.be.a('object');
user.should.be.type('object');
assert(user.name === 'Joe');
assert(user.age === undefined);
assert(user.toObject().age === undefined);
@ -550,7 +550,7 @@ describe('DataSource define model', function () {
var user = new User({name: 'Joe', age: 20}, {strict: false});
user.should.have.property('__strict', false);
user.should.be.a('object');
user.should.be.type('object');
user.should.have.property('name', 'Joe');
user.should.have.property('age', 20);
user.toObject().should.have.property('age', 20);
@ -1533,11 +1533,11 @@ describe('Load models from json', function () {
var customer = new Customer({name: 'Joe', age: 20, customerId: 'c01'});
customer.should.be.a('object').and.have.property('name', 'Joe');
customer.should.be.type('object').and.have.property('name', 'Joe');
customer.should.have.property('name', 'Joe');
customer.should.have.property('age', 20);
customer.should.have.property('customerId', 'c01');
customer.should.not.have.property('bio');
customer.should.have.property('bio', undefined);
// The properties are defined at prototype level
assert.equal(Object.keys(customer).filter(function (k) {

View File

@ -50,7 +50,7 @@ describe('manipulation', function () {
should.not.exist(err);
should.exist(p);
Person.findById(p.id, function (err, person) {
person.id.should.equal(p.id);
person.id.should.eql(p.id);
person.name.should.equal('Anatoliy');
done();
});
@ -122,7 +122,7 @@ describe('manipulation', function () {
should.exist(p);
should.not.exists(p.name);
Person.findById(p.id, function (err, person) {
person.id.should.equal(p.id);
person.id.should.eql(p.id);
should.not.exists(person.name);
done();
});
@ -192,6 +192,29 @@ describe('manipulation', function () {
done();
});
});
it('should preserve properties with "undefined" value', function(done) {
Person.create(
{ name: 'a-name', gender: undefined },
function(err, created) {
if (err) return done(err);
created.toObject().should.have.properties({
id: created.id,
name: 'a-name',
gender: undefined
});
Person.findById(created.id, function(err, found) {
if (err) return done(err);
found.toObject().should.have.properties({
id: created.id,
name: 'a-name',
gender: undefined
});
done();
});
});
});
});
describe('save', function () {
@ -349,6 +372,31 @@ describe('manipulation', function () {
});
});
});
it('should preserve properties with "undefined" value', function(done) {
Person.create(
{ name: 'a-name', gender: undefined },
function(err, instance) {
if (err) return done(err);
instance.toObject().should.have.properties({
id: instance.id,
name: 'a-name',
gender: undefined
});
Person.updateOrCreate(
{ id: instance.id, name: 'updated name' },
function(err, updated) {
if (err) return done(err);
updated.toObject().should.have.properties({
id: instance.id,
name: 'updated name',
gender: undefined
});
done();
});
});
});
});
describe('destroy', function () {

View File

@ -47,8 +47,8 @@ describe('relations', function () {
b.chapters.should.be.an.instanceOf(Function);
b.users.should.be.an.instanceOf(Function);
b.authors.should.be.an.instanceOf(Function);
Object.keys((new Chapter).toObject()).should.include('bookId');
Object.keys((new Author).toObject()).should.include('projectId');
Object.keys((new Chapter).toObject()).should.containEql('bookId');
Object.keys((new Author).toObject()).should.containEql('projectId');
db.automigrate(done);
});
@ -56,7 +56,7 @@ describe('relations', function () {
it('can be declared in short form', function (done) {
Author.hasMany('readers');
(new Author).readers.should.be.an.instanceOf(Function);
Object.keys((new Reader).toObject()).should.include('authorId');
Object.keys((new Reader).toObject()).should.containEql('authorId');
db.autoupdate(done);
});
@ -1561,12 +1561,12 @@ describe('relations', function () {
// syntax 1 (old)
Item.belongsTo(List);
Object.keys((new Item).toObject()).should.include('listId');
Object.keys((new Item).toObject()).should.containEql('listId');
(new Item).list.should.be.an.instanceOf(Function);
// syntax 2 (new)
Fear.belongsTo('mind');
Object.keys((new Fear).toObject()).should.include('mindId');
Object.keys((new Fear).toObject()).should.containEql('mindId');
(new Fear).mind.should.be.an.instanceOf(Function);
// (new Fear).mind.build().should.be.an.instanceOf(Mind);
});
@ -1709,8 +1709,8 @@ describe('relations', function () {
p.personId.should.eql(personCreated.id);
p.person(function(err, person) {
person.name.should.equal('Fred');
person.should.not.have.property('age');
person.should.not.have.property('passportNotes');
person.should.have.property('age', undefined);
person.should.have.property('passportNotes', undefined);
done();
});
});
@ -1769,7 +1769,7 @@ describe('relations', function () {
it('can be declared using hasOne method', function () {
Supplier.hasOne(Account, { properties: { name: 'supplierName' } });
Object.keys((new Account()).toObject()).should.include('supplierId');
Object.keys((new Account()).toObject()).should.containEql('supplierId');
(new Supplier()).account.should.be.an.instanceOf(Function);
});
@ -1821,7 +1821,7 @@ describe('relations', function () {
function(err, act) {
should.not.exist(e);
act.supplierName.should.equal('Supplier A');
act.supplierId.should.equal(supplierId);
act.supplierId.should.eql(supplierId);
done();
});
});
@ -1948,7 +1948,7 @@ describe('relations', function () {
properties: {name: 'supplierName'},
foreignKey: 'sid'
});
Object.keys((new Account()).toObject()).should.include('sid');
Object.keys((new Account()).toObject()).should.containEql('sid');
(new Supplier()).account.should.be.an.instanceOf(Function);
});

View File

@ -13,13 +13,17 @@ describe('dataSource', function () {
});
it('should clone existing model', function () {
// Workaround for a possible bug in `should`
// where it takes ages to evaluate `should.equal` for complex objects
this.timeout(20000);
SlaveModel = slave.copyModel(Model);
SlaveModel.dataSource.should.eql(slave);
slave.should.not.eql(db);
SlaveModel.dataSource.should.equal(slave);
slave.should.not.equal(db);
var sm = new SlaveModel;
sm.should.be.instanceOf(Model);
sm.getDataSource().should.not.eql(db);
sm.getDataSource().should.eql(slave);
sm.getDataSource().should.not.equal(db);
sm.getDataSource().should.equal(slave);
});
it('should automigrate', function (done) {