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

Commit 5ab3b62b by Mick Hansen

Tests for build with includes

1 parent ca6a9e2a
Showing with 56 additions and 2 deletions
...@@ -529,6 +529,8 @@ module.exports = (function() { ...@@ -529,6 +529,8 @@ module.exports = (function() {
for (key in attrs) { for (key in attrs) {
if (options.include && options.includeNames.indexOf(key) !== -1) { if (options.include && options.includeNames.indexOf(key) !== -1) {
if (!Array.isArray(attrs[key])) attrs[key] = [attrs[key]];
var include = _.find(options.include, function (include) { var include = _.find(options.include, function (include) {
return include.as === key || (include.as.slice(0,1).toLowerCase() + include.as.slice(1)) === key return include.as === key || (include.as.slice(0,1).toLowerCase() + include.as.slice(1)) === key
}) })
......
...@@ -369,7 +369,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -369,7 +369,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
done() done()
}) })
/*describe('include', function () { describe('include', function () {
it('should support basic includes', function () { it('should support basic includes', function () {
var Product = this.sequelize.define('Product', { var Product = this.sequelize.define('Product', {
title: Sequelize.STRING title: Sequelize.STRING
...@@ -410,7 +410,59 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -410,7 +410,59 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(product.user).to.be.ok expect(product.user).to.be.ok
expect(product.user.Model).to.equal(User) expect(product.user.Model).to.equal(User)
}) })
})*/
it('should support includes with aliases', function () {
var Product = this.sequelize.define('Product', {
title: Sequelize.STRING
})
var Tag = this.sequelize.define('Tag', {
name: Sequelize.STRING
})
var User = this.sequelize.define('User', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
})
Product.hasMany(Tag, {as: 'Categories'})
Product.hasMany(User, {as: 'Followers', through: 'product_followers'})
User.hasMany(Product, {as: 'Following', through: 'product_followers'})
var product = Product.build({
id: 1,
title: 'Chair',
categories: [
{id: 1, name: 'Alpha'},
{id: 2, name: 'Beta'},
{id: 3, name: 'Charlie'},
{id: 4, name: 'Delta'}
],
followers: [
{
id: 1,
first_name: 'Mick',
last_name: 'Hansen'
},
{
id: 2,
first_name: 'Jan',
last_name: 'Meier'
}
]
}, {
include: [
{model: User, as: 'Followers'},
{model: Tag, as: 'Categories'}
]
})
expect(product.categories).to.be.ok
expect(product.categories.length).to.equal(4)
expect(product.categories[0].Model).to.equal(Tag)
expect(product.followers).to.be.ok
expect(product.followers.length).to.equal(2)
expect(product.followers[0].Model).to.equal(User)
})
})
}) })
describe('findOrInitialize', function() { describe('findOrInitialize', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!