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

Commit f5f3adf6 by Sascha Depold

check if the association uses the right alias

1 parent bf8246bc
Showing with 46 additions and 4 deletions
......@@ -391,7 +391,7 @@ module.exports = (function() {
, association = (usesAlias ? this.getAssociationByAlias(include.as) : this.getAssociation(include.daoFactory))
// check if the current daoFactory is actually associated with the passed daoFactory
if (!!association) {
if (!!association && (!association.options.as || (association.options.as === include.as))) {
include.association = association
return include
......
......@@ -472,7 +472,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
this.Worker = this.sequelize.define('Worker', { name: Sequelize.STRING })
})
describe('=>belongsTo only', function() {
describe('=>belongsTo', function() {
before(function(done) {
this.Task.belongsTo(this.Worker)
......@@ -520,7 +520,7 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
describe('=>hasOne only', function() {
describe('=>hasOne', function() {
before(function(done) {
this.Worker.hasOne(this.Task)
......@@ -556,7 +556,49 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
})
})
describe('=>hasMany only', function() {
describe('=>hasOne with alias', function() {
before(function(done) {
this.Worker.hasOne(this.Task, { as: 'ToDo' })
this.sequelize.sync({ force: true }).complete(function() {
this.Worker.create({ name: 'worker' }).success(function(worker) {
this.Task.create({ title: 'homework' }).success(function(task) {
this.worker = worker
this.task = task
this.worker.setToDo(this.task).success(done)
}.bind(this))
}.bind(this))
}.bind(this))
})
it('throws an error if included DaoFactory is not referenced by alias', function() {
Helpers.assertException(function() {
this.Worker.find({ include: [ this.Task ] })
}.bind(this), 'Task is not associated to Worker!')
})
it('throws an error if alias is not associated', function() {
Helpers.assertException(function() {
this.Worker.find({ include: [ { daoFactory: this.Task, as: 'Work' } ] })
}.bind(this), 'Task (Work) is not associated to Worker!')
})
it('returns the associated task via worker.task', function(done) {
this.Worker.find({
where: { id: this.worker.id },
include: [ { daoFactory: this.Task, as: 'ToDo' } ]
}).complete(function(err, worker) {
expect(err).toBeNull()
expect(worker).toBeDefined()
expect(worker.toDo).toBeDefined()
expect(worker.toDo.title).toEqual('homework')
done()
}.bind(this))
})
})
describe('=>hasMany', function() {
before(function(done) {
this.Worker.hasMany(this.Task)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!