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

Commit c4cbb3f3 by Ruben Bridgewater

Remove done from sync tests

Refactor another test to use promises

Remove done from sync tests

Refactor the last non promise style find test

Refactor almost all schema tests to use promises

Refactor another schema test to use promises

Insert missing semicolon

Refactor all schema tests

Insert missing epect statement

Rewrite multiple increments / decrements to be more readable

Refactor last create test to use promises
1 parent 50b23bc6
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
The entry point. The entry point.
@module Sequelize @module Sequelize
**/ **/
module.exports = require("./lib/sequelize") module.exports = require("./lib/sequelize");
...@@ -13,7 +13,7 @@ if (dialect.match(/^postgres/)) { ...@@ -13,7 +13,7 @@ if (dialect.match(/^postgres/)) {
describe('[POSTGRES Specific] associations', function() { describe('[POSTGRES 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});
...@@ -21,35 +21,24 @@ if (dialect.match(/^postgres/)) { ...@@ -21,35 +21,24 @@ if (dialect.match(/^postgres/)) {
Table2.hasMany(Table1); Table2.hasMany(Table1);
expect(this.sequelize.daoFactoryManager.getDAO('wp_table1swp_table2s')).to.exist; expect(this.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'});
setTimeout(function() {
done();
}, 50);
}); });
it('should not use a combined name', function(done) { it('should not use a combined name', function() {
expect(this.sequelize.daoFactoryManager.getDAO('ms_table1sms_table2s')).not.to.exist; expect(this.sequelize.daoFactoryManager.getDAO('ms_table1sms_table2s')).not.to.exist;
setTimeout(function() {
done();
}, 50);
}); });
it('should use the specified name', function(done) { it('should use the specified name', function() {
expect(this.sequelize.daoFactoryManager.getDAO('table1_to_table2')).to.exist; expect(this.sequelize.daoFactoryManager.getDAO('table1_to_table2')).to.exist;
setTimeout(function() {
done();
}, 50);
}); });
}); });
}); });
......
...@@ -363,38 +363,37 @@ if (dialect.match(/^postgres/)) { ...@@ -363,38 +363,37 @@ if (dialect.match(/^postgres/)) {
}); });
}); });
it('should be able to add enum types', function(done) { it('should be able to add enum types', function() {
var self = this var self = this
, User = this.sequelize.define('UserEnums', { , User = this.sequelize.define('UserEnums', {
mood: DataTypes.ENUM('happy', 'sad', 'meh') mood: DataTypes.ENUM('happy', 'sad', 'meh')
}); })
, count = 0;
var _done = _.after(4, function() {
done();
});
User.sync({ force: true }).then(function() { return User.sync({ force: true }).then(function() {
User = self.sequelize.define('UserEnums', { User = self.sequelize.define('UserEnums', {
mood: DataTypes.ENUM('neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful') mood: DataTypes.ENUM('neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful')
}); });
User.sync().then(function() { return User.sync().then(function() {
expect(User.rawAttributes.mood.values).to.deep.equal(['neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful']); expect(User.rawAttributes.mood.values).to.deep.equal(['neutral', 'happy', 'sad', 'ecstatic', 'meh', 'joyful']);
_done(); count++;
}).on('sql', function(sql) { }).on('sql', function(sql) {
if (sql.indexOf('neutral') > -1) { if (sql.indexOf('neutral') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'neutral' BEFORE 'happy'"); expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'neutral' BEFORE 'happy'");
_done(); count++;
} }
else if (sql.indexOf('ecstatic') > -1) { else if (sql.indexOf('ecstatic') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'ecstatic' BEFORE 'meh'"); expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'ecstatic' BEFORE 'meh'");
_done(); count++;
} }
else if (sql.indexOf('joyful') > -1) { else if (sql.indexOf('joyful') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'joyful' AFTER 'meh'"); expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'joyful' AFTER 'meh'");
_done(); count++;
} }
}); });
}).then(function() {
expect(count).to.equal(4);
}); });
}); });
}); });
......
...@@ -341,11 +341,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -341,11 +341,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('should still work right with other concurrent increments', function() { it('should still work right with other concurrent increments', function() {
var self = this; var self = this;
return this.User.find(1).then(function(user1) { return this.User.find(1).then(function(user1) {
return user1.increment(['aNumber'], { by: 2 }).then(function() { return this.sequelize.Promise.all([
return user1.increment(['aNumber'], { by: 2 }); user1.increment(['aNumber'], { by: 2 }),
}).then(function() { user1.increment(['aNumber'], { by: 2 }),
return user1.increment(['aNumber'], { by: 2 }); user1.increment(['aNumber'], { by: 2 })
}).then(function() { ]).then(function() {
return self.User.find(1).then(function(user2) { return self.User.find(1).then(function(user2) {
expect(user2.aNumber).to.equal(6); expect(user2.aNumber).to.equal(6);
}); });
...@@ -468,11 +468,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -468,11 +468,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('should still work right with other concurrent increments', function() { it('should still work right with other concurrent increments', function() {
var self = this; var self = this;
return this.User.find(1).then(function(user1) { return this.User.find(1).then(function(user1) {
return user1.decrement(['aNumber'], { by: 2 }).then(function() { return this.sequelize.Promise.all([
return user1.decrement(['aNumber'], { by: 2 }); user1.decrement(['aNumber'], { by: 2 }),
}).then(function() { user1.decrement(['aNumber'], { by: 2 }),
return user1.decrement(['aNumber'], { by: 2 }); user1.decrement(['aNumber'], { by: 2 })
}).then(function() { ]).then(function() {
return self.User.find(1).then(function(user2) { return self.User.find(1).then(function(user2) {
expect(user2.aNumber).to.equal(-6); expect(user2.aNumber).to.equal(-6);
}); });
......
...@@ -924,12 +924,12 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -924,12 +924,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
it('can omit autoincremental columns', function(done) { it('can omit autoincremental columns', function() {
var self = this var self = this
, data = { title: 'Iliad' } , data = { title: 'Iliad' }
, dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT] , dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT]
, chain = new Sequelize.Utils.QueryChainer() , sync = []
, chain2 = new Sequelize.Utils.QueryChainer() , promises = []
, books = []; , books = [];
dataTypes.forEach(function(dataType, index) { dataTypes.forEach(function(dataType, index) {
...@@ -940,21 +940,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -940,21 +940,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
books.forEach(function(b) { books.forEach(function(b) {
chain.add(b.sync({ force: true })); sync.push(b.sync({ force: true }));
}); });
chain.run().then(function() { return Promise.all(sync).then(function() {
books.forEach(function(b) { books.forEach(function(b, index) {
chain2.add(b.create(data)); promises.push(b.create(data).then(function(book) {
});
chain2.run().then(function(results) {
results.forEach(function(book, index) {
expect(book.title).to.equal(data.title); expect(book.title).to.equal(data.title);
expect(book.author).to.equal(data.author); expect(book.author).to.equal(data.author);
expect(books[index].rawAttributes.id.type instanceof dataTypes[index]).to.be.ok; expect(books[index].rawAttributes.id.type instanceof dataTypes[index]).to.be.ok;
}); }));
done();
}); });
return Promise.all(promises);
}); });
}); });
......
...@@ -11,7 +11,6 @@ var chai = require('chai') ...@@ -11,7 +11,6 @@ var chai = require('chai')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, promised = require('chai-as-promised') , promised = require('chai-as-promised')
, _ = require('lodash') , _ = require('lodash')
, async = require('async')
, current = Support.sequelize; , current = Support.sequelize;
chai.use(promised); chai.use(promised);
...@@ -760,104 +759,71 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -760,104 +759,71 @@ describe(Support.getTestDialectTeaser('Model'), function() {
this.Tag = this.sequelize.define('Tag', { name: Sequelize.STRING }); this.Tag = this.sequelize.define('Tag', { name: Sequelize.STRING });
}); });
it('returns the associated models when using through as string and alias', function(done) { it('returns the associated models when using through as string and alias', function() {
var self = this; var self = this;
this.Product.hasMany(this.Tag, {as: 'tags', through: 'product_tag'}); this.Product.hasMany(this.Tag, {as: 'tags', through: 'product_tag'});
this.Tag.hasMany(this.Product, {as: 'products', through: 'product_tag'}); this.Tag.hasMany(this.Product, {as: 'products', through: 'product_tag'});
this.sequelize.sync().done(function() { return this.sequelize.sync().then(function() {
async.auto({ return Promise.all([
createProducts: function(callback) { self.Product.bulkCreate([
self.Product.bulkCreate([ {title: 'Chair'},
{title: 'Chair'}, {title: 'Desk'},
{title: 'Desk'}, {title: 'Handbag'},
{title: 'Handbag'}, {title: 'Dress'},
{title: 'Dress'}, {title: 'Jan'}
{title: 'Jan'} ]),
]).done(callback); self.Tag.bulkCreate([
}, {name: 'Furniture'},
// bulkCreate doesn't include id for some reason, not going to fix tis now {name: 'Clothing'},
products: ['createProducts', function(callback) { {name: 'People'}
self.Product.findAll().done(callback); ])
}], ]).then(function() {
createTags: function(callback) { return Promise.all([
self.Tag.bulkCreate([ self.Product.findAll(),
{name: 'Furniture'}, self.Tag.findAll()
{name: 'Clothing'}, ]);
{name: 'People'} }).spread(function(products, tags) {
]).done(callback); self.products = products;
}, self.tags = tags;
tags: ['createTags', function(callback) { return Promise.all([
self.Tag.findAll().done(callback); products[0].setTags([tags[0], tags[1]]),
}] products[1].addTag(tags[0]),
}, function(err, results) { products[2].addTag(tags[1]),
expect(err).not.to.exist; products[3].setTags([tags[1]]),
products[4].setTags([tags[2]])
var products = results.products ]).then(function() {
, tags = results.tags; return Promise.all([
self.Tag.find({
async.parallel([ where: {
function(callback) { id: tags[0].id
products[0].setTags([tags[0], tags[1]]).done(callback); },
}, include: [
function(callback) { {model: self.Product, as: 'products'}
products[1].addTag(tags[0]).done(callback); ]
}, }).then(function(tag) {
function(callback) { expect(tag).to.exist;
products[2].addTag(tags[1]).done(callback); expect(tag.products.length).to.equal(2);
}, }),
function(callback) { tags[1].getProducts().then(function(products) {
products[3].setTags([tags[1]]).done(callback); expect(products.length).to.equal(3);
}, }),
function(callback) { self.Product.find({
products[4].setTags([tags[2]]).done(callback); where: {
} id: products[0].id
], function(err) { },
expect(err).not.to.exist; include: [
{model: self.Tag, as: 'tags'}
async.parallel([ ]
function(callback) { }).then(function(product) {
self.Tag.find({ expect(product).to.exist;
where: { expect(product.tags.length).to.equal(2);
id: tags[0].id }),
}, products[1].getTags().then(function(tags) {
include: [ expect(tags.length).to.equal(1);
{model: self.Product, as: 'products'} })
] ]);
}).done(function(err, tag) {
expect(tag).to.exist;
expect(tag.products.length).to.equal(2);
callback();
});
},
function(callback) {
tags[1].getProducts().done(function(err, products) {
expect(products.length).to.equal(3);
callback();
});
},
function(callback) {
self.Product.find({
where: {
id: products[0].id
},
include: [
{model: self.Tag, as: 'tags'}
]
}).done(function(err, product) {
expect(product).to.exist;
expect(product.tags.length).to.equal(2);
callback();
});
},
function(callback) {
products[1].getTags().done(function(err, tags) {
expect(tags.length).to.equal(1);
callback();
});
}
], done);
}); });
}); });
}); });
......
...@@ -9,24 +9,21 @@ var chai = require('chai') ...@@ -9,24 +9,21 @@ var chai = require('chai')
chai.config.includeStack = true; chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('QueryChainer'), function() { describe(Support.getTestDialectTeaser('QueryChainer'), function() {
beforeEach(function(done) { beforeEach(function() {
this.queryChainer = new QueryChainer(); this.queryChainer = new QueryChainer();
done();
}); });
describe('add', function() { describe('add', function() {
it('adds a new serial item if method is passed', function(done) { it('adds a new serial item if method is passed', function() {
expect(this.queryChainer.serials.length).to.equal(0); expect(this.queryChainer.serials.length).to.equal(0);
this.queryChainer.add({}, 'foo'); this.queryChainer.add({}, 'foo');
expect(this.queryChainer.serials.length).to.equal(1); expect(this.queryChainer.serials.length).to.equal(1);
done();
}); });
it('adds a new emitter if no method is passed', function(done) { it('adds a new emitter if no method is passed', function() {
expect(this.queryChainer.emitters.length).to.equal(0); expect(this.queryChainer.emitters.length).to.equal(0);
this.queryChainer.add(new CustomEventEmitter()); this.queryChainer.add(new CustomEventEmitter());
expect(this.queryChainer.emitters.length).to.equal(1); expect(this.queryChainer.emitters.length).to.equal(1);
done();
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!