loopback-connector-mysql/test/datetime.test.js

109 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-02-08 22:23:33 +00:00
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
2017-05-01 23:38:53 +00:00
// Node module: loopback-connector-mysql
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const DateString = require('../node_modules/loopback-datasource-juggler/lib/date-string');
const fmt = require('util').format;
const should = require('./init.js');
2017-05-01 23:38:53 +00:00
let db, Person;
2017-05-01 23:38:53 +00:00
describe('MySQL datetime handling', function() {
const personDefinition = {
2017-05-01 23:38:53 +00:00
name: String,
gender: String,
married: Boolean,
dob: {type: 'DateString'},
createdAt: {type: Date, default: Date},
2017-05-03 21:45:02 +00:00
lastLogon: {type: Date, precision: 3, default: Date},
2017-05-01 23:38:53 +00:00
};
// Modifying the connection timezones mid-flight is a pain,
// but closing the existing connection requires more effort.
function setConnectionTimezones(tz) {
// _allConnections is a Queue in mysql2 library
db.connector.client._allConnections.toArray().forEach(function(con) {
2017-05-01 23:38:53 +00:00
con.config.timezone = tz;
});
}
before(function(done) {
2017-07-24 16:48:26 +00:00
db = global.getSchema({
2017-05-01 23:38:53 +00:00
dateStrings: true,
});
Person = db.define('Person', personDefinition, {forceId: true, strict: true});
db.automigrate(['Person'], done);
});
beforeEach(function() {
setConnectionTimezones('Z');
});
after(function(done) {
Person.destroyAll(function(err) {
db.disconnect(function() {
return done(err);
});
});
});
2019-11-05 20:46:10 +00:00
it('should allow use of DateStrings', () => {
const d = new DateString('1971-06-22');
2017-05-01 23:38:53 +00:00
return Person.create({
name: 'Mr. Pink',
gender: 'M',
dob: d,
createdAt: new Date(),
}).then(function(inst) {
return Person.findById(inst.id);
}).then(function(inst) {
inst.should.not.eql(null);
inst.dob.toString().should.eql(d.toString());
2019-11-05 20:46:10 +00:00
return;
2017-05-01 23:38:53 +00:00
});
});
describe('should allow use of alternate timezone settings', function() {
const d = new Date('1971-06-22T00:00:00.000Z');
2017-05-01 23:38:53 +00:00
testDateTime(d, '+04:00', '1971-06-22 04:00:00');
testDateTime(d, '-04:00', '1971-06-21 20:00:00');
testDateTime(d, '-11:00', '1971-06-21 13:00:00');
testDateTime(d, '+12:00', '1971-06-22 12:00:00');
function testDateTime(date, tz, expected) {
2019-11-05 20:46:10 +00:00
it(tz, function() {
2017-05-01 23:38:53 +00:00
setConnectionTimezones(tz);
db.settings.legacyUtcDateProcessing = false;
db.settings.timezone = tz;
const dt = new Date(date);
2017-05-01 23:38:53 +00:00
return Person.create({
name: 'Mr. Pink',
gender: 'M',
createdAt: dt,
}).then(function(inst) {
return Person.findById(inst.id);
}).then(function(inst) {
inst.should.not.eql(null);
inst.createdAt.toString().should.eql(expected);
});
});
}
});
2017-05-03 21:45:02 +00:00
2019-11-05 20:46:10 +00:00
it('should allow use of fractional seconds', function() {
const d = new Date('1971-06-22T12:34:56.789Z');
2017-05-03 21:45:02 +00:00
return Person.create({
name: 'Mr. Pink',
gender: 'M',
lastLogon: d,
}).then(function(inst) {
return Person.findById(inst.id);
}).then(function(inst) {
inst.should.not.eql(null);
const lastLogon = new Date(inst.lastLogon);
2017-05-03 21:45:02 +00:00
lastLogon.toJSON().should.eql(d.toJSON());
2019-11-05 20:46:10 +00:00
return;
2017-05-03 21:45:02 +00:00
});
});
2017-05-01 23:38:53 +00:00
});