@malloydata/malloy-tests 0.0.66-dev230807232724

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/README.md +29 -0
  2. package/bin/dump_malloytest.sh +1 -0
  3. package/bin/postgres_init.sh +9 -0
  4. package/bin/postgres_start.sh +2 -0
  5. package/bin/postgres_stop.sh +2 -0
  6. package/data/duckdb/README.md +4 -0
  7. package/data/duckdb/aircraft.parquet +0 -0
  8. package/data/duckdb/aircraft_models.parquet +0 -0
  9. package/data/duckdb/airports.parquet +0 -0
  10. package/data/duckdb/alltypes.parquet +0 -0
  11. package/data/duckdb/alltypes2.parquet +0 -0
  12. package/data/duckdb/bq_medicare_test.parquet +0 -0
  13. package/data/duckdb/carriers.parquet +0 -0
  14. package/data/duckdb/flights/part.0.parquet +0 -0
  15. package/data/duckdb/flights/part.1.parquet +0 -0
  16. package/data/duckdb/flights/part.2.parquet +0 -0
  17. package/data/duckdb/flights.parquet +0 -0
  18. package/data/duckdb/flights_partitioned.parquet +0 -0
  19. package/data/duckdb/ga_sample.parquet +0 -0
  20. package/data/duckdb/numbers.parquet +0 -0
  21. package/data/duckdb/state_facts.parquet +0 -0
  22. package/data/duckdb/test.json +3 -0
  23. package/data/duckdb/words.parquet +0 -0
  24. package/data/duckdb/words_bigger.parquet +0 -0
  25. package/data/postgres/malloytest-postgres.sql.gz +0 -0
  26. package/data/postgres/state_facts.sql +366 -0
  27. package/dist/api.spec.d.ts +1 -0
  28. package/dist/api.spec.js +77 -0
  29. package/dist/api.spec.js.map +1 -0
  30. package/dist/databases/all/expr.spec.d.ts +3 -0
  31. package/dist/databases/all/expr.spec.js +581 -0
  32. package/dist/databases/all/expr.spec.js.map +1 -0
  33. package/dist/databases/all/functions.spec.d.ts +2 -0
  34. package/dist/databases/all/functions.spec.js +784 -0
  35. package/dist/databases/all/functions.spec.js.map +1 -0
  36. package/dist/databases/all/index.spec.d.ts +2 -0
  37. package/dist/databases/all/index.spec.js +154 -0
  38. package/dist/databases/all/index.spec.js.map +1 -0
  39. package/dist/databases/all/join.spec.d.ts +2 -0
  40. package/dist/databases/all/join.spec.js +309 -0
  41. package/dist/databases/all/join.spec.js.map +1 -0
  42. package/dist/databases/all/nomodel.spec.d.ts +2 -0
  43. package/dist/databases/all/nomodel.spec.js +957 -0
  44. package/dist/databases/all/nomodel.spec.js.map +1 -0
  45. package/dist/databases/all/orderby.spec.d.ts +2 -0
  46. package/dist/databases/all/orderby.spec.js +224 -0
  47. package/dist/databases/all/orderby.spec.js.map +1 -0
  48. package/dist/databases/all/problems.spec.d.ts +2 -0
  49. package/dist/databases/all/problems.spec.js +113 -0
  50. package/dist/databases/all/problems.spec.js.map +1 -0
  51. package/dist/databases/all/sql_expressions.spec.d.ts +2 -0
  52. package/dist/databases/all/sql_expressions.spec.js +96 -0
  53. package/dist/databases/all/sql_expressions.spec.js.map +1 -0
  54. package/dist/databases/all/time.spec.d.ts +3 -0
  55. package/dist/databases/all/time.spec.js +647 -0
  56. package/dist/databases/all/time.spec.js.map +1 -0
  57. package/dist/databases/bigquery/double_truncation.spec.d.ts +1 -0
  58. package/dist/databases/bigquery/double_truncation.spec.js +50 -0
  59. package/dist/databases/bigquery/double_truncation.spec.js.map +1 -0
  60. package/dist/databases/bigquery/handexpr.spec.d.ts +1 -0
  61. package/dist/databases/bigquery/handexpr.spec.js +713 -0
  62. package/dist/databases/bigquery/handexpr.spec.js.map +1 -0
  63. package/dist/databases/bigquery/injestion_time_partitioning.spec.d.ts +1 -0
  64. package/dist/databases/bigquery/injestion_time_partitioning.spec.js +235 -0
  65. package/dist/databases/bigquery/injestion_time_partitioning.spec.js.map +1 -0
  66. package/dist/databases/bigquery/joined_filters.spec.d.ts +1 -0
  67. package/dist/databases/bigquery/joined_filters.spec.js +72 -0
  68. package/dist/databases/bigquery/joined_filters.spec.js.map +1 -0
  69. package/dist/databases/bigquery/json.spec.d.ts +1 -0
  70. package/dist/databases/bigquery/json.spec.js +86 -0
  71. package/dist/databases/bigquery/json.spec.js.map +1 -0
  72. package/dist/databases/bigquery/malloy_query.spec.d.ts +1 -0
  73. package/dist/databases/bigquery/malloy_query.spec.js +906 -0
  74. package/dist/databases/bigquery/malloy_query.spec.js.map +1 -0
  75. package/dist/databases/bigquery/performance.skipped.spec.d.ts +1 -0
  76. package/dist/databases/bigquery/performance.skipped.spec.js +70 -0
  77. package/dist/databases/bigquery/performance.skipped.spec.js.map +1 -0
  78. package/dist/databases/bigquery/time.spec.d.ts +1 -0
  79. package/dist/databases/bigquery/time.spec.js +55 -0
  80. package/dist/databases/bigquery/time.spec.js.map +1 -0
  81. package/dist/databases/bigquery/wildcard_table_names.spec.d.ts +1 -0
  82. package/dist/databases/bigquery/wildcard_table_names.spec.js +212 -0
  83. package/dist/databases/bigquery/wildcard_table_names.spec.js.map +1 -0
  84. package/dist/databases/bigquery-duckdb/nested_source_table.spec.d.ts +1 -0
  85. package/dist/databases/bigquery-duckdb/nested_source_table.spec.js +247 -0
  86. package/dist/databases/bigquery-duckdb/nested_source_table.spec.js.map +1 -0
  87. package/dist/databases/bigquery-postgres/multi_connection.spec.d.ts +1 -0
  88. package/dist/databases/bigquery-postgres/multi_connection.spec.js +138 -0
  89. package/dist/databases/bigquery-postgres/multi_connection.spec.js.map +1 -0
  90. package/dist/databases/bigquery-postgres/streaming.spec.d.ts +1 -0
  91. package/dist/databases/bigquery-postgres/streaming.spec.js +88 -0
  92. package/dist/databases/bigquery-postgres/streaming.spec.js.map +1 -0
  93. package/dist/databases/duckdb/duckdb.spec.d.ts +1 -0
  94. package/dist/databases/duckdb/duckdb.spec.js +116 -0
  95. package/dist/databases/duckdb/duckdb.spec.js.map +1 -0
  96. package/dist/databases/postgres/postgres.spec.d.ts +1 -0
  97. package/dist/databases/postgres/postgres.spec.js +137 -0
  98. package/dist/databases/postgres/postgres.spec.js.map +1 -0
  99. package/dist/databases/shared/test_list.d.ts +3 -0
  100. package/dist/databases/shared/test_list.js +24 -0
  101. package/dist/databases/shared/test_list.js.map +1 -0
  102. package/dist/dependencies.spec.d.ts +1 -0
  103. package/dist/dependencies.spec.js +63 -0
  104. package/dist/dependencies.spec.js.map +1 -0
  105. package/dist/index.d.ts +11 -0
  106. package/dist/index.js +49 -0
  107. package/dist/index.js.map +1 -0
  108. package/dist/model/utils.spec.d.ts +1 -0
  109. package/dist/model/utils.spec.js +38 -0
  110. package/dist/model/utils.spec.js.map +1 -0
  111. package/dist/models/faa_model.d.ts +5 -0
  112. package/dist/models/faa_model.js +977 -0
  113. package/dist/models/faa_model.js.map +1 -0
  114. package/dist/models/medicare_model.d.ts +4 -0
  115. package/dist/models/medicare_model.js +243 -0
  116. package/dist/models/medicare_model.js.map +1 -0
  117. package/dist/render/render.spec.d.ts +1 -0
  118. package/dist/render/render.spec.js +551 -0
  119. package/dist/render/render.spec.js.map +1 -0
  120. package/dist/runtimes.d.ts +33 -0
  121. package/dist/runtimes.js +147 -0
  122. package/dist/runtimes.js.map +1 -0
  123. package/dist/tags.spec.d.ts +1 -0
  124. package/dist/tags.spec.js +259 -0
  125. package/dist/tags.spec.js.map +1 -0
  126. package/dist/util/db-jest-matchers.d.ts +19 -0
  127. package/dist/util/db-jest-matchers.js +90 -0
  128. package/dist/util/db-jest-matchers.js.map +1 -0
  129. package/dist/util/index.d.ts +16 -0
  130. package/dist/util/index.js +171 -0
  131. package/dist/util/index.js.map +1 -0
  132. package/package.json +35 -0
  133. package/src/api.spec.ts +84 -0
  134. package/src/databases/all/expr.spec.ts +704 -0
  135. package/src/databases/all/functions.spec.ts +1135 -0
  136. package/src/databases/all/index.spec.ts +178 -0
  137. package/src/databases/all/join.spec.ts +350 -0
  138. package/src/databases/all/nomodel.spec.ts +1159 -0
  139. package/src/databases/all/orderby.spec.ts +268 -0
  140. package/src/databases/all/problems.spec.ts +121 -0
  141. package/src/databases/all/sql_expressions.spec.ts +107 -0
  142. package/src/databases/all/time.spec.ts +793 -0
  143. package/src/databases/bigquery/double_truncation.spec.ts +51 -0
  144. package/src/databases/bigquery/handexpr.spec.ts +782 -0
  145. package/src/databases/bigquery/injestion_time_partitioning.spec.ts +268 -0
  146. package/src/databases/bigquery/joined_filters.spec.ts +75 -0
  147. package/src/databases/bigquery/json.spec.ts +95 -0
  148. package/src/databases/bigquery/malloy_query.spec.ts +1036 -0
  149. package/src/databases/bigquery/performance.skipped.spec.ts +73 -0
  150. package/src/databases/bigquery/time.spec.ts +59 -0
  151. package/src/databases/bigquery/wildcard_table_names.spec.ts +233 -0
  152. package/src/databases/bigquery-duckdb/nested_source_table.spec.ts +268 -0
  153. package/src/databases/bigquery-postgres/multi_connection.spec.ts +143 -0
  154. package/src/databases/bigquery-postgres/streaming.spec.ts +100 -0
  155. package/src/databases/duckdb/duckdb.spec.ts +131 -0
  156. package/src/databases/postgres/postgres.spec.ts +167 -0
  157. package/src/databases/shared/test_list.ts +24 -0
  158. package/src/dependencies.spec.ts +62 -0
  159. package/src/index.ts +36 -0
  160. package/src/model/utils.spec.ts +37 -0
  161. package/src/models/faa_model.ts +995 -0
  162. package/src/models/medicare_model.ts +246 -0
  163. package/src/render/__snapshots__/render.spec.ts.snap +12574 -0
  164. package/src/render/render.spec.ts +670 -0
  165. package/src/runtimes.ts +182 -0
  166. package/src/tags.spec.ts +277 -0
  167. package/src/util/db-jest-matchers.ts +128 -0
  168. package/src/util/index.ts +191 -0
  169. package/tsconfig.json +25 -0
