Skip to content

Commit f4b51b9

Browse files
authored
chore(ci): Fix timezones in cacheKeyQueries tests (#10087)
1 parent aabb3c5 commit f4b51b9

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

packages/cubejs-schema-compiler/test/unit/base-query.test.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ describe('SQL Generation', () => {
11831183
const utcOffset = moment.tz('America/Los_Angeles').utcOffset() * 60;
11841184
expect(query.everyRefreshKeySql({
11851185
every: '1 hour'
1186-
})).toEqual(['FLOOR((-25200 + EXTRACT(EPOCH FROM NOW())) / 3600)', false, expect.any(BaseQuery)]);
1186+
})).toEqual([`FLOOR((${utcOffset} + EXTRACT(EPOCH FROM NOW())) / 3600)`, false, expect.any(BaseQuery)]);
11871187

11881188
// Standard syntax (minutes hours day month dow)
11891189
expect(query.everyRefreshKeySql({ every: '0 * * * *', timezone }))
@@ -1277,20 +1277,24 @@ describe('SQL Generation', () => {
12771277
it('cacheKeyQueries for cube with refreshKey.every (source)', async () => {
12781278
await compilers.compiler.compile();
12791279

1280+
const timezone = 'America/Los_Angeles';
1281+
// Calculate UTC offset dynamically to handle DST changes
1282+
const utcOffset = moment.tz(timezone).utcOffset() * 60;
1283+
12801284
const query = new PostgresQuery(compilers, {
12811285
measures: [
12821286
'cards.sum'
12831287
],
12841288
timeDimensions: [],
12851289
filters: [],
1286-
timezone: 'America/Los_Angeles',
1290+
timezone,
12871291
});
12881292

12891293
// Query should not match any pre-aggregation!
12901294
expect(query.cacheKeyQueries()).toEqual([
12911295
[
12921296
// Postgres dialect
1293-
'SELECT FLOOR((-25200 + EXTRACT(EPOCH FROM NOW())) / 600) as refresh_key',
1297+
`SELECT FLOOR((${utcOffset} + EXTRACT(EPOCH FROM NOW())) / 600) as refresh_key`,
12941298
[],
12951299
{
12961300
// false, because there is no externalQueryClass
@@ -1304,22 +1308,27 @@ describe('SQL Generation', () => {
13041308
it('cacheKeyQueries for cube with refreshKey.every (external)', async () => {
13051309
await compilers.compiler.compile();
13061310

1311+
const timezone = 'Europe/London';
1312+
// Calculate UTC offset dynamically to handle DST changes
1313+
const utcOffset = moment.tz(timezone).utcOffset() * 60;
1314+
const utcOffsetPrefix = utcOffset ? `${utcOffset} + ` : '';
1315+
13071316
// Query should not match any pre-aggregation!
13081317
const query = new PostgresQuery(compilers, {
13091318
measures: [
13101319
'cards.sum'
13111320
],
13121321
timeDimensions: [],
13131322
filters: [],
1314-
timezone: 'Europe/London',
1323+
timezone,
13151324
externalQueryClass: MssqlQuery
13161325
});
13171326

13181327
// Query should not match any pre-aggregation!
13191328
expect(query.cacheKeyQueries()).toEqual([
13201329
[
13211330
// MSSQL dialect, because externalQueryClass
1322-
'SELECT FLOOR((3600 + DATEDIFF(SECOND,\'1970-01-01\', GETUTCDATE())) / 600) as refresh_key',
1331+
`SELECT FLOOR((${utcOffsetPrefix}DATEDIFF(SECOND,'1970-01-01', GETUTCDATE())) / 600) as refresh_key`,
13231332
[],
13241333
{
13251334
// true, because externalQueryClass
@@ -1337,13 +1346,17 @@ describe('SQL Generation', () => {
13371346
it('preAggregationsDescription for query - refreshKey every (external)', async () => {
13381347
await compilers.compiler.compile();
13391348

1349+
const timezone = 'America/Los_Angeles';
1350+
// Calculate UTC offset dynamically to handle DST changes
1351+
const utcOffset = moment.tz(timezone).utcOffset() * 60;
1352+
13401353
const query = new PostgresQuery(compilers, {
13411354
measures: [
13421355
'cards.count'
13431356
],
13441357
timeDimensions: [],
13451358
filters: [],
1346-
timezone: 'America/Los_Angeles',
1359+
timezone,
13471360
externalQueryClass: MssqlQuery
13481361
});
13491362

@@ -1352,7 +1365,7 @@ describe('SQL Generation', () => {
13521365
expect(preAggregations[0].invalidateKeyQueries).toEqual([
13531366
[
13541367
// MSSQL dialect
1355-
'SELECT FLOOR((-25200 + DATEDIFF(SECOND,\'1970-01-01\', GETUTCDATE())) / 3600) as refresh_key',
1368+
`SELECT FLOOR((${utcOffset} + DATEDIFF(SECOND,'1970-01-01', GETUTCDATE())) / 3600) as refresh_key`,
13561369
[],
13571370
{
13581371
external: true,
@@ -1395,6 +1408,10 @@ describe('SQL Generation', () => {
13951408
it('preAggregationsDescription for query - refreshKey incremental (timeDimensions range)', async () => {
13961409
await compilers.compiler.compile();
13971410

1411+
const timezone = 'Asia/Tokyo';
1412+
// Calculate UTC offset dynamically to handle any timezone changes
1413+
const utcOffset = moment.tz(timezone).utcOffset() * 60;
1414+
13981415
const query = new PostgresQuery(compilers, {
13991416
measures: [
14001417
'cards.min'
@@ -1405,15 +1422,15 @@ describe('SQL Generation', () => {
14051422
dateRange: ['2016-12-30', '2017-01-05']
14061423
}],
14071424
filters: [],
1408-
timezone: 'Asia/Tokyo',
1425+
timezone,
14091426
externalQueryClass: MssqlQuery
14101427
});
14111428

14121429
const preAggregations: any = query.newPreAggregations().preAggregationsDescription();
14131430
expect(preAggregations.length).toEqual(1);
14141431
expect(preAggregations[0].invalidateKeyQueries).toEqual([
14151432
[
1416-
'SELECT CASE\n WHEN CURRENT_TIMESTAMP < CAST(@_1 AS DATETIMEOFFSET) THEN FLOOR((32400 + DATEDIFF(SECOND,\'1970-01-01\', GETUTCDATE())) / 3600) END as refresh_key',
1433+
`SELECT CASE\n WHEN CURRENT_TIMESTAMP < CAST(@_1 AS DATETIMEOFFSET) THEN FLOOR((${utcOffset} + DATEDIFF(SECOND,'1970-01-01', GETUTCDATE())) / 3600) END as refresh_key`,
14171434
[
14181435
'__TO_PARTITION_RANGE',
14191436
],
@@ -1479,6 +1496,10 @@ describe('SQL Generation', () => {
14791496
it('refreshKey from cube (source)', async () => {
14801497
await compilers.compiler.compile();
14811498

1499+
const timezone = 'America/Los_Angeles';
1500+
// Calculate UTC offset dynamically to handle DST changes
1501+
const utcOffset = moment.tz(timezone).utcOffset() * 60;
1502+
14821503
const query = new PostgresQuery(compilers, {
14831504
measures: [
14841505
'cards.count'
@@ -1489,14 +1510,14 @@ describe('SQL Generation', () => {
14891510
dateRange: ['2016-12-30', '2017-01-05']
14901511
}],
14911512
filters: [],
1492-
timezone: 'America/Los_Angeles',
1513+
timezone,
14931514
});
14941515

14951516
const preAggregations: any = query.newPreAggregations().preAggregationsDescription();
14961517
expect(preAggregations.length).toEqual(1);
14971518
expect(preAggregations[0].invalidateKeyQueries).toEqual([
14981519
[
1499-
'SELECT FLOOR((-25200 + EXTRACT(EPOCH FROM NOW())) / 600) as refresh_key',
1520+
`SELECT FLOOR((${utcOffset} + EXTRACT(EPOCH FROM NOW())) / 600) as refresh_key`,
15001521
[],
15011522
{
15021523
external: false,
@@ -1509,6 +1530,10 @@ describe('SQL Generation', () => {
15091530
it('refreshKey from cube (external)', async () => {
15101531
await compilers.compiler.compile();
15111532

1533+
const timezone = 'America/Los_Angeles';
1534+
// Calculate UTC offset dynamically to handle DST changes
1535+
const utcOffset = moment.tz(timezone).utcOffset() * 60;
1536+
15121537
const query = new PostgresQuery(compilers, {
15131538
measures: [
15141539
'cards.count'
@@ -1519,15 +1544,15 @@ describe('SQL Generation', () => {
15191544
dateRange: ['2016-12-30', '2017-01-05']
15201545
}],
15211546
filters: [],
1522-
timezone: 'America/Los_Angeles',
1547+
timezone,
15231548
externalQueryClass: MssqlQuery
15241549
});
15251550

15261551
const preAggregations: any = query.newPreAggregations().preAggregationsDescription();
15271552
expect(preAggregations.length).toEqual(1);
15281553
expect(preAggregations[0].invalidateKeyQueries).toEqual([
15291554
[
1530-
'SELECT FLOOR((-25200 + DATEDIFF(SECOND,\'1970-01-01\', GETUTCDATE())) / 600) as refresh_key',
1555+
`SELECT FLOOR((${utcOffset} + DATEDIFF(SECOND,'1970-01-01', GETUTCDATE())) / 600) as refresh_key`,
15311556
[],
15321557
{
15331558
external: true,

0 commit comments

Comments
 (0)