Permalink
Browse files

Pull out eager association tests into mixin

  • Loading branch information...
mde committed Oct 21, 2013
1 parent 3e74901 commit 4f06fb4872f6a12646d6e1a46a38a7292b926602
Showing with 200 additions and 191 deletions.
  1. +1 −0 Jakefile
  2. +197 −0 test/integration/adapters/sql/eager_assn.js
  3. +2 −191 test/integration/adapters/sql/postgres.js
View
@@ -9,6 +9,7 @@ var t = new jake.TestTask('Model', function () {
this.testFiles.exclude('test/fixtures/*.js');
this.testFiles.exclude('test/integration/adapters/shared.js');
this.testFiles.exclude('test/integration/adapters/helpers.js');
+ this.testFiles.exclude('test/integration/adapters/sql/eager_assn.js');
this.testFiles.exclude('test/config.js');
this.testFiles.exclude('test/db.json');
this.testFiles.exclude('test/db.sample.json');
@@ -0,0 +1,197 @@
+var assert = require('assert')
+ , model = require('../../../../lib')
+ , helpers = require('../helpers')
+ , tests;
+
+tests = {
+
+ 'test includes eager-fetch of hasMany association': function (next) {
+ model.Photo.all(function (err, data) {
+ if (err) { throw err; }
+ var photos = data;
+ model.Event.all(function (err, data) {
+ if (err) { throw err; }
+ var ev = data[0];
+ photos.forEach(function (p) {
+ ev.addPhoto(p);
+ });
+ ev.save(function (err, data) {
+ if (err) { throw err; }
+ model.Event.first({id: ev.id}, {includes: 'photos'}, function (err, data) {
+ if (err) { throw err; }
+ assert.equal(20, data.photos.length);
+ next();
+ });
+ });
+ });
+ });
+ }
+
+, 'test includes eager-fetch of named hasMany/through association': function (next) {
+ model.Event.all(function (err, data) {
+ if (err) { throw err; }
+ var ev = data[0];
+ model.Person.all(function (err, data) {
+ if (err) { throw err; }
+ var people = data;
+ people.forEach(function (person) {
+ ev.addParticipant(person);
+ });
+ ev.save(function (err, data) {
+ if (err) { throw err; }
+ model.Event.first({id: ev.id}, {includes: 'participant'}, function (err, data) {
+ if (err) { throw err; }
+ assert.equal(20, data.participants.length);
+ next();
+ });
+ });
+ });
+ });
+ }
+
+, 'test includes eager-fetch of multiple hasMany associations': function (next) {
+ model.Event.all(function (err, data) {
+ if (err) { throw err; }
+ var ev = data[0];
+ model.Person.all(function (err, data) {
+ var people = data;
+ people.forEach(function (person) {
+ ev.addParticipant(person);
+ ev.addAdmin(person);
+ });
+ ev.save(function (err, data) {
+ if (err) { throw err; }
+ model.Event.first({id: ev.id}, {includes: ['participant',
+ 'admin']}, function (err, data) {
+ assert.equal(20, data.participants.length);
+ assert.equal(20, data.admins.length);
+ next();
+ });
+ });
+ });
+ });
+ }
+
+, 'test includes eager-fetch of belongsTo association': function (next) {
+ model.Event.all(function (err, data) {
+ if (err) { throw err; }
+ var ev = data[0];
+ model.Photo.all(function (err, data) {
+ if (err) { throw err; }
+ data.forEach(function (p) {
+ p.setEvent(ev);
+ });
+ helpers.updateItems(data, function () {
+ model.Photo.all({}, {includes: ['event']}, function (err, data) {
+ if (err) { throw err; }
+ var every = data.every(function (p) {
+ return !!p.event;
+ });
+ assert.ok(every);
+ next();
+ });
+ });
+ });
+ });
+ }
+
+, 'test includes eager-fetch of hasMany with association sort': function (next) {
+ model.Event.all(function (err, data) {
+ if (err) { throw err; }
+ var evA = data[0]
+ , evB = data[1];
+ model.Photo.all(function (err, data) {
+ var incr = 0;
+ if (err) { throw err; }
+ data.forEach(function (p) {
+ // Half to A, half to B
+ if (!!(incr % 2)) {
+ p.setEvent(evA);
+ }
+ else {
+ p.setEvent(evB);
+ }
+ incr++;
+ });
+ helpers.updateItems(data, function () {
+ model.Event.all({id: [evA.id, evB.id]},
+ {includes: ['photos'], sort: {'title': 'desc',
+ 'photo.title': 'asc'}}, function (err, data) {
+ assert.equal('b', data[0].title);
+ assert.equal('a', data[1].title);
+ assert.ok(!(data[0].photos.some(helpers.foundOutOfOrderItemAscending,
+ data[0].photos)));
+ assert.ok(!(data[1].photos.some(helpers.foundOutOfOrderItemAscending,
+ data[1].photos)));
+ next();
+ });
+ });
+ });
+ });
+ }
+
+, 'test includes eager-fetch of hasMany with `first` lookup for owner obj': function (next) {
+ model.Event.all(function (err, data) {
+ if (err) { throw err; }
+ var ev = data[0];
+ model.Person.all(function (err, data) {
+ if (err) { throw err; }
+ data.forEach(function (p) {
+ ev.addAdmin(p);
+ });
+ ev.save(function (err, data) {
+ if (err) { throw err; }
+ model.Event.first({id: ev.id}, {includes: 'admins'}, function (err, data) {
+ if (err) { throw err; }
+ assert.equal(20, data.admins.length);
+ next();
+ });
+ });
+ });
+ });
+ }
+
+, 'test includes eager-fetch of hasMany/through association': function (next) {
+ model.Event.all(function (err, data) {
+ if (err) { throw err; }
+ var ev = data[0];
+ model.Person.all(function (err, data) {
+ var people = data;
+ people.forEach(function (person) {
+ ev.addParticipant(person);
+ });
+ ev.save(function (err, data) {
+ if (err) { throw err; }
+ model.Event.first({id: ev.id}, {includes: ['participant',
+ 'admin']}, function (err, data) {
+ assert.equal(20, data.participants.length);
+ next();
+ });
+ });
+ });
+ });
+ }
+
+, 'test hasMany/through with auto-save for owned-object side': function (next) {
+ model.Event.all(function (err, data) {
+ if (err) { throw err; }
+ var ev = data[0];
+ ev.addPhoto(model.Photo.create({
+ title: 'u'
+ }));
+ ev.save(function (err, data) {
+ if (err) { throw err; }
+ model.Event.first({id: ev.id}, {includes: 'photos'},
+ function (err, data) {
+ assert.equal(1, data.photos.length);
+ assert.equal('u', data.photos[0].title);
+ next();
+ });
+ });
+ });
+ }
+
+};
+
+module.exports = tests;
+
Oops, something went wrong.

0 comments on commit 4f06fb4

Please sign in to comment.