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

Commit 58d51729 by Mick Hansen

Merge pull request #1182 from mickhansen/hydration-setter-getter-refactor

Refactor of setters/getters/values in regards to hydration, toJSON and more
2 parents 09ac0969 feef42c5
...@@ -120,8 +120,9 @@ module.exports = (function() { ...@@ -120,8 +120,9 @@ module.exports = (function() {
} }
}) })
this.primaryKeyCount = Object.keys(this.primaryKeys).length; this.primaryKeyAttributes = Object.keys(this.primaryKeys)
this.options.hasPrimaryKeys = this.hasPrimaryKeys = this.primaryKeyCount > 0; this.primaryKeyCount = this.primaryKeyAttributes.length
this.options.hasPrimaryKeys = this.hasPrimaryKeys = this.primaryKeyCount > 0
if (typeof this.options.defaultScope === "object") { if (typeof this.options.defaultScope === "object") {
Utils.injectScope.call(this, this.options.defaultScope) Utils.injectScope.call(this, this.options.defaultScope)
...@@ -141,6 +142,26 @@ module.exports = (function() { ...@@ -141,6 +142,26 @@ module.exports = (function() {
this.DAO.prototype.rawAttributes = this.rawAttributes; this.DAO.prototype.rawAttributes = this.rawAttributes;
this.DAO.prototype._hasPrimaryKeys = this.options.hasPrimaryKeys
this.DAO.prototype._isPrimaryKey = Utils._.memoize(function (key) {
return self.primaryKeyAttributes.indexOf(key) !== -1
})
if (this.options.timestamps) {
this.DAO.prototype._timestampAttributes = {
createdAt: Utils._.underscoredIf(this.options.createdAt, this.options.underscored),
updatedAt: Utils._.underscoredIf(this.options.updatedAt, this.options.underscored),
deletedAt: Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)
}
this.DAO.prototype._readOnlyAttributes = Object.keys(this.DAO.prototype._timestampAttributes)
}
this.DAO.prototype._hasReadOnlyAttributes = this.DAO.prototype._readOnlyAttributes && this.DAO.prototype._readOnlyAttributes.length
this.DAO.prototype._isReadOnlyAttribute = Utils._.memoize(function (key) {
return self.DAO.prototype._hasReadOnlyAttributes && self.DAO.prototype._readOnlyAttributes.indexOf(key) !== -1
})
if (this.options.instanceMethods) { if (this.options.instanceMethods) {
Utils._.each(this.options.instanceMethods, function(fct, name) { Utils._.each(this.options.instanceMethods, function(fct, name) {
self.DAO.prototype[name] = fct self.DAO.prototype[name] = fct
...@@ -150,6 +171,7 @@ module.exports = (function() { ...@@ -150,6 +171,7 @@ module.exports = (function() {
this.refreshAttributes(); this.refreshAttributes();
this.DAO.prototype.booleanValues = [] this.DAO.prototype.booleanValues = []
this.DAO.prototype.dateAttributes = []
this.DAO.prototype.defaultValues = {} this.DAO.prototype.defaultValues = {}
this.DAO.prototype.validators = {} this.DAO.prototype.validators = {}
...@@ -157,6 +179,9 @@ module.exports = (function() { ...@@ -157,6 +179,9 @@ module.exports = (function() {
if (((definition === DataTypes.BOOLEAN) || (definition.type === DataTypes.BOOLEAN))) { if (((definition === DataTypes.BOOLEAN) || (definition.type === DataTypes.BOOLEAN))) {
self.DAO.prototype.booleanValues.push(name); self.DAO.prototype.booleanValues.push(name);
} }
if (((definition === DataTypes.DATE) || (definition.type === DataTypes.DATE) || (definition.originalType === DataTypes.DATE))) {
self.DAO.prototype.dateAttributes.push(name);
}
if (definition.hasOwnProperty('defaultValue')) { if (definition.hasOwnProperty('defaultValue')) {
self.DAO.prototype.defaultValues[name] = Utils._.partial( self.DAO.prototype.defaultValues[name] = Utils._.partial(
Utils.toDefaultValue, definition.defaultValue) Utils.toDefaultValue, definition.defaultValue)
...@@ -167,6 +192,16 @@ module.exports = (function() { ...@@ -167,6 +192,16 @@ module.exports = (function() {
} }
}) })
this.DAO.prototype._hasBooleanAttributes = !!this.DAO.prototype.booleanValues.length
this.DAO.prototype._isBooleanAttribute = Utils._.memoize(function (key) {
return self.DAO.prototype.booleanValues.indexOf(key) !== -1
})
this.DAO.prototype._hasDateAttributes = !!this.DAO.prototype.dateAttributes.length
this.DAO.prototype._isDateAttribute = Utils._.memoize(function (key) {
return self.DAO.prototype.dateAttributes.indexOf(key) !== -1
})
this.DAO.prototype.__factory = this this.DAO.prototype.__factory = this
this.DAO.prototype.daoFactory = this this.DAO.prototype.daoFactory = this
this.DAO.prototype.Model = this this.DAO.prototype.Model = this
...@@ -180,31 +215,42 @@ module.exports = (function() { ...@@ -180,31 +215,42 @@ module.exports = (function() {
var self = this var self = this
, attributeManipulation = {}; , attributeManipulation = {};
this.DAO.prototype._customGetters = {}
this.DAO.prototype._customSetters = {}
Utils._.each(['get', 'set'], function(type) { Utils._.each(['get', 'set'], function(type) {
var opt = type + 'terMethods' var opt = type + 'terMethods'
, funcs = Utils._.isObject(self.options[opt]) ? self.options[opt] : {} , funcs = Utils._.clone(Utils._.isObject(self.options[opt]) ? self.options[opt] : {})
, _custom = type === 'get' ? self.DAO.prototype._customGetters : self.DAO.prototype._customSetters
Utils._.each(funcs, function (method, attribute) {
_custom[attribute] = method
if (type === 'get') {
funcs[attribute] = function() {
return this.get(attribute)
}
}
if (type === 'set') {
funcs[attribute] = function(value) {
return this.set(attribute, value)
}
}
})
Utils._.each(self.rawAttributes, function(options, attribute) { Utils._.each(self.rawAttributes, function(options, attribute) {
if (options.hasOwnProperty(type)) { if (options.hasOwnProperty(type)) {
funcs[attribute] = options[type] _custom[attribute] = options[type]
} else if (typeof funcs[attribute] === "undefined") { }
if (type === 'get') {
funcs[attribute] = function() { return this.dataValues[attribute]; } if (type === 'get') {
funcs[attribute] = function() {
return this.get(attribute)
} }
if (type === 'set') { }
funcs[attribute] = function(value) { if (type === 'set') {
if (Utils.hasChanged(this.dataValues[attribute], value)) { funcs[attribute] = function(value) {
//Only dirty the object if the change is not due to id, touchedAt, createdAt or updatedAt being initiated return this.set(attribute, value)
var updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
, createdAtAttr = Utils._.underscoredIf(this.__options.createdAt, this.__options.underscored)
, touchedAtAttr = Utils._.underscoredIf(this.__options.touchedAt, this.__options.underscored)
if (this.dataValues[attribute] || (attribute != 'id' && attribute != touchedAtAttr && attribute != createdAtAttr && attribute != updatedAtAttr)) {
this.isDirty = true
}
}
this.dataValues[attribute] = value
}
} }
} }
}) })
...@@ -219,6 +265,9 @@ module.exports = (function() { ...@@ -219,6 +265,9 @@ module.exports = (function() {
}) })
}) })
this.DAO.prototype._hasCustomGetters = Object.keys(this.DAO.prototype._customGetters).length
this.DAO.prototype._hasCustomSetters = Object.keys(this.DAO.prototype._customSetters).length
Object.defineProperties(this.DAO.prototype, attributeManipulation) Object.defineProperties(this.DAO.prototype, attributeManipulation)
this.DAO.prototype.attributes = Object.keys(this.DAO.prototype.rawAttributes) this.DAO.prototype.attributes = Object.keys(this.DAO.prototype.rawAttributes)
} }
...@@ -373,7 +422,7 @@ module.exports = (function() { ...@@ -373,7 +422,7 @@ module.exports = (function() {
options = optClone(options) options = optClone(options)
if (typeof options === 'object') { if (typeof options === 'object') {
if (options.hasOwnProperty('include')) { if (options.hasOwnProperty('include') && options.include) {
hasJoin = true hasJoin = true
validateIncludedElements.call(this, options) validateIncludedElements.call(this, options)
...@@ -456,7 +505,7 @@ module.exports = (function() { ...@@ -456,7 +505,7 @@ module.exports = (function() {
} else if (typeof options === 'object') { } else if (typeof options === 'object') {
options = Utils._.clone(options) options = Utils._.clone(options)
if (options.hasOwnProperty('include')) { if (options.hasOwnProperty('include') && options.include) {
hasJoin = true hasJoin = true
validateIncludedElements.call(this, options) validateIncludedElements.call(this, options)
...@@ -554,13 +603,8 @@ module.exports = (function() { ...@@ -554,13 +603,8 @@ module.exports = (function() {
DAOFactory.prototype.build = function(values, options) { DAOFactory.prototype.build = function(values, options) {
options = options || { isNewRecord: true, isDirty: true } options = options || { isNewRecord: true, isDirty: true }
if (options.hasOwnProperty('include') && options.include && (!options.includeValidated || !options.includeNames)) { if (options.hasOwnProperty('include') && options.include && !options.includeValidated) {
options.includeNames = [] validateIncludedElements.call(this, options)
options.include = options.include.map(function(include) {
include = validateIncludedElement.call(this, include)
options.includeNames.push(include.as)
return include
}.bind(this))
} }
if (options.includeNames) { if (options.includeNames) {
...@@ -1154,7 +1198,9 @@ module.exports = (function() { ...@@ -1154,7 +1198,9 @@ module.exports = (function() {
var addDefaultAttributes = function() { var addDefaultAttributes = function() {
var self = this var self = this
, defaultAttributes = { , head = {}
, tail = {}
, head = {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
...@@ -1164,18 +1210,29 @@ module.exports = (function() { ...@@ -1164,18 +1210,29 @@ module.exports = (function() {
} }
if (this.hasPrimaryKeys) { if (this.hasPrimaryKeys) {
defaultAttributes = {} head = {}
} }
if (this.options.timestamps) { if (this.options.timestamps) {
defaultAttributes[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} tail[Utils._.underscoredIf(this.options.createdAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
defaultAttributes[Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} tail[Utils._.underscoredIf(this.options.updatedAt, this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
if (this.options.paranoid) if (this.options.paranoid)
defaultAttributes[Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)] = {type: DataTypes.DATE} tail[Utils._.underscoredIf(this.options.deletedAt, this.options.underscored)] = {type: DataTypes.DATE}
} }
Utils._.each(defaultAttributes, function(value, attr) { var existingAttributes = Utils._.clone(self.rawAttributes)
self.rawAttributes = {}
Utils._.each(head, function(value, attr) {
self.rawAttributes[attr] = value
})
Utils._.each(existingAttributes, function(value, attr) {
self.rawAttributes[attr] = value
})
Utils._.each(tail, function(value, attr) {
if (Utils._.isUndefined(self.rawAttributes[attr])) { if (Utils._.isUndefined(self.rawAttributes[attr])) {
self.rawAttributes[attr] = value self.rawAttributes[attr] = value
} }
......
...@@ -70,9 +70,8 @@ var validateAttributes = function() { ...@@ -70,9 +70,8 @@ var validateAttributes = function() {
var self = this var self = this
, errors = {} , errors = {}
// for each field and value Utils._.each(this.model.rawAttributes, function(rawAttribute, field) {
Utils._.each(this.model.dataValues, function(value, field) { var value = self.model.dataValues[field] || undefined
var rawAttribute = self.model.rawAttributes[field]
, hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined))) , hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined)))
, isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) === -1 , isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) === -1
......
...@@ -227,7 +227,8 @@ module.exports = (function() { ...@@ -227,7 +227,8 @@ module.exports = (function() {
include:this.options.include, include:this.options.include,
includeNames: this.options.includeNames, includeNames: this.options.includeNames,
includeMap: this.options.includeMap, includeMap: this.options.includeMap,
includeValidated: true includeValidated: true,
raw: true
}) })
}.bind(this)) }.bind(this))
} else if (this.options.hasJoinTableModel === true) { } else if (this.options.hasJoinTableModel === true) {
...@@ -249,7 +250,7 @@ module.exports = (function() { ...@@ -249,7 +250,7 @@ module.exports = (function() {
// Regular queries // Regular queries
} else { } else {
result = results.map(function(result) { result = results.map(function(result) {
return this.callee.build(result, { isNewRecord: false, isDirty: false }) return this.callee.build(result, { isNewRecord: false, isDirty: false, raw: true })
}.bind(this)) }.bind(this))
} }
......
...@@ -175,20 +175,33 @@ module.exports = (function() { ...@@ -175,20 +175,33 @@ module.exports = (function() {
}, },
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var tuples = [] var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = []
, allAttributes = []
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (allAttributes.indexOf(key) === -1) allAttributes.push(key)
})
})
Utils._.forEach(attrValueHashes, function(attrValueHash) { Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
tuples.push("(" + tuples.push("(" +
Utils._.values(attrValueHash).map(function(v) { return this.escape(v) }.bind(this)).join(",") + allAttributes.map(function (key) {
return this.escape(attrValueHash[key])
}.bind(this)).join(",") +
")") ")")
}.bind(this)) }.bind(this))
var table = this.quoteIdentifier(tableName) var replacements = {
var attributes = Object.keys(attrValueHashes[0]).map(function(attr){return this.quoteIdentifier(attr)}.bind(this)).join(",") table: this.quoteIdentifier(tableName),
attributes: allAttributes.map(function(attr){
var query = "INSERT INTO " + table + " (" + attributes + ") VALUES " + tuples.join(",") + ";" return this.quoteIdentifier(attr)
}.bind(this)).join(","),
tuples: tuples
}
return query return Utils._.template(query)(replacements)
}, },
updateQuery: function(tableName, attrValueHash, where, options) { updateQuery: function(tableName, attrValueHash, where, options) {
......
...@@ -293,34 +293,37 @@ module.exports = (function() { ...@@ -293,34 +293,37 @@ module.exports = (function() {
}, },
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
, tuples = [] , tuples = []
, serials = [] , serials = []
, allAttributes = []
Utils._.forEach(attrValueHashes, function(attrValueHash, i) { Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
if (i === 0) { Utils._.forEach(attrValueHash, function(value, key, hash) {
Utils._.forEach(attrValueHash, function(value, key, hash) { if (allAttributes.indexOf(key) === -1) allAttributes.push(key)
if (tables[tableName] && tables[tableName][key]) {
if (['bigserial', 'serial'].indexOf(tables[tableName][key]) !== -1) { if (tables[tableName] && tables[tableName][key]) {
serials.push(key) if (['bigserial', 'serial'].indexOf(tables[tableName][key]) !== -1 && serials.indexOf(key) === -1) {
} serials.push(key)
} }
}) }
} })
})
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
tuples.push("(" + tuples.push("(" +
Utils._.map(attrValueHash, function(value, key){ allAttributes.map(function (key) {
if (serials.indexOf(key) !== -1) { if (serials.indexOf(key) !== -1) {
return value || 'DEFAULT'; return attrValueHash[key] || 'DEFAULT';
} }
return this.escape(value) return this.escape(attrValueHash[key])
}.bind(this)).join(",") + }.bind(this)).join(",") +
")") ")")
}.bind(this)) }.bind(this))
var replacements = { var replacements = {
table: this.quoteIdentifiers(tableName) table: this.quoteIdentifiers(tableName)
, attributes: Object.keys(attrValueHashes[0]).map(function(attr){ , attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr) return this.quoteIdentifier(attr)
}.bind(this)).join(",") }.bind(this)).join(",")
, tuples: tuples.join(",") , tuples: tuples.join(",")
...@@ -516,6 +519,7 @@ module.exports = (function() { ...@@ -516,6 +519,7 @@ module.exports = (function() {
} }
if (dataType.type === "DATETIME") { if (dataType.type === "DATETIME") {
dataType.originalType = "DATETIME"
dataType.type = 'TIMESTAMP WITH TIME ZONE' dataType.type = 'TIMESTAMP WITH TIME ZONE'
} }
......
...@@ -175,18 +175,27 @@ module.exports = (function() { ...@@ -175,18 +175,27 @@ module.exports = (function() {
bulkInsertQuery: function(tableName, attrValueHashes) { bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = [] , tuples = []
, allAttributes = []
Utils._.forEach(attrValueHashes, function(attrValueHash) { Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (allAttributes.indexOf(key) === -1) allAttributes.push(key)
})
})
Utils._.forEach(attrValueHashes, function(attrValueHash, i) {
tuples.push("(" + tuples.push("(" +
Utils._.values(attrValueHash).map(function(value){ allAttributes.map(function (key) {
return this.escape(value) return this.escape(attrValueHash[key])
}.bind(this)).join(",") + }.bind(this)).join(",") +
")") ")")
}.bind(this)) }.bind(this))
var replacements = { var replacements = {
table: this.quoteIdentifier(tableName), table: this.quoteIdentifier(tableName),
attributes: Object.keys(attrValueHashes[0]).map(function(attr){return this.quoteIdentifier(attr)}.bind(this)).join(","), attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr)
}.bind(this)).join(","),
tuples: tuples tuples: tuples
} }
......
...@@ -645,7 +645,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() { ...@@ -645,7 +645,7 @@ describe(Support.getTestDialectTeaser("HasMany"), function() {
self.Task.create({ title: 'task2' }).success(function(task2) { self.Task.create({ title: 'task2' }).success(function(task2) {
user.setTasks([task1, task2]).on('sql', spy).on('sql', _.after(2, function (sql) { user.setTasks([task1, task2]).on('sql', spy).on('sql', _.after(2, function (sql) {
expect(sql).to.have.string("INSERT INTO") expect(sql).to.have.string("INSERT INTO")
expect(sql).to.have.string("VALUES (1,1),(2,1)") expect(sql).to.have.string("VALUES (1,1),(1,2)")
})).success(function () { })).success(function () {
expect(spy.calledTwice).to.be.ok expect(spy.calledTwice).to.be.ok
done() done()
......
...@@ -28,6 +28,7 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() { ...@@ -28,6 +28,7 @@ describe(Support.getTestDialectTeaser("Multiple Level Filters"), function() {
UserId: 1, UserId: 1,
title: 'republic' title: 'republic'
},{ },{
UserId: 2,
title: 'empire' title: 'empire'
}]).success(function() { }]).success(function() {
Task.bulkCreate([{ Task.bulkCreate([{
......
...@@ -265,6 +265,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -265,6 +265,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
foobar: {type: Sequelize.TEXT, defaultValue: 'asd'}, foobar: {type: Sequelize.TEXT, defaultValue: 'asd'},
flag: {type: Sequelize.BOOLEAN, defaultValue: false} flag: {type: Sequelize.BOOLEAN, defaultValue: false}
}) })
expect(Task.build().title).to.equal('a task!') expect(Task.build().title).to.equal('a task!')
expect(Task.build().foo).to.equal(2) expect(Task.build().foo).to.equal(2)
expect(Task.build().bar).to.not.be.ok expect(Task.build().bar).to.not.be.ok
...@@ -548,12 +549,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -548,12 +549,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING }) var User = sequelize.define('User', { username: Sequelize.STRING })
User.sync({ force: true }).success(function() { User.sync({ force: true }).done(function() {
User.create({ username: 'foo' }).success(function() { User.create({ username: 'foo' }).done(function() {
sequelize.transaction(function(t) { sequelize.transaction(function(t) {
User.update({ username: 'bar' }, {}, { transaction: t }).success(function() { User.update({ username: 'bar' }, {}, { transaction: t }).done(function(err) {
User.all().success(function(users1) { User.all().done(function(err, users1) {
User.all({ transaction: t }).success(function(users2) { User.all({ transaction: t }).done(function(err, users2) {
expect(users1[0].username).to.equal('foo') expect(users1[0].username).to.equal('foo')
expect(users2[0].username).to.equal('bar') expect(users2[0].username).to.equal('bar')
t.rollback().success(function(){ done() }) t.rollback().success(function(){ done() })
...@@ -669,7 +670,9 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -669,7 +670,9 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
{ username: 'Bob', secretValue: '43' }] { username: 'Bob', secretValue: '43' }]
this.User.bulkCreate(data).success(function() { this.User.bulkCreate(data).success(function() {
self.User.update({username: 'Bill'}, {secretValue: '42'}).success(function() { self.User.update({username: 'Bill'}, {secretValue: '42'}).done(function(err) {
console.log(err)
expect(err).not.to.be.ok
self.User.findAll({order: 'id'}).success(function(users) { self.User.findAll({order: 'id'}).success(function(users) {
expect(users.length).to.equal(3) expect(users.length).to.equal(3)
......
...@@ -124,7 +124,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -124,7 +124,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
where: { id: 1 }, where: { id: 1 },
attributes: ['id', ['username', 'name']] attributes: ['id', ['username', 'name']]
}).success(function(user) { }).success(function(user) {
expect(user.name).to.equal('barfooz') expect(user.dataValues.name).to.equal('barfooz')
done() done()
}) })
}) })
......
...@@ -153,7 +153,8 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -153,7 +153,8 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}) })
}) })
it("returns false for two empty attributes", function(done) { // In my opinion this is bad logic, null is different from an empty string
xit("returns false for two empty attributes", function(done) {
this.User.create({ username: null }).success(function(user) { this.User.create({ username: null }).success(function(user) {
user.username = '' user.username = ''
expect(user.isDirty).to.be.false expect(user.isDirty).to.be.false
...@@ -618,10 +619,12 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -618,10 +619,12 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
include: [Page] include: [Page]
}).success(function(leBook) { }).success(function(leBook) {
page.updateAttributes({ content: 'something totally different' }).success(function(page) { page.updateAttributes({ content: 'something totally different' }).success(function(page) {
expect(leBook.pages.length).to.equal(1)
expect(leBook.pages[0].content).to.equal('om nom nom') expect(leBook.pages[0].content).to.equal('om nom nom')
expect(page.content).to.equal('something totally different') expect(page.content).to.equal('something totally different')
leBook.reload().success(function(leBook) { leBook.reload().success(function(leBook) {
expect(leBook.pages.length).to.equal(1)
expect(leBook.pages[0].content).to.equal('something totally different') expect(leBook.pages[0].content).to.equal('something totally different')
expect(page.content).to.equal('something totally different') expect(page.content).to.equal('something totally different')
done() done()
...@@ -792,15 +795,13 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -792,15 +795,13 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
// timeout is needed, in order to check the update of the timestamp // timeout is needed, in order to check the update of the timestamp
var build = function(callback) { var build = function(callback) {
user = User.build({ username: 'user' }) user = User.build({ username: 'user' })
updatedAt = user.updatedAt
expect(updatedAt.getTime()).to.be.above(now) var save = user.save()
setTimeout(function() { save.success(function() {
user.save().success(function() { expect(now).to.be.below(user.updatedAt.getTime())
expect(updatedAt.getTime()).to.be.below(user.updatedAt.getTime()) callback()
callback() })
})
}, 1000)
} }
// closures are fun :) // closures are fun :)
...@@ -1079,7 +1080,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -1079,7 +1080,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
it('returns a response that can be stringified', function(done) { it('returns a response that can be stringified', function(done) {
var user = this.User.build({ username: 'test.user', age: 99, isAdmin: true }) var user = this.User.build({ username: 'test.user', age: 99, isAdmin: true })
expect(JSON.stringify(user)).to.deep.equal('{"id":null,"username":"test.user","age":99,"isAdmin":true}') expect(JSON.stringify(user)).to.deep.equal('{"username":"test.user","age":99,"isAdmin":true,"id":null}')
done() done()
}) })
...@@ -1235,7 +1236,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -1235,7 +1236,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
this.ParanoidUser.create({ username: 'fnord' }).success(function() { this.ParanoidUser.create({ username: 'fnord' }).success(function() {
self.ParanoidUser.findAll().success(function(users) { self.ParanoidUser.findAll().success(function(users) {
users[0].updateAttributes({username: 'newFnord'}).success(function(user) { users[0].updateAttributes({username: 'newFnord'}).success(function(user) {
expect(user.deletedAt).to.be.null expect(user.deletedAt).not.to.exist
done() done()
}) })
}) })
...@@ -1248,7 +1249,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -1248,7 +1249,7 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
self.ParanoidUser.findAll().success(function(users) { self.ParanoidUser.findAll().success(function(users) {
self.ParanoidUser.create({ username: 'linkedFnord' }).success(function(linkedUser) { self.ParanoidUser.create({ username: 'linkedFnord' }).success(function(linkedUser) {
users[0].setParanoidUser( linkedUser ).success(function(user) { users[0].setParanoidUser( linkedUser ).success(function(user) {
expect(user.deletedAt).to.be.null expect(user.deletedAt).not.to.exist
done() done()
}) })
}) })
......
/* jshint camelcase: false */
var chai = require('chai')
, Sequelize = require('../../index')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, dialect = Support.getTestDialect()
, config = require(__dirname + "/../config/config")
, sinon = require('sinon')
, datetime = require('chai-datetime')
, uuid = require('node-uuid')
, _ = require('lodash')
chai.use(datetime)
chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("DAO"), function () {
describe('Values', function () {
describe('set', function () {
it('doesn\'t overwrite primary keys', function () {
var User = this.sequelize.define('User', {
identifier: {type: DataTypes.STRING, primaryKey: true}
})
var user = User.build({identifier: 'identifier'})
expect(user.get('identifier')).to.equal('identifier')
user.set('identifier', 'another identifier')
expect(user.get('identifier')).to.equal('identifier')
})
it('doesn\'t set timestamps', function () {
var User = this.sequelize.define('User', {
identifier: {type: DataTypes.STRING, primaryKey: true}
})
var user = User.build()
user.set({
createdAt: new Date(2000, 1, 1),
updatedAt: new Date(2000, 1, 1)
})
expect(user.get('createdAt')).not.to.be.ok
expect(user.get('updatedAt')).not.to.be.ok
})
describe('includes', function () {
it('should support basic includes', function () {
var Product = this.sequelize.define('Product', {
title: Sequelize.STRING
})
var Tag = this.sequelize.define('Tag', {
name: Sequelize.STRING
})
var User = this.sequelize.define('User', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
})
Product.hasMany(Tag)
Product.belongsTo(User)
var product
product = Product.build({}, {
include: [
User,
Tag
]
})
product.set({
id: 1,
title: 'Chair',
tags: [
{id: 1, name: 'Alpha'},
{id: 2, name: 'Beta'}
],
user: {
id: 1,
first_name: 'Mick',
last_name: 'Hansen'
}
})
expect(product.tags).to.be.ok
expect(product.tags.length).to.equal(2)
expect(product.tags[0].Model).to.equal(Tag)
expect(product.user).to.be.ok
expect(product.user.Model).to.equal(User)
})
it('should support basic includes (with raw: true)', function () {
var Product = this.sequelize.define('Product', {
title: Sequelize.STRING
})
var Tag = this.sequelize.define('Tag', {
name: Sequelize.STRING
})
var User = this.sequelize.define('User', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
})
Product.hasMany(Tag)
Product.belongsTo(User)
var product
product = Product.build({}, {
include: [
User,
Tag
]
})
product.set({
id: 1,
title: 'Chair',
tags: [
{id: 1, name: 'Alpha'},
{id: 2, name: 'Beta'}
],
user: {
id: 1,
first_name: 'Mick',
last_name: 'Hansen'
}
}, {raw: true})
expect(product.tags).to.be.ok
expect(product.tags.length).to.equal(2)
expect(product.tags[0].Model).to.equal(Tag)
expect(product.user).to.be.ok
expect(product.user.Model).to.equal(User)
})
})
})
describe('get', function () {
it('should use custom attribute getters in get(key)', function () {
var Product = this.sequelize.define('Product', {
price: {
type: Sequelize.FLOAT,
get: function() {
return this.dataValues['price'] * 100
}
}
})
var product = Product.build({
price: 10
})
expect(product.get('price')).to.equal(1000)
})
it('should custom virtual getters in get(key)', function () {
var Product = this.sequelize.define('Product', {
priceInCents: {
type: Sequelize.FLOAT
}
}, {
getterMethods: {
price: function() {
return this.dataValues['priceInCents'] / 100
}
}
})
var product = Product.build({
priceInCents: 1000
})
expect(product.get('price')).to.equal(10)
})
it('should use custom getters in toJSON', function () {
var Product = this.sequelize.define('Product', {
price: {
type: Sequelize.STRING,
get: function() {
return this.dataValues['price'] * 100
}
}
}, {
getterMethods: {
withTaxes: function() {
return this.get('price') * 1.25
}
}
})
var product = Product.build({
price: 10
})
expect(product.toJSON()).to.deep.equal({withTaxes: 1250, price: 1000, id: null})
})
})
describe('changed', function () {
it('should return false if object was built from database', function (done) {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
User.sync().done(function (err) {
User.create({name: 'Jan Meier'}).done(function (err, user) {
expect(err).not.to.be.ok
expect(user.changed('name')).to.be.false
expect(user.changed()).not.to.be.ok
expect(user.isDirty).to.be.false
done()
});
})
})
it('should return true if previous value is different', function () {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
var user = User.build({
name: 'Jan Meier'
})
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.true
expect(user.changed()).to.be.ok
expect(user.isDirty).to.be.true
})
it('should return false immediately after saving', function (done) {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
User.sync().done(function (err) {
var user = User.build({
name: 'Jan Meier'
})
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.true
expect(user.changed()).to.be.ok
expect(user.isDirty).to.be.true
user.save().done(function (err) {
expect(err).not.to.be.ok
expect(user.changed('name')).to.be.false
expect(user.changed()).not.to.be.ok
expect(user.isDirty).to.be.false
done()
})
})
})
it('setting the same value twice should not impact the result', function () {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
var user = User.build({
name: 'Jan Meier'
})
user.set('name', 'Mick Hansen')
user.set('name', 'Mick Hansen')
expect(user.changed('name')).to.be.true
expect(user.changed()).to.be.ok
expect(user.isDirty).to.be.true
expect(user.previous('name')).to.equal('Jan Meier')
})
})
describe('previous', function () {
it('should return the previous value', function () {
var User = this.sequelize.define('User', {
name: {type: DataTypes.STRING}
})
var user = User.build({
name: 'Jan Meier'
})
user.set('name', 'Mick Hansen')
expect(user.previous('name')).to.equal('Jan Meier')
expect(user.get('name')).to.equal('Mick Hansen')
})
})
})
})
\ No newline at end of file
...@@ -23,6 +23,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -23,6 +23,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
}, { }, {
hooks: { hooks: {
beforeValidate: function(user, fn) { beforeValidate: function(user, fn) {
console.log("beforeValidate")
user.mood = 'happy' user.mood = 'happy'
fn(null, user) fn(null, user)
}, },
...@@ -111,7 +112,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -111,7 +112,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
self.User.all().success(function(users) { self.User.all().success(function(users) {
expect(users[0].mood).to.equal('happy') expect(users[0].mood).to.equal('happy')
expect(users[0].mood).to.equal('happy') expect(users[1].mood).to.equal('happy')
done() done()
}) })
}) })
...@@ -1105,7 +1106,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -1105,7 +1106,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
}) })
describe('#create', function() { describe('#create', function() {
describe('via deifne', function() { describe('via define', function() {
describe('on success', function() { describe('on success', function() {
describe('with a single hook', function() { describe('with a single hook', function() {
it('should return the user from the callback', function(done) { it('should return the user from the callback', function(done) {
...@@ -1869,7 +1870,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -1869,7 +1870,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
}) })
describe('#updateAttributes', function() { describe('#updateAttributes', function() {
describe('via deifne', function() { describe('via define', function() {
describe('on success', function() { describe('on success', function() {
describe('with a single hook', function() { describe('with a single hook', function() {
it('should return the user from the callback', function(done) { it('should return the user from the callback', function(done) {
...@@ -1987,7 +1988,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -1987,7 +1988,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
User.sync({ force: true }).success(function() { User.sync({ force: true }).success(function() {
User.create({username: 'Cheech', mood: 'sad'}).success(function(user) { User.create({username: 'Cheech', mood: 'sad'}).success(function(user) {
user.updateAttributes({username: 'Chong'}).success(function(user) { user.updateAttributes({username: 'Chong'}).done(function(err, user) {
expect(user.username).to.equal('Chong') expect(user.username).to.equal('Chong')
expect(user.mood).to.equal('happy') expect(user.mood).to.equal('happy')
expect(beforeHook).to.be.true expect(beforeHook).to.be.true
......
...@@ -365,9 +365,11 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -365,9 +365,11 @@ describe(Support.getTestDialectTeaser("Include"), function () {
ranks: function(callback) { ranks: function(callback) {
Rank.bulkCreate([ Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1}, {name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1} {name: 'Member', canInvite: 1, canRemove: 0}
]).done(function () { ]).done(function () {
Rank.findAll().done(callback) Rank.findAll().done(callback)
}).on('sql', function (sql) {
console.log(sql)
}) })
}, },
memberships: ['user', 'groups', 'ranks', function (callback, results) { memberships: ['user', 'groups', 'ranks', function (callback, results) {
...@@ -579,7 +581,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -579,7 +581,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
ranks: function(callback) { ranks: function(callback) {
Rank.bulkCreate([ Rank.bulkCreate([
{name: 'Admin', canInvite: 1, canRemove: 1}, {name: 'Admin', canInvite: 1, canRemove: 1},
{name: 'Member', canInvite: 1} {name: 'Member', canInvite: 1, canRemove: 0}
]).done(function () { ]).done(function () {
Rank.findAll().done(callback) Rank.findAll().done(callback)
}) })
......
...@@ -339,7 +339,7 @@ if (Support.dialectIsMySQL()) { ...@@ -339,7 +339,7 @@ if (Support.dialectIsMySQL()) {
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1),('bar',2);" expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1),('bar',2);"
}, { }, {
arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: null}, {name: 'bar', nullValue: null}]], arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: null}, {name: 'bar', nullValue: null}]],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',NULL);" expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',NULL,NULL);"
}, { }, {
arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: null}, {name: 'bar', foo: 2, nullValue: null}]], arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: null}, {name: 'bar', foo: 2, nullValue: null}]],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);", expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);",
...@@ -350,7 +350,7 @@ if (Support.dialectIsMySQL()) { ...@@ -350,7 +350,7 @@ if (Support.dialectIsMySQL()) {
context: {options: {omitNull: true}} // Note: We don't honour this because it makes little sense when some rows may have nulls and others not context: {options: {omitNull: true}} // Note: We don't honour this because it makes little sense when some rows may have nulls and others not
}, { }, {
arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: undefined}, {name: 'bar', foo: 2, undefinedValue: undefined}]], arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: undefined}, {name: 'bar', foo: 2, undefinedValue: undefined}]],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);", expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`,`undefinedValue`) VALUES ('foo',1,NULL,NULL),('bar',2,NULL,NULL);",
context: {options: {omitNull: true}} // Note: As above context: {options: {omitNull: true}} // Note: As above
}, { }, {
arguments: ['myTable', [{name: "foo", value: true}, {name: 'bar', value: false}]], arguments: ['myTable', [{name: "foo", value: true}, {name: 'bar', value: false}]],
......
...@@ -124,7 +124,7 @@ if (dialect === 'sqlite') { ...@@ -124,7 +124,7 @@ if (dialect === 'sqlite') {
}) })
}) })
it("should make aliased attributes available", function(done) { xit("should make aliased attributes available", function(done) {
this.User.find({ where: { name:'user' }, attributes: ['id', ['name', 'username']] }).success(function(user) { this.User.find({ where: { name:'user' }, attributes: ['id', ['name', 'username']] }).success(function(user) {
expect(user.username).to.equal('user') expect(user.username).to.equal('user')
done() done()
......
...@@ -20,19 +20,20 @@ if (dialect === 'sqlite') { ...@@ -20,19 +20,20 @@ if (dialect === 'sqlite') {
describe('findAll', function() { describe('findAll', function() {
it("handles dates correctly", function(done) { it("handles dates correctly", function(done) {
var self = this var self = this
, user = this.User.build({ username: 'user' })
this.User user.dataValues['createdAt'] = new Date(2011, 04, 04)
.create({ username: 'user', createdAt: new Date(2011, 04, 04) })
.success(function() { user.save().success(function() {
self.User.create({ username: 'new user' }).success(function() { self.User.create({ username: 'new user' }).success(function() {
self.User.findAll({ self.User.findAll({
where: ['createdAt > ?', new Date(2012, 01, 01)] where: ['createdAt > ?', new Date(2012, 01, 01)]
}).success(function(users) { }).success(function(users) {
expect(users).to.have.length(1) expect(users).to.have.length(1)
done() done()
})
}) })
}) })
})
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!