不要怂,就是干,撸起袖子干!

Commit 5af6097e by Overlook Motel

tests for include all

1 parent 1e1a10b6
Showing with 117 additions and 0 deletions
......@@ -830,6 +830,123 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
describe('include all', function() {
beforeEach(function(done) {
var self = this
self.Continent = this.sequelize.define('Continent', { name: Sequelize.STRING })
self.Country = this.sequelize.define('Country', { name: Sequelize.STRING })
self.Industry = this.sequelize.define('Industry', { name: Sequelize.STRING })
self.Person = this.sequelize.define('Person', { name: Sequelize.STRING, lastName: Sequelize.STRING })
self.Continent.hasMany(self.Country)
self.Country.belongsTo(self.Continent)
self.Country.hasMany(self.Industry)
self.Industry.hasMany(self.Country)
self.Country.hasMany(self.Person)
self.Person.belongsTo(self.Country)
self.Country.hasMany(self.Person, { as: 'Residents', foreignKey: 'CountryResidentId' })
self.Person.belongsTo(self.Country, { as: 'CountryResident', foreignKey: 'CountryResidentId' })
async.forEach([ self.Continent, self.Country, self.Industry, self.Person ], function(model, callback) {
model.sync({ force: true }).done(callback)
}, function () {
async.parallel({
europe: function(callback) {self.Continent.create({ name: 'Europe' }).done(callback)},
england: function(callback) {self.Country.create({ name: 'England' }).done(callback)},
coal: function(callback) {self.Industry.create({ name: 'Coal' }).done(callback)},
bob: function(callback) {self.Person.create({ name: 'Bob', lastName: 'Becket' }).done(callback)}
}, function(err, r) {
if (err) throw err
_.forEach(r, function(item, itemName) {
self[itemName] = item
})
async.parallel([
function(callback) {self.england.setContinent(self.europe).done(callback)},
function(callback) {self.england.addIndustry(self.coal).done(callback)},
function(callback) {self.bob.setCountry(self.england).done(callback)},
function(callback) {self.bob.setCountryResident(self.england).done(callback)}
], function(err) {
if (err) throw err
done()
})
})
})
})
it('includes all associations', function(done) {
this.Country.findAll({ include: [ { all: true } ] }).done(function(err, countries) {
expect(err).not.to.be.ok
expect(countries).to.exist
expect(countries[0]).to.exist
expect(countries[0].continent).to.exist
expect(countries[0].industries).to.exist
expect(countries[0].persons).to.exist
expect(countries[0].residents).to.exist
done()
})
})
it('includes specific type of association', function(done) {
this.Country.findAll({ include: [ { all: 'BelongsTo' } ] }).done(function(err, countries) {
expect(err).not.to.be.ok
expect(countries).to.exist
expect(countries[0]).to.exist
expect(countries[0].continent).to.exist
expect(countries[0].industries).not.to.exist
expect(countries[0].persons).not.to.exist
expect(countries[0].residents).not.to.exist
done()
})
})
it('utilises specified attributes', function(done) {
this.Country.findAll({ include: [ { all: 'HasMany', attributes: [ 'name' ] } ] }).done(function(err, countries) {
expect(err).not.to.be.ok
expect(countries).to.exist
expect(countries[0]).to.exist
expect(countries[0].industries).to.exist
expect(countries[0].persons).to.exist
expect(countries[0].persons[0]).to.exist
expect(countries[0].persons[0].name).not.to.be.undefined
expect(countries[0].persons[0].lastName).to.be.undefined
expect(countries[0].residents).to.exist
expect(countries[0].residents[0]).to.exist
expect(countries[0].residents[0].name).not.to.be.undefined
expect(countries[0].residents[0].lastName).to.be.undefined
done()
})
})
it('is over-ruled by specified include', function(done) {
this.Country.findAll({ include: [ { all: true }, { model: this.Continent, attributes: [] } ] }).done(function(err, countries) {
expect(err).not.to.be.ok
expect(countries).to.exist
expect(countries[0]).to.exist
expect(countries[0].continent).to.exist
expect(countries[0].continent.name).to.be.undefined
done()
})
})
it('includes all nested associations', function(done) {
this.Continent.findAll({ include: [ { all: true, nested: true } ] }).done(function(err, continents) {
expect(err).not.to.be.ok
expect(continents).to.exist
expect(continents[0]).to.exist
expect(continents[0].countries).to.exist
expect(continents[0].countries[0]).to.exist
expect(continents[0].countries[0].industries).to.exist
expect(continents[0].countries[0].persons).to.exist
expect(continents[0].countries[0].residents).to.exist
expect(continents[0].countries[0].continent).not.to.exist
done()
})
})
})
})
describe('order by eager loaded tables', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!