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

Commit 760966d2 by Sascha Depold

bugfixing

1 parent 47bc7007
...@@ -240,14 +240,19 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -240,14 +240,19 @@ SequelizeTable = function(sequelize, tableName, attributes) {
var self = this var self = this
this[assocName](function(currentAssociation) { this[assocName](function(currentAssociation) {
if(object.id == currentAssociation.id) callback() var attr = {}
else { if(currentAssociation == null) {
var attr = {} attr[table.identifier] = self.id
attr[table.identifier] = null object.updateAttributes(attr, callback)
currentAssociation.updateAttributes(attr, function() { } else {
attr[table.identifier] = self.id if(object.id == currentAssociation.id) callback()
object.updateAttributes(attr, callback) else {
}) attr[table.identifier] = null
currentAssociation.updateAttributes(attr, function() {
attr[table.identifier] = self.id
object.updateAttributes(attr, callback)
})
}
} }
}) })
} }
...@@ -263,13 +268,15 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -263,13 +268,15 @@ SequelizeTable = function(sequelize, tableName, attributes) {
}) })
table.prototype[assocName] = function(callback) { table.prototype[assocName] = function(callback) {
var whereConditions = ["id", this[_table.identifier]].join("=") _table.find(this[_table.identifier], callback)
_table.find({where: whereConditions}, callback)
} }
table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(object, callback) { table.prototype[SequelizeHelper.SQL.addPrefix('set', assocName)] = function(object, callback) {
var attr = {}; attr[object.table.identifier] = object.id var attr = {}; attr[object.table.identifier] = object.id
this.updateAttributes(attr, callback) var self = this
this.updateAttributes(attr, function() {
self[assocName](callback)
})
} }
return table return table
......
...@@ -132,7 +132,7 @@ module.exports = { ...@@ -132,7 +132,7 @@ module.exports = {
Day.hasMany('HasManyBlubbs', HasManyBlubb) Day.hasMany('HasManyBlubbs', HasManyBlubb)
assert.isNotUndefined(new Day({name:''}).HasManyBlubbs) assert.isNotUndefined(new Day({name:''}).HasManyBlubbs)
}, },
'hasMany findAll => crossAssociated': function(assert, beforeExit) { 'hasMany: set association': function(assert, beforeExit) {
var assoc = null var assoc = null
var Character = s.define('Character', {}) var Character = s.define('Character', {})
var Word = s.define('Word', {}) var Word = s.define('Word', {})
...@@ -166,11 +166,53 @@ module.exports = { ...@@ -166,11 +166,53 @@ module.exports = {
Day.hasOne('HasOneBlubb', HasOneBlubb) Day.hasOne('HasOneBlubb', HasOneBlubb)
assert.isNotUndefined(new Day({name:''}).HasOneBlubb) assert.isNotUndefined(new Day({name:''}).HasOneBlubb)
}, },
'hasOne set association': function(assert, beforeExit) {
var s2 = new Sequelize('sequelize_test', 'test', 'test', {disableLogging: true})
var Task = s2.define('Task', {title: Sequelize.STRING})
var Deadline = s2.define('Deadline', {date: Sequelize.DATE})
Task.hasOne('deadline', Deadline)
Deadline.belongsTo('task', Task)
var task = new Task({ title:'do smth' })
var deadline = new Deadline({date: new Date()})
var assertMe = null
Sequelize.chainQueries([{sync: s2}, {drop: s2}, {sync: s2}, {save: task}, {save: deadline}], function() {
task.setDeadline(deadline, function(_deadline) { assertMe = _deadline })
})
beforeExit(function() {
assert.isNotNull(assertMe)
assert.eql(assertMe, deadline)
})
},
'belongsTo': function(assert) { 'belongsTo': function(assert) {
var BelongsToBlubb = s.define('BelongsToBlubb', {}) var BelongsToBlubb = s.define('BelongsToBlubb', {})
Day.belongsTo('BelongsToBlubb', BelongsToBlubb) Day.belongsTo('BelongsToBlubb', BelongsToBlubb)
assert.isNotUndefined(new Day({name:''}).BelongsToBlubb) assert.isNotUndefined(new Day({name:''}).BelongsToBlubb)
}, },
'belongsTo: set association': function(assert, beforeExit) {
var s2 = new Sequelize('sequelize_test', 'test', 'test', {disableLogging: true})
var Task = s2.define('Task', {title: Sequelize.STRING})
var Deadline = s2.define('Deadline', {date: Sequelize.DATE})
Task.hasOne('deadline', Deadline)
Deadline.belongsTo('task', Task)
var task = new Task({ title:'do smth' })
var deadline = new Deadline({date: new Date()})
var assertMe = null
Sequelize.chainQueries([{drop: s2}, {sync: s2}, {save: task}, {save: deadline}], function() {
deadline.setTask(task, function(_task) { assertMe = _task })
})
beforeExit(function() {
assert.isNotNull(assertMe)
assert.eql(assertMe.id, task.id)
})
},
'identifier': function(assert) { 'identifier': function(assert) {
assert.equal(s.define('Identifier', {}).identifier, 'identifierId') assert.equal(s.define('Identifier', {}).identifier, 'identifierId')
}, },
...@@ -195,15 +237,15 @@ module.exports = { ...@@ -195,15 +237,15 @@ module.exports = {
}, },
'updateAttributes should update available attributes': function(assert, beforeExit) { 'updateAttributes should update available attributes': function(assert, beforeExit) {
var subject = null var subject = null
Day.drop(function() {
Day.sync(function() { Sequelize.chainQueries([{drop: Day}, {sync: Day}], function() {
new Day({name:'Monday'}).save(function(day) { new Day({name:'Monday'}).save(function(day) {
day.updateAttributes({name: 'Sunday', foo: 'bar'}, function(day) { day.updateAttributes({name: 'Sunday', foo: 'bar'}, function(day) {
subject = day subject = day
})
}) })
}) })
}) })
beforeExit(function() { beforeExit(function() {
assert.equal(subject.name, 'Sunday') assert.equal(subject.name, 'Sunday')
assert.isUndefined(subject.foo) assert.isUndefined(subject.foo)
...@@ -212,13 +254,11 @@ module.exports = { ...@@ -212,13 +254,11 @@ module.exports = {
'destroy should make the object unavailable': function(assert, beforeExit) { 'destroy should make the object unavailable': function(assert, beforeExit) {
var subject = 1 var subject = 1
var UpdateAttributesTest = s.define('UpdateAttributeTest', {name: Sequelize.STRING}) var UpdateAttributesTest = s.define('UpdateAttributeTest', {name: Sequelize.STRING})
UpdateAttributesTest.drop(function() { Sequelize.chainQueries([{drop: UpdateAttributesTest}, {sync: UpdateAttributesTest}], function() {
UpdateAttributesTest.sync(function() { new UpdateAttributesTest({name:'Monday'}).save(function(day) {
new UpdateAttributesTest({name:'Monday'}).save(function(day) { day.destroy(function() {
day.destroy(function() { UpdateAttributesTest.find(day.id, function(result) {
UpdateAttributesTest.find(day.id, function(result) { subject = result
subject = result
})
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!