@@ -0,0 +1,647 @@
1
+ "use strict";
2
+ /* eslint-disable no-console */
3
+ /*
4
+ * Copyright 2023 Google LLC
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining
7
+ * a copy of this software and associated documentation files
8
+ * (the "Software"), to deal in the Software without restriction,
9
+ * including without limitation the rights to use, copy, modify, merge,
10
+ * publish, distribute, sublicense, and/or sell copies of the Software,
11
+ * and to permit persons to whom the Software is furnished to do so,
12
+ * subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be
15
+ * included in all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.timeSharedTests = void 0;
27
+ const runtimes_1 = require("../../runtimes");
28
+ require("../../util/db-jest-matchers");
29
+ const util_1 = require("../../util");
30
+ const luxon_1 = require("luxon");
31
+ const timeSharedTests = (runtimes, _splitFunction) => {
32
+ const timeSQL = "SELECT DATE '2021-02-24' as t_date, TIMESTAMP '2021-02-24 03:05:06' as t_timestamp";
33
+ // MTOY todo look at this list for timezone problems, I know there are some
34
+ describe.each(runtimes.runtimeList)('%s date and time', (dbName, runtime) => {
35
+ const sqlEq = (0, util_1.mkSqlEqWith)(runtime, { sql: timeSQL });
36
+ describe('interval measurement', () => {
37
+ test('forwards is positive', async () => {
38
+ const eq = sqlEq('day(@2000-01-01 to @2000-01-02)', '1');
39
+ expect(await eq).isSqlEq();
40
+ });
41
+ test('reverse is negative', async () => {
42
+ const eq = sqlEq('day(@2000-01-02 to @2000-01-01)', '-1');
43
+ expect(await eq).isSqlEq();
44
+ });
45
+ test('seconds', async () => {
46
+ expect(await sqlEq('seconds(now to now + 1 second)', 1)).isSqlEq();
47
+ expect(await sqlEq('seconds(now to now)', 0)).isSqlEq();
48
+ expect(await sqlEq('seconds(now to now + 2 seconds)', 2)).isSqlEq();
49
+ expect(await sqlEq('seconds(now to now - 2 seconds)', -2)).isSqlEq();
50
+ const a = '@2001-01-01 00:00:00';
51
+ const b = '@2001-01-01 00:00:00.999';
52
+ expect(await sqlEq(`seconds(${a} to ${b})`, 0)).isSqlEq();
53
+ expect(await sqlEq(`seconds(${b} to @2001-01-01 00:00:01)`, 0)).isSqlEq();
54
+ });
55
+ test('minutes', async () => {
56
+ expect(await sqlEq('minutes(@2022-10-03 10:23:08 to @2022-10-03 10:24:07)', 0)).isSqlEq();
57
+ expect(await sqlEq('minutes(now to now + 1 minute)', 1)).isSqlEq();
58
+ expect(await sqlEq('minutes(now to now + 59 seconds)', 0)).isSqlEq();
59
+ expect(await sqlEq('minutes(now to now + 2 minutes)', 2)).isSqlEq();
60
+ expect(await sqlEq('minutes(now to now - 2 minutes)', -2)).isSqlEq();
61
+ });
62
+ test('hours', async () => {
63
+ expect(await sqlEq('hours(@2022-10-03 10:23:00 to @2022-10-03 11:22:00)', 0)).isSqlEq();
64
+ expect(await sqlEq('hours(now to now + 1 hour)', 1)).isSqlEq();
65
+ expect(await sqlEq('hours(now to now + 59 minutes)', 0)).isSqlEq();
66
+ expect(await sqlEq('hours(now to now + 120 minutes)', 2)).isSqlEq();
67
+ expect(await sqlEq('hours(now to now - 2 hours)', -2)).isSqlEq();
68
+ });
69
+ test('days', async () => {
70
+ expect(await sqlEq('days(now.day to now.day + 1 day)', 1)).isSqlEq();
71
+ expect(await sqlEq('days(now.day to now.day + 23 hours)', 0)).isSqlEq();
72
+ expect(await sqlEq('days(now.day to now.day + 48 hours)', 2)).isSqlEq();
73
+ expect(await sqlEq('days(now.day to now.day - 48 hours)', -2)).isSqlEq();
74
+ expect(await sqlEq('days(@2022-10-03 10:23:00 to @2022-10-04 09:23:00)', 0)).isSqlEq();
75
+ });
76
+ // MTOY TODO remove or implment
77
+ // These all are complicated by civul time issues, skipping for now
78
+ // test.skip('weeks', async () => {
79
+ // expect(await sqlEq('week(now.week to now.week + 6 days)', 0)).isSqlEq();
80
+ // expect(await sqlEq('week(now.week to now.week + 7 days)', 1)).isSqlEq();
81
+ // expect(
82
+ // await sqlEq('week(now.week to now.week + 7 days - 1 second)', 0)
83
+ // ).isSqlEq();
84
+ // expect(await sqlEq('weeks(@2022-10-01 to @2022-10-07)', 0)).isSqlEq();
85
+ // expect(await sqlEq('weeks(@2022-10-01 to @2022-10-08)', 1)).isSqlEq();
86
+ // expect(await sqlEq('weeks(@2022-10-15 to @2022-10-01)', -2)).isSqlEq();
87
+ // expect(await sqlEq('weeks(@2022-10-02 to @2023-10-02)', 52)).isSqlEq();
88
+ // expect(
89
+ // await sqlEq('weeks(@2022-10-01 12:00 to @2022-10-08 11:59)', 0)
90
+ // ).isSqlEq();
91
+ // });
92
+ // test.skip('months', async () => {
93
+ // expect(await sqlEq('months(now to now)', 0)).isSqlEq();
94
+ // expect(await sqlEq('months(@2001-01-01 to @2001-02-01)', 1)).isSqlEq();
95
+ // expect(await sqlEq('months(@2001-01-01 to @2001-03-01)', 2)).isSqlEq();
96
+ // expect(await sqlEq('months(@2001-01-01 to @2002-02-01)', 13)).isSqlEq();
97
+ // expect(
98
+ // await sqlEq('months(@2022-10-02 12:00 to @2022-11-02 11:59)', 0)
99
+ // ).isSqlEq();
100
+ // });
101
+ // test.skip('quarters', async () => {
102
+ // expect(await sqlEq('quarters(now to now + 1 quarter)', 1)).isSqlEq();
103
+ // expect(
104
+ // await sqlEq('quarters(now.quarter to now.quarter + 27 days)', 0)
105
+ // ).isSqlEq();
106
+ // expect(await sqlEq('quarters(now to now + 2 quarters)', 2)).isSqlEq();
107
+ // expect(await sqlEq('quarters(now to now - 2 quarters)', -2)).isSqlEq();
108
+ // expect(
109
+ // await sqlEq('quarters(@2022-01-01 12:00 to @2022-04-01 12:00)', 1)
110
+ // ).isSqlEq();
111
+ // expect(
112
+ // await sqlEq('quarters(@2022-01-01 12:00 to @2022-04-01 11:59)', 0)
113
+ // ).isSqlEq();
114
+ // });
115
+ // test.skip('years', async () => {
116
+ // expect(await sqlEq('years(@2022 to @2023)', 1)).isSqlEq();
117
+ // expect(await sqlEq('years(@2022-01-01 to @2022-12-31)', 0)).isSqlEq();
118
+ // expect(await sqlEq('years(@2022 to @2024)', 2)).isSqlEq();
119
+ // expect(await sqlEq('years(@2024 to @2022)', -2)).isSqlEq();
120
+ // expect(
121
+ // await sqlEq('years(@2022-01-01 12:00 to @2024-01-01 11:59)', 1)
122
+ // ).isSqlEq();
123
+ // });
124
+ });
125
+ describe('timestamp truncation', () => {
126
+ // 2021-02-24 03:05:06
127
+ test('trunc second', async () => {
128
+ const eq = sqlEq('t_timestamp.second', '@2021-02-24 03:05:06');
129
+ expect(await eq).isSqlEq();
130
+ });
131
+ test('trunc minute', async () => {
132
+ const eq = sqlEq('t_timestamp.minute', '@2021-02-24 03:05:00');
133
+ expect(await eq).isSqlEq();
134
+ });
135
+ test('trunc hour', async () => {
136
+ const eq = sqlEq('t_timestamp.hour', '@2021-02-24 03:00:00');
137
+ expect(await eq).isSqlEq();
138
+ });
139
+ test('trunc day', async () => {
140
+ const eq = sqlEq('t_timestamp.day', '@2021-02-24 00:00:00');
141
+ expect(await eq).isSqlEq();
142
+ });
143
+ test('trunc week', async () => {
144
+ const eq = sqlEq('t_timestamp.week', '@2021-02-21 00:00:00');
145
+ expect(await eq).isSqlEq();
146
+ });
147
+ test('trunc month', async () => {
148
+ const eq = sqlEq('t_timestamp.month', '@2021-02-01 00:00:00');
149
+ expect(await eq).isSqlEq();
150
+ });
151
+ test('trunc quarter', async () => {
152
+ const eq = sqlEq('t_timestamp.quarter', '@2021-01-01 00:00:00');
153
+ expect(await eq).isSqlEq();
154
+ });
155
+ test('trunc year', async () => {
156
+ const eq = sqlEq('t_timestamp.year', '@2021-01-01 00:00:00');
157
+ expect(await eq).isSqlEq();
158
+ });
159
+ });
160
+ describe('timestamp extraction', () => {
161
+ // 2021-02-24 03:05:06
162
+ test('extract second', async () => {
163
+ const eq = sqlEq('second(t_timestamp)', '6');
164
+ expect(await eq).isSqlEq();
165
+ });
166
+ test('extract minute', async () => {
167
+ const eq = sqlEq('minute(t_timestamp)', '5');
168
+ expect(await eq).isSqlEq();
169
+ });
170
+ test('extract hour', async () => {
171
+ const eq = sqlEq('hour(t_timestamp)', '3');
172
+ expect(await eq).isSqlEq();
173
+ });
174
+ test('extract day', async () => {
175
+ const eq = sqlEq('day(t_timestamp)', '24');
176
+ expect(await eq).isSqlEq();
177
+ });
178
+ test('extract day_of_week', async () => {
179
+ const eq = sqlEq('day_of_week(t_timestamp)', '4');
180
+ expect(await eq).isSqlEq();
181
+ });
182
+ test('first week day is one ', async () => {
183
+ const eq = sqlEq('day_of_week(t_timestamp.week)', '1');
184
+ expect(await eq).isSqlEq();
185
+ });
186
+ test('extract day_of_year', async () => {
187
+ const eq = sqlEq('day_of_year(t_timestamp)', '55');
188
+ expect(await eq).isSqlEq();
189
+ });
190
+ test('extract week', async () => {
191
+ const eq = sqlEq('week(t_timestamp)', '8');
192
+ expect(await eq).isSqlEq();
193
+ });
194
+ test('extract month', async () => {
195
+ const eq = sqlEq('month(t_timestamp)', '2');
196
+ expect(await eq).isSqlEq();
197
+ });
198
+ test('extract quarter', async () => {
199
+ const eq = sqlEq('quarter(t_timestamp)', '1');
200
+ expect(await eq).isSqlEq();
201
+ });
202
+ test('extract year', async () => {
203
+ const eq = sqlEq('year(t_timestamp)', '2021');
204
+ expect(await eq).isSqlEq();
205
+ });
206
+ });
207
+ describe('date truncation', () => {
208
+ test('date trunc day', async () => {
209
+ const eq = sqlEq('t_date.day', '@2021-02-24');
210
+ expect(await eq).isSqlEq();
211
+ });
212
+ test('date trunc week', async () => {
213
+ const eq = sqlEq('t_date.week', '@2021-02-21');
214
+ expect(await eq).isSqlEq();
215
+ });
216
+ test('date trunc month', async () => {
217
+ const eq = sqlEq('t_date.month', '@2021-02-01');
218
+ expect(await eq).isSqlEq();
219
+ });
220
+ test('date trunc quarter', async () => {
221
+ const eq = sqlEq('t_date.quarter', '@2021-01-01');
222
+ expect(await eq).isSqlEq();
223
+ });
224
+ test('date trunc year', async () => {
225
+ const eq = sqlEq('t_date.year', '@2021');
226
+ expect(await eq).isSqlEq();
227
+ });
228
+ });
229
+ describe('date extraction', () => {
230
+ test('date extract day', async () => {
231
+ const eq = sqlEq('day(t_date)', '24');
232
+ expect(await eq).isSqlEq();
233
+ });
234
+ test('date extract day_of_week', async () => {
235
+ const eq = sqlEq('day_of_week(t_date)', '4');
236
+ expect(await eq).isSqlEq();
237
+ });
238
+ test('date extract day_of_year', async () => {
239
+ const eq = sqlEq('day_of_year(t_date)', '55');
240
+ expect(await eq).isSqlEq();
241
+ });
242
+ test('date extract week', async () => {
243
+ const eq = sqlEq('week(t_date)', '8');
244
+ expect(await eq).isSqlEq();
245
+ });
246
+ test('date extract month', async () => {
247
+ const eq = sqlEq('month(t_date)', '2');
248
+ expect(await eq).isSqlEq();
249
+ });
250
+ test('date extract quarter', async () => {
251
+ const eq = sqlEq('quarter(t_date)', '1');
252
+ expect(await eq).isSqlEq();
253
+ });
254
+ test('date extract year', async () => {
255
+ const eq = sqlEq('year(t_date)', '2021');
256
+ expect(await eq).isSqlEq();
257
+ });
258
+ });
259
+ describe('delta computations', () => {
260
+ test('timestamp delta second', async () => {
261
+ const eq = sqlEq('t_timestamp + 10 seconds', '@2021-02-24 03:05:16');
262
+ expect(await eq).isSqlEq();
263
+ });
264
+ test('timestamp delta negative second', async () => {
265
+ const eq = sqlEq('t_timestamp - 6 seconds', '@2021-02-24 03:05:00');
266
+ expect(await eq).isSqlEq();
267
+ });
268
+ test('timestamp delta minute', async () => {
269
+ const eq = sqlEq('t_timestamp + 10 minutes', '@2021-02-24 03:15:06');
270
+ expect(await eq).isSqlEq();
271
+ });
272
+ test('timestamp delta hours', async () => {
273
+ const eq = await sqlEq('t_timestamp + 10 hours', '@2021-02-24 13:05:06');
274
+ expect(eq).isSqlEq();
275
+ });
276
+ test('timestamp delta week', async () => {
277
+ const eq = sqlEq('(t_timestamp - 2 weeks)::date', '@2021-02-10');
278
+ expect(await eq).isSqlEq();
279
+ });
280
+ test('timestamp delta month', async () => {
281
+ const eq = sqlEq('(t_timestamp + 9 months)::date', '@2021-11-24');
282
+ expect(await eq).isSqlEq();
283
+ });
284
+ test('timestamp delta quarter', async () => {
285
+ const eq = sqlEq('(t_timestamp + 2 quarters)::date', '@2021-08-24');
286
+ expect(await eq).isSqlEq();
287
+ });
288
+ test('timestamp delta year', async () => {
289
+ const eq = sqlEq('(t_timestamp + 10 years)::date', '@2031-02-24');
290
+ expect(await eq).isSqlEq();
291
+ });
292
+ test('date delta week', async () => {
293
+ const eq = sqlEq('t_date - 2 weeks', '@2021-02-10');
294
+ expect(await eq).isSqlEq();
295
+ });
296
+ test('date delta month', async () => {
297
+ const eq = sqlEq('t_date + 9 months', '@2021-11-24');
298
+ expect(await eq).isSqlEq();
299
+ });
300
+ test('date delta quarter', async () => {
301
+ const eq = sqlEq('t_date + 2 quarters', '@2021-08-24');
302
+ expect(await eq).isSqlEq();
303
+ });
304
+ test('date delta year', async () => {
305
+ const eq = sqlEq('t_date + 10 years', '@2031-02-24');
306
+ expect(await eq).isSqlEq();
307
+ });
308
+ });
309
+ describe('for range edge tests', () => {
310
+ describe('date', () => {
311
+ test('before for-range is outside', async () => {
312
+ const eq = sqlEq('t_date ? @2021-02-25 for 1 day', false);
313
+ expect(await eq).isSqlEq();
314
+ });
315
+ test('first for-range is inside', async () => {
316
+ const eq = sqlEq('t_date ? @2021-02-24 for 1 day', true);
317
+ expect(await eq).isSqlEq();
318
+ });
319
+ test('last for-range is outside', async () => {
320
+ const eq = sqlEq('t_date ? @2021-02-23 for 1 day', false);
321
+ expect(await eq).isSqlEq();
322
+ });
323
+ });
324
+ describe('timestamp', () => {
325
+ test('before for-range is outside', async () => {
326
+ const eq = sqlEq('t_timestamp ? @2021-02-25 00:00:00 for 1 day', false);
327
+ expect(await eq).isSqlEq();
328
+ });
329
+ test('first for-range is inside', async () => {
330
+ const eq = sqlEq('t_timestamp ? @2021-02-24 03:04:05 for 1 day', true);
331
+ expect(await eq).isSqlEq();
332
+ });
333
+ test('last for-range is outside', async () => {
334
+ const eq = sqlEq('t_timestamp ? @2021-02-23 03:05:06 for 1 day', false);
335
+ expect(await eq).isSqlEq();
336
+ });
337
+ });
338
+ });
339
+ describe('to range edge tests', () => {
340
+ describe('date', () => {
341
+ test('before to is outside', async () => {
342
+ const eq = sqlEq('t_date ? @2021-02-25 to @2021-03-01', false);
343
+ expect(await eq).isSqlEq();
344
+ });
345
+ test('first to is inside', async () => {
346
+ const eq = sqlEq('t_date ? @2021-02-24 to @2021-03-01', true);
347
+ expect(await eq).isSqlEq();
348
+ });
349
+ test('last to is outside', async () => {
350
+ const eq = sqlEq('t_date ? @2021-02-01 to @2021-02-24', false);
351
+ expect(await eq).isSqlEq();
352
+ });
353
+ });
354
+ describe('timestamp', () => {
355
+ test('before to is outside', async () => {
356
+ const eq = sqlEq('t_timestamp ? @2021-02-25 00:00:00 to @2021-02-26 00:00:00', false);
357
+ expect(await eq).isSqlEq();
358
+ });
359
+ test('first to is inside', async () => {
360
+ const eq = sqlEq('t_timestamp ? @2021-02-24 03:04:05 to @2021-02-26 00:00:00', true);
361
+ expect(await eq).isSqlEq();
362
+ });
363
+ test('last to is outside', async () => {
364
+ const eq = sqlEq('t_timestamp ? @2021-02-24 00:00:00 to @2021-02-24 03:05:06', false);
365
+ expect(await eq).isSqlEq();
366
+ });
367
+ });
368
+ });
369
+ test('date in sql_block no explore', async () => {
370
+ const eq = sqlEq('t_date', '@2021-02-24');
371
+ expect(await eq).isSqlEq();
372
+ });
373
+ test('timestamp in sql_block no explore', async () => {
374
+ const eq = sqlEq('t_timestamp', '@2021-02-24 03:05:06');
375
+ expect(await eq).isSqlEq();
376
+ });
377
+ test('valid timestamp without seconds', async () => {
378
+ // discovered this writing tests ...
379
+ const eq = sqlEq('year(@2000-01-01 00:00)', '2000');
380
+ expect(await eq).isSqlEq();
381
+ });
382
+ describe('granular time range checks', () => {
383
+ const tsMoment = '@2021-02-24 03:05:06';
384
+ test('minute implied truncated range', async () => {
385
+ const tsBefore = '@2021-03-24 03:04:59';
386
+ expect(await sqlEq(`${tsMoment} ? t_timestamp.minute`, true)).isSqlEq();
387
+ expect(await sqlEq(`${tsBefore} ? t_timestamp.minute`, false)).isSqlEq();
388
+ });
389
+ test('day implied truncated range', async () => {
390
+ expect(await sqlEq(`${tsMoment} ? t_timestamp.day`, true)).isSqlEq();
391
+ });
392
+ test('year implied truncated range', async () => {
393
+ expect(await sqlEq(`${tsMoment} ? t_timestamp.year`, true)).isSqlEq();
394
+ });
395
+ test('timestamp in literal minute', async () => {
396
+ expect(await sqlEq('t_timestamp ? @2021-02-24 03:05', true)).isSqlEq();
397
+ });
398
+ test('timestamp in literal day', async () => {
399
+ expect(await sqlEq('t_timestamp ? @2021-02-24', true)).isSqlEq();
400
+ });
401
+ test('date in literal month', async () => {
402
+ expect(await sqlEq('t_date ? @2021-02', true)).isSqlEq();
403
+ });
404
+ test('timestamp in literal month', async () => {
405
+ expect(await sqlEq('t_timestamp ? @2021-02', true)).isSqlEq();
406
+ });
407
+ test('timestamp in literal year', async () => {
408
+ expect(await sqlEq('t_timestamp ? @2021', true)).isSqlEq();
409
+ });
410
+ });
411
+ test('dependant join dialect fragments', async () => {
412
+ await expect(runtime).queryMatches(`
413
+ sql: timeData is { connection: "${dbName}" select: """${timeSQL}""" }
414
+ query: from_sql(timeData) -> {
415
+ join_one: joined is from_sql(timeData) on t_date = joined.t_date
416
+ group_by: t_month is joined.t_timestamp.month
417
+ }
418
+ `, { t_month: new Date('2021-02-01') });
419
+ });
420
+ describe('timezone set correctly', () => {
421
+ test('timezone set in source used by query', async () => {
422
+ expect((await (0, util_1.runQuery)(runtime, `sql: timeData is { connection: "${dbName}" select: """SELECT 1"""}
423
+ source: timezone is from_sql(timeData) + {
424
+ timezone: 'America/Los_Angeles'
425
+ dimension: la_time is @2021-02-24 03:05:06
426
+ }
427
+ query: timezone -> {
428
+ group_by: la_time
429
+ }
430
+ `)).resultExplore.queryTimezone).toBe('America/Los_Angeles');
431
+ });
432
+ (0, util_1.testIf)(runtime.supportsNesting)('timezone set in query inside source', async () => {
433
+ expect((await (0, util_1.runQuery)(runtime, `sql: timeData is { connection: "${dbName}" select: """SELECT 1"""}
434
+ source: timezone is from_sql(timeData) + {
435
+ dimension: default_time is @2021-02-24 03:05:06
436
+ query: la_query is {
437
+ timezone: 'America/Los_Angeles'
438
+ project: la_time is @2021-02-24 03:05:06
439
+ }
440
+ }
441
+
442
+ query: timezone -> {
443
+ group_by: default_time
444
+ nest: la_query
445
+ }
446
+ `)).resultExplore.structDef).toMatchObject({
447
+ fields: [
448
+ {},
449
+ { name: 'la_query', queryTimezone: 'America/Los_Angeles' },
450
+ ],
451
+ });
452
+ });
453
+ (0, util_1.testIf)(runtime.supportsNesting)('timezone set in query using source', async () => {
454
+ expect((await (0, util_1.runQuery)(runtime, `sql: timeData is { connection: "${dbName}" select: """SELECT 1"""}
455
+ source: timezone is from_sql(timeData) + {
456
+ dimension: default_time is @2021-02-24 03:05:06
457
+ query: undef_query is {
458
+ project: undef_time is @2021-02-24 03:05:06
459
+ }
460
+ }
461
+
462
+ query: timezone -> {
463
+ timezone: 'America/Los_Angeles'
464
+ group_by: default_time
465
+ nest: undef_query
466
+ }
467
+ `)).resultExplore.queryTimezone).toBe('America/Los_Angeles');
468
+ });
469
+ (0, util_1.testIf)(runtime.supportsNesting)('multiple timezones', async () => {
470
+ expect((await (0, util_1.runQuery)(runtime, `sql: timeData is { connection: "${dbName}" select: """SELECT 1"""}
471
+ source: timezone is from_sql(timeData) + {
472
+ timezone: 'America/New_York'
473
+ dimension: ny_time is @2021-02-24 03:05:06
474
+ query: la_query is {
475
+ timezone: 'America/Los_Angeles'
476
+ project: la_time is @2021-02-24 03:05:06
477
+ }
478
+ query: mex_query is {
479
+ timezone: 'America/Mexico_City'
480
+ project: mex_time is @2021-02-24 03:05:06
481
+ }
482
+ }
483
+
484
+ query: timezone -> {
485
+ group_by: ny_time
486
+ nest: la_query, mex_query
487
+ }
488
+ `)).resultExplore.structDef).toMatchObject({
489
+ queryTimezone: 'America/New_York',
490
+ fields: [
491
+ {},
492
+ { name: 'la_query', queryTimezone: 'America/Los_Angeles' },
493
+ { name: 'mex_query', queryTimezone: 'America/Mexico_City' },
494
+ ],
495
+ });
496
+ });
497
+ });
498
+ });
499
+ /*
500
+ not entirely sure what to test here so i am going to free-wheel a bit
501
+
502
+ 1) All of the extraction and truncation functions need to work
503
+ in the query timezone.
504
+ 2) All rendering needs to happen in the query time zone
505
+ 3) If we feed rendered data back into a query, it needs to retain
506
+ offsets on all timestamps. Worried that rendering it in the query
507
+ time zone would somehow confuse bigquery which is always in UTC
508
+ 4) when we filter on a binned time, that we generate a filter between
509
+ the edges of the bin, instead of computing the bin and use '='
510
+ 5) connection, model, and query time zone setting
511
+ 6) piping a query in one time zone into a query in another
512
+ 7) graphs neeed to respect query time zone
513
+ */
514
+ const zone = 'America/Mexico_City'; // -06:00 no DST
515
+ const zone_2020 = luxon_1.DateTime.fromObject({
516
+ year: 2020,
517
+ month: 2,
518
+ day: 20,
519
+ hour: 0,
520
+ minute: 0,
521
+ second: 0,
522
+ zone,
523
+ });
524
+ const utc_2020 = luxon_1.DateTime.fromObject({
525
+ year: 2020,
526
+ month: 2,
527
+ day: 20,
528
+ hour: 0,
529
+ minute: 0,
530
+ second: 0,
531
+ zone: 'UTC',
532
+ });
533
+ describe.each(runtimes.runtimeList)('%s: tz literals', (dbName, runtime) => {
534
+ test(`${dbName} - default timezone is UTC`, async () => {
535
+ // this makes sure that the tests which use the test timezome are actually
536
+ // testing something ... file this under "abundance of caution". It
537
+ // really tests nothing, but I feel calmer with this here.
538
+ const query = runtime.loadQuery(`
539
+ sql: tzTest is { connection: "${dbName}" select: """SELECT 1 as one""" }
540
+ query: from_sql(tzTest) -> {
541
+ group_by: literalTime is @2020-02-20 00:00:00
542
+ }
543
+ `);
544
+ const result = await query.run();
545
+ const literal = result.data.path(0, 'literalTime').value;
546
+ const have = luxon_1.DateTime.fromJSDate(literal);
547
+ expect(have.valueOf()).toEqual(utc_2020.valueOf());
548
+ });
549
+ test('literal with zone name', async () => {
550
+ const query = runtime.loadQuery(`
551
+ sql: tzTest is { connection: "${dbName}" select: """SELECT 1 as one""" }
552
+ query: from_sql(tzTest) -> {
553
+ group_by: literalTime is @2020-02-20 00:00:00[America/Mexico_City]
554
+ }
555
+ `);
556
+ const result = await query.run();
557
+ const literal = result.data.path(0, 'literalTime').value;
558
+ const have = luxon_1.DateTime.fromJSDate(literal);
559
+ expect(have.valueOf()).toEqual(zone_2020.valueOf());
560
+ });
561
+ });
562
+ describe.each(runtimes.runtimeList)('%s: query tz', (dbName, runtime) => {
563
+ test('literal timestamps', async () => {
564
+ const query = runtime.loadQuery(`
565
+ sql: tzTest is { connection: "${dbName}" select: """SELECT 1 as one""" }
566
+ query: from_sql(tzTest) -> {
567
+ timezone: '${zone}'
568
+ group_by: literalTime is @2020-02-20 00:00:00
569
+ }
570
+ `);
571
+ const result = await query.run();
572
+ const literal = result.data.path(0, 'literalTime').value;
573
+ const have = luxon_1.DateTime.fromJSDate(literal);
574
+ expect(have.valueOf()).toEqual(zone_2020.valueOf());
575
+ });
576
+ test('extract', async () => {
577
+ await expect(runtime).queryMatches(`sql: tzTest is { connection: "${dbName}" select: """SELECT 1 as one""" }
578
+ query: from_sql(tzTest) -> {
579
+ timezone: '${zone}'
580
+ declare: utc_midnight is @2020-02-20 00:00:00[UTC]
581
+ project:
582
+ mex_midnight is hour(utc_midnight)
583
+ mex_day is day(utc_midnight)
584
+ }`, { mex_midnight: 18, mex_day: 19 });
585
+ });
586
+ test('truncate day', async () => {
587
+ // At midnight in london it the 19th in Mexico, so that truncates to
588
+ // midnight on the 19th
589
+ const mex_19 = luxon_1.DateTime.fromISO('2020-02-19T00:00:00', { zone });
590
+ await expect(runtime).queryMatches(`sql: tzTest is { connection: "${dbName}" select: """SELECT 1 as one""" }
591
+ query: from_sql(tzTest) -> {
592
+ timezone: '${zone}'
593
+ declare: utc_midnight is @2020-02-20 00:00:00[UTC]
594
+ project:
595
+ mex_day is utc_midnight.day
596
+ }`, { mex_day: mex_19.toJSDate() });
597
+ });
598
+ test('cast timestamp to date', async () => {
599
+ // At midnight in london it the 19th in Mexico, so when we cast that
600
+ // to a date, it should be the 19th.
601
+ await expect(runtime).queryMatches(`sql: tzTest is { connection: "${dbName}" select: """SELECT 1 as one""" }
602
+ query: from_sql(tzTest) -> {
603
+ timezone: '${zone}'
604
+ declare: utc_midnight is @2020-02-20 00:00:00[UTC]
605
+ project: mex_day is day(utc_midnight::date)
606
+ }`, { mex_day: 19 });
607
+ });
608
+ test('cast date to timestamp', async () => {
609
+ await expect(runtime).queryMatches(`sql: tzTest is { connection: "${dbName}" select: """
610
+ SELECT DATE '2020-02-20' AS mex_20
611
+ """ }
612
+ query: from_sql(tzTest) -> {
613
+ timezone: '${zone}'
614
+ project: mex_ts is mex_20::timestamp
615
+ }`, { mex_ts: zone_2020.toJSDate() });
616
+ });
617
+ test('can use unsupported types', async () => {
618
+ if (dbName === 'bigquery') {
619
+ await expect(runtime).queryMatches(`sql: timeData is { connection: "${dbName}" select: """
620
+ SELECT DATETIME '2020-02-20 00:00:00' as t_datetime
621
+ """}
622
+ query: from_sql(timeData) -> {
623
+ project: mex_220 is t_datetime::timestamp
624
+ }`, { mex_220: utc_2020.toJSDate() });
625
+ }
626
+ else if (dbName === 'duckdb' || dbName === 'postgres') {
627
+ await expect(runtime).queryMatches(`sql: timeData is { connection: "duckdb" select: """
628
+ SELECT TIMESTAMPTZ '2020-02-20 00:00:00 ${zone}' as t_tstz
629
+ """}
630
+ query: from_sql(timeData) -> {
631
+ project: mex_220 is t_tstz::timestamp
632
+ }`, { mex_220: zone_2020.toJSDate() });
633
+ }
634
+ });
635
+ });
636
+ afterAll(async () => {
637
+ await runtimes.closeAll();
638
+ });
639
+ };
640
+ exports.timeSharedTests = timeSharedTests;
641
+ const runtimes = new runtimes_1.RuntimeList(runtimes_1.allDatabases);
642
+ /*
643
+ * This test file reuses common tests definitions.
644
+ * For actual test development please go to: test/src/databases/shared/time.spec.ts
645
+ */
646
+ (0, exports.timeSharedTests)(runtimes);
647
+ //# sourceMappingURL=time.spec.js.map