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

Commit 963e63f4 by Sascha Depold

fixed some brainfuck in belongs-to test

1 parent b89572f0
......@@ -11,7 +11,6 @@ var BelongsTo = module.exports = function(associationName, srcModel, targetModel
// the id is in the source table
BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {}
this.identifier = Utils._.underscoredIf(this.associationName + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
......
......@@ -3,17 +3,20 @@
*/
var Associations = module.exports = {
classMethods: {
hasOne: function(associationName, associatedModel, options) {
hasOne: function(associationName, associatedModel) {
// the id is in the foreign table
var HasOne = require('./has-one')
var association = new HasOne(association, this, associatedModel, this.options)
this.associations[associationName] = association.injectAttributes()
},
hasMany: function(associationName, associatedModel, options) {
// the id is in the foreign table or in a connecting table
},
belongsTo: function(associationName, associatedModel, options) {
belongsTo: function(associationName, associatedModel) {
// the id is in this table
var BelongsTo = require("./belongs-to")
var association = new BelongsTo(associationName, this, associatedModel, this.options)
this.associations[associationName] = association.injectAttributes()
},
hasMany: function(associationName, associatedModel, options) {
// the id is in the foreign table or in a connecting table
}
},
instanceMethods: {
......
......@@ -8,39 +8,39 @@ module.exports = {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
assert.eql(User.attributes.taskId, "INT")
Task.belongsTo('user', User)
assert.eql(Task.attributes.userId, "INT")
},
'it should correctly add the foreign id with underscore': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }, {underscored: true})
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }, {underscored: true})
User.belongsTo('task', Task)
assert.eql(User.attributes.task_id, "INT")
Task.belongsTo('user', User)
assert.eql(Task.attributes.user_id, "INT")
},
'it should define getter and setter': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
Task.belongsTo('user', User)
var u = User.build({username: 'asd'})
assert.isDefined(u.setTask)
assert.isDefined(u.getTask)
var t = Task.build({title: 'asd'})
assert.isDefined(t.setUser)
assert.isDefined(t.getUser)
},
'it should set and get the correct object': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
Task.belongsTo('user', User)
User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) {
Task.create({title: 'a task'}).on('success', function(t) {
u.setTask(t).on('success', function() {
u.getTask().on('success', function(task) {
assert.eql(task.title, 'a task')
t.setUser(u).on('success', function() {
t.getUser().on('success', function(user) {
assert.eql(user.username, 'asd')
exit(function(){})
})
})
......@@ -53,18 +53,18 @@ module.exports = {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
Task.belongsTo('user', User)
User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) {
Task.create({title: 'a task'}).on('success', function(t) {
u.setTask(t).on('success', function() {
u.getTask().on('success', function(task) {
assert.eql(task.title, 'a task')
u.setTask(null).on('success', function() {
u.getTask().on('success', function(task) {
assert.isNull(task)
t.setUser(u).on('success', function() {
t.getUser().on('success', function(user) {
assert.eql(user.username, 'asd')
t.setUser(null).on('success', function() {
t.getUser().on('success', function(user) {
assert.isNull(user)
exit(function(){})
})
})
......
var assert = require("assert")
, config = require("./../config")
, Sequelize = require("./../../index")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
return module.exports = {'asd': function(){}}
module.exports = {
'it should correctly add the foreign id': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.hasOne('user', User)
assert.eql(User.attributes.taskId, "INT")
},
'it should correctly add the foreign id with underscore': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }, {underscored: true})
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
assert.eql(User.attributes.task_id, "INT")
},
'it should define getter and setter': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
var u = User.build({username: 'asd'})
assert.isDefined(u.setTask)
assert.isDefined(u.getTask)
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!