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

Commit 0698669c by Overlook Motel

Tests use promises

1 parent dd6aeac5
Showing with 18 additions and 62 deletions
...@@ -57,11 +57,13 @@ describe(Support.getTestDialectTeaser("Self"), function() { ...@@ -57,11 +57,13 @@ describe(Support.getTestDialectTeaser("Self"), function() {
}); });
}); });
it('can handle n:m associations', function(done) { it('can handle n:m associations', function() {
var Person = this.sequelize.define('Person', { name: DataTypes.STRING }); var self = this
var Person = this.sequelize.define('Person', { name: DataTypes.STRING })
Person.hasMany(Person, { as: 'Parents', through: 'Family' }); Person.hasMany(Person, { as: 'Parents', through: 'Family' })
Person.hasMany(Person, { as: 'Childs', through: 'Family' }); Person.hasMany(Person, { as: 'Childs', through: 'Family' })
var foreignIdentifiers = _.map(_.values(Person.associations), 'foreignIdentifier') var foreignIdentifiers = _.map(_.values(Person.associations), 'foreignIdentifier')
var rawAttributes = _.keys(this.sequelize.models.Family.rawAttributes) var rawAttributes = _.keys(this.sequelize.models.Family.rawAttributes)
...@@ -72,64 +74,18 @@ describe(Support.getTestDialectTeaser("Self"), function() { ...@@ -72,64 +74,18 @@ describe(Support.getTestDialectTeaser("Self"), function() {
expect(foreignIdentifiers).to.have.members([ 'PersonId', 'ChildId' ]) expect(foreignIdentifiers).to.have.members([ 'PersonId', 'ChildId' ])
expect(rawAttributes).to.have.members([ 'createdAt', 'updatedAt', 'PersonId', 'ChildId' ]) expect(rawAttributes).to.have.members([ 'createdAt', 'updatedAt', 'PersonId', 'ChildId' ])
this.sequelize.sync({ force: true }).complete(function() { return this.sequelize.sync({ force: true }).then(function() {
Person.create({ name: 'Mary' }).complete(function(err, mary) { return self.sequelize.Promise.all([
expect(err).to.not.be.ok Person.create({ name: 'Mary' }),
Person.create({ name: 'John' }).complete(function(err, john) { Person.create({ name: 'John' }),
expect(err).to.not.be.ok Person.create({ name: 'Chris' })
Person.create({ name: 'Chris' }).complete(function(err, chris) { ]).spread(function (mary, john, chris) {
expect(err).to.not.be.ok return mary.setParents([john]).then(function() {
mary.setParents([john]).done(function (err) { return chris.addParent(john)
expect(err).to.not.be.ok }).then(function() {
chris.addParent(john).complete(function(err) { return john.getChilds()
expect(err).to.not.be.ok }).then(function(children) {
john.getChilds().complete(function(err, children) { expect(_.map(children, 'id')).to.have.members([mary.id, chris.id])
expect(err).to.not.be.ok
expect(_.map(children, 'id')).to.have.members([mary.id, chris.id])
done()
})
})
})
})
})
})
})
})
it('can handle n:m associations with no through table specified', function(done) {
var Person = this.sequelize.define('Person', { name: DataTypes.STRING });
Person.hasMany(Person);
Person.hasMany(Person);
var foreignIdentifiers = _.map(_.values(Person.associations), 'foreignIdentifier')
var rawAttributes = _.keys(this.sequelize.models.PersonsPersons.rawAttributes)
expect(foreignIdentifiers.length).to.equal(2)
expect(rawAttributes.length).to.equal(4)
expect(foreignIdentifiers).to.have.members([ 'PersonId', 'PersonReverseId' ])
expect(rawAttributes).to.have.members([ 'createdAt', 'updatedAt', 'PersonId', 'PersonReverseId' ])
this.sequelize.sync({ force: true }).complete(function() {
Person.create({ name: 'Mary' }).complete(function(err, mary) {
expect(err).to.not.be.ok
Person.create({ name: 'John' }).complete(function(err, john) {
expect(err).to.not.be.ok
Person.create({ name: 'Chris' }).complete(function(err, chris) {
expect(err).to.not.be.ok
mary.setPersons([john]).done(function (err) {
expect(err).to.not.be.ok
chris.addPerson(john).complete(function(err) {
expect(err).to.not.be.ok
john.getPersonReverses().complete(function(err, children) {
expect(err).to.not.be.ok
expect(_.map(children, 'id')).to.have.members([mary.id, chris.id])
done()
})
})
})
})
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!