-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
timezone.test.js
80 lines (66 loc) · 2.83 KB
/
timezone.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('./support'),
dialect = Support.getTestDialect();
if (dialect !== 'sqlite') {
// Sqlite does not support setting timezone
describe(Support.getTestDialectTeaser('Timezone'), () => {
beforeEach(function () {
this.sequelizeWithTimezone = Support.createSequelizeInstance({
timezone: '+07:00'
});
this.sequelizeWithNamedTimezone = Support.createSequelizeInstance({
timezone: 'America/New_York'
});
});
it('returns the same value for current timestamp', async function () {
let now = 'now()';
const startQueryTime = Date.now();
if (dialect === 'mssql') {
now = 'GETDATE()';
}
const query = `SELECT ${now} as now`;
const [now1, now2] = await Promise.all([
this.sequelize.query(query, { type: this.sequelize.QueryTypes.SELECT }),
this.sequelizeWithTimezone.query(query, {
type: this.sequelize.QueryTypes.SELECT
})
]);
const elapsedQueryTime = Date.now() - startQueryTime + 1001;
expect(now1[0].now.getTime()).to.be.closeTo(now2[0].now.getTime(), elapsedQueryTime);
});
if (dialect === 'mysql' || dialect === 'mariadb') {
it('handles existing timestamps', async function () {
const NormalUser = this.sequelize.define('user', {}),
TimezonedUser = this.sequelizeWithTimezone.define('user', {});
await this.sequelize.sync({ force: true });
const normalUser = await NormalUser.create({});
this.normalUser = normalUser;
const timezonedUser = await TimezonedUser.findByPk(normalUser.id);
// Expect 7 hours difference, in milliseconds.
// This difference is expected since two instances, configured for each their timezone is trying to read the same timestamp
// this test does not apply to PG, since it stores the timezone along with the timestamp.
expect(this.normalUser.createdAt.getTime() - timezonedUser.createdAt.getTime()).to.be.closeTo(
60 * 60 * 7 * 1000,
1000
);
});
it('handles named timezones', async function () {
const NormalUser = this.sequelize.define('user', {}),
TimezonedUser = this.sequelizeWithNamedTimezone.define('user', {});
await this.sequelize.sync({ force: true });
const timezonedUser0 = await TimezonedUser.create({});
const [normalUser, timezonedUser] = await Promise.all([
NormalUser.findByPk(timezonedUser0.id),
TimezonedUser.findByPk(timezonedUser0.id)
]);
// Expect 5 hours difference, in milliseconds, +/- 1 hour for DST
expect(normalUser.createdAt.getTime() - timezonedUser.createdAt.getTime()).to.be.closeTo(
60 * 60 * 4 * 1000 * -1,
60 * 60 * 1000
);
});
}
});
}