Skip to content

Commit faafc7c

Browse files
author
Kevin Delisle
committed
Tests for datetime types
1 parent cee6303 commit faafc7c

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

test/datetime.test.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright IBM Corp. 2012,2016. All Rights Reserved.
2+
// Node module: loopback-connector-mysql
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
'use strict';
7+
8+
var DateString = require('../node_modules/loopback-datasource-juggler/lib/date-string');
9+
var fmt = require('util').format;
10+
var should = require('./init.js');
11+
12+
var db, Order, Person;
13+
describe('MySQL datetime handling', function() {
14+
var personDefinition = {
15+
name: String,
16+
gender: String,
17+
married: Boolean,
18+
dob: DateString,
19+
createdAt: {type: Date, default: Date},
20+
};
21+
22+
// Modifying the connection timezones mid-flight is a pain,
23+
// but closing the existing connection requires more effort.
24+
function setConnectionTimezones(tz) {
25+
db.connector.client._allConnections.forEach(function(con) {
26+
con.config.timezone = tz;
27+
});
28+
}
29+
before(function(done) {
30+
db = getSchema({
31+
dateStrings: true,
32+
});
33+
Person = db.define('Person', personDefinition, {forceId: true, strict: true});
34+
db.automigrate(['Person'], done);
35+
});
36+
37+
beforeEach(function() {
38+
setConnectionTimezones('Z');
39+
});
40+
after(function(done) {
41+
Person.destroyAll(function(err) {
42+
db.disconnect(function() {
43+
return done(err);
44+
});
45+
});
46+
});
47+
48+
it('should allow use of DateStrings', function(done) {
49+
var d = new DateString('1971-06-22');
50+
var p = Person.create({
51+
name: 'Mr. Pink',
52+
gender: 'M',
53+
dob: d,
54+
createdAt: new Date(),
55+
}).then(function(inst) {
56+
inst.should.not.eql(null);
57+
inst.dob.toString().should.eql(d.toString());
58+
return done();
59+
}).catch(function(err) {
60+
return done(err);
61+
});
62+
});
63+
64+
describe('should allow use of alternate timezone settings', function() {
65+
var d = new Date('1971-06-22T00:00:00.000Z');
66+
testDateTime(d, '+04:00', '1971-06-22 04:00:00');
67+
testDateTime(d, '-04:00', '1971-06-21 20:00:00');
68+
testDateTime(d, '-11:00', '1971-06-21 13:00:00');
69+
testDateTime(d, '+12:00', '1971-06-22 12:00:00');
70+
71+
function testDateTime(date, tz, expected) {
72+
it(tz, function(done) {
73+
setConnectionTimezones(tz);
74+
db.settings.legacyUtcDateProcessing = false;
75+
db.settings.timezone = tz;
76+
var dt = new Date(date);
77+
return Person.create({
78+
name: 'Mr. Pink',
79+
gender: 'M',
80+
createdAt: dt,
81+
}).then(function(inst) {
82+
return Person.findById(inst.id);
83+
}).then(function(inst) {
84+
inst.should.not.eql(null);
85+
inst.createdAt.toString().should.eql(expected);
86+
return done();
87+
}).catch(function(err) {
88+
return done(err);
89+
});
90+
});
91+
}
92+
});
93+
});

0 commit comments

Comments
 (0)