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

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 ...@@ -11,7 +11,6 @@ var BelongsTo = module.exports = function(associationName, srcModel, targetModel
// the id is in the source table // the id is in the source table
BelongsTo.prototype.injectAttributes = function() { BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = Utils._.underscoredIf(this.associationName + "Id", this.options.underscored) this.identifier = Utils._.underscoredIf(this.associationName + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
......
...@@ -3,17 +3,20 @@ ...@@ -3,17 +3,20 @@
*/ */
var Associations = module.exports = { var Associations = module.exports = {
classMethods: { classMethods: {
hasOne: function(associationName, associatedModel, options) { hasOne: function(associationName, associatedModel) {
// the id is in the foreign table // 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) { belongsTo: function(associationName, associatedModel) {
// the id is in the foreign table or in a connecting table
},
belongsTo: function(associationName, associatedModel, options) {
// the id is in this table // the id is in this table
var BelongsTo = require("./belongs-to") var BelongsTo = require("./belongs-to")
var association = new BelongsTo(associationName, this, associatedModel, this.options) var association = new BelongsTo(associationName, this, associatedModel, this.options)
this.associations[associationName] = association.injectAttributes() this.associations[associationName] = association.injectAttributes()
},
hasMany: function(associationName, associatedModel, options) {
// the id is in the foreign table or in a connecting table
} }
}, },
instanceMethods: { instanceMethods: {
......
...@@ -8,39 +8,39 @@ module.exports = { ...@@ -8,39 +8,39 @@ module.exports = {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: 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 }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task) Task.belongsTo('user', User)
assert.eql(User.attributes.taskId, "INT") assert.eql(Task.attributes.userId, "INT")
}, },
'it should correctly add the foreign id with underscore': function() { '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 User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }, {underscored: true})
User.belongsTo('task', Task) Task.belongsTo('user', User)
assert.eql(User.attributes.task_id, "INT") assert.eql(Task.attributes.user_id, "INT")
}, },
'it should define getter and setter': function() { 'it should define getter and setter': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: 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 }) 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'}) var t = Task.build({title: 'asd'})
assert.isDefined(u.setTask) assert.isDefined(t.setUser)
assert.isDefined(u.getTask) assert.isDefined(t.getUser)
}, },
'it should set and get the correct object': function(exit) { 'it should set and get the correct object': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: 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 }) 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() { User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() { Task.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) { User.create({username: 'asd'}).on('success', function(u) {
Task.create({title: 'a task'}).on('success', function(t) { Task.create({title: 'a task'}).on('success', function(t) {
u.setTask(t).on('success', function() { t.setUser(u).on('success', function() {
u.getTask().on('success', function(task) { t.getUser().on('success', function(user) {
assert.eql(task.title, 'a task') assert.eql(user.username, 'asd')
exit(function(){}) exit(function(){})
}) })
}) })
...@@ -53,18 +53,18 @@ module.exports = { ...@@ -53,18 +53,18 @@ module.exports = {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: 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 }) 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() { User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() { Task.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) { User.create({username: 'asd'}).on('success', function(u) {
Task.create({title: 'a task'}).on('success', function(t) { Task.create({title: 'a task'}).on('success', function(t) {
u.setTask(t).on('success', function() { t.setUser(u).on('success', function() {
u.getTask().on('success', function(task) { t.getUser().on('success', function(user) {
assert.eql(task.title, 'a task') assert.eql(user.username, 'asd')
u.setTask(null).on('success', function() { t.setUser(null).on('success', function() {
u.getTask().on('success', function(task) { t.getUser().on('success', function(user) {
assert.isNull(task) assert.isNull(user)
exit(function(){}) 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!