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

Commit c4000494 by Ruben Bridgewater

Refactor some tests to use promise style

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