Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pull out eager association tests into mixin

  • Loading branch information...
commit 4f06fb4872f6a12646d6e1a46a38a7292b926602 1 parent 3e74901
Matthew Eernisse mde authored
1  Jakefile
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');
197 test/integration/adapters/sql/eager_assn.js
View
@@ -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;
+
193 test/integration/adapters/sql/postgres.js
View
@@ -1,7 +1,8 @@
var utils = require('utilities')
, assert = require('assert')
, model = require('../../../../lib')
- , helpers = require('.././helpers')
+ , helpers = require('../helpers')
+ , eagerAssnTests = require('./eager_assn')
, Adapter = require('../../../../lib/adapters/sql/postgres').Adapter
, generator = require('../../../../lib/generators/sql')
, adapter
@@ -74,196 +75,6 @@ for (var p in shared) {
}
}
-var eagerAssnTests = {
-
- '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();
- });
- });
- });
- }
-
-};
-
for (var p in eagerAssnTests) {
tests[p + ' (Postgres)'] = eagerAssnTests[p];
}
Please sign in to comment.
Something went wrong with that request. Please try again.