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

Commit 6f168944 by Clement Teule

Refactor the unit tests to be less waterfall.

1 parent 34f0eacb
Showing with 65 additions and 101 deletions
...@@ -520,25 +520,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -520,25 +520,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
Player.hasOne(Shoe); Player.hasOne(Shoe);
Shoe.belongsTo(Player); Shoe.belongsTo(Player);
return Player.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
return Shoe.sync({force: true}).then(function() { return Shoe.create({
return Shoe.create({ brand: 'the brand' }).then(function(team) { brand: 'the brand',
return Player.create({ name: 'the player' }).then(function(player) { Player: {
return player.setShoe(team).then(function() { name: 'the player'
return Player.findOne({ }
where: { id: player.id }, }, {include: [Player]});
include: [Shoe] }).then(function(shoe) {
}).then(function(lePlayer) { return Player.findOne({
expect(lePlayer.Shoe).not.to.be.null; where: { id: shoe.Player.id },
return lePlayer.Shoe.destroy().then(function() { include: [Shoe]
return lePlayer.reload().then(function(lePlayer) { }).then(function(lePlayer) {
expect(lePlayer.Shoe).to.be.null; expect(lePlayer.Shoe).not.to.be.null;
}); return lePlayer.Shoe.destroy().return(lePlayer);
}); }).then(function(lePlayer) {
}); return lePlayer.reload();
}); }).then(function(lePlayer) {
}); expect(lePlayer.Shoe).to.be.null;
});
}); });
}); });
}); });
...@@ -550,95 +549,60 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -550,95 +549,60 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
Team.hasMany(Player); Team.hasMany(Player);
Player.belongsTo(Team); Player.belongsTo(Team);
return Team.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
return Player.sync({force: true}).then(function() { return Team.create({
return Team.create({ name: 'the team' }).then(function(team) { name: 'the team',
return Player.create({ name: 'the player1' }).then(function(player1) { Players: [{
return Player.create({ name: 'the player2' }).then(function(player2) { name: 'the player1'
return team.setPlayers([player1, player2]).then(function() { }, {
return Team.findOne({ name: 'the player2'
where: { id: team.id }, }]
include: [Player] }, {include: [Player]});
}).then(function(leTeam) { }).then(function(team) {
expect(leTeam.Players).not.to.be.empty; return Team.findOne({
return player1.destroy().then(function() { where: { id: team.id },
return player2.destroy().then(function() { include: [Player]
return leTeam.reload().then(function(leTeam) { }).then(function(leTeam) {
expect(leTeam.Players).to.be.empty; expect(leTeam.Players).not.to.be.empty;
}); return leTeam.Players[1].destroy().then(function() {
}); return leTeam.Players[0].destroy();
}); }).return(leTeam);
}); }).then(function(leTeam) {
}); return leTeam.reload();
}); }).then(function(leTeam) {
}); expect(leTeam.Players).to.be.empty;
}); });
}); });
}); });
});
it('should update the associations after one element deleted', function() {
it('should update the associations after first element deleted, 1-N', function() {
var Team = this.sequelize.define('Team', { name: DataTypes.STRING }) var Team = this.sequelize.define('Team', { name: DataTypes.STRING })
, Player = this.sequelize.define('Player', { name: DataTypes.STRING }); , Player = this.sequelize.define('Player', { name: DataTypes.STRING });
Team.hasMany(Player); Team.hasMany(Player);
Player.belongsTo(Team); Player.belongsTo(Team);
return Team.sync({force: true}).then(function() {
return Player.sync({force: true}).then(function() {
return Team.create({ name: 'the team' }).then(function(team) {
return Player.create({ name: 'the player1' }).then(function(player1) {
return Player.create({ name: 'the player2' }).then(function(player2) {
return team.setPlayers([player1, player2]).then(function() {
return Team.findOne({
where: { id: team.id },
include: [Player]
}).then(function(leTeam) {
expect(leTeam.Players).to.have.length(2);
return player2.destroy().then(function() {
return leTeam.reload().then(function(leTeam) {
expect(leTeam.Players).to.have.length(1);
expect(leTeam.Players[0].name).to.equal('the player1');
});
});
});
});
});
});
});
});
});
});
it('should update the associations after not-first element deleted, 1-N', function() {
var Team = this.sequelize.define('Team', { name: DataTypes.STRING })
, Player = this.sequelize.define('Player', { name: DataTypes.STRING });
Team.hasMany(Player);
Player.belongsTo(Team);
return Team.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
return Player.sync({force: true}).then(function() { return Team.create({
return Team.create({ name: 'the team' }).then(function(team) { name: 'the team',
return Player.create({ name: 'the player1' }).then(function(player1) { Players: [{
return Player.create({ name: 'the player2' }).then(function(player2) { name: 'the player1'
return team.setPlayers([player1, player2]).then(function() { }, {
return Team.findOne({ name: 'the player2'
where: { id: team.id }, }]
include: [Player] }, {include: [Player]});
}).then(function(leTeam) { }).then(function(team) {
expect(leTeam.Players).to.have.length(2); return Team.findOne({
return player1.destroy().then(function() { where: { id: team.id },
return leTeam.reload().then(function(leTeam) { include: [Player]
expect(leTeam.Players).to.have.length(1); }).then(function(leTeam) {
expect(leTeam.Players[0].name).to.equal('the player2'); expect(leTeam.Players).to.have.length(2);
}); return leTeam.Players[0].destroy().return(leTeam);
}); }).then(function(leTeam) {
}); return leTeam.reload();
}); }).then(function(leTeam) {
}); expect(leTeam.Players).to.have.length(1);
});
});
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!