Skip to content

Commit

Permalink
added tests for new timestamp feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sdepold committed Jan 28, 2013
2 parents a26450b + 31a05e2 commit c739f1a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/dao-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ module.exports = (function() {
}

DAOFactory.prototype.build = function(values, options) {
options = options || {}
options = options || {isNewRecord: true}

var self = this
, instance = new this.DAO(values, this.options)
, instance = new this.DAO(values, this.options, options.isNewRecord)

instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true
instance.isNewRecord = options.isNewRecord

return instance
}
Expand Down
8 changes: 4 additions & 4 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ var Utils = require("./utils")
, DataTypes = require("./data-types")

module.exports = (function() {
var DAO = function(values, options) {
var DAO = function(values, options, isNewRecord) {
var self = this;
this.__options = options;
this.hasPrimaryKeys = options.hasPrimaryKeys;
this.selectedValues = values;

initAttributes.call(this, values)
initAttributes.call(this, values, isNewRecord)

if (this.hasDefaultValues) {
Utils._.each(this.defaultValues, function (value, name) {
Expand Down Expand Up @@ -271,7 +271,7 @@ module.exports = (function() {

// private

var initAttributes = function(values) {
var initAttributes = function(values, isNewRecord) {
// add all passed values to the dao and store the attribute names in this.attributes
for (var key in values) {
if (values.hasOwnProperty(key)) {
Expand All @@ -283,7 +283,7 @@ module.exports = (function() {
// a newly created dao has no id
var defaults = this.hasPrimaryKeys ? {} : { id: null }

if (this.__options.timestamps) {
if (this.__options.timestamps && isNewRecord) {
defaults[this.__options.underscored ? 'created_at' : 'createdAt'] = new Date()
defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date()

Expand Down
20 changes: 20 additions & 0 deletions spec/dao.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,25 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
})
})
})

it("returns the timestamps if no attributes have been specified", function(done) {
this.User.create({ username: 'fnord' }).success(function() {
this.User.findAll().success(function(users) {
expect(users[0].createdAt).toBeDefined()
done()
}.bind(this))
}.bind(this))
})

it("does not return the timestamps if the username attribute has been specified", function(done) {
this.User.create({ username: 'fnord' }).success(function() {
this.User.findAll({ attributes: ['username'] }).success(function(users) {
expect(users[0].createdAt).not.toBeDefined()
expect(users[0].username).toBeDefined()

done()
}.bind(this))
}.bind(this))
})
})
})

0 comments on commit c739f1a

Please sign in to comment.