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

Commit 60943808 by Jan Aagaard Meier

Add a test for merging of existing attribute with foreign key attribute

1 parent 13b436cf
...@@ -510,8 +510,27 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -510,8 +510,27 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
}) })
it('allows the user to provide an attribute definition as foreignKey', function () { describe('allows the user to provide an attribute definition object as foreignKey', function () {
var User = this.sequelize.define('user', { it('works with a column that hasnt been defined before', function () {
var Task = this.sequelize.define('task', {})
, User = this.sequelize.define('user', {
});
Task.belongsTo(User, {
foreignKey: {
allowNull: false,
fieldName: 'uid'
}
});
expect(Task.rawAttributes.uid).to.be.defined
expect(Task.rawAttributes.uid.allowNull).to.be.false;
expect(Task.rawAttributes.uid.references).to.equal(User.getTableName())
expect(Task.rawAttributes.uid.referencesKey).to.equal('id')
});
it('works when taking a column directly from the object', function () {
var User = this.sequelize.define('user', {
uid: { uid: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
primaryKey: true primaryKey: true
...@@ -524,41 +543,29 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -524,41 +543,29 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
} }
}) })
Profile.belongsTo(User, { foreignKey: Profile.rawAttributes.user_id}) Profile.belongsTo(User, { foreignKey: Profile.rawAttributes.user_id})
expect(Profile.rawAttributes.user_id).to.be.defined
expect(Profile.rawAttributes.user_id.references).to.equal(User.getTableName())
expect(Profile.rawAttributes.user_id.referencesKey).to.equal('uid')
expect(Profile.rawAttributes.user_id.allowNull).to.be.false
})
it('allows the user to provide an attribute definition as foreignKey', function () {
var Task = this.sequelize.define('task', {})
, User = this.sequelize.define('user', {
uid: {
type: Sequelize.INTEGER,
primaryKey: true
}
});
Task.belongsTo(User, { expect(Profile.rawAttributes.user_id).to.be.defined
foreignKey: { expect(Profile.rawAttributes.user_id.references).to.equal(User.getTableName())
allowNull: false expect(Profile.rawAttributes.user_id.referencesKey).to.equal('uid')
} expect(Profile.rawAttributes.user_id.allowNull).to.be.false
}); });
expect(Task.rawAttributes.userUid.allowNull).to.be.false; it('works when merging with an existing definition', function () {
var Task = this.sequelize.define('task', {
var Project = this.sequelize.define('project', { projectId: {
user_id: { defaultValue: 42,
type: Sequelize.INTEGER type: Sequelize.INTEGER
} }
}); })
, Project = this.sequelize.define('project', {});
Project.belongsTo(User, { foreignKey: Project.rawAttributes.user_id}); Task.belongsTo(Project, { foreignKey: { allowNull: true }});
expect(Project.rawAttributes.user_id.references).to.equal(User.getTableName()); expect(Task.rawAttributes.projectId).to.be.defined
expect(Project.rawAttributes.user_id.referencesKey).to.equal('uid'); expect(Task.rawAttributes.projectId.defaultValue).to.equal(42);
expect(Task.rawAttributes.projectId.allowNull).to.be.ok;
})
}); });
it('should throw an error if foreignKey and as result in a name clash', function () { it('should throw an error if foreignKey and as result in a name clash', function () {
......
...@@ -2206,8 +2206,49 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -2206,8 +2206,49 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
}); });
}); });
it('allows the user to provide an attribute definition as foreignKey', function () { describe('allows the user to provide an attribute definition object as foreignKey', function () {
var Task = this.sequelize.define('task', {}) it('works with a column that hasnt been defined before', function () {
var Task = this.sequelize.define('task', {})
, User = this.sequelize.define('user', {});
User.hasMany(Task, {
foreignKey: {
fieldName: 'uid',
allowNull: false
}
});
expect(Task.rawAttributes.uid).to.be.defined;
expect(Task.rawAttributes.uid.allowNull).to.be.false;
expect(Task.rawAttributes.uid.references).to.equal(User.getTableName());
expect(Task.rawAttributes.uid.referencesKey).to.equal('id');
Task.hasMany(User, {
foreignKey: {
allowNull: false
}
});
expect(Task.rawAttributes.uid).not.to.be.defined;
expect(Task.associations.tasksusers.through.rawAttributes.taskId).to.be.defined;
expect(Task.associations.tasksusers.through.rawAttributes.taskId.allowNull).to.be.false;
expect(Task.associations.tasksusers.through.rawAttributes.taskId.references).to.equal(Task.getTableName());
expect(Task.associations.tasksusers.through.rawAttributes.taskId.referencesKey).to.equal('id');
expect(Task.associations.tasksusers.through.rawAttributes.uid).to.be.defined;
expect(Task.associations.tasksusers.through.rawAttributes.uid.allowNull).to.be.false;
expect(Task.associations.tasksusers.through.rawAttributes.uid.references).to.equal(User.getTableName());
expect(Task.associations.tasksusers.through.rawAttributes.uid.referencesKey).to.equal('id');
});
it('works when taking a column directly from the object', function () {
var Project = this.sequelize.define('project', {
user_id: {
type: Sequelize.INTEGER,
defaultValue: 42
}
})
, User = this.sequelize.define('user', { , User = this.sequelize.define('user', {
uid: { uid: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
...@@ -2215,33 +2256,29 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -2215,33 +2256,29 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
} }
}); });
User.hasMany(Task, { User.hasMany(Project, { foreignKey: Project.rawAttributes.user_id});
foreignKey: {
fieldName: 'user_id',
allowNull: false
}
});
expect(Task.rawAttributes.user_id.allowNull).to.be.false;
Task.hasMany(User, { expect(Project.rawAttributes.user_id).to.be.defined;
foreignKey: { expect(Project.rawAttributes.user_id.references).to.equal(User.getTableName());
allowNull: false expect(Project.rawAttributes.user_id.referencesKey).to.equal('uid');
} expect(Project.rawAttributes.user_id.defaultValue).to.equal(42);
}); });
expect(Task.associations.tasksusers.through.rawAttributes.taskId.allowNull).to.be.false; it('works when merging with an existing definition', function () {
var Task = this.sequelize.define('task', {
var Project = this.sequelize.define('project', { userId: {
user_id: { defaultValue: 42,
type: Sequelize.INTEGER type: Sequelize.INTEGER
} }
}); })
, User = this.sequelize.define('user', {});
User.hasMany(Project, { foreignKey: Project.rawAttributes.user_id}); User.hasMany(Task, { foreignKey: { allowNull: true }});
expect(Project.rawAttributes.user_id.references).to.equal(User.getTableName()); expect(Task.rawAttributes.userId).to.be.defined;
expect(Project.rawAttributes.user_id.referencesKey).to.equal('uid'); expect(Task.rawAttributes.userId.defaultValue).to.equal(42);
expect(Task.rawAttributes.userId.allowNull).to.be.ok;
});
}); });
it('should throw an error if foreignKey and as result in a name clash', function () { it('should throw an error if foreignKey and as result in a name clash', function () {
......
...@@ -471,41 +471,69 @@ describe(Support.getTestDialectTeaser("HasOne"), function() { ...@@ -471,41 +471,69 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
}) })
}) })
it('allows the user to provide an attribute definition as foreignKey', function () { describe('allows the user to provide an attribute definition object as foreignKey', function () {
var User = this.sequelize.define('user', { it('works with a column that hasnt been defined before', function () {
var User = this.sequelize.define('user', {})
, Profile = this.sequelize.define('project', {})
User.hasOne(Profile, {
foreignKey: {
allowNull: false,
fieldName: 'uid'
}
});
expect(Profile.rawAttributes.uid).to.be.defined
expect(Profile.rawAttributes.uid.references).to.equal(User.getTableName())
expect(Profile.rawAttributes.uid.referencesKey).to.equal('id')
expect(Profile.rawAttributes.uid.allowNull).to.be.false;
});
it('works when taking a column directly from the object', function () {
var User = this.sequelize.define('user', {
uid: {
type: Sequelize.INTEGER,
primaryKey: true
}
})
, Profile = this.sequelize.define('project', {
user_id: {
type: Sequelize.INTEGER,
allowNull: false
}
})
User.hasOne(Profile, { foreignKey: Profile.rawAttributes.user_id})
expect(Profile.rawAttributes.user_id).to.be.defined
expect(Profile.rawAttributes.user_id.references).to.equal(User.getTableName())
expect(Profile.rawAttributes.user_id.referencesKey).to.equal('uid')
expect(Profile.rawAttributes.user_id.allowNull).to.be.false;
});
it('works when merging with an existing definition', function () {
var User = this.sequelize.define('user', {
uid: { uid: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
primaryKey: true primaryKey: true
} }
}) })
, Profile = this.sequelize.define('project', { , Project = this.sequelize.define('project', {
user_id: { userUid: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
allowNull: false defaultValue: 42
} }
}) });
User.hasOne(Profile, { foreignKey: Profile.rawAttributes.user_id}) User.hasOne(Project, { foreignKey: { allowNull: false }});
var Project = this.sequelize.define('project', { expect(Project.rawAttributes.userUid).to.be.defined;
user_id: { expect(Project.rawAttributes.userUid.allowNull).to.be.false;
type: Sequelize.INTEGER expect(Project.rawAttributes.userUid.references).to.equal(User.getTableName())
} expect(Project.rawAttributes.userUid.referencesKey).to.equal('uid')
expect(Project.rawAttributes.userUid.defaultValue).to.equal(42);
}); });
});
User.hasOne(Project, {
foreignKey: {
allowNull: false
}
})
expect(Project.rawAttributes.userUid.allowNull).to.be.false;
expect(Profile.rawAttributes.user_id).to.be.defined
expect(Profile.rawAttributes.user_id.references).to.equal(User.getTableName())
expect(Profile.rawAttributes.user_id.referencesKey).to.equal('uid')
expect(Profile.rawAttributes.user_id.allowNull).to.be.false
})
it('should throw an error if an association clashes with the name of an already define attribute', function () { it('should throw an error if an association clashes with the name of an already define attribute', function () {
var User = this.sequelize.define('user', { var User = this.sequelize.define('user', {
......
...@@ -935,7 +935,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -935,7 +935,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
it("should be possible to define a belongsTo include as required with child hasMany not required", function(done) { it.only("should be possible to define a belongsTo include as required with child hasMany not required", function(done) {
var S = this.sequelize var S = this.sequelize
, Address = S.define('Address', { 'active': DataTypes.BOOLEAN }) , Address = S.define('Address', { 'active': DataTypes.BOOLEAN })
, Street = S.define('Street', { 'active': DataTypes.BOOLEAN }) , Street = S.define('Street', { 'active': DataTypes.BOOLEAN })
......
...@@ -74,7 +74,7 @@ var Support = { ...@@ -74,7 +74,7 @@ var Support = {
var sequelizeOptions = _.defaults(options, { var sequelizeOptions = _.defaults(options, {
host: options.host || config.host, host: options.host || config.host,
logging: false, // logging: false,
dialect: options.dialect, dialect: options.dialect,
port: options.port || process.env.SEQ_PORT || config.port, port: options.port || process.env.SEQ_PORT || config.port,
pool: config.pool, pool: config.pool,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!