Skip to content

Commit 31ded2e

Browse files
committed
Merge pull request #618 from dresende/some-fixes
Fix postgres date crash & test. io.js + 0.12 support
2 parents 93eb6d1 + 2ec139f commit 31ded2e

File tree

6 files changed

+62
-17
lines changed

6 files changed

+62
-17
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
language: node_js
22
node_js:
3-
- '0.8'
43
- '0.10'
4+
- '0.12'
5+
- 'iojs-v1.5.1'
56
before_script:
67
- mysql -e 'create database orm_test;'
78
- psql -c 'create database orm_test;' -U postgres

lib/Drivers/DML/postgres.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,12 @@ Driver.prototype.valueToProperty = function (value, property) {
264264
}
265265
break;
266266
case "date":
267-
if (this.config.timezone && this.config.timezone != 'local') {
267+
if (_.isDate(value) && this.config.timezone && this.config.timezone != 'local') {
268268
var tz = convertTimezone(this.config.timezone);
269269

270270
// shift local to UTC
271271
value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000));
272+
272273
if (tz !== false) {
273274
// shift UTC to timezone
274275
value.setTime(value.getTime() - (tz * 60000));
@@ -320,7 +321,7 @@ Driver.prototype.propertyToValue = function (value, property) {
320321
}
321322
break;
322323
case "date":
323-
if (this.config.timezone && this.config.timezone != 'local') {
324+
if (_.isDate(value) && this.config.timezone && this.config.timezone != 'local') {
324325
var tz = convertTimezone(this.config.timezone);
325326

326327
// shift local to UTC

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
"lodash" : "2.4.1"
4444
},
4545
"devDependencies": {
46-
"mysql" : "2.0.0-alpha9",
47-
"pg" : "2.6.2",
48-
"sqlite3" : "2.1.7",
46+
"mysql" : "2.5.5",
47+
"pg" : "4.3.0",
48+
"sqlite3" : "3.0.5",
4949
"async" : "0.9.0",
5050
"mocha" : "1.13.0",
5151
"should" : "1.2.2",

test/integration/association-hasmany.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ describe("hasMany", function () {
778778
should.equal(data[0].name, 'account_id');
779779
should.equal(data[0].pk, 1);
780780
should.equal(data[1].name, 'emails_text');
781-
should.equal(data[1].pk, 1);
781+
should.equal(data[1].pk, 2);
782782

783783
done();
784784
});

test/integration/drivers/postgres_spec.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("Postgres driver", function() {
1010
describe("#valueToProperty", function () {
1111
var driver = null;
1212

13-
before(function () {
13+
beforeEach(function () {
1414
driver = new Driver({}, {}, {});
1515
});
1616

@@ -72,6 +72,35 @@ describe("Postgres driver", function() {
7272
should.strictEqual(valueToProperty('1.200 '), 1);
7373
});
7474
});
75+
76+
describe("dates with non local timezone", function () {
77+
beforeEach(function () {
78+
driver = new Driver({ timezone: 'Z' }, {}, {});
79+
});
80+
81+
function valueToProperty (value) {
82+
return driver.valueToProperty(value, { type: 'date' });
83+
}
84+
85+
it("should accept null", function () {
86+
should.strictEqual(valueToProperty(null), null);
87+
});
88+
89+
it("should work", function () {
90+
should.strictEqual(_.isDate(valueToProperty(new Date())), true);
91+
});
92+
93+
describe("calculations", function () {
94+
it("should offset time, relative to timezone", function () {
95+
d = new Date();
96+
97+
expected = d.getTime() - d.getTimezoneOffset() * 60000;
98+
converted = valueToProperty(d).getTime();
99+
100+
should.equal(converted, expected);
101+
});
102+
});
103+
});
75104
});
76105
});
77106

@@ -116,9 +145,24 @@ describe("Postgres driver", function() {
116145
should.equal(inputStr, out.toString());
117146
});
118147

119-
it("should offset time by specified timezone amount for + timezones");
148+
it("should work with null dates", function () {
149+
should.strictEqual(evaluate(null, { config: { timezone: 'Z' }}), null);
150+
});
151+
152+
it("should work with date objects", function () {
153+
should.strictEqual(_.isDate(evaluate(new Date(), { config: { timezone: 'Z' }})), true);
154+
});
155+
156+
describe("calculations", function () {
157+
it("should offset time, relative to timezone", function () {
158+
d = new Date();
159+
160+
expected = d.getTime() + d.getTimezoneOffset() * 60000;
161+
converted = evaluate(d, { config: { timezone: 'Z' }}).getTime();
120162

121-
it("should offset time by specified timezone amount for + timezones");
163+
should.equal(converted, expected);
164+
});
165+
});
122166
});
123167

124168
describe("type point", function () {

test/integration/validation.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ describe("Validations", function() {
421421
should(Array.isArray(err));
422422
should.equal(err.length, 2);
423423

424-
should.deepEqual(err[0], _.extend(new Error(),{
425-
property: 'name', value: 'n', msg: 'out-of-range-length'
424+
should.deepEqual(err[0], _.extend(new Error('out-of-range-length'), {
425+
property: 'name', value: 'n', msg: 'out-of-range-length', type: 'validation'
426426
}));
427427

428428
should.deepEqual(err[1], _.extend(new Error(),{
429-
property: 'height', value: '4', msg: 'out-of-range-number'
429+
property: 'height', value: '4', msg: 'out-of-range-number', type: 'validation'
430430
}));
431431

432432
should.equal(john.id, null);
@@ -447,17 +447,16 @@ describe("Validations", function() {
447447
should(Array.isArray(err));
448448
should.equal(err.length, 3);
449449

450-
// `type` is a non enumerable undocumented property of `Error` in V8.
451450
should.deepEqual(err[0], _.extend(new Error(),{
452-
property: 'name', value: null, msg: 'required'
451+
property: 'name', value: null, msg: 'required', type: 'validation'
453452
}));
454453

455454
should.deepEqual(err[1], _.extend(new Error(),{
456-
property: 'name', value: null, msg: 'undefined'
455+
property: 'name', value: null, msg: 'undefined', type: 'validation'
457456
}));
458457

459458
should.deepEqual(err[2], _.extend(new Error(),{
460-
property: 'height', value: '4', msg: 'out-of-range-number'
459+
property: 'height', value: '4', msg: 'out-of-range-number', type: 'validation'
461460
}));
462461

463462
should.equal(john.id, null);

0 commit comments

Comments
 (0)