ReferencesMany fixes after LB integration tests

This commit is contained in:
Fabien Franzen 2014-07-30 15:01:55 +02:00
parent 5cee6a4b79
commit e38c92af87
2 changed files with 39 additions and 37 deletions

View File

@ -1503,6 +1503,7 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
// validate all embedded items // validate all embedded items
if (definition.options.validate) { if (definition.options.validate) {
modelFrom.validate(relationName, function(err) { modelFrom.validate(relationName, function(err) {
var self = this;
var embeddedList = this[relationName] || []; var embeddedList = this[relationName] || [];
var hasErrors = false; var hasErrors = false;
embeddedList.forEach(function(item) { embeddedList.forEach(function(item) {
@ -1513,13 +1514,13 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
var first = Object.keys(item.errors)[0]; var first = Object.keys(item.errors)[0];
var msg = 'contains invalid item: `' + id + '`'; var msg = 'contains invalid item: `' + id + '`';
msg += ' (' + first + ' ' + item.errors[first] + ')'; msg += ' (' + first + ' ' + item.errors[first] + ')';
this.errors.add(relationName, msg, 'invalid'); self.errors.add(relationName, msg, 'invalid');
} }
} else { } else {
hasErrors = true; hasErrors = true;
this.errors.add(relationName, 'Contains invalid item', 'invalid'); self.errors.add(relationName, 'Contains invalid item', 'invalid');
} }
}.bind(this)); });
if (hasErrors) err(false); if (hasErrors) err(false);
}); });
} }
@ -1751,16 +1752,14 @@ EmbedsMany.prototype.create = function (targetModelData, cb) {
var err = inst.isValid() ? null : new ValidationError(inst); var err = inst.isValid() ? null : new ValidationError(inst);
if (err) { if (err) {
var index = embeddedList.indexOf(inst); return process.nextTick(function() {
if (index > -1) embeddedList.splice(index, 1); cb(err);
return process.nextTick(function() {
cb(err, embeddedList);
}); });
} }
modelInstance.updateAttribute(relationName, modelInstance.updateAttribute(relationName,
embeddedList, function(err, modelInst) { embeddedList, function(err, modelInst) {
cb(err, modelInst[relationName]); cb(err, err ? null : inst);
}); });
}; };
@ -1952,6 +1951,12 @@ RelationDefinition.referencesMany = function referencesMany(modelFrom, modelTo,
var updateByIdFunc = scopeMethods.updateById; var updateByIdFunc = scopeMethods.updateById;
modelFrom.prototype['__updateById__' + relationName] = updateByIdFunc; modelFrom.prototype['__updateById__' + relationName] = updateByIdFunc;
var addFunc = scopeMethods.add;
modelFrom.prototype['__link__' + relationName] = addFunc;
var removeFunc = scopeMethods.remove;
modelFrom.prototype['__unlink__' + relationName] = removeFunc;
scopeMethods.create = scopeMethod(definition, 'create'); scopeMethods.create = scopeMethod(definition, 'create');
scopeMethods.build = scopeMethod(definition, 'build'); scopeMethods.build = scopeMethod(definition, 'build');
@ -2143,7 +2148,9 @@ ReferencesMany.prototype.add = function (acInst, cb) {
var pk = this.definition.keyTo; var pk = this.definition.keyTo;
var fk = this.definition.keyFrom; var fk = this.definition.keyFrom;
var insertId = function(id, done) { var insert = function(inst, done) {
var id = inst[pk];
if (typeof id === 'object') { if (typeof id === 'object') {
id = id.toString(); // mongodb id = id.toString(); // mongodb
} }
@ -2156,13 +2163,13 @@ ReferencesMany.prototype.add = function (acInst, cb) {
ids.push(id); ids.push(id);
} }
modelInstance.updateAttribute(fk, ids, function(err, inst) { modelInstance.updateAttribute(fk, ids, function(err) {
done(err, inst[fk] || []); done(err, err ? null : inst);
}); });
}; };
if (acInst instanceof modelTo) { if (acInst instanceof modelTo) {
insertId(acInst[pk], cb); insert(acInst, cb);
} else { } else {
var filter = { where: {} }; var filter = { where: {} };
filter.where[pk] = acInst; filter.where[pk] = acInst;
@ -2170,8 +2177,8 @@ ReferencesMany.prototype.add = function (acInst, cb) {
definition.applyScope(modelInstance, filter); definition.applyScope(modelInstance, filter);
modelTo.findOne(filter, function (err, inst) { modelTo.findOne(filter, function (err, inst) {
if (err || !inst) return cb(err, modelInstance[fk]); if (err || !inst) return cb(err, null);
insertId(inst[pk], cb); insert(inst, cb);
}); });
} }
}; };

