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

Commit c739f1a6 by Sascha Depold

added tests for new timestamp feature

2 parents a26450bb 31a05e22
...@@ -255,12 +255,12 @@ module.exports = (function() { ...@@ -255,12 +255,12 @@ module.exports = (function() {
} }
DAOFactory.prototype.build = function(values, options) { DAOFactory.prototype.build = function(values, options) {
options = options || {} options = options || {isNewRecord: true}
var self = this 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 return instance
} }
......
...@@ -4,13 +4,13 @@ var Utils = require("./utils") ...@@ -4,13 +4,13 @@ var Utils = require("./utils")
, DataTypes = require("./data-types") , DataTypes = require("./data-types")
module.exports = (function() { module.exports = (function() {
var DAO = function(values, options) { var DAO = function(values, options, isNewRecord) {
var self = this; var self = this;
this.__options = options; this.__options = options;
this.hasPrimaryKeys = options.hasPrimaryKeys; this.hasPrimaryKeys = options.hasPrimaryKeys;
this.selectedValues = values; this.selectedValues = values;
initAttributes.call(this, values) initAttributes.call(this, values, isNewRecord)
if (this.hasDefaultValues) { if (this.hasDefaultValues) {
Utils._.each(this.defaultValues, function (value, name) { Utils._.each(this.defaultValues, function (value, name) {
...@@ -271,7 +271,7 @@ module.exports = (function() { ...@@ -271,7 +271,7 @@ module.exports = (function() {
// private // 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 // add all passed values to the dao and store the attribute names in this.attributes
for (var key in values) { for (var key in values) {
if (values.hasOwnProperty(key)) { if (values.hasOwnProperty(key)) {
...@@ -283,7 +283,7 @@ module.exports = (function() { ...@@ -283,7 +283,7 @@ module.exports = (function() {
// a newly created dao has no id // a newly created dao has no id
var defaults = this.hasPrimaryKeys ? {} : { id: null } 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 ? 'created_at' : 'createdAt'] = new Date()
defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date() defaults[this.__options.underscored ? 'updated_at' : 'updatedAt'] = new Date()
......
...@@ -148,5 +148,25 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() { ...@@ -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))
})
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!