Skip to content

Commit

Permalink
Add test for ManyToManyThrough
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Jan 25, 2017
1 parent ba82526 commit 998f849
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
131 changes: 131 additions & 0 deletions interfaces/associations/manyToManyThrough/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
var assert = require('assert'),
_ = require('lodash');

describe('Association Interface', function() {

describe.only('n:m association with through table :: .add()', function() {

describe('with an object', function() {

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

var driverRecord;
before(function(done) {
Associations.Driverthrough.create({ name: 'manymany add' })
.exec(function(err, model) {
if(err) return done(err);
driverRecord = model;
done();
});
});

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

it('should create a new taxi association', function(done) {
driverRecord.taxis.add({ medallion: 1 });
driverRecord.save(function(err) {
assert(!err, err);


// Look up the customer again to be sure the payment was added
Associations.Driverthrough.findOne(driverRecord.id)
.populate('taxis')
.exec(function(err, driver) {
assert.ifError(err);

assert.strictEqual(driver.taxis.length, 1, 'Expected driver to have one taxi, but actually there are '+driver.taxis.length+', see? `driver.taxi` =>'+require('util').inspect(driver.taxis,false,null));
assert.strictEqual(driver.taxis[0].medallion, 1);

done();
});

});
});
});

describe('with an id', function() {

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

var driverRecord, taxiRecord, taxiRecord2;

before(function(done) {
Associations.Driverthrough.create({ name: 'manymany add' })
.exec(function(err, model) {
if(err) return done(err);
driverRecord = model;

Associations.Taxithrough.create([{ medallion: 20 }, { medallion: 30 }])
.exec(function(err, taxis) {
if(err) return done(err);
taxiRecord = taxis[0];
taxiRecord2 = taxis[1];
done();
});
});
});

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

it('should link a payment to a customer through a join table', function(done) {
driverRecord.taxis.add(taxiRecord.id);
driverRecord.save(function(err) {
assert.ifError(err);

// Look up the driver again to be sure the taxi was added
Associations.Driverthrough.findOne(driverRecord.id)
.populate('taxis', { medallion: 20 })
.exec(function(err, data) {
assert.ifError(err);

assert.strictEqual(data.taxis.length, 1);
assert.strictEqual(data.taxis[0].medallion, 20);
done();
});
});
});

it('after populating parent should link a payment to a customer through a join table', function(done) {
Associations.Driverthrough.findOne(driverRecord.id)
.populate('taxis')
.exec(function(err, driver) {
driver.taxis.add(taxiRecord2.id);
driver.save(function(err) {
assert.ifError(err);

// Look up the driver again to be sure the taxi was added
Associations.Driverthrough.findOne(driverRecord.id)
.populate('taxis', { medallion: 30 })
.exec(function(err, data) {
assert.ifError(err);

assert.strictEqual(data.taxis.length, 1);
assert.strictEqual(data.taxis[0].medallion, 30);
done();
});
});
});
});

it('should error if the associated record doesn\'t exist', function(done) {
driverRecord.taxis.add(taxiRecord.id + 2);
driverRecord.save(function(err) {
assert(err);
assert(err.failedTransactions);
assert(Array.isArray(err.failedTransactions));
assert.strictEqual(err.failedTransactions.length, 1);
done();
});
});
});

});
});
3 changes: 3 additions & 0 deletions interfaces/associations/support/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var fixtures = {
DriverWithSchemaFixture: require('./fixtures/manyToMany.driver.withSchema.fixture'),
TaxiCustomFixture: require('./fixtures/manyToMany.taxi.customPK.fixture'),
DriverCustomFixture: require('./fixtures/manyToMany.driver.customPK.fixture'),
TaxiThroughFixture: require('./fixtures/manyToManyThrough.taxi.fixture'),
DriverThroughFixture: require('./fixtures/manyToManyThrough.driver.fixture'),
RideshareFixture: require('./fixtures/manyToManyThrough.rideshare.fixture'),
UserOneFixture: require('./fixtures/oneToOne.fixture').user_resource,
ProfileOneFixture: require('./fixtures/oneToOne.fixture').profile
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Dependencies
*/

var Waterline = require('waterline');

module.exports = Waterline.Collection.extend({

tableName: 'driverThroughTable',
identity: 'driverThrough',
connection: 'associations',

// migrate: 'drop',
attributes: {
name: 'string',
taxis: {
collection: 'taxiThrough',
via: 'taxidriver',
through: 'rideshare'
},

toJSON: function() {
var obj = this.toObject();
delete obj.name;
return obj;
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Dependencies
*/

var Waterline = require('waterline');

module.exports = Waterline.Collection.extend({

tableName: 'rideshareTable',
identity: 'rideshare',
connection: 'associations',

attributes: {
tips: 'boolean',
taxidriver: {
model: 'driverThrough',
columnName: 'driver_id'
},
drivertaxi: {
model: 'taxiThrough',
columnName: 'taxi_id'
}
}

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Dependencies
*/

var Waterline = require('waterline');

module.exports = Waterline.Collection.extend({

tableName: 'taxiThroughTable',
identity: 'taxiThrough',
connection: 'associations',

// migrate: 'drop',
attributes: {
medallion: 'integer',
drivers: {
collection: 'driverThrough',
via: 'drivertaxi',
through: 'rideshare'
},

toJSON: function() {
var obj = this.toObject();
delete obj.medallion;
return obj;
}
}
});

0 comments on commit 998f849

Please sign in to comment.