More tests for embedsMany with persistent model

This commit is contained in:
Fabien Franzen 2014-09-07 12:44:45 +02:00
parent 4c6f35d23d
commit ef816d490a
2 changed files with 31 additions and 6 deletions

View File

@ -1869,8 +1869,7 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
type: [modelTo], default: function() { return []; } type: [modelTo], default: function() { return []; }
}); });
if (typeof modelTo.dataSource.connector.generateId !== 'function' if (typeof modelTo.dataSource.connector.generateId !== 'function') {
|| !modelFrom.definition.settings.idInjection) {
modelFrom.validate(propertyName, function(err) { modelFrom.validate(propertyName, function(err) {
var self = this; var self = this;
var embeddedList = this[propertyName] || []; var embeddedList = this[propertyName] || [];

View File

@ -2186,6 +2186,7 @@ describe('relations', function () {
describe('embedsMany - persisted model', function () { describe('embedsMany - persisted model', function () {
var address0, address1, address2; var address0, address1, address2;
var person;
before(function (done) { before(function (done) {
db = getSchema(); db = getSchema();
@ -2199,7 +2200,7 @@ describe('relations', function () {
}); });
it('can be declared', function (done) { it('can be declared', function (done) {
Person.embedsMany(Address); Person.embedsMany(Address, {scope: {order: 'street'}});
db.automigrate(done); db.automigrate(done);
}); });
@ -2224,22 +2225,47 @@ describe('relations', function () {
}); });
}); });
it('should create embedded items on scope', function(done) { it('should add embedded items on scope', function(done) {
Person.create({ name: 'Fred' }, function(err, p) { Person.create({ name: 'Fred' }, function(err, p) {
person = p;
p.addressList.create(address1.toObject(), function(err, address) { p.addressList.create(address1.toObject(), function(err, address) {
should.not.exist(err); should.not.exist(err);
address.id.should.eql(address1.id); address.id.should.eql(address1.id);
address1.street.should.equal('Street 1'); address.street.should.equal('Street 1');
p.addressList.create(address2.toObject(), function(err, address) { p.addressList.create(address2.toObject(), function(err, address) {
should.not.exist(err); should.not.exist(err);
address.id.should.eql(address2.id); address.id.should.eql(address2.id);
address2.street.should.equal('Street 2'); address.street.should.equal('Street 2');
done(); done();
}); });
}); });
}); });
}); });
it('should create embedded items on scope', function(done) {
Person.findById(person.id, function(err, p) {
p.addressList.create({ street: 'Street 3' }, function(err, address) {
should.not.exist(err);
address.should.have.property('id'); // not within Address seq!
address.street.should.equal('Street 3');
done();
});
});
});
it('should have embedded items on scope', function(done) {
Person.findById(person.id, function(err, p) {
p.addressList(function(err, addresses) {
should.not.exist(err);
addresses.should.have.length(3);
addresses[0].street.should.equal('Street 1');
addresses[1].street.should.equal('Street 2');
addresses[2].street.should.equal('Street 3');
done();
});
});
});
it('should validate embedded items on scope - id', function(done) { it('should validate embedded items on scope - id', function(done) {
Person.create({ name: 'Wilma' }, function(err, p) { Person.create({ name: 'Wilma' }, function(err, p) {
p.addressList.create({ id: null, street: 'Street 1' }, function(err, address) { p.addressList.create({ id: null, street: 'Street 1' }, function(err, address) {