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

Commit ebf03692 by Mick Hansen

refactor: change tests to promises

1 parent 81c8b330
Showing with 105 additions and 127 deletions
...@@ -29,27 +29,25 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -29,27 +29,25 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
describe('getAssociation', function() { describe('getAssociation', function() {
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function(done) { it('supports transactions', function() {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING }) var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING }); , Group = sequelize.define('Group', { name: Support.Sequelize.STRING });
Group.belongsTo(User); Group.belongsTo(User);
sequelize.sync({ force: true }).success(function() { return sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Group.create({ name: 'bar' }).success(function(group) { return Group.create({ name: 'bar' }).then(function(group) {
sequelize.transaction().then(function(t) { return sequelize.transaction().then(function(t) {
group.setUser(user, { transaction: t }).success(function() { return group.setUser(user, { transaction: t }).then(function() {
Group.all().success(function(groups) { return Group.all().then(function(groups) {
groups[0].getUser().success(function(associatedUser) { return groups[0].getUser().then(function(associatedUser) {
expect(associatedUser).to.be.null; expect(associatedUser).to.be.null;
Group.all({ transaction: t }).success(function(groups) { return Group.all({ transaction: t }).then(function(groups) {
groups[0].getUser({ transaction: t }).success(function(associatedUser) { return groups[0].getUser({ transaction: t }).then(function(associatedUser) {
expect(associatedUser).to.be.not.null; expect(associatedUser).to.be.not.null;
t.rollback().success(function() { return t.rollback();
done();
});
}); });
}); });
}); });
...@@ -152,22 +150,22 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -152,22 +150,22 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
describe('setAssociation', function() { describe('setAssociation', function() {
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function(done) { it('supports transactions', function() {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING }) var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING }); , Group = sequelize.define('Group', { name: Support.Sequelize.STRING });
Group.belongsTo(User); Group.belongsTo(User);
sequelize.sync({ force: true }).success(function() { return sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Group.create({ name: 'bar' }).success(function(group) { return Group.create({ name: 'bar' }).then(function(group) {
sequelize.transaction().then(function(t) { return sequelize.transaction().then(function(t) {
group.setUser(user, { transaction: t }).success(function() { return group.setUser(user, { transaction: t }).then(function() {
Group.all().success(function(groups) { return Group.all().then(function(groups) {
groups[0].getUser().success(function(associatedUser) { return groups[0].getUser().then(function(associatedUser) {
expect(associatedUser).to.be.null; expect(associatedUser).to.be.null;
t.rollback().success(function() { done(); }); return t.rollback();
}); });
}); });
}); });
...@@ -179,26 +177,24 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -179,26 +177,24 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
} }
it('can set the association with declared primary keys...', function(done) { it('can set the association with declared primary keys...', function() {
var User = this.sequelize.define('UserXYZ', { user_id: {type: DataTypes.INTEGER, primaryKey: true }, username: DataTypes.STRING }) var User = this.sequelize.define('UserXYZ', { user_id: {type: DataTypes.INTEGER, primaryKey: true }, username: DataTypes.STRING })
, Task = this.sequelize.define('TaskXYZ', { task_id: {type: DataTypes.INTEGER, primaryKey: true }, title: DataTypes.STRING }); , Task = this.sequelize.define('TaskXYZ', { task_id: {type: DataTypes.INTEGER, primaryKey: true }, title: DataTypes.STRING });
Task.belongsTo(User, { foreignKey: 'user_id' }); Task.belongsTo(User, { foreignKey: 'user_id' });
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
User.create({ user_id: 1, username: 'foo' }).success(function(user) { return User.create({ user_id: 1, username: 'foo' }).then(function(user) {
Task.create({ task_id: 1, title: 'task' }).success(function(task) { return Task.create({ task_id: 1, title: 'task' }).then(function(task) {
task.setUserXYZ(user).success(function() { return task.setUserXYZ(user).then(function() {
task.getUserXYZ().success(function(user) { return task.getUserXYZ().then(function(user) {
expect(user).not.to.be.null; expect(user).not.to.be.null;
task.setUserXYZ(null).success(function() { return task.setUserXYZ(null).then(function() {
task.getUserXYZ().success(function(user) { return task.getUserXYZ().then(function(user) {
expect(user).to.be.null; expect(user).to.be.null;
done();
}); });
}); });
}); });
}); });
}); });
...@@ -206,26 +202,24 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -206,26 +202,24 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
it('clears the association if null is passed', function(done) { it('clears the association if null is passed', function() {
var User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING }) var User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: DataTypes.STRING }); , Task = this.sequelize.define('TaskXYZ', { title: DataTypes.STRING });
Task.belongsTo(User); Task.belongsTo(User);
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Task.create({ title: 'task' }).success(function(task) { return Task.create({ title: 'task' }).then(function(task) {
task.setUserXYZ(user).success(function() { return task.setUserXYZ(user).then(function() {
task.getUserXYZ().success(function(user) { return task.getUserXYZ().then(function(user) {
expect(user).not.to.be.null; expect(user).not.to.be.null;
task.setUserXYZ(null).success(function() { return task.setUserXYZ(null).then(function() {
task.getUserXYZ().success(function(user) { return task.getUserXYZ().then(function(user) {
expect(user).to.be.null; expect(user).to.be.null;
done();
}); });
}); });
}); });
}); });
}); });
...@@ -270,7 +264,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -270,7 +264,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
it('should not clobber atributes', function(done) { it('should not clobber atributes', function() {
var Comment = this.sequelize.define('comment', { var Comment = this.sequelize.define('comment', {
text: DataTypes.STRING text: DataTypes.STRING
}); });
...@@ -282,19 +276,17 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -282,19 +276,17 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
Post.hasOne(Comment); Post.hasOne(Comment);
Comment.belongsTo(Post); Comment.belongsTo(Post);
this.sequelize.sync().done(function() { return this.sequelize.sync().then(function() {
Post.create({ return Post.create({
title: 'Post title' title: 'Post title'
}).done(function(err, post) { }).then(function(post) {
Comment.create({ return Comment.create({
text: 'OLD VALUE' text: 'OLD VALUE'
}).done(function(err, comment) { }).then(function(comment) {
comment.setPost(post).done(function() { comment.text = 'UPDATED VALUE';
return comment.setPost(post).then(function() {
expect(comment.text).to.equal('UPDATED VALUE'); expect(comment.text).to.equal('UPDATED VALUE');
done();
}); });
comment.text = 'UPDATED VALUE';
}); });
}); });
}); });
...@@ -330,20 +322,18 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -330,20 +322,18 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
describe('createAssociation', function() { describe('createAssociation', function() {
it('creates an associated model instance', function(done) { it('creates an associated model instance', function() {
var User = this.sequelize.define('User', { username: DataTypes.STRING }) var User = this.sequelize.define('User', { username: DataTypes.STRING })
, Task = this.sequelize.define('Task', { title: DataTypes.STRING }); , Task = this.sequelize.define('Task', { title: DataTypes.STRING });
Task.belongsTo(User); Task.belongsTo(User);
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
Task.create({ title: 'task' }).success(function(task) { return Task.create({ title: 'task' }).then(function(task) {
task.createUser({ username: 'bob' }).success(function() { return task.createUser({ username: 'bob' }).then(function() {
task.getUser().success(function(user) { return task.getUser().then(function(user) {
expect(user).not.to.be.null; expect(user).not.to.be.null;
expect(user.username).to.equal('bob'); expect(user.username).to.equal('bob');
done();
}); });
}); });
}); });
...@@ -351,24 +341,24 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -351,24 +341,24 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function(done) { it('supports transactions', function() {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { return Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING }) var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING }); , Group = sequelize.define('Group', { name: Support.Sequelize.STRING });
Group.belongsTo(User); Group.belongsTo(User);
sequelize.sync({ force: true }).success(function() { return sequelize.sync({ force: true }).then(function() {
Group.create({ name: 'bar' }).success(function(group) { return Group.create({ name: 'bar' }).then(function(group) {
sequelize.transaction().then(function(t) { return sequelize.transaction().then(function(t) {
group.createUser({ username: 'foo' }, { transaction: t }).success(function() { return group.createUser({ username: 'foo' }, { transaction: t }).then(function() {
group.getUser().success(function(user) { return group.getUser().then(function(user) {
expect(user).to.be.null; expect(user).to.be.null;
group.getUser({ transaction: t }).success(function(user) { return group.getUser({ transaction: t }).then(function(user) {
expect(user).not.to.be.null; expect(user).not.to.be.null;
t.rollback().success(function() { done(); }); return t.rollback();
}); });
}); });
}); });
...@@ -439,20 +429,19 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -439,20 +429,19 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
describe('foreign key constraints', function() { describe('foreign key constraints', function() {
it('are enabled by default', function(done) { it('are enabled by default', function() {
var Task = this.sequelize.define('Task', { title: DataTypes.STRING }) var Task = this.sequelize.define('Task', { title: DataTypes.STRING })
, User = this.sequelize.define('User', { username: DataTypes.STRING }); , User = this.sequelize.define('User', { username: DataTypes.STRING });
Task.belongsTo(User); // defaults to SET NULL Task.belongsTo(User); // defaults to SET NULL
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Task.create({ title: 'task' }).success(function(task) { return Task.create({ title: 'task' }).then(function(task) {
task.setUser(user).success(function() { return task.setUser(user).then(function() {
user.destroy().success(function() { return user.destroy().then(function() {
task.reload().success(function() { return task.reload().then(function() {
expect(task.UserId).to.equal(null); expect(task.UserId).to.equal(null);
done();
}); });
}); });
}); });
...@@ -461,20 +450,19 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -461,20 +450,19 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
it('should be possible to disable them', function(done) { it('should be possible to disable them', function() {
var Task = this.sequelize.define('Task', { title: Sequelize.STRING }) var Task = this.sequelize.define('Task', { title: Sequelize.STRING })
, User = this.sequelize.define('User', { username: Sequelize.STRING }); , User = this.sequelize.define('User', { username: Sequelize.STRING });
Task.belongsTo(User, { constraints: false }); Task.belongsTo(User, { constraints: false });
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Task.create({ title: 'task' }).success(function(task) { return Task.create({ title: 'task' }).then(function(task) {
task.setUser(user).success(function() { return task.setUser(user).then(function() {
user.destroy().success(function() { return user.destroy().then(function() {
task.reload().success(function() { return task.reload().then(function() {
expect(task.UserId).to.equal(user.id); expect(task.UserId).to.equal(user.id);
done();
}); });
}); });
}); });
...@@ -483,20 +471,19 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -483,20 +471,19 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
it('can cascade deletes', function(done) { it('can cascade deletes', function() {
var Task = this.sequelize.define('Task', { title: DataTypes.STRING }) var Task = this.sequelize.define('Task', { title: DataTypes.STRING })
, User = this.sequelize.define('User', { username: DataTypes.STRING }); , User = this.sequelize.define('User', { username: DataTypes.STRING });
Task.belongsTo(User, {onDelete: 'cascade'}); Task.belongsTo(User, {onDelete: 'cascade'});
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Task.create({ title: 'task' }).success(function(task) { return Task.create({ title: 'task' }).then(function(task) {
task.setUser(user).success(function() { return task.setUser(user).then(function() {
user.destroy().success(function() { return user.destroy().then(function() {
Task.findAll().success(function(tasks) { return Task.findAll().then(function(tasks) {
expect(tasks).to.have.length(0); expect(tasks).to.have.length(0);
done();
}); });
}); });
}); });
...@@ -506,23 +493,20 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -506,23 +493,20 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
if (current.dialect.supports.constraints.restrict) { if (current.dialect.supports.constraints.restrict) {
it('can restrict deletes', function(done) { it('can restrict deletes', function() {
var self = this; var self = this;
var Task = this.sequelize.define('Task', { title: DataTypes.STRING }) var Task = this.sequelize.define('Task', { title: DataTypes.STRING })
, User = this.sequelize.define('User', { username: DataTypes.STRING }); , User = this.sequelize.define('User', { username: DataTypes.STRING });
Task.belongsTo(User, {onDelete: 'restrict'}); Task.belongsTo(User, {onDelete: 'restrict'});
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Task.create({ title: 'task' }).success(function(task) { return Task.create({ title: 'task' }).then(function(task) {
task.setUser(user).success(function() { return task.setUser(user).then(function() {
// Should fail due to FK restriction return expect(user.destroy()).to.eventually.be.rejectedWith(Sequelize.ForeignKeyConstraintError).then(function () {
user.destroy().catch (self.sequelize.ForeignKeyConstraintError, function(err) { return Task.findAll().then(function(tasks) {
expect(err).to.be.ok;
Task.findAll().success(function(tasks) {
expect(tasks).to.have.length(1); expect(tasks).to.have.length(1);
done();
}); });
}); });
}); });
...@@ -531,29 +515,29 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -531,29 +515,29 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
}); });
it('can restrict updates', function(done) { it('can restrict updates', function() {
var self = this; var self = this;
var Task = this.sequelize.define('Task', { title: DataTypes.STRING }) var Task = this.sequelize.define('Task', { title: DataTypes.STRING })
, User = this.sequelize.define('User', { username: DataTypes.STRING }); , User = this.sequelize.define('User', { username: DataTypes.STRING });
Task.belongsTo(User, {onUpdate: 'restrict'}); Task.belongsTo(User, {onUpdate: 'restrict'});
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Task.create({ title: 'task' }).success(function(task) { return Task.create({ title: 'task' }).then(function(task) {
task.setUser(user).success(function() { return task.setUser(user).then(function() {
// Changing the id of a DAO requires a little dance since // Changing the id of a DAO requires a little dance since
// the `UPDATE` query generated by `save()` uses `id` in the // the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause // `WHERE` clause
var tableName = user.QueryInterface.QueryGenerator.addSchema(user.Model); var tableName = user.QueryInterface.QueryGenerator.addSchema(user.Model);
user.QueryInterface.update(user, tableName, {id: 999}, user.id) return expect(
.catch (self.sequelize.ForeignKeyConstraintError, function() { user.QueryInterface.update(user, tableName, {id: 999}, user.id)
).to.eventually.be.rejectedWith(Sequelize.ForeignKeyConstraintError).then(function () {
// Should fail due to FK restriction // Should fail due to FK restriction
Task.findAll().success(function(tasks) { return Task.findAll().then(function(tasks) {
expect(tasks).to.have.length(1); expect(tasks).to.have.length(1);
done();
}); });
}); });
}); });
...@@ -566,28 +550,27 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -566,28 +550,27 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
// NOTE: mssql does not support changing an autoincrement primary key // NOTE: mssql does not support changing an autoincrement primary key
if (Support.getTestDialect() !== 'mssql') { if (Support.getTestDialect() !== 'mssql') {
it('can cascade updates', function(done) { it('can cascade updates', function() {
var Task = this.sequelize.define('Task', { title: DataTypes.STRING }) var Task = this.sequelize.define('Task', { title: DataTypes.STRING })
, User = this.sequelize.define('User', { username: DataTypes.STRING }); , User = this.sequelize.define('User', { username: DataTypes.STRING });
Task.belongsTo(User, {onUpdate: 'cascade'}); Task.belongsTo(User, {onUpdate: 'cascade'});
this.sequelize.sync({ force: true }).success(function() { return this.sequelize.sync({ force: true }).then(function() {
User.create({ username: 'foo' }).success(function(user) { return User.create({ username: 'foo' }).then(function(user) {
Task.create({ title: 'task' }).success(function(task) { return Task.create({ title: 'task' }).then(function(task) {
task.setUser(user).success(function() { return task.setUser(user).then(function() {
// Changing the id of a DAO requires a little dance since // Changing the id of a DAO requires a little dance since
// the `UPDATE` query generated by `save()` uses `id` in the // the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause // `WHERE` clause
var tableName = user.QueryInterface.QueryGenerator.addSchema(user.Model); var tableName = user.QueryInterface.QueryGenerator.addSchema(user.Model);
user.QueryInterface.update(user, tableName, {id: 999}, user.id) return user.QueryInterface.update(user, tableName, {id: 999}, user.id)
.success(function() { .then(function() {
Task.findAll().success(function(tasks) { return Task.findAll().then(function(tasks) {
expect(tasks).to.have.length(1); expect(tasks).to.have.length(1);
expect(tasks[0].UserId).to.equal(999); expect(tasks[0].UserId).to.equal(999);
done();
}); });
}); });
}); });
...@@ -624,7 +607,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -624,7 +607,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
}); });
describe('Association options', function() { describe('Association options', function() {
it('can specify data type for autogenerated relational keys', function(done) { it('can specify data type for autogenerated relational keys', function() {
var User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING }) var User = this.sequelize.define('UserXYZ', { username: DataTypes.STRING })
, dataTypes = [DataTypes.INTEGER, DataTypes.BIGINT, DataTypes.STRING] , dataTypes = [DataTypes.INTEGER, DataTypes.BIGINT, DataTypes.STRING]
, self = this , self = this
...@@ -637,14 +620,9 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -637,14 +620,9 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
Tasks[dataType].belongsTo(User, { foreignKey: 'userId', keyType: dataType, constraints: false }); Tasks[dataType].belongsTo(User, { foreignKey: 'userId', keyType: dataType, constraints: false });
}); });
self.sequelize.sync({ force: true }) return self.sequelize.sync({ force: true }).then(function() {
.success(function() {
dataTypes.forEach(function(dataType, i) { dataTypes.forEach(function(dataType, i) {
expect(Tasks[dataType].rawAttributes.userId.type).to.be.an.instanceof(dataType); expect(Tasks[dataType].rawAttributes.userId.type).to.be.an.instanceof(dataType);
if ((i + 1) === dataTypes.length) {
done();
}
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!