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

Commit d44014ec by Björn Dahlgren

Test for includes with find using both hasMany and belongsTo

1 parent 3464ade4
Showing with 74 additions and 2 deletions
......@@ -52,12 +52,35 @@ describe(Support.getTestDialectTeaser("Model"), function () {
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, {
foreignKey: 'user_id'
});
this.Task.belongsTo(this.User, {
foreignKey: 'user_id'
});
this.Task.hasMany(this.Comment, {
foreignKey: 'task_id'
});
this.Comment.belongsTo(this.Task, {
foreignKey: 'task_id'
});
return Promise.all([
queryInterface.createTable('users', {
......@@ -84,6 +107,20 @@ describe(Support.getTestDialectTeaser("Model"), function () {
name: {
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 () {
});
});
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;
return this.User.create({
......@@ -120,16 +185,23 @@ describe(Support.getTestDialectTeaser("Model"), function () {
return user.createTask({
title: 'DoDat'
});
}).then(function (task) {
return task.createComment({
text: 'Comment'
});
}).then(function () {
return self.User.findAll({
include: [
{model: self.Task, where: {title: 'DoDat'}}
{model: self.Task, where: {title: 'DoDat'}, include: [
{model: self.Comment}
]}
]
});
}).then(function (users) {
users.forEach(function (user) {
expect(user.get('name')).to.be.ok;
expect(user.get('tasks')[0].get('title')).to.equal('DoDat');
expect(user.get('tasks')[0].get('comments')).to.be;
});
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!