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

@ -8,7 +8,7 @@ describe('datatypes', function () {
before(function (done) {
db = getSchema();
Nested = db.define('Nested', {});
Model = db.define('Model', {
str: String,
date: Date,
@ -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,15 +138,15 @@ 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();
});
}
});
it('should not coerce nested objects into ModelConstructor types', function() {
var coerced = Model._coerce({ nested: { foo: 'bar' } });
coerced.nested.constructor.name.should.equal('Object');
});
});

View File

@ -35,7 +35,7 @@ describe('defaults', function () {
done();
});
});
it('should ignore defaults with limited fields', function (done) {
Server.create({ host: 'localhost', port: 8080 }, function(err, s) {
should.not.exist(err);
@ -43,10 +43,10 @@ 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

@ -79,7 +79,7 @@ describe('include', function () {
done();
});
});
it('should fetch Passport - Owner - Posts - alternate syntax', function (done) {
Passport.find({include: {owner: {relation: 'posts'}}}, function (err, passports) {
should.not.exist(err);
@ -120,7 +120,7 @@ describe('include', function () {
done();
});
});
it('should fetch Passports with include scope on Posts', function (done) {
Passport.find({
include: {owner: {relation: 'posts', scope:{
@ -131,31 +131,31 @@ describe('include', function () {
should.not.exist(err);
should.exist(passports);
passports.length.should.equal(3);
var passport = passports[0];
passport.number.should.equal('1');
passport.owner().name.should.equal('User A');
var owner = passport.owner().toObject();
var posts = passport.owner().posts();
posts.should.be.an.array;
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');
posts[1].title.should.equal('Post B');
posts[1].author().name.should.equal('User A');
posts[2].title.should.equal('Post A');
posts[2].author().name.should.equal('User A');
done();
});
});
it('should fetch Users with include scope on Posts - belongsTo', function (done) {
Post.find({
include: { relation: 'author', scope:{ fields: ['name'] }}
@ -163,16 +163,16 @@ describe('include', function () {
should.not.exist(err);
should.exist(posts);
posts.length.should.equal(5);
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();
});
});
it('should fetch Users with include scope on Posts - hasMany', function (done) {
User.find({
include: {relation: 'posts', scope:{
@ -182,27 +182,27 @@ describe('include', function () {
should.not.exist(err);
should.exist(users);
users.length.should.equal(5);
users[0].name.should.equal('User A');
users[1].name.should.equal('User B');
var posts = users[0].posts();
posts.should.be.an.array;
posts.should.have.length(3);
posts[0].title.should.equal('Post C');
posts[1].title.should.equal('Post B');
posts[2].title.should.equal('Post A');
var posts = users[1].posts();
posts.should.be.an.array;
posts.should.have.length(1);
posts[0].title.should.equal('Post D');
done();
});
});
it('should fetch Users with include scope on Passports - hasMany', function (done) {
User.find({
include: {relation: 'passports', scope:{
@ -212,14 +212,14 @@ describe('include', function () {
should.not.exist(err);
should.exist(users);
users.length.should.equal(5);
users[0].name.should.equal('User A');
users[0].passports().should.be.empty;
users[1].name.should.equal('User B');
var passports = users[1].passports();
passports[0].number.should.equal('2');
done();
});
});
@ -317,23 +317,23 @@ describe('include', function () {
should.exists(parts);
parts.length.should.equal(1);
parts[0].partNumber.should.equal('engine');
// Create a part
assembly.parts.create({partNumber: 'door'}, function (err, part4) {
Assembly.find({include: 'parts'}, function (err, assemblies) {
assemblies.length.should.equal(1);
assemblies[0].parts().length.should.equal(2);
done();
});
});
});
});
});
});
});
// Not implemented correctly, see: loopback-datasource-juggler/issues/166
//
// it('should support include scope on hasAndBelongsToMany', function (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);
@ -622,11 +622,11 @@ describe('DataSource define model', function () {
done();
});
it('should allow an explicit remoting path', function () {
var ds = new DataSource('memory');
var User = ds.define('User', {name: String, bio: String}, {
var User = ds.define('User', {name: String, bio: String}, {
http: { path: 'accounts' }
});
User.http.path.should.equal('/accounts');
@ -652,7 +652,7 @@ describe('Load models with base', function () {
assert(Customer.prototype.instanceMethod === User.prototype.instanceMethod);
assert.equal(Customer.base, User);
assert.equal(Customer.base, Customer.super_);
try {
var Customer1 = ds.define('Customer1', {vip: Boolean}, {base: 'User1'});
} catch (e) {
@ -949,37 +949,37 @@ describe('Load models with relations', function () {
assert(Post.relations['user']);
done();
});
it('should set up referencesMany relations', function (done) {
var ds = new DataSource('memory');
var Post = ds.define('Post', {userId: Number, content: String});
var User = ds.define('User', {name: String}, {relations: {posts: {type: 'referencesMany', model: 'Post'}}});
assert(User.relations['posts']);
done();
});
it('should set up embedsMany relations', function (done) {
var ds = new DataSource('memory');
var Post = ds.define('Post', {userId: Number, content: String});
var User = ds.define('User', {name: String}, {relations: {posts: {type: 'embedsMany', model: 'Post' }}});
assert(User.relations['posts']);
done();
});
it('should set up polymorphic relations', function (done) {
var ds = new DataSource('memory');
var Author = ds.define('Author', {name: String}, {relations: {
pictures: {type: 'hasMany', model: 'Picture', polymorphic: 'imageable'}
}});
var Picture = ds.define('Picture', {name: String}, {relations: {
imageable: {type: 'belongsTo', polymorphic: true}
}});
assert(Author.relations['pictures']);
assert.deepEqual(Author.relations['pictures'].toJSON(), {
name: 'pictures',
@ -989,13 +989,13 @@ describe('Load models with relations', function () {
modelTo: 'Picture',
keyTo: 'imageableId',
multiple: true,
polymorphic: {
polymorphic: {
as: 'imageable',
foreignKey: 'imageableId',
discriminator: 'imageableType'
}
});
assert(Picture.relations['imageable']);
assert.deepEqual(Picture.relations['imageable'].toJSON(), {
name: 'imageable',
@ -1005,7 +1005,7 @@ describe('Load models with relations', function () {
modelTo: '<polymorphic>',
keyTo: 'id',
multiple: false,
polymorphic: {
polymorphic: {
as: 'imageable',
foreignKey: 'imageableId',
discriminator: 'imageableType'
@ -1013,7 +1013,7 @@ describe('Load models with relations', function () {
});
done();
});
it('should set up foreign key with the correct type', function (done) {
var ds = new DataSource('memory');
@ -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 () {

File diff suppressed because it is too large Load Diff

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) {