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

Commit 19cf1494 by Ruben Bridgewater

Refactor more tests to use promises and remove unused async from tests

1 parent 6087e907
...@@ -7,7 +7,6 @@ var chai = require('chai') ...@@ -7,7 +7,6 @@ var chai = require('chai')
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, DataTypes = require(__dirname + '/../../lib/data-types') , DataTypes = require(__dirname + '/../../lib/data-types')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, async = require('async')
, _ = require('lodash') , _ = require('lodash')
, dialect = Support.getTestDialect(); , dialect = Support.getTestDialect();
......
...@@ -6,8 +6,7 @@ var chai = require('chai') ...@@ -6,8 +6,7 @@ var chai = require('chai')
, Sequelize = require(__dirname + '/../../../index') , Sequelize = require(__dirname + '/../../../index')
, Promise = Sequelize.Promise , Promise = Sequelize.Promise
, DataTypes = require(__dirname + '/../../../lib/data-types') , DataTypes = require(__dirname + '/../../../lib/data-types')
, datetime = require('chai-datetime') , datetime = require('chai-datetime');
, async = require('async');
chai.use(datetime); chai.use(datetime);
chai.config.includeStack = true; chai.config.includeStack = true;
...@@ -215,7 +214,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -215,7 +214,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
return model.create(values).then(function (instance) { return model.create(values).then(function (instance) {
if (previousInstance) { if (previousInstance) {
return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() { return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
previousInstance = instance; previousInstance = instance;
}); });
} else { } else {
previousInstance = b = instance; previousInstance = b = instance;
...@@ -275,7 +274,7 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -275,7 +274,7 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Post.belongsTo(User, { foreignKey: 'owner_id', as: 'Owner', constraints: false }); Post.belongsTo(User, { foreignKey: 'owner_id', as: 'Owner', constraints: false });
return this.sequelize.sync({force: true}).then(function () { return this.sequelize.sync({force: true}).then(function () {
return User.find({ return User.find({
where: { id: 2 }, where: { id: 2 },
include: [ include: [
{ model: Post, as: 'UserPosts', where: {"private": true} } { model: Post, as: 'UserPosts', where: {"private": true} }
......
...@@ -11,7 +11,6 @@ var chai = require('chai') ...@@ -11,7 +11,6 @@ var chai = require('chai')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, _ = require('lodash') , _ = require('lodash')
, moment = require('moment') , moment = require('moment')
, async = require('async')
, current = Support.sequelize; , current = Support.sequelize;
chai.use(datetime); chai.use(datetime);
...@@ -789,7 +788,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -789,7 +788,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
describe('include all', function() { describe('include all', function() {
beforeEach(function(done) { beforeEach(function() {
var self = this; var self = this;
self.Continent = this.sequelize.define('continent', { name: Sequelize.STRING }); self.Continent = this.sequelize.define('continent', { name: Sequelize.STRING });
...@@ -806,28 +805,22 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -806,28 +805,22 @@ describe(Support.getTestDialectTeaser('Model'), function() {
self.Country.hasMany(self.Person, { as: 'residents', foreignKey: 'CountryResidentId' }); self.Country.hasMany(self.Person, { as: 'residents', foreignKey: 'CountryResidentId' });
self.Person.belongsTo(self.Country, { as: 'CountryResident', foreignKey: 'CountryResidentId' }); self.Person.belongsTo(self.Country, { as: 'CountryResident', foreignKey: 'CountryResidentId' });
this.sequelize.sync({ force: true }).then(function() { return this.sequelize.sync({ force: true }).then(function() {
async.parallel({ return self.sequelize.Promise.props({
europe: function(callback) { self.Continent.create({ name: 'Europe' }).done(callback); }, europe: self.Continent.create({ name: 'Europe' }),
england: function(callback) { self.Country.create({ name: 'England' }).done(callback); }, england: self.Country.create({ name: 'England' }),
coal: function(callback) { self.Industry.create({ name: 'Coal' }).done(callback); }, coal: self.Industry.create({ name: 'Coal' }),
bob: function(callback) { self.Person.create({ name: 'Bob', lastName: 'Becket' }).done(callback); } bob: self.Person.create({ name: 'Bob', lastName: 'Becket' })
}, function(err, r) { }).then(function(r) {
if (err) throw err;
_.forEach(r, function(item, itemName) { _.forEach(r, function(item, itemName) {
self[itemName] = item; self[itemName] = item;
}); });
return self.sequelize.Promise.all([
async.parallel([ self.england.setContinent(self.europe),
function(callback) { self.england.setContinent(self.europe).done(callback); }, self.england.addIndustry(self.coal),
function(callback) { self.england.addIndustry(self.coal).done(callback); }, self.bob.setCountry(self.england),
function(callback) { self.bob.setCountry(self.england).done(callback); }, self.bob.setCountryResident(self.england)
function(callback) { self.bob.setCountryResident(self.england).done(callback); } ]);
], function(err) {
if (err) throw err;
done();
});
}); });
}); });
}); });
...@@ -896,7 +889,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -896,7 +889,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
describe('order by eager loaded tables', function() { describe('order by eager loaded tables', function() {
describe('HasMany', function() { describe('HasMany', function() {
beforeEach(function(done) { beforeEach(function() {
var self = this; var self = this;
self.Continent = this.sequelize.define('continent', { name: Sequelize.STRING }); self.Continent = this.sequelize.define('continent', { name: Sequelize.STRING });
...@@ -910,88 +903,78 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -910,88 +903,78 @@ describe(Support.getTestDialectTeaser('Model'), function() {
self.Country.hasMany(self.Person, { as: 'residents', foreignKey: 'CountryResidentId' }); self.Country.hasMany(self.Person, { as: 'residents', foreignKey: 'CountryResidentId' });
self.Person.belongsTo(self.Country, { as: 'CountryResident', foreignKey: 'CountryResidentId' }); self.Person.belongsTo(self.Country, { as: 'CountryResident', foreignKey: 'CountryResidentId' });
this.sequelize.sync({ force: true }).then(function() { return this.sequelize.sync({ force: true }).then(function() {
async.parallel({ return self.sequelize.Promise.props({
europe: function(callback) { self.Continent.create({ name: 'Europe' }).done(callback); }, europe: self.Continent.create({ name: 'Europe' }),
asia: function(callback) { self.Continent.create({ name: 'Asia' }).done(callback); }, asia: self.Continent.create({ name: 'Asia' }),
england: function(callback) { self.Country.create({ name: 'England' }).done(callback); }, england: self.Country.create({ name: 'England' }),
france: function(callback) { self.Country.create({ name: 'France' }).done(callback); }, france: self.Country.create({ name: 'France' }),
korea: function(callback) { self.Country.create({ name: 'Korea' }).done(callback); }, korea: self.Country.create({ name: 'Korea' }),
bob: function(callback) { self.Person.create({ name: 'Bob', lastName: 'Becket' }).done(callback); }, bob: self.Person.create({ name: 'Bob', lastName: 'Becket' }),
fred: function(callback) { self.Person.create({ name: 'Fred', lastName: 'Able' }).done(callback); }, fred: self.Person.create({ name: 'Fred', lastName: 'Able' }),
pierre: function(callback) { self.Person.create({ name: 'Pierre', lastName: 'Paris' }).done(callback); }, pierre: self.Person.create({ name: 'Pierre', lastName: 'Paris' }),
kim: function(callback) { self.Person.create({ name: 'Kim', lastName: 'Z' }).done(callback); } kim: self.Person.create({ name: 'Kim', lastName: 'Z' })
}, function(err, r) { }).then(function(r) {
if (err) throw err;
_.forEach(r, function(item, itemName) { _.forEach(r, function(item, itemName) {
self[itemName] = item; self[itemName] = item;
}); });
async.parallel([ return self.sequelize.Promise.all([
function(callback) { self.england.setContinent(self.europe).done(callback); }, self.england.setContinent(self.europe),
function(callback) { self.france.setContinent(self.europe).done(callback); }, self.france.setContinent(self.europe),
function(callback) { self.korea.setContinent(self.asia).done(callback); }, self.korea.setContinent(self.asia),
function(callback) { self.bob.setCountry(self.england).done(callback); }, self.bob.setCountry(self.england),
function(callback) { self.fred.setCountry(self.england).done(callback); }, self.fred.setCountry(self.england),
function(callback) { self.pierre.setCountry(self.france).done(callback); }, self.pierre.setCountry(self.france),
function(callback) { self.kim.setCountry(self.korea).done(callback); }, self.kim.setCountry(self.korea),
function(callback) { self.bob.setCountryResident(self.england).done(callback); }, self.bob.setCountryResident(self.england),
function(callback) { self.fred.setCountryResident(self.france).done(callback); }, self.fred.setCountryResident(self.france),
function(callback) { self.pierre.setCountryResident(self.korea).done(callback); }, self.pierre.setCountryResident(self.korea),
function(callback) { self.kim.setCountryResident(self.england).done(callback); } self.kim.setCountryResident(self.england)
], function(err) { ]);
if (err) throw err;
done();
});
}); });
}); });
}); });
it('sorts simply', function(done) { it('sorts simply', function() {
var self = this; var self = this;
async.eachSeries([['ASC', 'Asia'], ['DESC', 'Europe']], function(params, callback) { return this.sequelize.Promise.map([['ASC', 'Asia'], ['DESC', 'Europe']], function(params) {
self.Continent.findAll({ return self.Continent.findAll({
order: [['name', params[0]]] order: [['name', params[0]]]
}).done(function(err, continents) { }).then(function(continents) {
expect(err).not.to.be.ok;
expect(continents).to.exist; expect(continents).to.exist;
expect(continents[0]).to.exist; expect(continents[0]).to.exist;
expect(continents[0].name).to.equal(params[1]); expect(continents[0].name).to.equal(params[1]);
callback();
}); });
}, function() { done(); }); });
}); });
it('sorts by 1st degree association', function(done) { it('sorts by 1st degree association', function() {
var self = this; var self = this;
async.forEach([['ASC', 'Europe', 'England'], ['DESC', 'Asia', 'Korea']], function(params, callback) { return this.sequelize.Promise.map([['ASC', 'Europe', 'England'], ['DESC', 'Asia', 'Korea']], function(params) {
self.Continent.findAll({ return self.Continent.findAll({
include: [self.Country], include: [self.Country],
order: [[self.Country, 'name', params[0]]] order: [[self.Country, 'name', params[0]]]
}).done(function(err, continents) { }).then(function(continents) {
expect(err).not.to.be.ok;
expect(continents).to.exist; expect(continents).to.exist;
expect(continents[0]).to.exist; expect(continents[0]).to.exist;
expect(continents[0].name).to.equal(params[1]); expect(continents[0].name).to.equal(params[1]);
expect(continents[0].countries).to.exist; expect(continents[0].countries).to.exist;
expect(continents[0].countries[0]).to.exist; expect(continents[0].countries[0]).to.exist;
expect(continents[0].countries[0].name).to.equal(params[2]); expect(continents[0].countries[0].name).to.equal(params[2]);
callback();
}); });
}, function() { done(); }); });
}); });
it('sorts by 2nd degree association', function(done) { it('sorts by 2nd degree association', function() {
var self = this; var self = this;
async.forEach([['ASC', 'Europe', 'England', 'Fred'], ['DESC', 'Asia', 'Korea', 'Kim']], function(params, callback) { return this.sequelize.Promise.map([['ASC', 'Europe', 'England', 'Fred'], ['DESC', 'Asia', 'Korea', 'Kim']], function(params) {
self.Continent.findAll({ return self.Continent.findAll({
include: [{ model: self.Country, include: [self.Person] }], include: [{ model: self.Country, include: [self.Person] }],
order: [[self.Country, self.Person, 'lastName', params[0]]] order: [[self.Country, self.Person, 'lastName', params[0]]]
}).done(function(err, continents) { }).then(function(continents) {
expect(err).not.to.be.ok;
expect(continents).to.exist; expect(continents).to.exist;
expect(continents[0]).to.exist; expect(continents[0]).to.exist;
expect(continents[0].name).to.equal(params[1]); expect(continents[0].name).to.equal(params[1]);
...@@ -1001,19 +984,17 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1001,19 +984,17 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(continents[0].countries[0].people).to.exist; expect(continents[0].countries[0].people).to.exist;
expect(continents[0].countries[0].people[0]).to.exist; expect(continents[0].countries[0].people[0]).to.exist;
expect(continents[0].countries[0].people[0].name).to.equal(params[3]); expect(continents[0].countries[0].people[0].name).to.equal(params[3]);
callback();
}); });
}, function() { done(); }); });
}), }),
it('sorts by 2nd degree association with alias', function(done) { it('sorts by 2nd degree association with alias', function() {
var self = this; var self = this;
async.forEach([['ASC', 'Europe', 'France', 'Fred'], ['DESC', 'Europe', 'England', 'Kim']], function(params, callback) { return this.sequelize.Promise.map([['ASC', 'Europe', 'France', 'Fred'], ['DESC', 'Europe', 'England', 'Kim']], function(params) {
self.Continent.findAll({ return self.Continent.findAll({
include: [{ model: self.Country, include: [self.Person, {model: self.Person, as: 'residents' }] }], include: [{ model: self.Country, include: [self.Person, {model: self.Person, as: 'residents' }] }],
order: [[self.Country, {model: self.Person, as: 'residents' }, 'lastName', params[0]]] order: [[self.Country, {model: self.Person, as: 'residents' }, 'lastName', params[0]]]
}).done(function(err, continents) { }).then(function(continents) {
expect(err).not.to.be.ok;
expect(continents).to.exist; expect(continents).to.exist;
expect(continents[0]).to.exist; expect(continents[0]).to.exist;
expect(continents[0].name).to.equal(params[1]); expect(continents[0].name).to.equal(params[1]);
...@@ -1023,20 +1004,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1023,20 +1004,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(continents[0].countries[0].residents).to.exist; expect(continents[0].countries[0].residents).to.exist;
expect(continents[0].countries[0].residents[0]).to.exist; expect(continents[0].countries[0].residents[0]).to.exist;
expect(continents[0].countries[0].residents[0].name).to.equal(params[3]); expect(continents[0].countries[0].residents[0].name).to.equal(params[3]);
callback();
}); });
}, function() { done(); }); });
}); });
it('sorts by 2nd degree association with alias while using limit', function(done) { it('sorts by 2nd degree association with alias while using limit', function() {
var self = this; var self = this;
async.forEach([['ASC', 'Europe', 'France', 'Fred'], ['DESC', 'Europe', 'England', 'Kim']], function(params, callback) { return this.sequelize.Promise.map([['ASC', 'Europe', 'France', 'Fred'], ['DESC', 'Europe', 'England', 'Kim']], function(params) {
self.Continent.findAll({ return self.Continent.findAll({
include: [{ model: self.Country, include: [self.Person, {model: self.Person, as: 'residents' }] }], include: [{ model: self.Country, include: [self.Person, {model: self.Person, as: 'residents' }] }],
order: [[{ model: self.Country }, {model: self.Person, as: 'residents' }, 'lastName', params[0]]], order: [[{ model: self.Country }, {model: self.Person, as: 'residents' }, 'lastName', params[0]]],
limit: 3 limit: 3
}).done(function(err, continents) { }).then(function(continents) {
expect(err).not.to.be.ok;
expect(continents).to.exist; expect(continents).to.exist;
expect(continents[0]).to.exist; expect(continents[0]).to.exist;
expect(continents[0].name).to.equal(params[1]); expect(continents[0].name).to.equal(params[1]);
...@@ -1046,14 +1025,13 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1046,14 +1025,13 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(continents[0].countries[0].residents).to.exist; expect(continents[0].countries[0].residents).to.exist;
expect(continents[0].countries[0].residents[0]).to.exist; expect(continents[0].countries[0].residents[0]).to.exist;
expect(continents[0].countries[0].residents[0].name).to.equal(params[3]); expect(continents[0].countries[0].residents[0].name).to.equal(params[3]);
callback();
}); });
}, function() { done(); }); });
}); });
}), }),
describe('ManyToMany', function() { describe('ManyToMany', function() {
beforeEach(function(done) { beforeEach(function() {
var self = this; var self = this;
self.Country = this.sequelize.define('country', { name: Sequelize.STRING }); self.Country = this.sequelize.define('country', { name: Sequelize.STRING });
...@@ -1063,38 +1041,33 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1063,38 +1041,33 @@ describe(Support.getTestDialectTeaser('Model'), function() {
self.Country.hasMany(self.Industry, {through: self.IndustryCountry}); self.Country.hasMany(self.Industry, {through: self.IndustryCountry});
self.Industry.hasMany(self.Country, {through: self.IndustryCountry}); self.Industry.hasMany(self.Country, {through: self.IndustryCountry});
this.sequelize.sync({ force: true }).then(function() { return this.sequelize.sync({ force: true }).then(function() {
async.parallel({ return self.sequelize.Promise.props({
england: function(callback) { self.Country.create({ name: 'England' }).done(callback); }, england: self.Country.create({ name: 'England' }),
france: function(callback) { self.Country.create({ name: 'France' }).done(callback); }, france: self.Country.create({ name: 'France' }),
korea: function(callback) { self.Country.create({ name: 'Korea' }).done(callback); }, korea: self.Country.create({ name: 'Korea' }),
energy: function(callback) { self.Industry.create({ name: 'Energy' }).done(callback); }, energy: self.Industry.create({ name: 'Energy' }),
media: function(callback) { self.Industry.create({ name: 'Media' }).done(callback); }, media: self.Industry.create({ name: 'Media' }),
tech: function(callback) { self.Industry.create({ name: 'Tech' }).done(callback); } tech: self.Industry.create({ name: 'Tech' })
}, function(err, r) { }).then(function(r) {
if (err) throw err;
_.forEach(r, function(item, itemName) { _.forEach(r, function(item, itemName) {
self[itemName] = item; self[itemName] = item;
}); });
async.parallel([ return self.sequelize.Promise.all([
function(callback) { self.england.addIndustry(self.energy, {numYears: 20}).done(callback); }, self.england.addIndustry(self.energy, {numYears: 20}),
function(callback) { self.england.addIndustry(self.media, {numYears: 40}).done(callback); }, self.england.addIndustry(self.media, {numYears: 40}),
function(callback) { self.france.addIndustry(self.media, {numYears: 80}).done(callback); }, self.france.addIndustry(self.media, {numYears: 80}),
function(callback) { self.korea.addIndustry(self.tech, {numYears: 30}).done(callback); } self.korea.addIndustry(self.tech, {numYears: 30})
], function(err) { ]);
if (err) throw err;
done();
});
}); });
}); });
}); });
it('sorts by 1st degree association', function(done) { it('sorts by 1st degree association', function() {
var self = this; var self = this;
async.forEach([['ASC', 'England', 'Energy'], ['DESC', 'Korea', 'Tech']], function(params, callback) { return this.sequelize.Promise.map([['ASC', 'England', 'Energy'], ['DESC', 'Korea', 'Tech']], function(params) {
self.Country.findAll({ return self.Country.findAll({
include: [self.Industry], include: [self.Industry],
order: [[self.Industry, 'name', params[0]]] order: [[self.Industry, 'name', params[0]]]
}).then(function(countries) { }).then(function(countries) {
...@@ -1104,15 +1077,14 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1104,15 +1077,14 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(countries[0].industries).to.exist; expect(countries[0].industries).to.exist;
expect(countries[0].industries[0]).to.exist; expect(countries[0].industries[0]).to.exist;
expect(countries[0].industries[0].name).to.equal(params[2]); expect(countries[0].industries[0].name).to.equal(params[2]);
callback();
}); });
}, function() { done(); }); });
}); });
it('sorts by 1st degree association while using limit', function(done) { it('sorts by 1st degree association while using limit', function() {
var self = this; var self = this;
async.forEach([['ASC', 'England', 'Energy'], ['DESC', 'Korea', 'Tech']], function(params, callback) { return this.sequelize.Promise.map([['ASC', 'England', 'Energy'], ['DESC', 'Korea', 'Tech']], function(params) {
self.Country.findAll({ return self.Country.findAll({
include: [self.Industry], include: [self.Industry],
order: [ order: [
[self.Industry, 'name', params[0]] [self.Industry, 'name', params[0]]
...@@ -1125,15 +1097,14 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1125,15 +1097,14 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(countries[0].industries).to.exist; expect(countries[0].industries).to.exist;
expect(countries[0].industries[0]).to.exist; expect(countries[0].industries[0]).to.exist;
expect(countries[0].industries[0].name).to.equal(params[2]); expect(countries[0].industries[0].name).to.equal(params[2]);
callback();
}); });
}, function() { done(); }); });
}); });
it('sorts by through table attribute', function(done) { it('sorts by through table attribute', function() {
var self = this; var self = this;
async.forEach([['ASC', 'England', 'Energy'], ['DESC', 'France', 'Media']], function(params, callback) { return this.sequelize.Promise.map([['ASC', 'England', 'Energy'], ['DESC', 'France', 'Media']], function(params) {
self.Country.findAll({ return self.Country.findAll({
include: [self.Industry], include: [self.Industry],
order: [[self.Industry, self.IndustryCountry, 'numYears', params[0]]] order: [[self.Industry, self.IndustryCountry, 'numYears', params[0]]]
}).then(function(countries) { }).then(function(countries) {
...@@ -1143,9 +1114,8 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1143,9 +1114,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(countries[0].industries).to.exist; expect(countries[0].industries).to.exist;
expect(countries[0].industries[0]).to.exist; expect(countries[0].industries[0]).to.exist;
expect(countries[0].industries[0].name).to.equal(params[2]); expect(countries[0].industries[0].name).to.equal(params[2]);
callback();
}); });
}, function() { done(); }); });
}); });
}); });
}); });
......
...@@ -10,7 +10,6 @@ var chai = require('chai') ...@@ -10,7 +10,6 @@ var chai = require('chai')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, _ = require('lodash') , _ = require('lodash')
, moment = require('moment') , moment = require('moment')
, async = require('async')
, current = Support.sequelize; , current = Support.sequelize;
chai.use(datetime); chai.use(datetime);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!