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

Commit 28c062ec by Overlook Motel

Tests for many-to-many self-associations

1 parent 75a1e3b8
Showing with 78 additions and 0 deletions
...@@ -59,6 +59,84 @@ describe(Support.getTestDialectTeaser("Self"), function() { ...@@ -59,6 +59,84 @@ describe(Support.getTestDialectTeaser("Self"), function() {
it('can handle n:m associations', function(done) { it('can handle n:m associations', function(done) {
var Person = this.sequelize.define('Person', { name: DataTypes.STRING }); var Person = this.sequelize.define('Person', { name: DataTypes.STRING });
Person.hasMany(Person, { as: 'Parents', through: 'Family' });
Person.hasMany(Person, { as: 'Childs', through: 'Family' });
var foreignIdentifiers = _.map(_.values(Person.associations), 'foreignIdentifier')
var rawAttributes = _.keys(this.sequelize.models.Family.rawAttributes)
expect(foreignIdentifiers.length).to.equal(2)
expect(rawAttributes.length).to.equal(4)
expect(foreignIdentifiers).to.have.members([ 'PersonId', 'ChildId' ])
expect(rawAttributes).to.have.members([ 'createdAt', 'updatedAt', 'PersonId', 'ChildId' ])
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.setParents([john]).done(function (err) {
expect(err).to.not.be.ok
chris.addParent(john).complete(function(err) {
expect(err).to.not.be.ok
john.getChilds().complete(function(err, children) {
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()
})
})
})
})
})
})
})
})
it('can handle n:m associations with pre-defined through table', function(done) {
var Person = this.sequelize.define('Person', { name: DataTypes.STRING });
var Family = this.sequelize.define('Family', { var Family = this.sequelize.define('Family', {
preexisting_child: { preexisting_child: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!