diff --git a/interfaces/associations/manyToManyThrough/add.js b/interfaces/associations/manyToManyThrough/add.js new file mode 100644 index 0000000..77da40f --- /dev/null +++ b/interfaces/associations/manyToManyThrough/add.js @@ -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(); + }); + }); + }); + + }); +}); diff --git a/interfaces/associations/support/bootstrap.js b/interfaces/associations/support/bootstrap.js index e6766ac..d934cc1 100644 --- a/interfaces/associations/support/bootstrap.js +++ b/interfaces/associations/support/bootstrap.js @@ -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 }; diff --git a/interfaces/associations/support/fixtures/manyToManyThrough.driver.fixture.js b/interfaces/associations/support/fixtures/manyToManyThrough.driver.fixture.js new file mode 100644 index 0000000..061cbee --- /dev/null +++ b/interfaces/associations/support/fixtures/manyToManyThrough.driver.fixture.js @@ -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; + } + } +}); diff --git a/interfaces/associations/support/fixtures/manyToManyThrough.rideshare.fixture.js b/interfaces/associations/support/fixtures/manyToManyThrough.rideshare.fixture.js new file mode 100644 index 0000000..c971d31 --- /dev/null +++ b/interfaces/associations/support/fixtures/manyToManyThrough.rideshare.fixture.js @@ -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' + } + } + +}); diff --git a/interfaces/associations/support/fixtures/manyToManyThrough.taxi.fixture.js b/interfaces/associations/support/fixtures/manyToManyThrough.taxi.fixture.js new file mode 100644 index 0000000..43ae871 --- /dev/null +++ b/interfaces/associations/support/fixtures/manyToManyThrough.taxi.fixture.js @@ -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; + } + } +});