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

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 @@
The entry point.
@module Sequelize
**/
module.exports = require("./lib/sequelize")
module.exports = require("./lib/sequelize");
......@@ -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);
});
});
});
......
......@@ -6,8 +6,8 @@ var chai = require('chai')
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, datetime = require('chai-datetime')
, async = require('async')
, Promise = Sequelize.Promise
, async = require('async')
, _ = require('lodash');
chai.use(datetime);
......@@ -93,63 +93,53 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Group.hasMany(GroupMember, {as: 'Memberships'});
return self.sequelize.sync({force: true}).then(function() {
return Promise.props({
groups: Group.bulkCreate([
return Promise.all([
Group.bulkCreate([
{name: 'Developers'},
{name: 'Designers'},
{name: 'Managers'}
]).then(function() {
return Group.findAll();
}),
companies: Company.bulkCreate([
]),
Company.bulkCreate([
{name: 'Sequelize'},
{name: 'Coca Cola'},
{name: 'Bonanza'},
{name: 'NYSE'},
{name: 'Coshopr'}
]).then(function() {
return Company.findAll();
}),
ranks: Rank.bulkCreate([
]),
Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1},
{name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1},
{name: 'Member', canInvite: 1, canRemove: 0, canPost: 0}
]).then(function() {
return Rank.findAll();
}),
tags: Tag.bulkCreate([
]),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'},
{name: 'D'},
{name: 'E'}
])
]).then(function() {
return Tag.findAll();
})
}).then(function (results) {
var groups = results.groups
, ranks = results.ranks
, tags = results.tags
, companies = results.companies;
return Promise.all([
Group.findAll(),
Company.findAll(),
Rank.findAll(),
Tag.findAll()
]);
}).spread(function (groups, companies, ranks, tags) {
return Promise.reduce(_.range(5), function (memo, i) {
return Promise.props({
user: AccUser.create(),
products: Product.bulkCreate([
return Promise.all([
AccUser.create(),
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Bed'},
{title: 'Pen'},
{title: 'Monitor'}
]).then(function(err) {
]).then(function() {
return Product.findAll();
})
}).then(function (results) {
var user = results.user
, products = results.products
, groupMembers;
groupMembers = [
]).spread(function (user, products) {
var groupMembers = [
{AccUserId: user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{AccUserId: user.id, GroupId: groups[1].id, RankId: ranks[2].id}
];
......@@ -401,7 +391,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
});
});
it('should support many levels of belongsTo', function(done) {
it('should support many levels of belongsTo', function() {
var A = this.sequelize.define('a', {}, {schema: 'account'})
, B = this.sequelize.define('b', {}, {schema: 'account'})
, C = this.sequelize.define('c', {}, {schema: 'account'})
......@@ -429,51 +419,32 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
H
];
this.sequelize.sync().done(function() {
async.auto({
as: function(callback) {
A.bulkCreate([
{},
{},
{},
{},
{},
{},
{},
{}
]).done(function() {
A.findAll().done(callback);
});
},
singleChain: function(callback) {
return this.sequelize.sync().then(function() {
return A.bulkCreate([
{}, {}, {}, {}, {}, {}, {}, {}
]).then(function() {
var previousInstance;
async.eachSeries(singles, function(model, callback) {
model.create({}).done(function(err, instance) {
return Promise.resolve(singles).each(function(model) {
return model.create({}).then(function(instance) {
if (previousInstance) {
previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).done(function() {
return previousInstance['set'+ Sequelize.Utils.uppercaseFirst(model.name)](instance).then(function() {
previousInstance = instance;
callback();
});
} else {
previousInstance = b = instance;
callback();
}
previousInstance = b = instance;
return void(0);
});
}, callback);
},
abs: ['as', 'singleChain', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.as.forEach(function(a) {
chainer.add(a.setB(b));
});
chainer.run().done(callback);
}]
}, function() {
A.findAll({
}).then(function() {
return A.findAll();
}).then(function(as) {
var promises = [];
as.forEach(function(a) {
promises.push(a.setB(b));
});
return Promise.all(promises);
}).then(function() {
return A.findAll({
include: [
{model: B, include: [
{model: C, include: [
......@@ -489,20 +460,17 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
]}
]}
]
}).done(function(err, as) {
expect(err).not.to.be.ok;
}).then(function(as) {
expect(as.length).to.be.ok;
as.forEach(function(a) {
expect(a.b.c.d.e.f.g.h).to.be.ok;
});
done();
});
});
});
});
it('should support ordering with only belongsTo includes', function(done) {
it('should support ordering with only belongsTo includes', function() {
var User = this.sequelize.define('SpecialUser', {}, {schema: 'account'})
, Item = this.sequelize.define('Item', {'test': DataTypes.STRING}, {schema: 'account'})
, Order = this.sequelize.define('Order', {'position': DataTypes.INTEGER}, {schema: 'account'});
......@@ -511,64 +479,40 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Item, {'as': 'itemB', foreignKey: 'itemB_id'});
User.belongsTo(Order);
this.sequelize.sync().done(function() {
async.auto({
users: function(callback) {
User.bulkCreate([{}, {}, {}]).done(function() {
User.findAll().done(callback);
});
},
items: function(callback) {
return this.sequelize.sync().then(function() {
return Promise.all([
User.bulkCreate([{}, {}, {}]),
Item.bulkCreate([
{'test': 'abc'},
{'test': 'def'},
{'test': 'ghi'},
{'test': 'jkl'}
]).done(function() {
Item.findAll({order: ['id']}).done(callback);
});
},
orders: function(callback) {
]),
Order.bulkCreate([
{'position': 2},
{'position': 3},
{'position': 1}
]).done(function() {
Order.findAll({order: ['id']}).done(callback);
});
},
associate: ['users', 'items', 'orders', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
var user1 = results.users[0];
var user2 = results.users[1];
var user3 = results.users[2];
var item1 = results.items[0];
var item2 = results.items[1];
var item3 = results.items[2];
var item4 = results.items[3];
var order1 = results.orders[0];
var order2 = results.orders[1];
var order3 = results.orders[2];
chainer.add(user1.setItemA(item1));
chainer.add(user1.setItemB(item2));
chainer.add(user1.setOrder(order3));
chainer.add(user2.setItemA(item3));
chainer.add(user2.setItemB(item4));
chainer.add(user2.setOrder(order2));
chainer.add(user3.setItemA(item1));
chainer.add(user3.setItemB(item4));
chainer.add(user3.setOrder(order1));
chainer.run().done(callback);
}]
}, function() {
User.findAll({
])
]).then(function() {
return Promise.all([
User.findAll(),
Item.findAll({order: ['id']}),
Order.findAll({order: ['id']})
]);
}).spread(function(users, items, orders) {
return Promise.all([
users[0].setItemA(items[0]),
users[0].setItemB(items[1]),
users[0].setOrder(orders[2]),
users[1].setItemA(items[2]),
users[1].setItemB(items[3]),
users[1].setOrder(orders[1]),
users[2].setItemA(items[0]),
users[2].setItemB(items[3]),
users[2].setOrder(orders[0])
]);
}).spread(function() {
return User.findAll({
'include': [
{'model': Item, 'as': 'itemA', where: {test: 'abc'}},
{'model': Item, 'as': 'itemB'},
......@@ -576,23 +520,18 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
'order': [
[Order, 'position']
]
}).done(function(err, as) {
expect(err).not.to.be.ok;
}).then(function(as) {
expect(as.length).to.eql(2);
expect(as[0].itemA.test).to.eql('abc');
expect(as[1].itemA.test).to.eql('abc');
expect(as[0].Order.position).to.eql(1);
expect(as[1].Order.position).to.eql(2);
done();
});
});
});
});
it('should include attributes from through models', function(done) {
it('should include attributes from through models', function() {
var Product = this.sequelize.define('Product', {
title: DataTypes.STRING
}, {schema: 'account'})
......@@ -606,44 +545,34 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Product.hasMany(Tag, {through: ProductTag});
Tag.hasMany(Product, {through: ProductTag});
this.sequelize.sync({force: true}).done(function() {
async.auto({
products: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Dress'}
]).done(function() {
Product.findAll().done(callback);
});
},
tags: function(callback) {
]),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).done(function() {
Tag.findAll().done(callback);
});
},
productTags: ['products', 'tags', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.products[0].addTag(results.tags[0], {priority: 1}));
chainer.add(results.products[0].addTag(results.tags[1], {priority: 2}));
chainer.add(results.products[1].addTag(results.tags[1], {priority: 1}));
chainer.add(results.products[2].addTag(results.tags[0], {priority: 3}));
chainer.add(results.products[2].addTag(results.tags[1], {priority: 1}));
chainer.add(results.products[2].addTag(results.tags[2], {priority: 2}));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
Product.findAll({
])
]).spread(function() {
return Promise.all([
Product.findAll(),
Tag.findAll()
]);
}).spread(function(products, tags) {
return Promise.all([
products[0].addTag(tags[0], {priority: 1}),
products[0].addTag(tags[1], {priority: 2}),
products[1].addTag(tags[1], {priority: 1}),
products[2].addTag(tags[0], {priority: 3}),
products[2].addTag(tags[1], {priority: 1}),
products[2].addTag(tags[2], {priority: 2})
]);
}).spread(function() {
return Product.findAll({
include: [
{model: Tag}
],
......@@ -651,63 +580,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
['id', 'ASC'],
[Tag, 'id', 'ASC']
]
}).done(function(err, products) {
expect(err).not.to.be.ok;
}).then(function(products) {
expect(products[0].Tags[0].ProductTag.priority).to.equal(1);
expect(products[0].Tags[1].ProductTag.priority).to.equal(2);
expect(products[1].Tags[0].ProductTag.priority).to.equal(1);
expect(products[2].Tags[0].ProductTag.priority).to.equal(3);
expect(products[2].Tags[1].ProductTag.priority).to.equal(1);
expect(products[2].Tags[2].ProductTag.priority).to.equal(2);
done();
});
});
});
});
it('should support a required belongsTo include', function(done) {
it('should support a required belongsTo include', function() {
var User = this.sequelize.define('User', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {}, {schema: 'account'});
User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
Group.bulkCreate([{}, {}]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}, {}]).done(function() {
User.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
results.users[2].setGroup(results.groups[1]).done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([{}, {}]),
User.bulkCreate([{}, {}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll()
]);
}).spread(function(groups, users) {
return users[2].setGroup(groups[1]);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true}
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
expect(users[0].Group).to.be.ok;
done();
});
});
});
});
it('should be possible to extend the on clause with a where option on a belongsTo include', function(done) {
it('should be possible to extend the on clause with a where option on a belongsTo include', function() {
var User = this.sequelize.define('User', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
......@@ -715,46 +630,38 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setGroup(results.groups[1]));
chainer.add(results.users[1].setGroup(results.groups[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
]),
User.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll()
]);
}).spread(function(groups, users) {
return Promise.all([
users[0].setGroup(groups[1]),
users[1].setGroup(groups[0])
]);
}).then(function() {
return User.findAll({
include: [
{model: Group, where: {name: 'A'}}
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
expect(users[0].Group).to.be.ok;
expect(users[0].Group.name).to.equal('A');
done();
});
});
});
});
it('should be possible to extend the on clause with a where option on a belongsTo include', function(done) {
it('should be possible to extend the on clause with a where option on a belongsTo include', function() {
var User = this.sequelize.define('User', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
......@@ -762,46 +669,38 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setGroup(results.groups[1]));
chainer.add(results.users[1].setGroup(results.groups[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
]),
User.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll()
]);
}).spread(function(groups, users) {
return Promise.all([
users[0].setGroup(groups[1]),
users[1].setGroup(groups[0])
]);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true}
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
users.forEach(function(user) {
expect(user.Group).to.be.ok;
});
done();
});
});
});
});
it('should be possible to define a belongsTo include as required with child hasMany with limit', function(done) {
it('should be possible to define a belongsTo include as required with child hasMany with limit', function() {
var User = this.sequelize.define('User', {}, {schema: 'account'}) , Group = this.sequelize.define('Group', {
name: DataTypes.STRING
}, {schema: 'account'})
......@@ -812,65 +711,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group);
Group.hasMany(Category);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
categories: function(callback) {
Category.bulkCreate([{}, {}]).done(function() {
Category.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setGroup(results.groups[1]));
chainer.add(results.users[1].setGroup(results.groups[0]));
chainer.run().done(callback);
}],
groupCategories: ['groups', 'categories', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.groups.forEach(function(group) {
chainer.add(group.setCategories(results.categories));
]),
User.bulkCreate([{}, {}]),
Category.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll(),
Category.findAll()
]);
}).spread(function(groups, users, categories) {
var promises = [
users[0].setGroup(groups[1]),
users[1].setGroup(groups[0])
];
groups.forEach(function(group) {
promises.push(group.setCategories(categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
return Promise.all(promises);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true, include: [
{model: Category}
]}
],
limit: 1
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
users.forEach(function(user) {
expect(user.Group).to.be.ok;
expect(user.Group.Categories).to.be.ok;
});
done();
});
});
});
});
it('should be possible to define a belongsTo include as required with child hasMany with limit and aliases', function(done) {
it('should be possible to define a belongsTo include as required with child hasMany with limit and aliases', function() {
var User = this.sequelize.define('User', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
......@@ -882,65 +765,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group, {as: 'Team'});
Group.hasMany(Category, {as: 'Tags'});
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
categories: function(callback) {
Category.bulkCreate([{}, {}]).done(function() {
Category.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setTeam(results.groups[1]));
chainer.add(results.users[1].setTeam(results.groups[0]));
chainer.run().done(callback);
}],
groupCategories: ['groups', 'categories', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.groups.forEach(function(group) {
chainer.add(group.setTags(results.categories));
]),
User.bulkCreate([{}, {}]),
Category.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll(),
Category.findAll()
]);
}).spread(function(groups, users, categories) {
var promises = [
users[0].setTeam(groups[1]),
users[1].setTeam(groups[0])
];
groups.forEach(function(group) {
promises.push(group.setTags(categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
return Promise.all(promises);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true, as: 'Team', include: [
{model: Category, as: 'Tags'}
]}
],
limit: 1
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
users.forEach(function(user) {
expect(user.Team).to.be.ok;
expect(user.Team.Tags).to.be.ok;
});
done();
});
});
});
});
it('should be possible to define a belongsTo include as required with child hasMany which is not required with limit', function(done) {
it('should be possible to define a belongsTo include as required with child hasMany which is not required with limit', function() {
var User = this.sequelize.define('User', {}, {schema: 'account'})
, Group = this.sequelize.define('Group', {
name: DataTypes.STRING
......@@ -952,65 +819,49 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.belongsTo(Group);
Group.hasMany(Category);
this.sequelize.sync({force: true}).done(function() {
async.auto({
groups: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Group.bulkCreate([
{name: 'A'},
{name: 'B'}
]).done(function() {
Group.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
categories: function(callback) {
Category.bulkCreate([{}, {}]).done(function() {
Category.findAll().done(callback);
});
},
userGroups: ['users', 'groups', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[0].setGroup(results.groups[1]));
chainer.add(results.users[1].setGroup(results.groups[0]));
chainer.run().done(callback);
}],
groupCategories: ['groups', 'categories', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
results.groups.forEach(function(group) {
chainer.add(group.setCategories(results.categories));
]),
User.bulkCreate([{}, {}]),
Category.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Group.findAll(),
User.findAll(),
Category.findAll()
]);
}).spread(function (groups, users, categories) {
var promises = [
users[0].setGroup(groups[1]),
users[1].setGroup(groups[0])
];
groups.forEach(function(group) {
promises.push(group.setCategories(categories));
});
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
return Promise.all(promises);
}).then(function() {
return User.findAll({
include: [
{model: Group, required: true, include: [
{model: Category, required: false}
]}
],
limit: 1
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
users.forEach(function(user) {
expect(user.Group).to.be.ok;
expect(user.Group.Categories).to.be.ok;
});
done();
});
});
});
});
it('should be possible to extend the on clause with a where option on a hasOne include', function(done) {
it('should be possible to extend the on clause with a where option on a hasOne include', function() {
var User = this.sequelize.define('User', {}, {schema: 'account'})
, Project = this.sequelize.define('Project', {
title: DataTypes.STRING
......@@ -1018,46 +869,38 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
User.hasOne(Project, {as: 'LeaderOf'});
this.sequelize.sync({force: true}).done(function() {
async.auto({
projects: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Project.bulkCreate([
{title: 'Alpha'},
{title: 'Beta'}
]).done(function() {
Project.findAll().done(callback);
});
},
users: function(callback) {
User.bulkCreate([{}, {}]).done(function() {
User.findAll().done(callback);
});
},
userProjects: ['users', 'projects', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.users[1].setLeaderOf(results.projects[1]));
chainer.add(results.users[0].setLeaderOf(results.projects[0]));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
User.findAll({
]),
User.bulkCreate([{}, {}])
]).then(function() {
return Promise.all([
Project.findAll(),
User.findAll()
]);
}).spread(function(projects, users) {
return Promise.all([
users[1].setLeaderOf(projects[1]),
users[0].setLeaderOf(projects[0])
]);
}).then(function() {
return User.findAll({
include: [
{model: Project, as: 'LeaderOf', where: {title: 'Beta'}}
]
}).done(function(err, users) {
expect(err).not.to.be.ok;
}).then(function(users) {
expect(users.length).to.equal(1);
expect(users[0].LeaderOf).to.be.ok;
expect(users[0].LeaderOf.title).to.equal('Beta');
done();
});
});
});
});
it('should be possible to extend the on clause with a where option on a hasMany include with a through model', function(done) {
it('should be possible to extend the on clause with a where option on a hasMany include with a through model', function() {
var Product = this.sequelize.define('Product', {
title: DataTypes.STRING
}, {schema: 'account'})
......@@ -1071,54 +914,40 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
Product.hasMany(Tag, {through: ProductTag});
Tag.hasMany(Product, {through: ProductTag});
this.sequelize.sync({force: true}).done(function() {
async.auto({
products: function(callback) {
return this.sequelize.sync({force: true}).then(function() {
return Promise.all([
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Dress'}
]).done(function() {
Product.findAll().done(callback);
});
},
tags: function(callback) {
]),
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).done(function() {
Tag.findAll().done(callback);
});
},
productTags: ['products', 'tags', function(callback, results) {
var chainer = new Sequelize.Utils.QueryChainer();
chainer.add(results.products[0].addTag(results.tags[0], {priority: 1}));
chainer.add(results.products[0].addTag(results.tags[1], {priority: 2}));
chainer.add(results.products[1].addTag(results.tags[1], {priority: 1}));
chainer.add(results.products[2].addTag(results.tags[0], {priority: 3}));
chainer.add(results.products[2].addTag(results.tags[1], {priority: 1}));
chainer.add(results.products[2].addTag(results.tags[2], {priority: 2}));
chainer.run().done(callback);
}]
}, function(err) {
expect(err).not.to.be.ok;
Product.findAll({
])
]).then(function() {
return Promise.all([
Product.findAll(),
Tag.findAll()
]);
}).spread(function(products, tags) {
return Promise.all([
products[0].addTag(tags[0], {priority: 1}),
products[0].addTag(tags[1], {priority: 2}),
products[1].addTag(tags[1], {priority: 1}),
products[2].addTag(tags[0], {priority: 3}),
products[2].addTag(tags[1], {priority: 1}),
products[2].addTag(tags[2], {priority: 2})
]);
}).then(function() {
return Product.findAll({
include: [
{model: Tag, where: {name: 'C'}}
]
}).done(function(err, products) {
expect(err).not.to.be.ok;
}).then(function(products) {
expect(products.length).to.equal(1);
expect(products[0].Tags.length).to.equal(1);
done();
});
});
});
......
......@@ -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);
});
......
......@@ -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,64 +759,42 @@ 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) {
return this.sequelize.sync().then(function() {
return Promise.all([
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) {
])
]).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
......@@ -825,19 +802,13 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [
{model: self.Product, as: 'products'}
]
}).done(function(err, tag) {
}).then(function(tag) {
expect(tag).to.exist;
expect(tag.products.length).to.equal(2);
callback();
});
},
function(callback) {
tags[1].getProducts().done(function(err, products) {
}),
tags[1].getProducts().then(function(products) {
expect(products.length).to.equal(3);
callback();
});
},
function(callback) {
}),
self.Product.find({
where: {
id: products[0].id
......@@ -845,19 +816,14 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [
{model: self.Tag, as: 'tags'}
]
}).done(function(err, product) {
}).then(function(product) {
expect(product).to.exist;
expect(product.tags.length).to.equal(2);
callback();
});
},
function(callback) {
products[1].getTags().done(function(err, tags) {
}),
products[1].getTags().then(function(tags) {
expect(tags.length).to.equal(1);
callback();
});
}
], done);
})
]);
});
});
});
......
......@@ -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();
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!