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

Commit 8f59f616 by Mick Hansen

Merge branch 'attributes_field_find_includes' of https://github.com/Dahlgren/seq…

…uelize into Dahlgren-attributes_field_find_includes
2 parents 41fc53bf c2e1a88a
Showing with 74 additions and 2 deletions
...@@ -52,12 +52,35 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -52,12 +52,35 @@ describe(Support.getTestDialectTeaser("Model"), function () {
timestamps: false timestamps: false
}); });
this.Comment = this.sequelize.define('comment', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'commentId'
},
text: {
type: DataTypes.STRING,
field: 'comment_text'
}
}, {
tableName: 'comments',
timestamps: false
});
this.User.hasMany(this.Task, { this.User.hasMany(this.Task, {
foreignKey: 'user_id' foreignKey: 'user_id'
}); });
this.Task.belongsTo(this.User, { this.Task.belongsTo(this.User, {
foreignKey: 'user_id' foreignKey: 'user_id'
}); });
this.Task.hasMany(this.Comment, {
foreignKey: 'task_id'
});
this.Comment.belongsTo(this.Task, {
foreignKey: 'task_id'
});
return Promise.all([ return Promise.all([
queryInterface.createTable('users', { queryInterface.createTable('users', {
...@@ -84,6 +107,20 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -84,6 +107,20 @@ describe(Support.getTestDialectTeaser("Model"), function () {
name: { name: {
type: DataTypes.STRING type: DataTypes.STRING
} }
}),
queryInterface.createTable('comments', {
commentId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
task_id: {
type: DataTypes.INTEGER
},
comment_text: {
type: DataTypes.STRING
}
}) })
]); ]);
}); });
...@@ -111,7 +148,35 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -111,7 +148,35 @@ describe(Support.getTestDialectTeaser("Model"), function () {
}); });
}); });
it('should work with attributes and where on includes', function () { it('should work with attributes and where on includes for find', function () {
var self = this;
return this.User.create({
name: 'Barfoo'
}).then(function (user) {
return user.createTask({
title: 'DatDo'
});
}).then(function (task) {
return task.createComment({
text: 'Comment'
});
}).then(function () {
return self.Task.find({
include: [
{model: self.Comment},
{model: self.User}
],
where: {title: 'DoDat'}
});
}).then(function (task) {
expect(task.get('title')).to.equal('DoDat');
expect(task.get('comments'))[0].to.equal('Comment');
expect(task.get('user')).to.be.ok;
});
});
it('should work with attributes and where on includes for findAll', function () {
var self = this; var self = this;
return this.User.create({ return this.User.create({
...@@ -120,16 +185,23 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -120,16 +185,23 @@ describe(Support.getTestDialectTeaser("Model"), function () {
return user.createTask({ return user.createTask({
title: 'DoDat' title: 'DoDat'
}); });
}).then(function (task) {
return task.createComment({
text: 'Comment'
});
}).then(function () { }).then(function () {
return self.User.findAll({ return self.User.findAll({
include: [ include: [
{model: self.Task, where: {title: 'DoDat'}} {model: self.Task, where: {title: 'DoDat'}, include: [
{model: self.Comment}
]}
] ]
}); });
}).then(function (users) { }).then(function (users) {
users.forEach(function (user) { users.forEach(function (user) {
expect(user.get('name')).to.be.ok; expect(user.get('name')).to.be.ok;
expect(user.get('tasks')[0].get('title')).to.equal('DoDat'); expect(user.get('tasks')[0].get('title')).to.equal('DoDat');
expect(user.get('tasks')[0].get('comments')).to.be.ok;
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!