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

Commit f8d0eabf by Robert Scheinpflug Committed by Sushant

fix instance.set '1970-01-01' to null field (#6853)

1 parent 1fcd126b
Showing with 45 additions and 12 deletions
...@@ -3101,11 +3101,13 @@ class Model { ...@@ -3101,11 +3101,13 @@ class Model {
if (!(value instanceof Date)) { if (!(value instanceof Date)) {
value = new Date(value); value = new Date(value);
} }
if (!(originalValue instanceof Date)) { if (originalValue) {
originalValue = new Date(originalValue); if (!(originalValue instanceof Date)) {
} originalValue = new Date(originalValue);
if (originalValue && value.getTime() === originalValue.getTime()) { }
return this; if (value.getTime() === originalValue.getTime()) {
return this;
}
} }
} }
} }
......
'use strict'; 'use strict';
/* jshint -W030 */ /* jshint -W030 */
var chai = require('chai') const chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types') , DataTypes = require(__dirname + '/../../../lib/data-types')
...@@ -10,10 +10,10 @@ var chai = require('chai') ...@@ -10,10 +10,10 @@ var chai = require('chai')
describe(Support.getTestDialectTeaser('Instance'), function() { describe(Support.getTestDialectTeaser('Instance'), function() {
describe('set', function () { describe('set', function () {
it('sets nested keys in JSON objects', function () { it('sets nested keys in JSON objects', function () {
var User = current.define('User', { const User = current.define('User', {
meta: DataTypes.JSONB meta: DataTypes.JSONB
}); });
var user = User.build({ const user = User.build({
meta: { meta: {
location: 'Stockhollm' location: 'Stockhollm'
} }
...@@ -22,7 +22,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -22,7 +22,7 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
raw: true raw: true
}); });
var meta = user.get('meta'); const meta = user.get('meta');
user.set('meta.location', 'Copenhagen'); user.set('meta.location', 'Copenhagen');
expect(user.dataValues['meta.location']).not.to.be.ok; expect(user.dataValues['meta.location']).not.to.be.ok;
...@@ -32,17 +32,48 @@ describe(Support.getTestDialectTeaser('Instance'), function() { ...@@ -32,17 +32,48 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
}); });
it('doesnt mutate the JSONB defaultValue', function() { it('doesnt mutate the JSONB defaultValue', function() {
var User = current.define('User', { const User = current.define('User', {
meta: { meta: {
type: DataTypes.JSONB, type: DataTypes.JSONB,
allowNull: false, allowNull: false,
defaultValue: {} defaultValue: {}
} }
}); });
var user1 = User.build({}); const user1 = User.build({});
user1.set('meta.location', 'Stockhollm'); user1.set('meta.location', 'Stockhollm');
var user2 = User.build({}); const user2 = User.build({});
expect(user2.get('meta')).to.deep.equal({}); expect(user2.get('meta')).to.deep.equal({});
}); });
it('sets the date "1970-01-01" to previously null field', function() {
const User = current.define('User', {
date: {
type: DataTypes.DATE,
allowNull: true
}
});
const user1 = User.build({
date: null
});
user1.set('date', '1970-01-01');
expect(user1.get('date')).to.be.ok;
expect(user1.get('date').getTime()).to.equal(new Date('1970-01-01').getTime());
});
it('overwrites non-date originalValue with date', function() {
const User = current.define('User', {
date: DataTypes.DATE
});
const user = User.build({
date: ' '
}, {
isNewRecord: false,
raw: true
});
user.set('date', new Date());
expect(user.get('date')).to.be.an.instanceof(Date);
expect(user.get('date')).not.to.be.NaN;
});
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!