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

Commit 0d20e13d by Mick Hansen

Merge branch 'failing-field-tests' of https://github.com/vpontis/sequelize into …

…vpontis-failing-field-tests
2 parents 083af081 fafd7c10
...@@ -1472,6 +1472,96 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -1472,6 +1472,96 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}); });
}); });
describe('foreign key with fields specified', function() {
beforeEach(function() {
this.User = this.sequelize.define('User', { name: DataTypes.STRING });
this.Project = this.sequelize.define('Project', { name: DataTypes.STRING });
this.User.hasMany(this.Project, {
through: 'user_projects',
as: 'Projects',
foreignKey: {
field: 'user_id',
name: 'userId'
}
});
this.Project.hasMany(this.User, {
through: 'user_projects',
as: 'Users',
foreignKey: {
field: 'project_id',
name: 'projectId'
}
});
});
it('should correctly get associations', function() {
var self = this;
return this.sequelize.sync().then(function() {
return Promise.all([
self.User.create({name: 'Matt'}),
self.Project.create({name: 'Good Will Hunting'})
]);
}).spread(function (user, project) {
return user.addProject(project).return(user);
}).then(function(user) {
return user.getProjects();
}).then(function(projects) {
var project = projects[0];
expect(project).to.be.defined;
});
});
it('should be able to handle nested includes properly', function() {
var self = this;
this.Group = this.sequelize.define('Group', { groupName: DataTypes.STRING});
this.Group.hasMany(this.User, {
through: 'group_users',
as: 'Users',
foreignKey: {
field: 'group_id',
name: 'groupId'
}
});
return this.sequelize.sync().then(function() {
return Promise.all([
self.Group.create({groupName: 'The Illuminati'}),
self.User.create({name: 'Matt'}),
self.Project.create({name: 'Good Will Hunting'})
]);
}).spread(function (group, user, project) {
return user.addProject(project).then(function() {
return group.addUser(user).return(group);
});
}).then(function(group) {
// get the group and include both the users in the group and their project's
return self.Group.findAll({
where: {id: group.id},
include: [
{
model: self.User,
include: [ self.Project ]
}
]
})
}).then(function(groups) {
var group = groups[0];
expect(group).to.be.defined;
var user = group.Users[0];
expect(user).to.be.defined;
var project = user.Projects[0];
expect(project).to.be.defined;
expect(project.name).to.equal('Good Will Hunting');
});
});
});
describe('primary key handling for join table', function () { describe('primary key handling for join table', function () {
beforeEach(function () { beforeEach(function () {
this.User = this.sequelize.define('User', this.User = this.sequelize.define('User',
......
...@@ -3,6 +3,7 @@ var chai = require('chai') ...@@ -3,6 +3,7 @@ var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, Sequelize = require('../../index') , Sequelize = require('../../index')
, Promise = Sequelize.Promise
chai.config.includeStack = true chai.config.includeStack = true
...@@ -255,6 +256,44 @@ describe(Support.getTestDialectTeaser("HasOne"), function() { ...@@ -255,6 +256,44 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
expect(User.rawAttributes.AccountId).to.exist expect(User.rawAttributes.AccountId).to.exist
}) })
it('should support specifying the field of a foreign key', function() {
var User = this.sequelize.define('UserXYZ', { username: Sequelize.STRING, gender: Sequelize.STRING })
, Task = this.sequelize.define('TaskXYZ', { title: Sequelize.STRING, status: Sequelize.STRING })
, self = this;
Task.hasOne(User, {
foreignKey: {
name: 'taskId',
field: 'task_id'
}
});
expect(User.rawAttributes.taskId).to.exist
expect(User.rawAttributes.taskId.field).to.equal('task_id')
return Task.sync({ force: true }).then(function () {
// Can't use Promise.all cause of foreign key references
return User.sync({ force: true });
}).then(function () {
return Promise.all([
User.create({ username: 'foo', gender: 'male' }),
Task.create({ title: 'task', status: 'inactive' })
]);
}).spread(function (user, task) {
return task.setUserXYZ(user).then(function () {
return task.getUserXYZ();
});
}).then(function (user) {
// the sql query should correctly look at task_id instead of taskId
expect(user).to.not.be.null;
return Task.find({
where: {title: 'task'},
include: [ User ]
})
}).then(function(task) {
expect(task.UserXYZ).to.exist
});
});
}) })
describe("foreign key constraints", function() { describe("foreign key constraints", function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!