View File

@ -1255,12 +1255,11 @@ describe('relations', function () {
it('should create embedded items on scope', function(done) { it('should create embedded items on scope', function(done) {
Person.create({ name: 'Fred' }, function(err, p) { Person.create({ name: 'Fred' }, function(err, p) {
p.addressList.create({ street: 'Street 1' }, function(err, addresses) { p.addressList.create({ street: 'Street 1' }, function(err, address) {
should.not.exist(err); should.not.exist(err);
addresses.should.have.length(1); address1 = address;
address1 = addresses[0];
should.exist(address1.id); should.exist(address1.id);
addresses[0].street.should.equal('Street 1'); address1.street.should.equal('Street 1');
done(); done();
}); });
}); });
@ -1268,13 +1267,9 @@ describe('relations', function () {
it('should create embedded items on scope', function(done) { it('should create embedded items on scope', function(done) {
Person.findOne(function(err, p) { Person.findOne(function(err, p) {
p.addressList.create({ street: 'Street 2' }, function(err, addresses) { p.addressList.create({ street: 'Street 2' }, function(err, address) {
should.not.exist(err); should.not.exist(err);
addresses.should.have.length(2); address2 = address;
address1 = addresses[0];
address2 = addresses[1];
should.exist(address1.id);
address1.street.should.equal('Street 1');
should.exist(address2.id); should.exist(address2.id);
address2.street.should.equal('Street 2'); address2.street.should.equal('Street 2');
done(); done();
@ -1310,11 +1305,11 @@ describe('relations', function () {
it('should validate embedded items', function(done) { it('should validate embedded items', function(done) {
Person.findOne(function(err, p) { Person.findOne(function(err, p) {
p.addressList.create({}, function(err, addresses) { p.addressList.create({}, function(err, address) {
should.exist(err); should.exist(err);
should.not.exist(address);
err.name.should.equal('ValidationError'); err.name.should.equal('ValidationError');
err.details.codes.street.should.eql(['presence']); err.details.codes.street.should.eql(['presence']);
addresses.should.have.length(2);
done(); done();
}); });
}); });
@ -1428,12 +1423,10 @@ describe('relations', function () {
Person.create({ name: 'Fred' }, function(err, p) { Person.create({ name: 'Fred' }, function(err, p) {
p.addressList.create({ id: 'home', street: 'Street 1' }, function(err, addresses) { p.addressList.create({ id: 'home', street: 'Street 1' }, function(err, addresses) {
should.not.exist(err); should.not.exist(err);
p.addressList.create({ id: 'work', street: 'Work Street 2' }, function(err, addresses) { p.addressList.create({ id: 'work', street: 'Work Street 2' }, function(err, address) {
addresses.should.have.length(2); should.not.exist(err);
addresses[0].id.should.equal('home'); address.id.should.equal('work');
addresses[0].street.should.equal('Street 1'); address.street.should.equal('Work Street 2');
addresses[1].id.should.equal('work');
addresses[1].street.should.equal('Work Street 2');
done(); done();
}); });
}); });
@ -1972,10 +1965,11 @@ describe('relations', function () {
it('should add a record to scope - object', function (done) { it('should add a record to scope - object', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.add(product1, function(err, ids) { cat.products.add(product1, function(err, prod) {
should.not.exist(err); should.not.exist(err);
cat.productIds.should.eql([product2.id, product1.id]); cat.productIds.should.eql([product2.id, product1.id]);
ids.should.eql(cat.productIds); prod.id.should.eql(product1.id);
prod.should.have.property('name');
done(); done();
}); });
}); });
@ -1983,11 +1977,12 @@ describe('relations', function () {
it('should add a record to scope - object', function (done) { it('should add a record to scope - object', function (done) {
Category.findOne(function(err, cat) { Category.findOne(function(err, cat) {
cat.products.add(product3.id, function(err, ids) { cat.products.add(product3.id, function(err, prod) {
should.not.exist(err); should.not.exist(err);
var expected = [product2.id, product1.id, product3.id]; var expected = [product2.id, product1.id, product3.id];
cat.productIds.should.eql(expected); cat.productIds.should.eql(expected);
ids.should.eql(cat.productIds); prod.id.should.eql(product3.id);
prod.should.have.property('name');
done(); done();
}); });
}); });