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

Commit a1c82f3d by Mick Hansen

Merge pull request #3467 from BridgeAR/master

Refactor last tests to use promise style
2 parents 45e6700b c1fa0bb1
......@@ -2,4 +2,4 @@
The entry point.
@module Sequelize
**/
module.exports = require("./lib/sequelize")
module.exports = require("./lib/sequelize");
......@@ -203,7 +203,7 @@ module.exports = (function() {
options = Utils._.extend({
events: proxyEventKeys,
skipEvents: []
}, options ||  {});
}, options || {});
options.events = Utils._.difference(options.events, options.skipEvents);
......
......@@ -20,7 +20,7 @@ module.exports = (function() {
this.daos = this.daos.filter(function(_dao) {
return _dao.name !== dao.name;
});
delete this.sequelize.models[dao.name];
};
......
......@@ -81,11 +81,11 @@ module.exports = (function() {
return association.source.update(newValues, { where: query });
});
},
increment: function (targetId) {
increment: function (targetId) {
var query = CounterUtil._sourceQuery(targetId);
return association.source.find({ where: query }).then(function (instance) {
return instance.increment(counterCacheInstance.columnName, { by: 1 });
return instance.increment(counterCacheInstance.columnName, { by: 1 });
});
},
decrement: function (targetId) {
......
......@@ -43,7 +43,6 @@
"validator": "^3.34.0"
},
"devDependencies": {
"async": "~0.9.0",
"chai": "^2.1.2",
"chai-as-promised": "^4.3.0",
"chai-datetime": "~1.3.0",
......
......@@ -1793,7 +1793,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
self.User.belongsToMany(self.Task, { onDelete: 'RESTRICT'});
self.Task.belongsToMany(self.User, { onDelete: 'CASCADE'});
return this.sequelize.sync({ force: true, logging: true }).bind({}).then(function() {
return this.sequelize.sync({ force: true }).bind({}).then(function() {
return Sequelize.Promise.join(
self.User.create({ id: 67, username: 'foo' }),
self.Task.create({ id: 52, title: 'task' }),
......
......@@ -13,7 +13,7 @@ if (dialect.match(/^postgres/)) {
describe('[POSTGRES 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});
......@@ -21,35 +21,24 @@ if (dialect.match(/^postgres/)) {
Table2.hasMany(Table1);
expect(this.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'});
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;
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;
setTimeout(function() {
done();
}, 50);
});
});
});
......
......@@ -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
, User = this.sequelize.define('UserEnums', {
mood: DataTypes.ENUM('happy', 'sad', 'meh')
});
var _done = _.after(4, function() {
done();
});
})
, count = 0;
User.sync({ force: true }).then(function() {
return User.sync({ force: true }).then(function() {
User = self.sequelize.define('UserEnums', {
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']);
_done();
count++;
}).on('sql', function(sql) {
if (sql.indexOf('neutral') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'neutral' BEFORE 'happy'");
_done();
count++;
}
else if (sql.indexOf('ecstatic') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'ecstatic' BEFORE 'meh'");
_done();
count++;
}
else if (sql.indexOf('joyful') > -1) {
expect(sql).to.equal("ALTER TYPE \"enum_UserEnums_mood\" ADD VALUE 'joyful' AFTER 'meh'");
_done();
count++;
}
});
}).then(function() {
expect(count).to.equal(4);
});
});
});
......
......@@ -318,20 +318,19 @@ describe(Support.getTestDialectTeaser('Include'), function() {
Task.belongsTo(Project);
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Project.bulkCreate([{ id: 1 }, { id: 2 }]),
User.create({
Tasks: [
{ProjectId: 1},
{ProjectId: 2},
{ProjectId: 1},
{ProjectId: 2}
]
}, {
include: [Task]
})
]);
}).spread(function (projects, user) {
return Project.bulkCreate([{ id: 1 }, { id: 2 }]);
}).then(function (projects) {
return User.create({
Tasks: [
{ProjectId: 1},
{ProjectId: 2},
{ProjectId: 1},
{ProjectId: 2}
]
}, {
include: [Task]
});
}).then(function (user) {
return User.find({
where: {
id: user.id
......
......@@ -341,11 +341,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('should still work right with other concurrent increments', function() {
var self = this;
return this.User.find(1).then(function(user1) {
return user1.increment(['aNumber'], { by: 2 }).then(function() {
return user1.increment(['aNumber'], { by: 2 });
}).then(function() {
return user1.increment(['aNumber'], { by: 2 });
}).then(function() {
return this.sequelize.Promise.all([
user1.increment(['aNumber'], { by: 2 }),
user1.increment(['aNumber'], { by: 2 }),
user1.increment(['aNumber'], { by: 2 })
]).then(function() {
return self.User.find(1).then(function(user2) {
expect(user2.aNumber).to.equal(6);
});
......@@ -468,11 +468,11 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
it('should still work right with other concurrent increments', function() {
var self = this;
return this.User.find(1).then(function(user1) {
return user1.decrement(['aNumber'], { by: 2 }).then(function() {
return user1.decrement(['aNumber'], { by: 2 });
}).then(function() {
return user1.decrement(['aNumber'], { by: 2 });
}).then(function() {
return this.sequelize.Promise.all([
user1.decrement(['aNumber'], { by: 2 }),
user1.decrement(['aNumber'], { by: 2 }),
user1.decrement(['aNumber'], { by: 2 })
]).then(function() {
return self.User.find(1).then(function(user2) {
expect(user2.aNumber).to.equal(-6);
});
......
......@@ -11,7 +11,6 @@ var chai = require('chai')
, datetime = require('chai-datetime')
, _ = require('lodash')
, moment = require('moment')
, async = require('async')
, current = Support.sequelize;
chai.use(datetime);
......@@ -2348,12 +2347,13 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
if (dialect !== 'sqlite' && current.dialect.supports.transactions) {
it('supports multiple async transactions', function(done) {
it('supports multiple async transactions', function() {
this.timeout(25000);
var self = this;
return Support.prepareTransactionTest(this.sequelize).bind({}).then(function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING });
var testAsync = function(i, done) {
sequelize.transaction().then(function(t) {
var testAsync = function() {
return sequelize.transaction().then(function(t) {
return User.create({
username: 'foo'
}, {
......@@ -2380,14 +2380,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
}).then(function(t) {
return t.rollback();
}).nodeify(done);
});
};
User.sync({ force: true }).then(function() {
return User.sync({ force: true }).then(function() {
var tasks = [];
for (var i = 0; i < 1000; i++) {
tasks.push(testAsync.bind(this, i));
tasks.push(testAsync.bind(this));
}
async.parallelLimit(tasks, (sequelize.config.pool && sequelize.config.pool.max || 5) - 1, done); // Needs to be one less than 1 else the non transaction query won't ever get a connection
return self.sequelize.Promise.resolve(tasks).map(function(entry) {
return entry();
}, {
// Needs to be one less than ??? else the non transaction query won't ever get a connection
concurrency: (sequelize.config.pool && sequelize.config.pool.max || 5) - 1
});
});
});
});
......
......@@ -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
, data = { title: 'Iliad' }
, dataTypes = [Sequelize.INTEGER, Sequelize.BIGINT]
, chain = new Sequelize.Utils.QueryChainer()
, chain2 = new Sequelize.Utils.QueryChainer()
, sync = []
, promises = []
, books = [];
dataTypes.forEach(function(dataType, index) {
......@@ -940,21 +940,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
books.forEach(function(b) {
chain.add(b.sync({ force: true }));
sync.push(b.sync({ force: true }));
});
chain.run().then(function() {
books.forEach(function(b) {
chain2.add(b.create(data));
});
chain2.run().then(function(results) {
results.forEach(function(book, index) {
return Promise.all(sync).then(function() {
books.forEach(function(b, index) {
promises.push(b.create(data).then(function(book) {
expect(book.title).to.equal(data.title);
expect(book.author).to.equal(data.author);
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')
, datetime = require('chai-datetime')
, promised = require('chai-as-promised')
, _ = require('lodash')
, async = require('async')
, current = Support.sequelize;
chai.use(promised);
......@@ -760,104 +759,71 @@ describe(Support.getTestDialectTeaser('Model'), function() {
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;
this.Product.hasMany(this.Tag, {as: 'tags', through: 'product_tag'});
this.Tag.hasMany(this.Product, {as: 'products', through: 'product_tag'});
this.sequelize.sync().done(function() {
async.auto({
createProducts: function(callback) {
self.Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Handbag'},
{title: 'Dress'},
{title: 'Jan'}
]).done(callback);
},
// bulkCreate doesn't include id for some reason, not going to fix tis now
products: ['createProducts', function(callback) {
self.Product.findAll().done(callback);
}],
createTags: function(callback) {
self.Tag.bulkCreate([
{name: 'Furniture'},
{name: 'Clothing'},
{name: 'People'}
]).done(callback);
},
tags: ['createTags', function(callback) {
self.Tag.findAll().done(callback);
}]
}, function(err, results) {
expect(err).not.to.exist;
var products = results.products
, tags = results.tags;
async.parallel([
function(callback) {
products[0].setTags([tags[0], tags[1]]).done(callback);
},
function(callback) {
products[1].addTag(tags[0]).done(callback);
},
function(callback) {
products[2].addTag(tags[1]).done(callback);
},
function(callback) {
products[3].setTags([tags[1]]).done(callback);
},
function(callback) {
products[4].setTags([tags[2]]).done(callback);
}
], function(err) {
expect(err).not.to.exist;
async.parallel([
function(callback) {
self.Tag.find({
where: {
id: tags[0].id
},
include: [
{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);
return this.sequelize.sync().then(function() {
return Promise.all([
self.Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Handbag'},
{title: 'Dress'},
{title: 'Jan'}
]),
self.Tag.bulkCreate([
{name: 'Furniture'},
{name: 'Clothing'},
{name: 'People'}
])
]).then(function() {
return Promise.all([
self.Product.findAll(),
self.Tag.findAll()
]);
}).spread(function(products, tags) {
self.products = products;
self.tags = tags;
return Promise.all([
products[0].setTags([tags[0], tags[1]]),
products[1].addTag(tags[0]),
products[2].addTag(tags[1]),
products[3].setTags([tags[1]]),
products[4].setTags([tags[2]])
]).then(function() {
return Promise.all([
self.Tag.find({
where: {
id: tags[0].id
},
include: [
{model: self.Product, as: 'products'}
]
}).then(function(tag) {
expect(tag).to.exist;
expect(tag.products.length).to.equal(2);
}),
tags[1].getProducts().then(function(products) {
expect(products.length).to.equal(3);
}),
self.Product.find({
where: {
id: products[0].id
},
include: [
{model: self.Tag, as: 'tags'}
]
}).then(function(product) {
expect(product).to.exist;
expect(product.tags.length).to.equal(2);
}),
products[1].getTags().then(function(tags) {
expect(tags.length).to.equal(1);
})
]);
});
});
});
......
......@@ -9,24 +9,21 @@ var chai = require('chai')
chai.config.includeStack = true;
describe(Support.getTestDialectTeaser('QueryChainer'), function() {
beforeEach(function(done) {
beforeEach(function() {
this.queryChainer = new QueryChainer();
done();
});
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);
this.queryChainer.add({}, 'foo');
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);
this.queryChainer.add(new CustomEventEmitter());
expect(this.queryChainer.emitters.length).to.equal(1);
done();
});
});
......
......@@ -131,27 +131,22 @@ describe(Support.getTestDialectTeaser('Sequelize#transaction'), function() {
});
});
it('triggers the error event for the second transactions', function(done) {
it('triggers the error event for the second transactions', function() {
var self = this;
this.sequelize.transaction().then(function(t1) {
self.sequelize.transaction().then(function(t2) {
self
.Model
.create({ name: 'omnom' }, { transaction: t1 })
.success(function(m1) {
self
.Model
.create({ name: 'omnom' }, { transaction: t2 })
.error(function(err) {
t2.rollback().success(function() {
expect(err).to.be.defined;
done();
});
});
setTimeout(function() { t1.commit(); }, 100);
});
return this.sequelize.transaction().then(function(t1) {
return self.sequelize.transaction().then(function(t2) {
return self.Model.create({ name: 'omnom' }, { transaction: t1 }).then(function(m1) {
return Promise.all([
self.Model.create({ name: 'omnom' }, { transaction: t2 }).catch(function(err) {
expect(err).to.be.defined;
return t2.rollback();
}),
Promise.delay(100).then(function() {
return t1.commit();
})
]);
});
});
});
});
......
......@@ -57,7 +57,7 @@ var Support = {
, _sequelize = new Sequelize(sequelize.config.database, null, null, options);
if (callback) {
_sequelize.sync({ force: true }).success(function() { callback(_sequelize); });
_sequelize.sync({ force: true }).then(function() { callback(_sequelize); });
} else {
return _sequelize.sync({ force: true }).return (_sequelize);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!