timezone.test.js
1.95 KB
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
"use strict";
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/support')
, dialect = Support.getTestDialect()
, Transaction = require(__dirname + '/../lib/transaction')
, Sequelize = require(__dirname + '/../index')
, Promise = Sequelize.Promise
, sinon = require('sinon');
if (dialect !== 'sqlite') {
// Sqlite does not support setting timezone
describe(Support.getTestDialectTeaser('Timezone'), function () {
beforeEach(function () {
this.sequelizeWithTimezone = Support.createSequelizeInstance({
timezone: '+07:00',
});
});
it('returns the same value for current timestamp', function () {
var query = "SELECT now() as now";
return Promise.all([
this.sequelize.query(query),
this.sequelizeWithTimezone.query(query)
]).spread(function (now1, now2) {
expect(now1[0].now.getTime()).to.be.closeTo(now2[0].now.getTime(), 50);
});
});
if (Support.dialectIsMySQL()) {
it('handles existing timestamps', function () {
var NormalUser = this.sequelize.define('user', {})
, TimezonedUser = this.sequelizeWithTimezone.define('user', {});
return this.sequelize.sync({ force: true }).bind(this).then(function () {
return TimezonedUser.create({});
}).then(function (timezonedUser) {
this.timezonedUser = timezonedUser;
return NormalUser.find(timezonedUser.id);
}).then(function (normalUser) {
// 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(normalUser.createdAt.getTime() - this.timezonedUser.createdAt.getTime()).to.be.closeTo(60 * 60 * 7 * 1000, 50);
});
});
}
});
}