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

Commit 49723bf5 by Sascha Depold

fixed test issue + belongs-to is almost working

1 parent af7b75bb
...@@ -12,8 +12,7 @@ var BelongsTo = module.exports = function(associationName, srcModel, targetModel ...@@ -12,8 +12,7 @@ var BelongsTo = module.exports = function(associationName, srcModel, targetModel
BelongsTo.prototype.injectAttributes = function() { BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = this.associationName + "Id" this.identifier = Utils._.underscoredIf(this.associationName + "Id", this.options.underscored)
if(this.options.underscored) this.identifier = Utils._.camelize(this.identifier)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes))
...@@ -25,8 +24,8 @@ BelongsTo.prototype.injectAttributes = function() { ...@@ -25,8 +24,8 @@ BelongsTo.prototype.injectAttributes = function() {
BelongsTo.prototype.injectGetter = function(obj) { BelongsTo.prototype.injectGetter = function(obj) {
var self = this var self = this
obj['get' + this.associationName] = function() { obj[Utils._.camelize('get_' + this.associationName)] = function() {
return self.target.find(self.identifier) return self.target.find(obj[self.identifier])
} }
return this return this
...@@ -35,9 +34,9 @@ BelongsTo.prototype.injectGetter = function(obj) { ...@@ -35,9 +34,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
BelongsTo.prototype.injectSetter = function(obj) { BelongsTo.prototype.injectSetter = function(obj) {
var self = this var self = this
obj['set' + this.associationName] = function(associatedObject) { obj[Utils._.camelize('set_' + this.associationName)] = function(associatedObject) {
self.source[self.identifier] = associatedObject.id obj[self.identifier] = associatedObject.id
return self.source.save() return obj.save()
} }
return this return this
......
...@@ -25,11 +25,11 @@ ModelDefinition.prototype.addDefaultAttributes = function() { ...@@ -25,11 +25,11 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
if(this.hasPrimaryKeys) defaultAttributes = {} if(this.hasPrimaryKeys) defaultAttributes = {}
if(this.options.timestamps) { if(this.options.timestamps) {
defaultAttributes[this.options.underscored ? 'created_at' : 'createdAt'] = {type: DataTypes.DATE, allowNull: false} defaultAttributes[Utils._.underscoredIf('createdAt', this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
defaultAttributes[this.options.underscored ? 'updated_at' : 'updatedAt'] = {type: DataTypes.DATE, allowNull: false} defaultAttributes[Utils._.underscoredIf('updatedAt', this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
if(this.options.paranoid) if(this.options.paranoid)
defaultAttributes[this.options.underscored ? 'deleted_at' : 'deletedAt'] = {type: DataTypes.DATE} defaultAttributes[Utils._.underscoredIf('deletedAt', this.options.underscored)] = {type: DataTypes.DATE}
} }
defaultAttributes = Utils.simplifyAttributes(defaultAttributes) defaultAttributes = Utils.simplifyAttributes(defaultAttributes)
......
...@@ -11,7 +11,7 @@ module.exports = { ...@@ -11,7 +11,7 @@ module.exports = {
User.belongsTo('task', Task) User.belongsTo('task', Task)
assert.eql(User.attributes.taskId, "INT") assert.eql(User.attributes.taskId, "INT")
}, },
'it should correctly add the foreign id with underscored': 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 }, {underscored: true})
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
...@@ -25,7 +25,6 @@ module.exports = { ...@@ -25,7 +25,6 @@ module.exports = {
User.belongsTo('task', Task) User.belongsTo('task', Task)
var u = User.build({username: 'asd'}) var u = User.build({username: 'asd'})
console.log(u)
assert.isDefined(u.setTask) assert.isDefined(u.setTask)
assert.isDefined(u.getTask) assert.isDefined(u.getTask)
}, },
...@@ -36,12 +35,14 @@ module.exports = { ...@@ -36,12 +35,14 @@ module.exports = {
User.belongsTo('task', Task) User.belongsTo('task', Task)
User.sync({force: true}).on('success', function() { User.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) { Task.sync({force: true}).on('success', function() {
Task.create({title: 'a task'}).on('success', function(t) { User.create({username: 'asd'}).on('success', function(u) {
u.setTask(t).on('success', function() { Task.create({title: 'a task'}).on('success', function(t) {
u.getTask().on('success', function(task) { u.setTask(t).on('success', function() {
assert.eql(task.title, 'a task') u.getTask().on('success', function(task) {
exit() assert.eql(task.title, 'a task')
exit(function(){})
})
}) })
}) })
}) })
......
...@@ -19,7 +19,7 @@ module.exports = { ...@@ -19,7 +19,7 @@ module.exports = {
assert.eql(users.length, 10) assert.eql(users.length, 10)
User.all.on('success', function(users) { User.all.on('success', function(users) {
assert.eql(users.length, 0) assert.eql(users.length, 0)
exit() exit(function(){})
}) })
}) })
} }
......
...@@ -16,7 +16,7 @@ module.exports = { ...@@ -16,7 +16,7 @@ module.exports = {
User.create({username:'foo'}).on('success', function() { User.create({username:'foo'}).on('success', function() {
User.create({username: 'foo'}).on('failure', function(err) { User.create({username: 'foo'}).on('failure', function(err) {
assert.eql(err.message, "Duplicate entry 'foo' for key 'username'") assert.eql(err.message, "Duplicate entry 'foo' for key 'username'")
exit() exit(function(){})
}) })
}) })
}) })
...@@ -31,7 +31,7 @@ module.exports = { ...@@ -31,7 +31,7 @@ module.exports = {
assert.eql(err.message, "Column 'smth' cannot be null") assert.eql(err.message, "Column 'smth' cannot be null")
User.create({username: 'foo'}).on('failure', function(err) { User.create({username: 'foo'}).on('failure', function(err) {
assert.eql(err.message, "Column 'smth' cannot be null") assert.eql(err.message, "Column 'smth' cannot be null")
exit() exit(function(){})
}) })
}) })
}) })
......
...@@ -13,7 +13,7 @@ module.exports = { ...@@ -13,7 +13,7 @@ module.exports = {
u.destroy().on('success', function() { u.destroy().on('success', function() {
User.all.on('success', function(users) { User.all.on('success', function(users) {
assert.eql(users.length, 0) assert.eql(users.length, 0)
exit() exit(function(){})
}) })
}) })
}) })
...@@ -27,7 +27,7 @@ module.exports = { ...@@ -27,7 +27,7 @@ module.exports = {
assert.isNull(u.deletedAt) assert.isNull(u.deletedAt)
u.destroy().on('success', function(u) { u.destroy().on('success', function(u) {
assert.isNotNull(u.deletedAt) assert.isNotNull(u.deletedAt)
exit() exit(function(){})
}) })
}) })
}) })
......
...@@ -22,7 +22,7 @@ module.exports = { ...@@ -22,7 +22,7 @@ module.exports = {
initUsers(2, function(_, User) { initUsers(2, function(_, User) {
User.all.on('success', function(users) { User.all.on('success', function(users) {
assert.eql(users.length, 2) assert.eql(users.length, 2)
exit() exit(function(){})
}) })
}) })
}, },
...@@ -30,7 +30,7 @@ module.exports = { ...@@ -30,7 +30,7 @@ module.exports = {
initUsers(2, function(lastInsertedUser, User) { initUsers(2, function(lastInsertedUser, User) {
User.find(lastInsertedUser.id).on('success', function(user) { User.find(lastInsertedUser.id).on('success', function(user) {
assert.eql(user.id, lastInsertedUser.id) assert.eql(user.id, lastInsertedUser.id)
exit() exit(function(){})
}) })
}) })
}, },
...@@ -39,7 +39,7 @@ module.exports = { ...@@ -39,7 +39,7 @@ module.exports = {
var username = 'user1' var username = 'user1'
User.find({where: {name: username}}).on('success', function(user) { User.find({where: {name: username}}).on('success', function(user) {
assert.eql(user.name, username) assert.eql(user.name, username)
exit() exit(function(){})
}) })
}) })
}, },
...@@ -47,7 +47,7 @@ module.exports = { ...@@ -47,7 +47,7 @@ module.exports = {
initUsers(2, function(_, User) { initUsers(2, function(_, User) {
User.find({where: {name: 'foo'}}).on('success', function(user) { User.find({where: {name: 'foo'}}).on('success', function(user) {
assert.eql(user, null) assert.eql(user, null)
exit() exit(function(){})
}) })
}) })
}, },
...@@ -56,7 +56,7 @@ module.exports = { ...@@ -56,7 +56,7 @@ module.exports = {
User.find({limit: 10}).on('success', function(user) { User.find({limit: 10}).on('success', function(user) {
// it returns an object instead of an array // it returns an object instead of an array
assert.eql(user.hasOwnProperty('name'), true) assert.eql(user.hasOwnProperty('name'), true)
exit() exit(function(){})
}) })
}) })
}, },
...@@ -71,7 +71,7 @@ module.exports = { ...@@ -71,7 +71,7 @@ module.exports = {
User.find('an identifier').on('success', function(u2) { User.find('an identifier').on('success', function(u2) {
assert.eql(u.identifier, 'an identifier') assert.eql(u.identifier, 'an identifier')
assert.eql(u.name, 'John') assert.eql(u.name, 'John')
exit() exit(function(){})
}) })
}) })
}) })
...@@ -80,7 +80,7 @@ module.exports = { ...@@ -80,7 +80,7 @@ module.exports = {
initUsers(2, function(_, User) { initUsers(2, function(_, User) {
User.findAll().on('success', function(users) { User.findAll().on('success', function(users) {
assert.eql(users.length, 2) assert.eql(users.length, 2)
exit() exit(function(){})
}) })
}) })
}, },
...@@ -88,7 +88,7 @@ module.exports = { ...@@ -88,7 +88,7 @@ module.exports = {
initUsers(2, function(lastUser, User) { initUsers(2, function(lastUser, User) {
User.findAll({where: "id != " + lastUser.id}).on('success', function(users) { User.findAll({where: "id != " + lastUser.id}).on('success', function(users) {
assert.eql(users.length, 1) assert.eql(users.length, 1)
exit() exit(function(){})
}) })
}) })
}, },
...@@ -96,7 +96,7 @@ module.exports = { ...@@ -96,7 +96,7 @@ module.exports = {
initUsers(2, function(lastUser, User) { initUsers(2, function(lastUser, User) {
User.findAll({order: "id DESC"}).on('success', function(users) { User.findAll({order: "id DESC"}).on('success', function(users) {
assert.eql(users[0].id > users[1].id, true) assert.eql(users[0].id > users[1].id, true)
exit() exit(function(){})
}) })
}) })
}, },
...@@ -105,7 +105,7 @@ module.exports = { ...@@ -105,7 +105,7 @@ module.exports = {
User.findAll({limit: 2, offset: 2}).on('success', function(users) { User.findAll({limit: 2, offset: 2}).on('success', function(users) {
assert.eql(users.length, 2) assert.eql(users.length, 2)
assert.eql(users[0].id, 3) assert.eql(users[0].id, 3)
exit() exit(function(){})
}) })
}) })
} }
......
...@@ -18,7 +18,7 @@ module.exports = { ...@@ -18,7 +18,7 @@ module.exports = {
initUsers(1, function(users, User) { initUsers(1, function(users, User) {
assert.isNull(users[0].id) assert.isNull(users[0].id)
assert.eql(users[0].isNewRecord, true) assert.eql(users[0].isNewRecord, true)
exit() exit(function(){})
}) })
} }
} }
\ No newline at end of file
...@@ -6,6 +6,7 @@ var assert = require("assert") ...@@ -6,6 +6,7 @@ var assert = require("assert")
module.exports = { module.exports = {
'save should add a record to the database': function(exit) { 'save should add a record to the database': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT })
User.sync({force: true}).on('success', function() { User.sync({force: true}).on('success', function() {
var u = User.build({name: 'hallo', bio: 'welt'}) var u = User.build({name: 'hallo', bio: 'welt'})
User.all.on('success', function(users) { User.all.on('success', function(users) {
...@@ -14,7 +15,7 @@ module.exports = { ...@@ -14,7 +15,7 @@ module.exports = {
User.all.on('success', function(users) { User.all.on('success', function(users) {
assert.eql(users.length, 1) assert.eql(users.length, 1)
assert.eql(users[0].name, 'hallo') assert.eql(users[0].name, 'hallo')
exit() exit(function(){})
}) })
}) })
}) })
...@@ -24,18 +25,17 @@ module.exports = { ...@@ -24,18 +25,17 @@ module.exports = {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT }) var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { name: Sequelize.STRING, bio: Sequelize.TEXT })
User.sync({force: true}).on('success', function() { User.sync({force: true}).on('success', function() {
var now = Date.now() var now = Date.now()
// timeout is needed, in order to check the update of the timestamp // timeout is needed, in order to check the update of the timestamp
setTimeout(function() { setTimeout(function() {
var u = User.build({name: 'foo', bio: 'bar'}) var u = User.build({name: 'foo', bio: 'bar'})
, uNow = u.updatedAt , uNow = u.updatedAt
assert.eql(true, uNow.getTime() > now) assert.eql(true, uNow.getTime() > now)
setTimeout(function() { setTimeout(function() {
u.save().on('success', function() { u.save().on('success', function() {
assert.eql(true, uNow.getTime() < u.updatedAt) assert.eql(true, uNow.getTime() < u.updatedAt.getTime())
exit() exit(function(){})
}) })
}, 100) }, 100)
}, 100) }, 100)
......
...@@ -6,14 +6,14 @@ var assert = require("assert") ...@@ -6,14 +6,14 @@ var assert = require("assert")
module.exports = { module.exports = {
'sync should work with correct database config': function(exit) { 'sync should work with correct database config': function(exit) {
User.sync().on('success', exit) User.sync().on('success', function(){exit(function(){})})
}, },
'sync should fail with incorrect database config': function(exit) { 'sync should fail with incorrect database config': function(exit) {
var s = new Sequelize('foo', 'bar', null, {logging: false}) var s = new Sequelize('foo', 'bar', null, {logging: false})
var User2 = s.define('User', { name: Sequelize.STRING, bio: Sequelize.TEXT }) var User2 = s.define('User', { name: Sequelize.STRING, bio: Sequelize.TEXT })
User2.sync().on('failure', exit) User2.sync().on('failure', function(){exit(function(){})})
}, },
'drop should work': function(exit) { 'drop should work': function(exit) {
User.drop().on('success', exit) User.drop().on('success', function(){exit(function(){})})
} }
} }
\ No newline at end of file
...@@ -17,7 +17,7 @@ module.exports = { ...@@ -17,7 +17,7 @@ module.exports = {
'build should not create database entries': function(exit) { 'build should not create database entries': function(exit) {
initUsers(1, function(users, User) { initUsers(1, function(users, User) {
assert.eql(users[0].values, {"name":"user0","bio":"foobar","id":null}) assert.eql(users[0].values, {"name":"user0","bio":"foobar","id":null})
exit() exit(function(){})
}) })
} }
} }
\ No newline at end of file
var assert = require("assert")
, Utils = require("../../lib/sequelize/utils")
module.exports = {
'underscoredIf should be defined': function() {
assert.isDefined(Utils._.underscoredIf)
},
'underscoredIf should work correctly': function() {
assert.eql(Utils._.underscoredIf('fooBar', false), 'fooBar')
assert.eql(Utils._.underscoredIf('fooBar', true), 'foo_bar')
},
'camelizeIf should be defined': function() {
assert.isDefined(Utils._.camelizeIf)
},
'camelizeIf should work correctly': function() {
assert.eql(Utils._.camelizeIf('foo_bar', false), 'foo_bar')
assert.eql(Utils._.camelizeIf('foo_bar', true), 'fooBar')
}
}
\ 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!