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

Commit 35919254 by Mick Hansen

Move schema tests around

1 parent e16a26f7
...@@ -20,239 +20,230 @@ var sortById = function(a, b) { ...@@ -20,239 +20,230 @@ var sortById = function(a, b) {
return a.id < b.id ? -1 : 1 return a.id < b.id ? -1 : 1
} }
if (dialect.match(/^postgres/)) { describe(Support.getTestDialectTeaser("Includes with schemas"), function () {
describe(Support.getTestDialectTeaser("Schema Based PG Include"), function () {
describe('findAll', function () { describe('findAll', function () {
beforeEach(function () { beforeEach(function () {
var self = this var self = this
this.sequelize.options.logging = console.log this.fixtureA = function(done) {
this.fixtureA = function(done) { self.sequelize.dropAllSchemas().success(function(){
self.sequelize.dropAllSchemas().success(function(){ self.sequelize.createSchema("account").success(function(){
self.sequelize.createSchema("account").success(function(){ var AccUser = self.sequelize.define("AccUser", {}, {schema: "account"})
var AccUser = self.sequelize.define("AccUser", {}, {schema: "account"}) , Company = self.sequelize.define("Company", {
, Company = self.sequelize.define("Company", { name: DataTypes.STRING
name: DataTypes.STRING }, {schema: "account"})
}, {schema: "account"}) , Product = self.sequelize.define("Product", {
, Product = self.sequelize.define("Product", { title: DataTypes.STRING
title: DataTypes.STRING }, {schema: "account"})
}, {schema: "account"}) , Tag = self.sequelize.define("Tag", {
, Tag = self.sequelize.define("Tag", { name: DataTypes.STRING
name: DataTypes.STRING }, {schema: "account"})
}, {schema: "account"}) , Price = self.sequelize.define("Price", {
, Price = self.sequelize.define("Price", { value: DataTypes.FLOAT
value: DataTypes.FLOAT }, {schema: "account"})
}, {schema: "account"}) , Customer = self.sequelize.define("Customer", {
, Customer = self.sequelize.define("Customer", { name: DataTypes.STRING
name: DataTypes.STRING }, {schema: "account"})
}, {schema: "account"}) , Group = self.sequelize.define("Group", {
, Group = self.sequelize.define("Group", { name: DataTypes.STRING
name: DataTypes.STRING }, {schema: "account"})
}, {schema: "account"}) , GroupMember = self.sequelize.define("GroupMember", {
, GroupMember = self.sequelize.define("GroupMember", {
}, {schema: "account"})
}, {schema: "account"}) , Rank = self.sequelize.define("Rank", {
, Rank = self.sequelize.define("Rank", { name: DataTypes.STRING,
name: DataTypes.STRING, canInvite: {
canInvite: { type: DataTypes.INTEGER,
type: DataTypes.INTEGER, defaultValue: 0
defaultValue: 0 },
}, canRemove: {
canRemove: { type: DataTypes.INTEGER,
type: DataTypes.INTEGER, defaultValue: 0
defaultValue: 0 },
}, canPost: {
canPost: { type: DataTypes.INTEGER,
type: DataTypes.INTEGER, defaultValue: 0
defaultValue: 0 }
} }, {schema: "account"})
}, {schema: "account"})
self.models = {
self.models = { AccUser: AccUser,
AccUser: AccUser, Company: Company,
Company: Company, Product: Product,
Product: Product, Tag: Tag,
Tag: Tag, Price: Price,
Price: Price, Customer: Customer,
Customer: Customer, Group: Group,
Group: Group, GroupMember: GroupMember,
GroupMember: GroupMember, Rank: Rank
Rank: Rank }
}
AccUser.hasMany(Product) AccUser.hasMany(Product)
Product.belongsTo(AccUser) Product.belongsTo(AccUser)
Product.hasMany(Tag) Product.hasMany(Tag)
Tag.hasMany(Product) Tag.hasMany(Product)
Product.belongsTo(Tag, {as: 'Category'}) Product.belongsTo(Tag, {as: 'Category'})
Product.belongsTo(Company) Product.belongsTo(Company)
Product.hasMany(Price) Product.hasMany(Price)
Price.belongsTo(Product) Price.belongsTo(Product)
AccUser.hasMany(GroupMember, {as: 'Memberships'}) AccUser.hasMany(GroupMember, {as: 'Memberships'})
GroupMember.belongsTo(AccUser) GroupMember.belongsTo(AccUser)
GroupMember.belongsTo(Rank) GroupMember.belongsTo(Rank)
GroupMember.belongsTo(Group) GroupMember.belongsTo(Group)
Group.hasMany(GroupMember, {as: 'Memberships'}) Group.hasMany(GroupMember, {as: 'Memberships'})
self.sequelize.sync({force: true}).done(function () { self.sequelize.sync({force: true}).done(function () {
console.log("DONE WITH SYNC") console.log("DONE WITH SYNC")
var count = 4 var count = 4
, i = -1 , i = -1
async.auto({ async.auto({
groups: function(callback) { groups: function(callback) {
Group.bulkCreate([ Group.bulkCreate([
{name: 'Developers'}, {name: 'Developers'},
{name: 'Designers'}, {name: 'Designers'},
{name: 'Managers'} {name: 'Managers'}
]).done(function () { ]).done(function () {
Group.findAll().done(callback) Group.findAll().done(callback)
}) })
}, },
companies: function(callback) { companies: function(callback) {
Company.bulkCreate([ Company.bulkCreate([
{name: 'Sequelize'}, {name: 'Sequelize'},
{name: 'Coca Cola'}, {name: 'Coca Cola'},
{name: 'Bonanza'}, {name: 'Bonanza'},
{name: 'NYSE'}, {name: 'NYSE'},
{name: 'Coshopr'} {name: 'Coshopr'}
]).done(function (err) { ]).done(function (err) {
if (err) return callback(err); if (err) return callback(err);
Company.findAll().done(callback) Company.findAll().done(callback)
}) })
}, },
ranks: function(callback) { ranks: function(callback) {
Rank.bulkCreate([ Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1}, {name: 'Admin', canInvite: 1, canRemove: 1, canPost: 1},
{name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1}, {name: 'Trustee', canInvite: 1, canRemove: 0, canPost: 1},
{name: 'Member', canInvite: 1, canRemove: 0, canPost: 0} {name: 'Member', canInvite: 1, canRemove: 0, canPost: 0}
]).done(function (err) { ]).done(function (err) {
Rank.findAll().done(callback) Rank.findAll().done(callback)
}) })
}, },
tags: function(callback) { tags: function(callback) {
Tag.bulkCreate([ Tag.bulkCreate([
{name: 'A'}, {name: 'A'},
{name: 'B'}, {name: 'B'},
{name: 'C'}, {name: 'C'},
{name: 'D'}, {name: 'D'},
{name: 'E'} {name: 'E'}
]).done(function () { ]).done(function () {
Tag.findAll().done(callback) Tag.findAll().done(callback)
}) })
}, },
loop: ['groups', 'ranks', 'tags', 'companies', function (done, results) { loop: ['groups', 'ranks', 'tags', 'companies', function (done, results) {
var groups = results.groups var groups = results.groups
, ranks = results.ranks , ranks = results.ranks
, tags = results.tags , tags = results.tags
, companies = results.companies , companies = results.companies
async.whilst( async.whilst(
function () { return i < count; }, function () { return i < count; },
function (callback) { function (callback) {
i++ i++
async.auto({ async.auto({
user: function (callback) { user: function (callback) {
AccUser.create().done(callback) AccUser.create().done(callback)
},
memberships: ['user', function (callback, results) {
var groupMembers = [
{AccUserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id},
{AccUserId: results.user.id, GroupId: groups[1].id, RankId: ranks[2].id}
]
if (i < 3) {
groupMembers.push({AccUserId: results.user.id, GroupId: groups[2].id, RankId: ranks[1].id})
}
GroupMember.bulkCreate(groupMembers).done(callback)
}],
products: function (callback) {
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Bed'},
{title: 'Pen'},
{title: 'Monitor'}
]).done(function (err) {
if (err) return callback(err);
Product.findAll().done(callback)
})
},
userProducts: ['user', 'products', function (callback, results) {
results.user.setProducts([
results.products[(i * 5)+0],
results.products[(i * 5)+1],
results.products[(i * 5)+3]
]).done(callback)
}],
productTags: ['products', function (callback, results) {
var chainer = new Sequelize.Utils.QueryChainer()
chainer.add(results.products[(i * 5) + 0].setTags([
tags[0],
tags[2]
]))
chainer.add(results.products[(i * 5) + 1].setTags([
tags[1]
]))
chainer.add(results.products[(i * 5) + 0].setCategory(tags[1]))
chainer.add(results.products[(i * 5) + 2].setTags([
tags[0]
]))
chainer.add(results.products[(i * 5) + 3].setTags([
tags[0]
]))
chainer.run().done(callback)
}],
companies: ['products', function (callback, results) {
var chainer = new Sequelize.Utils.QueryChainer()
results.products[(i * 5)+0].setCompany(companies[4])
results.products[(i * 5)+1].setCompany(companies[3])
results.products[(i * 5)+2].setCompany(companies[2])
results.products[(i * 5)+3].setCompany(companies[1])
results.products[(i * 5)+4].setCompany(companies[0])
chainer.run().done(callback)
}],
prices: ['products', function (callback, results) {
Price.bulkCreate([
{ProductId: results.products[(i * 5) + 0].id, value: 5},
{ProductId: results.products[(i * 5) + 0].id, value: 10},
{ProductId: results.products[(i * 5) + 1].id, value: 5},
{ProductId: results.products[(i * 5) + 1].id, value: 10},
{ProductId: results.products[(i * 5) + 1].id, value: 15},
{ProductId: results.products[(i * 5) + 1].id, value: 20},
{ProductId: results.products[(i * 5) + 2].id, value: 20},
{ProductId: results.products[(i * 5) + 3].id, value: 20}
]).done(callback)
}]
}, callback)
}, },
function (err) { memberships: ['user', function (callback, results) {
expect(err).not.to.be.ok var groupMembers = [
done() {AccUserId: results.user.id, GroupId: groups[0].id, RankId: ranks[0].id},
} {AccUserId: results.user.id, GroupId: groups[1].id, RankId: ranks[2].id}
) ]
}]
}, done.bind(this)) if (i < 3) {
}).error(function(err) { groupMembers.push({AccUserId: results.user.id, GroupId: groups[2].id, RankId: ranks[1].id})
console.log("e: ", err) }
}).success(function(s) {
console.log("s: ", s) GroupMember.bulkCreate(groupMembers).done(callback)
}).sql(function(sql) { }],
console.log("sql: ", sql) products: function (callback) {
}) Product.bulkCreate([
}) {title: 'Chair'},
}) {title: 'Desk'},
} {title: 'Bed'},
{title: 'Pen'},
{title: 'Monitor'}
]).done(function (err) {
if (err) return callback(err);
Product.findAll().done(callback)
})
},
userProducts: ['user', 'products', function (callback, results) {
results.user.setProducts([
results.products[(i * 5)+0],
results.products[(i * 5)+1],
results.products[(i * 5)+3]
]).done(callback)
}],
productTags: ['products', function (callback, results) {
var chainer = new Sequelize.Utils.QueryChainer()
chainer.add(results.products[(i * 5) + 0].setTags([
tags[0],
tags[2]
]))
chainer.add(results.products[(i * 5) + 1].setTags([
tags[1]
]))
chainer.add(results.products[(i * 5) + 0].setCategory(tags[1]))
chainer.add(results.products[(i * 5) + 2].setTags([
tags[0]
]))
chainer.add(results.products[(i * 5) + 3].setTags([
tags[0]
]))
chainer.run().done(callback)
}],
companies: ['products', function (callback, results) {
var chainer = new Sequelize.Utils.QueryChainer()
results.products[(i * 5)+0].setCompany(companies[4])
results.products[(i * 5)+1].setCompany(companies[3])
results.products[(i * 5)+2].setCompany(companies[2])
results.products[(i * 5)+3].setCompany(companies[1])
results.products[(i * 5)+4].setCompany(companies[0])
chainer.run().done(callback)
}],
prices: ['products', function (callback, results) {
Price.bulkCreate([
{ProductId: results.products[(i * 5) + 0].id, value: 5},
{ProductId: results.products[(i * 5) + 0].id, value: 10},
{ProductId: results.products[(i * 5) + 1].id, value: 5},
{ProductId: results.products[(i * 5) + 1].id, value: 10},
{ProductId: results.products[(i * 5) + 1].id, value: 15},
{ProductId: results.products[(i * 5) + 1].id, value: 20},
{ProductId: results.products[(i * 5) + 2].id, value: 20},
{ProductId: results.products[(i * 5) + 3].id, value: 20}
]).done(callback)
}]
}, callback)
},
function (err) {
expect(err).not.to.be.ok
done()
}
)
}]
}, done.bind(this))
}).error(done)
})
})
}
}) })
it('should support an include with multiple different association types', function (done) { it('should support an include with multiple different association types', function (done) {
...@@ -1405,7 +1396,6 @@ it('should be possible to extend the on clause with a where option on a hasOne i ...@@ -1405,7 +1396,6 @@ it('should be possible to extend the on clause with a where option on a hasOne i
}) })
}) })
/
it('should be possible use limit, attributes and a where on a belongsTo with additional hasMany includes', function (done) { it('should be possible use limit, attributes and a where on a belongsTo with additional hasMany includes', function (done) {
var self = this var self = this
this.fixtureA(function () { this.fixtureA(function () {
...@@ -1526,5 +1516,4 @@ it('should be possible to extend the on clause with a where option on a hasOne i ...@@ -1526,5 +1516,4 @@ it('should be possible to extend the on clause with a where option on a hasOne i
}) })
}) })
}) })
} \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!