Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for Waterline Projections #106

Merged
merged 4 commits into from
Mar 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ module.exports = Waterline.Collection.extend({
identity: 'taxi',
connection: 'associations2',

// migrate: 'drop',
// migrate: 'drop',
attributes: {
medallion: 'integer',
model: 'string',
drivers: {
collection: 'driver',
via: 'taxis'
Expand Down
72 changes: 72 additions & 0 deletions interfaces/associations/belongsTo/projections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var _ = require('lodash');
var assert = require('assert');

describe('Association Interface', function() {

describe('Belongs To Association', function() {

/////////////////////////////////////////////////////
// TEST SETUP
////////////////////////////////////////////////////

var customer;
var payment;

before(function(done) {
var customerData = {
name: 'foo',
title: 'tester'
};

var paymentData = {
amount: 1,
type: 'credit'
};

Associations.Customerbelongs.create(customerData, function(err, _customer) {
if(err) {
return done(err);
}

customer = _customer;
paymentData.customer = customer.id;

Associations.Paymentbelongs.create(paymentData, function(err, _payment) {
if(err) {
return done(err);
}

payment = _payment;
return done();
});
});
});

describe('projections', function() {

/////////////////////////////////////////////////////
// TEST METHODS
////////////////////////////////////////////////////

it('should filter populated attributes when projections are used', function(done) {
Associations.Paymentbelongs.findOne({ id: payment.id })
.populate('customer', { select: ['title'] })
.exec(function(err, payment) {
assert.ifError(err);
assert(payment);
assert(payment.customer);

// JSON stringify the record to remove any virtual functions such
// as associations with .add/.remove
var record = payment.toJSON();

assert.equal(_.keys(record.customer).length, 2);
assert(record.customer.id);
assert.equal(record.customer.title, 'tester');
done();
});
});
});

});
});
74 changes: 74 additions & 0 deletions interfaces/associations/hasMany/projections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var assert = require('assert');
var _ = require('lodash');

describe('Association Interface', function() {

describe('Has Many Association', function() {

/////////////////////////////////////////////////////
// TEST SETUP
////////////////////////////////////////////////////

var customer;
var payment;

before(function(done) {
var customerData = {
name: 'foo',
title: 'tester',
capital : 123
};

var paymentData = {
amount: 100,
type: 'check'
};

Associations.Customer.create(customerData, function(err, _customer) {
if(err) {
return done(err);
}

customer = _customer;
paymentData.a_customer = customer.id;

Associations.Payment.create(paymentData, function(err, _payment) {
if(err) {
return done(err);
}

payment = _payment;
done();
});
});
});

describe('projections', function() {

/////////////////////////////////////////////////////
// TEST METHODS
////////////////////////////////////////////////////

it('should filter populated attributes when projections are used', function(done) {
Associations.Customer.findOne({ id: customer.id })
.populate('payments', { select: ['amount'] })
.exec(function(err, customer) {
assert.ifError(err);
assert(customer);
assert(_.isArray(customer.payments));
assert.equal(customer.payments.length, 1);

// JSON stringify the record to remove any virtual functions such
// as associations with .add/.remove
var record = customer.toJSON();

assert.equal(_.keys(record.payments[0]).length, 3);
assert(record.payments[0].id);
assert.equal(record.payments[0].a_customer, customer.id);
assert.equal(record.payments[0].amount, 100);
done();
});
});
});
});
});
59 changes: 59 additions & 0 deletions interfaces/associations/hasManyThrough/projections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var assert = require('assert');
var _ = require('lodash');

describe('Association Interface', function() {

describe('Has Many Through Association', function() {

/////////////////////////////////////////////////////
// TEST SETUP
////////////////////////////////////////////////////

var stadium, team;

before(function(done) {
Associations.Stadium.create({ name: 'hasManyThrough stadium'}, function(err, _stadium) {
if(err) return done(err);
stadium = _stadium;

Associations.Team.create({ name: 'hasManyThrough team', mascot: 'elephant' }, function(err, _team) {
if(err) return done(err);
team = _team;

Associations.Venue.create({ seats: 200, stadium: _stadium.id, team: _team.id }, function(err, venue) {
if(err) return done(err);
done();
});
});
});
});

describe('projections', function() {

/////////////////////////////////////////////////////
// TEST METHODS
////////////////////////////////////////////////////

it('should filter populated attributes when projections are used', function(done) {
Associations.Stadium.findOne({ id: stadium.id })
.populate('teams', { select: ['name'] })
.exec(function(err, data) {
assert.ifError(err);
assert(data);
assert(_.isArray(data.teams));
assert.equal(data.teams.length, 1);

// JSON stringify the record to remove any virtual functions such
// as associations with .add/.remove
var record = data.toJSON();

assert.equal(_.keys(record.teams[0]).length, 2);
assert(record.teams[0].id);
assert.equal(record.teams[0].name, team.name);
done();
});
});

});
});
});
59 changes: 59 additions & 0 deletions interfaces/associations/manyToMany/projections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var assert = require('assert');
var _ = require('lodash');

describe('Association Interface', function() {
describe('Many to Many Association', function() {

/////////////////////////////////////////////////////
// TEST SETUP
////////////////////////////////////////////////////

var driver;

before(function(done) {
Associations.Driver.create({ name: 'manymany findOne' }, function(err, _driver) {
if(err) {
return done(err);
}

driver = _driver;

_driver.taxis.add({ model: 'sedan', medallion: 101 });
_driver.save(function(err) {
if(err) {
return done(err);
}
done();
});
});
});

describe('projections', function() {

/////////////////////////////////////////////////////
// TEST METHODS
////////////////////////////////////////////////////

it('should filter populated attributes when projections are used', function(done) {
Associations.Driver.findOne({ id: driver.id })
.populate('taxis', { select: ['model'] })
.exec(function(err, driver) {
assert.ifError(err);
assert(driver);
assert(_.isArray(driver.taxis));
assert.equal(driver.taxis.length, 1);

// JSON stringify the record to remove any virtual functions such
// as associations with .add/.remove
var record = driver.toJSON();

assert.equal(_.keys(record.taxis[0]).length, 2);
assert(record.taxis[0].id);
assert.equal(record.taxis[0].model, 'sedan');
done();
});
});

});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = Waterline.Collection.extend({
identity: 'driver',
connection: 'associations',

// migrate: 'drop',
// migrate: 'drop',
attributes: {
name: 'string',
taxis: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ module.exports = Waterline.Collection.extend({
identity: 'taxi',
connection: 'associations',

// migrate: 'drop',
// migrate: 'drop',
attributes: {
medallion: 'integer',
model: 'string',
drivers: {
collection: 'driver',
via: 'taxis'
Expand Down
Loading