#350: Creating a batch via hasMany relation is failing. Added context 'with scope' to allow individual execution of tests.

This commit is contained in:
Alex Voitau 2014-11-07 15:21:15 -08:00
parent eca27ce0c6
commit f1638e9e4c
1 changed files with 164 additions and 156 deletions

View File

@ -61,187 +61,195 @@ describe('relations', function () {
db.autoupdate(done); db.autoupdate(done);
}); });
it('should build record on scope', function (done) { describe('with scope', function() {
Book.create(function (err, book) {
var c = book.chapters.build();
c.bookId.should.equal(book.id);
c.save(done);
});
});
it('should create record on scope', function (done) { before(function (done) {
Book.create(function (err, book) { Book.hasMany(Chapter);
book.chapters.create(function (err, c) { done();
should.not.exist(err); });
should.exist(c);
it('should build record on scope', function (done) {
Book.create(function (err, book) {
var c = book.chapters.build();
c.bookId.should.equal(book.id); c.bookId.should.equal(book.id);
done(); c.save(done);
});
});
});
it('should fetch all scoped instances', function (done) {
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function () {
book.chapters.create({name: 'z'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
});
function verify(book) {
book.chapters(function (err, ch) {
should.not.exist(err);
should.exist(ch);
ch.should.have.lengthOf(3);
var chapters = book.chapters();
chapters.should.eql(ch);
book.chapters({order: 'name DESC'}, function (e, c) {
should.not.exist(e);
should.exist(c);
c.shift().name.should.equal('z');
c.pop().name.should.equal('a');
done();
});
});
}
});
it('should find scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.create({name: 'z'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
}); });
}); });
function verify(book) { it('should create record on scope', function (done) {
book.chapters.findById(id, function (err, ch) { Book.create(function (err, book) {
should.not.exist(err); book.chapters.create(function (err, c) {
should.exist(ch);
ch.id.should.eql(id);
done();
});
}
});
it('should count scoped records - all and filtered', function (done) {
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
book.chapters.create({name: 'b'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
});
function verify(book) {
book.chapters.count(function (err, count) {
should.not.exist(err);
count.should.equal(3);
book.chapters.count({ name: 'b' }, function (err, count) {
should.not.exist(err); should.not.exist(err);
count.should.equal(1); should.exist(c);
c.bookId.should.equal(book.id);
done(); done();
}); });
}); });
}
});
it('should set targetClass on scope property', function() {
should.equal(Book.prototype.chapters._targetClass, 'Chapter');
});
it('should update scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.updateById(id, {name: 'aa'}, function(err, ch) {
verify(book);
});
});
}); });
function verify(book) { it('should fetch all scoped instances', function (done) {
book.chapters.findById(id, function (err, ch) { Book.create(function (err, book) {
should.not.exist(err); book.chapters.create({name: 'a'}, function () {
should.exist(ch); book.chapters.create({name: 'z'}, function () {
ch.id.should.eql(id); book.chapters.create({name: 'c'}, function () {
ch.name.should.equal('aa'); verify(book);
done(); });
}); });
}
});
it('should destroy scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.destroy(id, function(err, ch) {
verify(book);
}); });
}); });
function verify(book) {
book.chapters(function (err, ch) {
should.not.exist(err);
should.exist(ch);
ch.should.have.lengthOf(3);
var chapters = book.chapters();
chapters.should.eql(ch);
book.chapters({order: 'name DESC'}, function (e, c) {
should.not.exist(e);
should.exist(c);
c.shift().name.should.equal('z');
c.pop().name.should.equal('a');
done();
});
});
}
}); });
function verify(book) { it('should find scoped record', function (done) {
book.chapters.findById(id, function (err, ch) { var id;
should.exist(err); Book.create(function (err, book) {
done(); book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.create({name: 'z'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
}); });
}
});
it('should check existence of a scoped record', function (done) { function verify(book) {
var id; book.chapters.findById(id, function (err, ch) {
Book.create(function (err, book) { should.not.exist(err);
book.chapters.create({name: 'a'}, function (err, ch) { should.exist(ch);
id = ch.id; ch.id.should.eql(id);
book.chapters.create({name: 'z'}, function () { done();
book.chapters.create({name: 'c'}, function () { });
}
});
it('should count scoped records - all and filtered', function (done) {
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
book.chapters.create({name: 'b'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
});
function verify(book) {
book.chapters.count(function (err, count) {
should.not.exist(err);
count.should.equal(3);
book.chapters.count({ name: 'b' }, function (err, count) {
should.not.exist(err);
count.should.equal(1);
done();
});
});
}
});
it('should set targetClass on scope property', function() {
should.equal(Book.prototype.chapters._targetClass, 'Chapter');
});
it('should update scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.updateById(id, {name: 'aa'}, function(err, ch) {
verify(book); verify(book);
}); });
}); });
}); });
function verify(book) {
book.chapters.findById(id, function (err, ch) {
should.not.exist(err);
should.exist(ch);
ch.id.should.eql(id);
ch.name.should.equal('aa');
done();
});
}
}); });
function verify(book) { it('should destroy scoped record', function (done) {
book.chapters.exists(id, function (err, flag) { var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.destroy(id, function(err, ch) {
verify(book);
});
});
});
function verify(book) {
book.chapters.findById(id, function (err, ch) {
should.exist(err);
done();
});
}
});
it('should check existence of a scoped record', function (done) {
var id;
Book.create(function (err, book) {
book.chapters.create({name: 'a'}, function (err, ch) {
id = ch.id;
book.chapters.create({name: 'z'}, function () {
book.chapters.create({name: 'c'}, function () {
verify(book);
});
});
});
});
function verify(book) {
book.chapters.exists(id, function (err, flag) {
should.not.exist(err);
flag.should.be.eql(true);
done();
});
}
});
it('should check ignore related data on creation - array', function (done) {
Book.create({ chapters: [] }, function (err, book) {
should.not.exist(err); should.not.exist(err);
flag.should.be.eql(true); book.chapters.should.be.a.function;
var obj = book.toObject();
should.not.exist(obj.chapters);
done(); done();
}); });
}
});
it('should check ignore related data on creation - array', function (done) {
Book.create({ chapters: [] }, function (err, book) {
should.not.exist(err);
book.chapters.should.be.a.function;
var obj = book.toObject();
should.not.exist(obj.chapters);
done();
}); });
});
it('should check ignore related data on creation - object', function (done) { it('should check ignore related data on creation - object', function (done) {
Book.create({ chapters: {} }, function (err, book) { Book.create({ chapters: {} }, function (err, book) {
should.not.exist(err); should.not.exist(err);
book.chapters.should.be.a.function; book.chapters.should.be.a.function;
var obj = book.toObject(); var obj = book.toObject();
should.not.exist(obj.chapters); should.not.exist(obj.chapters);
done(); done();
});
}); });
}); });