From da8b86cd4f00dad625cf14b79b4885d3bd8e8275 Mon Sep 17 00:00:00 2001 From: Arek W Date: Wed, 18 Mar 2015 15:45:26 +1100 Subject: [PATCH 1/2] Don't choke on null dates with timezone set --- lib/Drivers/DML/postgres.js | 5 ++- test/integration/drivers/postgres_spec.js | 50 +++++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/lib/Drivers/DML/postgres.js b/lib/Drivers/DML/postgres.js index 6bd6cf48..49748411 100644 --- a/lib/Drivers/DML/postgres.js +++ b/lib/Drivers/DML/postgres.js @@ -264,11 +264,12 @@ Driver.prototype.valueToProperty = function (value, property) { } break; case "date": - if (this.config.timezone && this.config.timezone != 'local') { + if (_.isDate(value) && this.config.timezone && this.config.timezone != 'local') { var tz = convertTimezone(this.config.timezone); // shift local to UTC value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000)); + if (tz !== false) { // shift UTC to timezone value.setTime(value.getTime() - (tz * 60000)); @@ -320,7 +321,7 @@ Driver.prototype.propertyToValue = function (value, property) { } break; case "date": - if (this.config.timezone && this.config.timezone != 'local') { + if (_.isDate(value) && this.config.timezone && this.config.timezone != 'local') { var tz = convertTimezone(this.config.timezone); // shift local to UTC diff --git a/test/integration/drivers/postgres_spec.js b/test/integration/drivers/postgres_spec.js index 6cca07d7..db84d69f 100644 --- a/test/integration/drivers/postgres_spec.js +++ b/test/integration/drivers/postgres_spec.js @@ -10,7 +10,7 @@ describe("Postgres driver", function() { describe("#valueToProperty", function () { var driver = null; - before(function () { + beforeEach(function () { driver = new Driver({}, {}, {}); }); @@ -72,6 +72,35 @@ describe("Postgres driver", function() { should.strictEqual(valueToProperty('1.200 '), 1); }); }); + + describe("dates with non local timezone", function () { + beforeEach(function () { + driver = new Driver({ timezone: 'Z' }, {}, {}); + }); + + function valueToProperty (value) { + return driver.valueToProperty(value, { type: 'date' }); + } + + it("should accept null", function () { + should.strictEqual(valueToProperty(null), null); + }); + + it("should work", function () { + should.strictEqual(_.isDate(valueToProperty(new Date())), true); + }); + + describe("calculations", function () { + it("should offset time, relative to timezone", function () { + d = new Date(); + + expected = d.getTime() - d.getTimezoneOffset() * 60000; + converted = valueToProperty(d).getTime(); + + should.equal(converted, expected); + }); + }); + }); }); }); @@ -116,9 +145,24 @@ describe("Postgres driver", function() { should.equal(inputStr, out.toString()); }); - it("should offset time by specified timezone amount for + timezones"); + it("should work with null dates", function () { + should.strictEqual(evaluate(null, { config: { timezone: 'Z' }}), null); + }); + + it("should work with date objects", function () { + should.strictEqual(_.isDate(evaluate(new Date(), { config: { timezone: 'Z' }})), true); + }); + + describe("calculations", function () { + it("should offset time, relative to timezone", function () { + d = new Date(); + + expected = d.getTime() + d.getTimezoneOffset() * 60000; + converted = evaluate(d, { config: { timezone: 'Z' }}).getTime(); - it("should offset time by specified timezone amount for + timezones"); + should.equal(converted, expected); + }); + }); }); describe("type point", function () { From 2ec139f6d54d1398ec75ae73279cd0c9a2c734f6 Mon Sep 17 00:00:00 2001 From: Arek W Date: Wed, 18 Mar 2015 15:48:59 +1100 Subject: [PATCH 2/2] io.js + 0.12 Drop travis 0.8 tests due to sqlite 3.0 incompatibility. ORM still works on 0.8, just not on travis. --- .travis.yml | 3 ++- package.json | 6 +++--- test/integration/association-hasmany.js | 2 +- test/integration/validation.js | 13 ++++++------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index 268973d6..c2d4d525 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,8 @@ language: node_js node_js: - - '0.8' - '0.10' + - '0.12' + - 'iojs-v1.5.1' before_script: - mysql -e 'create database orm_test;' - psql -c 'create database orm_test;' -U postgres diff --git a/package.json b/package.json index 712d6e11..a88b013b 100644 --- a/package.json +++ b/package.json @@ -43,9 +43,9 @@ "lodash" : "2.4.1" }, "devDependencies": { - "mysql" : "2.0.0-alpha9", - "pg" : "2.6.2", - "sqlite3" : "2.1.7", + "mysql" : "2.5.5", + "pg" : "4.3.0", + "sqlite3" : "3.0.5", "async" : "0.9.0", "mocha" : "1.13.0", "should" : "1.2.2", diff --git a/test/integration/association-hasmany.js b/test/integration/association-hasmany.js index 3f937c69..167d79d8 100644 --- a/test/integration/association-hasmany.js +++ b/test/integration/association-hasmany.js @@ -778,7 +778,7 @@ describe("hasMany", function () { should.equal(data[0].name, 'account_id'); should.equal(data[0].pk, 1); should.equal(data[1].name, 'emails_text'); - should.equal(data[1].pk, 1); + should.equal(data[1].pk, 2); done(); }); diff --git a/test/integration/validation.js b/test/integration/validation.js index a5c4692e..604a4c3e 100644 --- a/test/integration/validation.js +++ b/test/integration/validation.js @@ -421,12 +421,12 @@ describe("Validations", function() { should(Array.isArray(err)); should.equal(err.length, 2); - should.deepEqual(err[0], _.extend(new Error(),{ - property: 'name', value: 'n', msg: 'out-of-range-length' + should.deepEqual(err[0], _.extend(new Error('out-of-range-length'), { + property: 'name', value: 'n', msg: 'out-of-range-length', type: 'validation' })); should.deepEqual(err[1], _.extend(new Error(),{ - property: 'height', value: '4', msg: 'out-of-range-number' + property: 'height', value: '4', msg: 'out-of-range-number', type: 'validation' })); should.equal(john.id, null); @@ -447,17 +447,16 @@ describe("Validations", function() { should(Array.isArray(err)); should.equal(err.length, 3); - // `type` is a non enumerable undocumented property of `Error` in V8. should.deepEqual(err[0], _.extend(new Error(),{ - property: 'name', value: null, msg: 'required' + property: 'name', value: null, msg: 'required', type: 'validation' })); should.deepEqual(err[1], _.extend(new Error(),{ - property: 'name', value: null, msg: 'undefined' + property: 'name', value: null, msg: 'undefined', type: 'validation' })); should.deepEqual(err[2], _.extend(new Error(),{ - property: 'height', value: '4', msg: 'out-of-range-number' + property: 'height', value: '4', msg: 'out-of-range-number', type: 'validation' })); should.equal(john.id, null);