-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests fix for balderdashy/sails#3946 (issued as PR https://github.com/balderdashy/waterline/pull/1435/files)
- Loading branch information
Showing
5 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
interfaces/associations/support/fixtures/manyToManyThrough.driver.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}); |
25 changes: 25 additions & 0 deletions
25
interfaces/associations/support/fixtures/manyToManyThrough.rideshare.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
|
||
}); |
28 changes: 28 additions & 0 deletions
28
interfaces/associations/support/fixtures/manyToManyThrough.taxi.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}); |