waterline-adapter-tests
Version:
Integration tests for waterline adapters
85 lines (68 loc) • 2.38 kB
JavaScript
var assert = require('assert');
var _ = require('@sailshq/lodash');
describe('Association Interface', function() {
describe('Many to Many Association', function() {
describe('projections', function() {
var driverRecord;
before(function(done) {
Associations.Driver.create({ name: 'manymany findOne'}, function(err, driver) {
if (err) {
return done(err);
}
driverRecord = driver;
var taxis = [];
// for(var i=0; i<2; i++) {
taxis.push({ model: 'sedan', medallion: 101 });
// }
Associations.Taxi.createEach(taxis, function(err, taxis) {
if (err) {
return done(err);
}
var childrenIds = _.map(taxis, function(taxi) {
return taxi.id;
});
Associations.Driver.addToCollection(driver.id, 'taxis', childrenIds)
.exec(function(err) {
if (err) {
return done(err);
}
return done();
});
});
});
});
it('should filter populated attributes when `select` projections are used', function(done) {
Associations.Driver.findOne({ id: driverRecord.id })
.populate('taxis', { select: ['model'] })
.exec(function(err, driver) {
if (err) {
return done(err);
}
assert(driver);
assert(_.isArray(driver.taxis));
assert.equal(driver.taxis.length, 1);
assert.equal(_.keys(driver.taxis[0]).length, 2);
assert(driver.taxis[0].id);
assert.equal(driver.taxis[0].model, 'sedan');
return done();
});
});
it('should filter populated attributes when `omit` projections are used', function(done) {
Associations.Driver.findOne({ id: driverRecord.id })
.populate('taxis', { omit: ['model'] })
.exec(function(err, driver) {
if (err) {
return done(err);
}
assert(driver);
assert(_.isArray(driver.taxis));
assert.equal(driver.taxis.length, 1);
assert.equal(_.keys(driver.taxis[0]).length, 6);
assert(driver.taxis[0].id);
assert(_.isUndefined(driver.taxis[0].model));
return done();
});
});
});
});
});