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

Commit c4000494 by Ruben Bridgewater

Refactor some tests to use promise style

1 parent 768c4400
......@@ -52,11 +52,10 @@ describe(Support.getTestDialectTeaser('Configuration'), function() {
}
});
it('when we don\'t have a valid dialect.', function(done) {
it('when we don\'t have a valid dialect.', function() {
expect(function() {
new Sequelize(config[dialect].database, config[dialect].username, config[dialect].password, {host: '0.0.0.1', port: config[dialect].port, dialect: undefined});
}).to.throw(Error, 'The dialect undefined is not supported.');
done();
});
});
......@@ -75,13 +74,12 @@ describe(Support.getTestDialectTeaser('Configuration'), function() {
expect(config.port).to.equal('9821');
});
it('should work with no authentication options', function(done) {
it('should work with no authentication options', function() {
var sequelize = new Sequelize('mysql://example.com:9821/dbname');
var config = sequelize.config;
expect(config.username).to.not.be.ok;
expect(config.password).to.be.null;
done();
});
it('should work with no authentication options and passing additional options', function() {
......@@ -113,26 +111,24 @@ describe(Support.getTestDialectTeaser('Configuration'), function() {
});
describe('Intantiation with arguments', function() {
it('should accept two parameters (database, username)', function(done) {
it('should accept two parameters (database, username)', function() {
var sequelize = new Sequelize('dbname', 'root');
var config = sequelize.config;
expect(config.database).to.equal('dbname');
expect(config.username).to.equal('root');
done();
});
it('should accept three parameters (database, username, password)', function(done) {
it('should accept three parameters (database, username, password)', function() {
var sequelize = new Sequelize('dbname', 'root', 'pass');
var config = sequelize.config;
expect(config.database).to.equal('dbname');
expect(config.username).to.equal('root');
expect(config.password).to.equal('pass');
done();
});
it('should accept four parameters (database, username, password, options)', function(done) {
it('should accept four parameters (database, username, password, options)', function() {
var sequelize = new Sequelize('dbname', 'root', 'pass', {
port: 999,
dialectOptions: {
......@@ -148,7 +144,6 @@ describe(Support.getTestDialectTeaser('Configuration'), function() {
expect(config.port).to.equal(999);
expect(config.dialectOptions.supportBigNumbers).to.be.true;
expect(config.dialectOptions.bigNumberStrings).to.be.true;
done();
});
});
......
......@@ -11,33 +11,30 @@ if (Support.dialectIsMySQL()) {
describe('[MYSQL Specific] Associations', function() {
describe('many-to-many', function() {
describe('where tables have the same prefix', function() {
it('should create a table wp_table1wp_table2s', function(done) {
it('should create a table wp_table1wp_table2s', function() {
var Table2 = this.sequelize.define('wp_table2', {foo: DataTypes.STRING})
, Table1 = this.sequelize.define('wp_table1', {foo: DataTypes.STRING})
, self = this;
Table1.hasMany(Table2);
Table2.hasMany(Table1);
Table1.sync({ force: true }).success(function() {
Table2.sync({ force: true }).success(function() {
return Table1.sync({ force: true }).then(function() {
return Table2.sync({ force: true }).then(function() {
expect(self.sequelize.daoFactoryManager.getDAO('wp_table1swp_table2s')).to.exist;
done();
});
});
});
});
describe('when join table name is specified', function() {
beforeEach(function(done) {
beforeEach(function() {
var Table2 = this.sequelize.define('ms_table1', {foo: DataTypes.STRING})
, Table1 = this.sequelize.define('ms_table2', {foo: DataTypes.STRING});
Table1.hasMany(Table2, {joinTableName: 'table1_to_table2'});
Table2.hasMany(Table1, {joinTableName: 'table1_to_table2'});
Table1.sync({ force: true }).success(function() {
Table2.sync({ force: true }).success(function() {
done();
});
return Table1.sync({ force: true }).then(function() {
return Table2.sync({ force: true });
});
});
......@@ -50,7 +47,7 @@ if (Support.dialectIsMySQL()) {
describe('HasMany', function() {
beforeEach(function(done) {
beforeEach(function() {
//prevent periods from occurring in the table name since they are used to delimit (table.column)
this.User = this.sequelize.define('User' + Math.ceil(Math.random() * 10000000), { name: DataTypes.STRING });
this.Task = this.sequelize.define('Task' + Math.ceil(Math.random() * 10000000), { name: DataTypes.STRING });
......@@ -72,40 +69,36 @@ if (Support.dialectIsMySQL()) {
tasks[tasks.length] = {name: 'Task' + Math.random()};
}
this.sequelize.sync({ force: true }).success(function() {
self.User.bulkCreate(users).success(function() {
self.Task.bulkCreate(tasks).success(function() {
done();
});
return this.sequelize.sync({ force: true }).then(function() {
return self.User.bulkCreate(users).then(function() {
return self.Task.bulkCreate(tasks);
});
});
});
describe('addDAO / getDAO', function() {
beforeEach(function(done) {
beforeEach(function() {
var self = this;
self.user = null;
self.task = null;
self.User.all().success(function(_users) {
self.Task.all().success(function(_tasks) {
return self.User.findAll().then(function(_users) {
return self.Task.findAll().then(function(_tasks) {
self.user = _users[0];
self.task = _tasks[0];
done();
});
});
});
it('should correctly add an association to the dao', function(done) {
it('should correctly add an association to the dao', function() {
var self = this;
self.user.getTasks().on('success', function(_tasks) {
return self.user.getTasks().then(function(_tasks) {
expect(_tasks.length).to.equal(0);
self.user.addTask(self.task).on('success', function() {
self.user.getTasks().on('success', function(_tasks) {
return self.user.addTask(self.task).then(function() {
return self.user.getTasks().then(function(_tasks) {
expect(_tasks.length).to.equal(1);
done();
});
});
});
......@@ -113,36 +106,34 @@ if (Support.dialectIsMySQL()) {
});
describe('removeDAO', function() {
beforeEach(function(done) {
beforeEach(function() {
var self = this;
self.user = null;
self.tasks = null;
self.User.all().success(function(_users) {
self.Task.all().success(function(_tasks) {
return self.User.findAll().then(function(_users) {
return self.Task.findAll().then(function(_tasks) {
self.user = _users[0];
self.tasks = _tasks;
done();
});
});
});
it('should correctly remove associated objects', function(done) {
it('should correctly remove associated objects', function() {
var self = this;
self.user.getTasks().on('success', function(__tasks) {
return self.user.getTasks().then(function(__tasks) {
expect(__tasks.length).to.equal(0);
self.user.setTasks(self.tasks).on('success', function() {
self.user.getTasks().on('success', function(_tasks) {
return self.user.setTasks(self.tasks).then(function() {
return self.user.getTasks().then(function(_tasks) {
expect(_tasks.length).to.equal(self.tasks.length);
self.user.removeTask(self.tasks[0]).on('success', function() {
self.user.getTasks().on('success', function(_tasks) {
return self.user.removeTask(self.tasks[0]).then(function() {
return self.user.getTasks().then(function(_tasks) {
expect(_tasks.length).to.equal(self.tasks.length - 1);
self.user.removeTasks([self.tasks[1], self.tasks[2]]).on('success', function() {
self.user.getTasks().on('success', function(_tasks) {
return self.user.removeTasks([self.tasks[1], self.tasks[2]]).then(function() {
return self.user.getTasks().then(function(_tasks) {
expect(_tasks).to.have.length(self.tasks.length - 3);
done();
});
});
});
......
......@@ -60,19 +60,19 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
if (current.dialect.supports.transactions) {
it('supports transactions', function(done) {
it('supports transactions', function() {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING });
User.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) {
sequelize.transaction().then(function(t) {
user.update({ username: 'bar' }, { transaction: t }).success(function() {
User.all().success(function(users1) {
User.all({ transaction: t }).success(function(users2) {
return User.sync({ force: true }).then(function() {
return User.create({ username: 'foo' }).then(function(user) {
return sequelize.transaction().then(function(t) {
return user.update({ username: 'bar' }, { transaction: t }).then(function() {
return User.findAll().then(function(users1) {
return User.findAll({ transaction: t }).then(function(users2) {
expect(users1[0].username).to.equal('foo');
expect(users2[0].username).to.equal('bar');
t.rollback().success(function() { done(); });
return t.rollback();
});
});
});
......@@ -325,88 +325,84 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
});
it('updates attributes in the database', function(done) {
this.User.create({ username: 'user' }).success(function(user) {
it('updates attributes in the database', function() {
return this.User.create({ username: 'user' }).then(function(user) {
expect(user.username).to.equal('user');
user.update({ username: 'person' }).success(function(user) {
return user.update({ username: 'person' }).then(function(user) {
expect(user.username).to.equal('person');
done();
});
});
});
it('ignores unknown attributes', function(done) {
this.User.create({ username: 'user' }).success(function(user) {
user.update({ username: 'person', foo: 'bar'}).success(function(user) {
it('ignores unknown attributes', function() {
return this.User.create({ username: 'user' }).then(function(user) {
return user.update({ username: 'person', foo: 'bar'}).then(function(user) {
expect(user.username).to.equal('person');
expect(user.foo).not.to.exist;
done();
});
});
});
it("doesn't update primary keys or timestamps", function(done) {
it("doesn't update primary keys or timestamps", function() {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
identifier: {type: DataTypes.STRING, primaryKey: true}
});
User.sync({ force: true }).success(function() {
return User.sync({ force: true }).then(function() {
User.create({
name: 'snafu',
identifier: 'identifier'
}).success(function(user) {
}).then(function(user) {
var oldCreatedAt = user.createdAt
, oldUpdatedAt = user.updatedAt
, oldIdentifier = user.identifier;
setTimeout(function() {
user.update({
return user.update({
name: 'foobar',
createdAt: new Date(2000, 1, 1),
identifier: 'another identifier'
}).success(function(user) {
}).then(function(user) {
expect(new Date(user.createdAt)).to.equalDate(new Date(oldCreatedAt));
expect(new Date(user.updatedAt)).to.not.equalTime(new Date(oldUpdatedAt));
expect(user.identifier).to.equal(oldIdentifier);
done();
});
}, 1000);
});
});
});
it('stores and restores null values', function(done) {
it('stores and restores null values', function() {
var Download = this.sequelize.define('download', {
startedAt: DataTypes.DATE,
canceledAt: DataTypes.DATE,
finishedAt: DataTypes.DATE
});
Download.sync().success(function() {
Download.create({
return Download.sync().then(function() {
return Download.create({
startedAt: new Date()
}).success(function(download) {
}).then(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt).to.not.be.ok;
expect(download.finishedAt).to.not.be.ok;
download.update({
return download.update({
canceledAt: new Date()
}).success(function(download) {
}).then(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt instanceof Date).to.be.true;
expect(download.finishedAt).to.not.be.ok;
Download.all({
return Download.findAll({
where: (dialect === 'postgres' || dialect === 'mssql' ? '"finishedAt" IS NULL' : '`finishedAt` IS NULL')
}).success(function(downloads) {
}).then(function(downloads) {
downloads.forEach(function(download) {
expect(download.startedAt instanceof Date).to.be.true;
expect(download.canceledAt instanceof Date).to.be.true;
expect(download.finishedAt).to.not.be.ok;
done();
});
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!