|
1 | 1 | /* eslint-disable sonarjs/no-duplicate-string */ |
2 | | -import { describe, expect, it } from 'vitest'; |
| 2 | +import { TableDomain } from '@teable/core'; |
| 3 | +import { beforeEach, describe, expect, it } from 'vitest'; |
3 | 4 |
|
| 5 | +import type { IFieldSelectName } from '../../../features/record/query-builder/field-select.type'; |
| 6 | +import type { ISelectFormulaConversionContext } from '../../../features/record/query-builder/sql-conversion.visitor'; |
4 | 7 | import { SelectQueryPostgres } from './select-query.postgres'; |
5 | 8 |
|
6 | 9 | describe('SelectQueryPostgres unit-aware date helpers', () => { |
7 | 10 | const query = new SelectQueryPostgres(); |
| 11 | + const mockTable = new TableDomain({ |
| 12 | + id: 'tblMock', |
| 13 | + name: 'Mock Table', |
| 14 | + dbTableName: 'mock_table', |
| 15 | + lastModifiedTime: '1970-01-01T00:00:00.000Z', |
| 16 | + fields: [], |
| 17 | + }); |
| 18 | + |
| 19 | + const createTimezoneContext = (timeZone: string): ISelectFormulaConversionContext => ({ |
| 20 | + table: mockTable, |
| 21 | + selectionMap: new Map<string, IFieldSelectName>(), |
| 22 | + timeZone, |
| 23 | + }); |
| 24 | + |
| 25 | + describe('timezone-aware wrappers', () => { |
| 26 | + let tzQuery: SelectQueryPostgres; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + tzQuery = new SelectQueryPostgres(); |
| 30 | + tzQuery.setContext(createTimezoneContext('Asia/Shanghai')); |
| 31 | + }); |
| 32 | + |
| 33 | + it('datestr wraps timezone-adjusted expressions before casting', () => { |
| 34 | + expect(tzQuery.datestr('date_col')).toBe( |
| 35 | + `((date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::date::text` |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('timestr wraps timezone-adjusted expressions before casting', () => { |
| 40 | + expect(tzQuery.timestr('date_col')).toBe( |
| 41 | + `((date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::time::text` |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + it('workday casts after timezone normalization', () => { |
| 46 | + expect(tzQuery.workday('start_col', '5')).toBe( |
| 47 | + `((start_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::date + INTERVAL '5 days'` |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('dateAdd uses timezone-normalized base expression', () => { |
| 52 | + expect(tzQuery.dateAdd('date_col', '2', `'day'`)).toBe( |
| 53 | + `(date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai' + ((2)) * INTERVAL '1 day'` |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('day extracts day after timezone normalization', () => { |
| 58 | + expect(tzQuery.day('date_col')).toBe( |
| 59 | + `EXTRACT(DAY FROM (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::int` |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('datetimeFormat formats timezone-normalized timestamp', () => { |
| 64 | + expect(tzQuery.datetimeFormat('date_col', `'%Y'`)).toBe( |
| 65 | + `TO_CHAR((date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai', '%Y')` |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('isAfter compares timezone-normalized expressions', () => { |
| 70 | + expect(tzQuery.isAfter('date_a', 'date_b')).toBe( |
| 71 | + `(date_a)::timestamptz AT TIME ZONE 'Asia/Shanghai' > (date_b)::timestamptz AT TIME ZONE 'Asia/Shanghai'` |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('isBefore compares timezone-normalized expressions', () => { |
| 76 | + expect(tzQuery.isBefore('date_a', 'date_b')).toBe( |
| 77 | + `(date_a)::timestamptz AT TIME ZONE 'Asia/Shanghai' < (date_b)::timestamptz AT TIME ZONE 'Asia/Shanghai'` |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('isSame normalizes unit comparisons after timezone conversion', () => { |
| 82 | + expect(tzQuery.isSame('date_a', 'date_b', `'hour'`)).toBe( |
| 83 | + `DATE_TRUNC('hour', (date_a)::timestamptz AT TIME ZONE 'Asia/Shanghai') = DATE_TRUNC('hour', (date_b)::timestamptz AT TIME ZONE 'Asia/Shanghai')` |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('hour extracts hour after timezone normalization', () => { |
| 88 | + expect(tzQuery.hour('date_col')).toBe( |
| 89 | + `EXTRACT(HOUR FROM (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::int` |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it('minute extracts minute after timezone normalization', () => { |
| 94 | + expect(tzQuery.minute('date_col')).toBe( |
| 95 | + `EXTRACT(MINUTE FROM (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::int` |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('second extracts second after timezone normalization', () => { |
| 100 | + expect(tzQuery.second('date_col')).toBe( |
| 101 | + `EXTRACT(SECOND FROM (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::int` |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('month extracts month after timezone normalization', () => { |
| 106 | + expect(tzQuery.month('date_col')).toBe( |
| 107 | + `EXTRACT(MONTH FROM (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::int` |
| 108 | + ); |
| 109 | + }); |
| 110 | + |
| 111 | + it('year extracts year after timezone normalization', () => { |
| 112 | + expect(tzQuery.year('date_col')).toBe( |
| 113 | + `EXTRACT(YEAR FROM (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::int` |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('weekNum extracts week number after timezone normalization', () => { |
| 118 | + expect(tzQuery.weekNum('date_col')).toBe( |
| 119 | + `EXTRACT(WEEK FROM (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::int` |
| 120 | + ); |
| 121 | + }); |
| 122 | + |
| 123 | + it('weekday extracts day of week after timezone normalization', () => { |
| 124 | + expect(tzQuery.weekday('date_col')).toBe( |
| 125 | + `EXTRACT(DOW FROM (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai')::int` |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('toNow computes epoch difference using timezone context', () => { |
| 130 | + expect(tzQuery.toNow('date_col')).toBe( |
| 131 | + `EXTRACT(EPOCH FROM ((date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai' - (NOW() AT TIME ZONE 'Asia/Shanghai')))` |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it('datetimeDiff subtracts timezone-normalized expressions', () => { |
| 136 | + expect(tzQuery.datetimeDiff('start_col', 'end_col', `'day'`)).toBe( |
| 137 | + `(EXTRACT(EPOCH FROM ((end_col)::timestamptz AT TIME ZONE 'Asia/Shanghai' - (start_col)::timestamptz AT TIME ZONE 'Asia/Shanghai'))) / 86400` |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('fromNow uses timezone-aware current timestamp', () => { |
| 142 | + expect(tzQuery.fromNow('date_col')).toBe( |
| 143 | + `EXTRACT(EPOCH FROM ((NOW() AT TIME ZONE 'Asia/Shanghai') - (date_col)::timestamptz AT TIME ZONE 'Asia/Shanghai'))` |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('escapes single quotes in timezone identifiers', () => { |
| 148 | + const customTzQuery = new SelectQueryPostgres(); |
| 149 | + customTzQuery.setContext(createTimezoneContext("America/St_John's")); |
| 150 | + |
| 151 | + expect(customTzQuery.datestr('date_col')).toBe( |
| 152 | + `((date_col)::timestamptz AT TIME ZONE 'America/St_John''s')::date::text` |
| 153 | + ); |
| 154 | + }); |
| 155 | + }); |
8 | 156 |
|
9 | 157 | const dateAddCases: Array<{ literal: string; unit: string; factor: number }> = [ |
10 | 158 | { literal: 'millisecond', unit: 'millisecond', factor: 1 }, |
@@ -37,85 +185,87 @@ describe('SelectQueryPostgres unit-aware date helpers', () => { |
37 | 185 | it.each(dateAddCases)('dateAdd normalizes unit "%s" to "%s"', ({ literal, unit, factor }) => { |
38 | 186 | const sql = query.dateAdd('date_col', 'count_expr', `'${literal}'`); |
39 | 187 | const scaled = factor === 1 ? '(count_expr)' : `(count_expr) * ${factor}`; |
40 | | - expect(sql).toBe(`date_col::timestamp + (${scaled}) * INTERVAL '1 ${unit}'`); |
| 188 | + expect(sql).toBe(`(date_col)::timestamp + (${scaled}) * INTERVAL '1 ${unit}'`); |
41 | 189 | }); |
42 | 190 |
|
43 | 191 | const datetimeDiffCases: Array<{ literal: string; expected: string }> = [ |
44 | 192 | { |
45 | 193 | literal: 'millisecond', |
46 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) * 1000', |
| 194 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) * 1000', |
47 | 195 | }, |
48 | 196 | { |
49 | 197 | literal: 'milliseconds', |
50 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) * 1000', |
| 198 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) * 1000', |
51 | 199 | }, |
52 | 200 | { |
53 | 201 | literal: 'ms', |
54 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) * 1000', |
| 202 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) * 1000', |
55 | 203 | }, |
56 | 204 | { |
57 | 205 | literal: 'second', |
58 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp)))', |
| 206 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp)))', |
59 | 207 | }, |
60 | 208 | { |
61 | 209 | literal: 'seconds', |
62 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp)))', |
| 210 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp)))', |
63 | 211 | }, |
64 | 212 | { |
65 | 213 | literal: 'sec', |
66 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp)))', |
| 214 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp)))', |
67 | 215 | }, |
68 | 216 | { |
69 | 217 | literal: 'secs', |
70 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp)))', |
| 218 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp)))', |
71 | 219 | }, |
72 | 220 | { |
73 | 221 | literal: 'minute', |
74 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 60', |
| 222 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 60', |
75 | 223 | }, |
76 | 224 | { |
77 | 225 | literal: 'minutes', |
78 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 60', |
| 226 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 60', |
79 | 227 | }, |
80 | 228 | { |
81 | 229 | literal: 'min', |
82 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 60', |
| 230 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 60', |
83 | 231 | }, |
84 | 232 | { |
85 | 233 | literal: 'mins', |
86 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 60', |
| 234 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 60', |
87 | 235 | }, |
88 | 236 | { |
89 | 237 | literal: 'hour', |
90 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 3600', |
| 238 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 3600', |
91 | 239 | }, |
92 | 240 | { |
93 | 241 | literal: 'hours', |
94 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 3600', |
| 242 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 3600', |
95 | 243 | }, |
96 | 244 | { |
97 | 245 | literal: 'hr', |
98 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 3600', |
| 246 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 3600', |
99 | 247 | }, |
100 | 248 | { |
101 | 249 | literal: 'hrs', |
102 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 3600', |
| 250 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 3600', |
103 | 251 | }, |
104 | 252 | { |
105 | 253 | literal: 'week', |
106 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / (86400 * 7)', |
| 254 | + expected: |
| 255 | + '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / (86400 * 7)', |
107 | 256 | }, |
108 | 257 | { |
109 | 258 | literal: 'weeks', |
110 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / (86400 * 7)', |
| 259 | + expected: |
| 260 | + '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / (86400 * 7)', |
111 | 261 | }, |
112 | 262 | { |
113 | 263 | literal: 'day', |
114 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 86400', |
| 264 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 86400', |
115 | 265 | }, |
116 | 266 | { |
117 | 267 | literal: 'days', |
118 | | - expected: '(EXTRACT(EPOCH FROM (date_end::timestamp - date_start::timestamp))) / 86400', |
| 268 | + expected: '(EXTRACT(EPOCH FROM ((date_end)::timestamp - (date_start)::timestamp))) / 86400', |
119 | 269 | }, |
120 | 270 | ]; |
121 | 271 |
|
@@ -155,7 +305,7 @@ describe('SelectQueryPostgres unit-aware date helpers', () => { |
155 | 305 | it.each(isSameCases)('isSame normalizes unit "%s"', ({ literal, expectedUnit }) => { |
156 | 306 | const sql = query.isSame('date_a', 'date_b', `'${literal}'`); |
157 | 307 | expect(sql).toBe( |
158 | | - `DATE_TRUNC('${expectedUnit}', date_a::timestamp) = DATE_TRUNC('${expectedUnit}', date_b::timestamp)` |
| 308 | + `DATE_TRUNC('${expectedUnit}', (date_a)::timestamp) = DATE_TRUNC('${expectedUnit}', (date_b)::timestamp)` |
159 | 309 | ); |
160 | 310 | }); |
161 | 311 |
|
|
0 commit comments