@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,182 @@
1
+ /*
2
+ * Copyright 2023 Google LLC
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files
6
+ * (the "Software"), to deal in the Software without restriction,
7
+ * including without limitation the rights to use, copy, modify, merge,
8
+ * publish, distribute, sublicense, and/or sell copies of the Software,
9
+ * and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {
25
+ Connection,
26
+ EmptyURLReader,
27
+ MalloyQueryData,
28
+ QueryDataRow,
29
+ Result,
30
+ RunSQLOptions,
31
+ SingleConnectionRuntime,
32
+ } from '@malloydata/malloy';
33
+ import {BigQueryConnection} from '@malloydata/db-bigquery';
34
+ import {DuckDBConnection} from '@malloydata/db-duckdb';
35
+ import {DuckDBWASMConnection} from '@malloydata/db-duckdb/wasm';
36
+ import {PooledPostgresConnection} from '@malloydata/db-postgres';
37
+
38
+ export class BigQueryTestConnection extends BigQueryConnection {
39
+ // we probably need a better way to do this.
40
+
41
+ public async runSQL(
42
+ sqlCommand: string,
43
+ options?: RunSQLOptions
44
+ ): Promise<MalloyQueryData> {
45
+ try {
46
+ return await super.runSQL(sqlCommand, options);
47
+ } catch (e) {
48
+ // eslint-disable-next-line no-console
49
+ console.log(`Error in SQL:\n ${sqlCommand}`);
50
+ throw e;
51
+ }
52
+ }
53
+ }
54
+
55
+ export class PostgresTestConnection extends PooledPostgresConnection {
56
+ // we probably need a better way to do this.
57
+
58
+ public async runSQL(
59
+ sqlCommand: string,
60
+ options?: RunSQLOptions
61
+ ): Promise<MalloyQueryData> {
62
+ try {
63
+ return await super.runSQL(sqlCommand, options);
64
+ } catch (e) {
65
+ // eslint-disable-next-line no-console
66
+ console.log(`Error in SQL:\n ${sqlCommand}`);
67
+ throw e;
68
+ }
69
+ }
70
+ }
71
+
72
+ export class DuckDBTestConnection extends DuckDBConnection {
73
+ // we probably need a better way to do this.
74
+
75
+ constructor(name: string) {
76
+ super(name, 'test/data/duckdb/duckdb_test.db');
77
+ }
78
+
79
+ public async runSQL(
80
+ sqlCommand: string,
81
+ options?: RunSQLOptions
82
+ ): Promise<MalloyQueryData> {
83
+ try {
84
+ return await super.runSQL(sqlCommand, options);
85
+ } catch (e) {
86
+ // eslint-disable-next-line no-console
87
+ console.log(`Error in SQL:\n ${sqlCommand}`);
88
+ throw e;
89
+ }
90
+ }
91
+ }
92
+
93
+ export class DuckDBWASMTestConnection extends DuckDBWASMConnection {
94
+ // we probably need a better way to do this.
95
+
96
+ constructor(name: string) {
97
+ super(name, 'test/data/duckdb/duckdb_test.db');
98
+ }
99
+
100
+ public async runSQL(
101
+ sqlCommand: string,
102
+ options?: RunSQLOptions
103
+ ): Promise<MalloyQueryData> {
104
+ try {
105
+ return await super.runSQL(sqlCommand, options);
106
+ } catch (e) {
107
+ // eslint-disable-next-line no-console
108
+ console.log(`Error in SQL:\n ${sqlCommand}`);
109
+ throw e;
110
+ }
111
+ }
112
+ }
113
+
114
+ const files = new EmptyURLReader();
115
+
116
+ export function rows(qr: Result): QueryDataRow[] {
117
+ return qr.data.value;
118
+ }
119
+
120
+ export function runtimeFor(dbName: string): SingleConnectionRuntime {
121
+ let connection;
122
+ switch (dbName) {
123
+ case 'bigquery':
124
+ connection = new BigQueryTestConnection(
125
+ dbName,
126
+ {},
127
+ {defaultProject: 'malloy-data'}
128
+ );
129
+ break;
130
+ case 'postgres':
131
+ connection = new PostgresTestConnection(dbName);
132
+ break;
133
+ case 'duckdb':
134
+ connection = new DuckDBTestConnection(dbName);
135
+ break;
136
+ case 'duckdb_wasm':
137
+ connection = new DuckDBWASMTestConnection(dbName);
138
+ break;
139
+ default:
140
+ throw new Error(`Unknown runtime "${dbName}`);
141
+ }
142
+ return testRuntimeFor(connection);
143
+ }
144
+
145
+ export function testRuntimeFor(connection: Connection) {
146
+ return new SingleConnectionRuntime(files, connection);
147
+ }
148
+
149
+ export const allDatabases = ['postgres', 'bigquery', 'duckdb', 'duckdb_wasm'];
150
+ type RuntimeDatabaseNames = typeof allDatabases[number];
151
+
152
+ export class RuntimeList {
153
+ runtimeMap = new Map<string, SingleConnectionRuntime>();
154
+ runtimeList: Array<[string, SingleConnectionRuntime]> = [];
155
+
156
+ constructor();
157
+ constructor(databaseList: RuntimeDatabaseNames[]);
158
+ constructor(externalConnections: SingleConnectionRuntime[]);
159
+ constructor(
160
+ ...args: (RuntimeDatabaseNames[] | undefined | SingleConnectionRuntime[])[]
161
+ ) {
162
+ const databases: RuntimeDatabaseNames[] | SingleConnectionRuntime[] =
163
+ args.length > 0 && args[0] !== undefined ? args[0] : allDatabases;
164
+ for (const database of databases) {
165
+ const rt: SingleConnectionRuntime =
166
+ database instanceof SingleConnectionRuntime
167
+ ? database
168
+ : runtimeFor(database);
169
+ this.runtimeMap.set(rt.connection.name, rt);
170
+ this.runtimeList.push([rt.connection.name, rt]);
171
+ }
172
+ }
173
+
174
+ async closeAll(): Promise<void> {
175
+ for (const [_key, runtime] of this.runtimeMap) {
176
+ await runtime.connection.close();
177
+ }
178
+ // Unfortunate hack to avoid slow to die background threads tripping
179
+ // up jest
180
+ await new Promise(resolve => setTimeout(resolve, 10000));
181
+ }
182
+ }
@@ -0,0 +1,277 @@
1
+ /*
2
+ * Copyright 2023 Google LLC
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files
6
+ * (the "Software"), to deal in the Software without restriction,
7
+ * including without limitation the rights to use, copy, modify, merge,
8
+ * publish, distribute, sublicense, and/or sell copies of the Software,
9
+ * and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {Tags, Annotation, MalloyTags} from '@malloydata/malloy';
25
+
26
+ function tstTaglist(a: Annotation): string[] {
27
+ return new Tags(a).getTagList();
28
+ }
29
+
30
+ function tstTagParse(s: string): MalloyTags {
31
+ return new Tags({notes: [s]}).getMalloyTags();
32
+ }
33
+
34
+ describe('tag utilities', () => {
35
+ test('own/block ordering is correct', () => {
36
+ const testA = tstTaglist({
37
+ notes: ['#tag'],
38
+ blockNotes: ['#blockTag'],
39
+ });
40
+ expect(testA).toEqual(['#blockTag', '#tag']);
41
+ });
42
+ test('inherit ordering is correct', () => {
43
+ const testA = tstTaglist({
44
+ inherits: {notes: ['#inherited']},
45
+ notes: ['#tag'],
46
+ });
47
+ expect(testA).toEqual(['#inherited', '#tag']);
48
+ });
49
+ test.todo('check that results are annotated');
50
+ test('doc annotations', () => {
51
+ const a = tstTagParse('#" This is a doc string');
52
+ expect(a).toMatchObject({docStrings: ['This is a doc string']});
53
+ });
54
+ test('simple property', () => {
55
+ const a = tstTagParse('# linechart');
56
+ expect(a.properties).toHaveProperty('linechart', true);
57
+ });
58
+ test('negated property', () => {
59
+ const a = tstTagParse('# linechart -linechart');
60
+ expect(a.properties).not.toHaveProperty('linechart', true);
61
+ });
62
+ test('simple quoted property', () => {
63
+ const a = tstTagParse('# "linechart"');
64
+ expect(a.properties).toHaveProperty('linechart', true);
65
+ });
66
+ test('quoted property with " and space', () => {
67
+ const annotation = '# "a \\"chart\\""';
68
+ const a = tstTagParse(annotation);
69
+ expect(a.properties).toHaveProperty('a "chart"', true);
70
+ });
71
+ test('escaped non-quote', () => {
72
+ const annotation = '# "\\x"';
73
+ const a = tstTagParse(annotation);
74
+ expect(a.properties).toHaveProperty('x', true);
75
+ });
76
+ test('non-terminated string', () => {
77
+ const annotation = '# x="\\"';
78
+ const a = tstTagParse(annotation);
79
+ expect(a.properties).not.toHaveProperty('x', true);
80
+ });
81
+ test('quoted property with value', () => {
82
+ const a = tstTagParse('# "linechart"=yes');
83
+ expect(a.properties).toHaveProperty('linechart', 'yes');
84
+ });
85
+ test('property with simple value', () => {
86
+ const a = tstTagParse('# chart=line');
87
+ expect(a.properties).toHaveProperty('chart', 'line');
88
+ });
89
+ test('property with quoted value', () => {
90
+ const a = tstTagParse('# chart="line"');
91
+ expect(a.properties).toHaveProperty('chart', 'line');
92
+ });
93
+ test('spaces ignored', () => {
94
+ const a = tstTagParse('# chart = line ');
95
+ expect(a.properties).toHaveProperty('chart', 'line');
96
+ });
97
+ test('= with no value', () => {
98
+ expect(tstTagParse('# name = ')).toMatchObject({properties: {}});
99
+ });
100
+ test('multiple = with no value', () => {
101
+ expect(tstTagParse('# name = = me')).toMatchObject({properties: {}});
102
+ });
103
+ test('missing quote', () => {
104
+ expect(tstTagParse('# name = "no quote')).toMatchObject({properties: {}});
105
+ });
106
+ test('leading =', () => {
107
+ expect(tstTagParse('# =name ')).toMatchObject({properties: {}});
108
+ });
109
+ test('complex line', () => {
110
+ const a = tstTagParse('# a b=c "d"=e f="g" "h"="i" "j" k -a');
111
+ expect(a).toBeDefined();
112
+ if (a) {
113
+ expect(a.properties).toEqual({
114
+ b: 'c',
115
+ d: 'e',
116
+ f: 'g',
117
+ h: 'i',
118
+ j: true,
119
+ k: true,
120
+ });
121
+ }
122
+ });
123
+ });
124
+
125
+ import {runtimeFor} from './runtimes';
126
+
127
+ const runtime = runtimeFor('duckdb');
128
+
129
+ describe('## top level', () => {
130
+ test('top level tags are available in the model def', async () => {
131
+ const model = await runtime
132
+ .loadModel(
133
+ `
134
+ ## propertyTag
135
+ ##" Doc String
136
+ `
137
+ )
138
+ .getModel();
139
+ const modelDesc = model.getTags().getMalloyTags();
140
+ expect(modelDesc).toEqual({
141
+ properties: {propertyTag: true},
142
+ docStrings: ['Doc String\n'],
143
+ });
144
+ });
145
+ });
146
+ describe('tags in results', () => {
147
+ test('nameless query', async () => {
148
+ const loaded = runtime.loadQuery(
149
+ `
150
+ sql: one is {connection: "duckdb" select: """SELECT 1 as one"""}
151
+ # b4query
152
+ query: # afterQuery
153
+ from_sql(one) -> { project: * }`
154
+ );
155
+ const query = await loaded.getPreparedQuery();
156
+ expect(query).toBeDefined();
157
+ const wantTags = ['# b4query\n', '# afterQuery\n'];
158
+ expect(query.getTags().getTagList()).toEqual(wantTags);
159
+ const result = await loaded.run();
160
+ expect(result.getTags().getTagList()).toEqual(wantTags);
161
+ });
162
+ const wantTags = ['# <Q\n', '# >Q\n', '# >name\n', '# >is\n'];
163
+ test('named query', async () => {
164
+ const loaded = runtime.loadQuery(
165
+ `
166
+ sql: one is {connection: "duckdb" select: """SELECT 1 as one"""}
167
+ # <Q
168
+ query: # >Q
169
+ theName
170
+ # >name
171
+ is
172
+ # >is
173
+ from_sql(one) -> { project: * }
174
+ query: -> theName`
175
+ );
176
+ const query = await loaded.getPreparedQuery();
177
+ expect(query).toBeDefined();
178
+ const wantTags = ['# <Q\n', '# >Q\n', '# >name\n', '# >is\n'];
179
+ expect(query.getTags().getTagList()).toEqual(wantTags);
180
+ const result = await loaded.run();
181
+ expect(result.getTags().getTagList()).toEqual(wantTags);
182
+ });
183
+ test('turtle query', async () => {
184
+ const loaded = runtime.loadQuery(
185
+ `
186
+ sql: one is {connection: "duckdb" select: """SELECT 1 as one"""}
187
+ query: from_sql(one) + {
188
+ # <Q
189
+ query: # >Q
190
+ in_one
191
+ # >name
192
+ is
193
+ # >is
194
+ { project: one }
195
+ }
196
+ -> in_one`
197
+ );
198
+ const query = await loaded.getPreparedQuery();
199
+ expect(query).toBeDefined();
200
+ expect(query.getTags().getTagList()).toEqual(wantTags);
201
+ const result = await loaded.run();
202
+ expect(result.getTags().getTagList()).toEqual(wantTags);
203
+ });
204
+ test('atomic field has tag', async () => {
205
+ const loaded = runtime.loadQuery(
206
+ `
207
+ sql: one is {connection: "duckdb" select: """SELECT 1 as one"""}
208
+ query: from_sql(one) -> {
209
+ project:
210
+ # note1
211
+ one
212
+ }`
213
+ );
214
+ const result = await loaded.run();
215
+ const shape = result.resultExplore;
216
+ const one = shape.getFieldByName('one');
217
+ expect(one).toBeDefined();
218
+ expect(one.getTags().getTagList()).toEqual(['# note1\n']);
219
+ });
220
+ test('nested query has tag', async () => {
221
+ const loaded = runtime.loadQuery(
222
+ `
223
+ sql: one is {connection: "duckdb" select: """SELECT 1 as one"""}
224
+ source: malloy_one is from_sql(one) + {
225
+ query: in_one is {
226
+ project: one
227
+ }
228
+ query: one_and_one is {
229
+ group_by: one
230
+ # note1
231
+ nest:
232
+ # note2
233
+ in_one
234
+ }
235
+ }
236
+ query: malloy_one -> one_and_one`
237
+ );
238
+ const result = await loaded.run();
239
+ const shape = result.resultExplore;
240
+ const one = shape.getFieldByName('in_one');
241
+ expect(one).toBeDefined();
242
+ expect(one.getTags().getTagList()).toEqual(['# note1\n', '# note2\n']);
243
+ });
244
+ test('render usage test case', async () => {
245
+ const loaded = runtime.loadQuery(
246
+ `
247
+ sql: one22 is { connection: "duckdb" select: """SELECT 1""" }
248
+ source: ages is from_sql(one22) + {
249
+ dimension: name is 'John'
250
+ query: height
251
+ # barchart
252
+ is {
253
+ project: heightd is 10
254
+ }
255
+
256
+ query: age
257
+ # barchart
258
+ is {
259
+ project: aged is 20
260
+ }
261
+
262
+ }
263
+ query: ages -> {
264
+ group_by: name
265
+ nest: height
266
+ nest: age
267
+ }
268
+ `
269
+ );
270
+ const result = await loaded.run();
271
+ const shape = result.resultExplore;
272
+ const ht = shape.getFieldByName('height').getTags().getMalloyTags();
273
+ const at = shape.getFieldByName('age').getTags().getMalloyTags();
274
+ expect(ht).toMatchObject({properties: {barchart: true}});
275
+ expect(at).toMatchObject({properties: {barchart: true}});
276
+ });
277
+ });
@@ -0,0 +1,128 @@
1
+ /*
2
+ * Copyright 2023 Google LLC
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining
5
+ * a copy of this software and associated documentation files
6
+ * (the "Software"), to deal in the Software without restriction,
7
+ * including without limitation the rights to use, copy, modify, merge,
8
+ * publish, distribute, sublicense, and/or sell copies of the Software,
9
+ * and to permit persons to whom the Software is furnished to do so,
10
+ * subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be
13
+ * included in all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {
25
+ QueryMaterializer,
26
+ Result,
27
+ SingleConnectionRuntime,
28
+ } from '@malloydata/malloy';
29
+
30
+ declare global {
31
+ // eslint-disable-next-line @typescript-eslint/no-namespace
32
+ namespace jest {
33
+ interface Matchers<R> {
34
+ isSqlEq(): R;
35
+ /**
36
+ * Jest matcher for running a Malloy query, assumes you only care about
37
+ * columns in the first row.
38
+ *
39
+ * await expect(runtime).queryMatches('query', {colName: colValue});
40
+ *
41
+ * @param runtime Database connection runtime
42
+ * @param querySrc Malloy source, last query in source will be run
43
+ * @param expected Key value pairs
44
+ */
45
+ queryMatches(
46
+ querySrc: string,
47
+ matchVals: Record<string, unknown>
48
+ ): Promise<R>;
49
+ }
50
+ }
51
+ }
52
+
53
+ expect.extend({
54
+ /**
55
+ * Check the return of `sqlEQ(expr1,expr2)` and error if the database
56
+ * does not find those two expressions to be equal.
57
+ */
58
+ isSqlEq(result: Result) {
59
+ const wantEq = result.data.path(0, 'calc').value;
60
+ const sql = result.sql.replace(/\n/g, '\n ');
61
+ if (wantEq !== '=') {
62
+ return {
63
+ pass: false,
64
+ message: () =>
65
+ `Got '${wantEq}' ${Object.prototype.toString.call(
66
+ wantEq
67
+ )} instead of '='\nSQL:\n ${sql}`,
68
+ };
69
+ }
70
+ return {
71
+ pass: true,
72
+ message: () => 'SQL expression matched',
73
+ };
74
+ },
75
+
76
+ async queryMatches(
77
+ runtime: SingleConnectionRuntime,
78
+ querySrc: string,
79
+ expected: Record<string, unknown>
80
+ ) {
81
+ if (!runtime.supportsNesting && querySrc.indexOf('nest:') >= 0) {
82
+ return {
83
+ pass: true,
84
+ message: () =>
85
+ 'Test was skipped since connection does not support nesting.',
86
+ };
87
+ }
88
+
89
+ let query: QueryMaterializer;
90
+ try {
91
+ query = runtime.loadQuery(querySrc);
92
+ } catch (e) {
93
+ return {pass: false, message: () => `loadQuery failed: ${e.message}`};
94
+ }
95
+
96
+ let result: Result;
97
+ try {
98
+ result = await query.run();
99
+ } catch (e) {
100
+ const failMsg =
101
+ `query.run failed: ${e.message}\n` + `SQL: ${await query.getSQL()}`;
102
+ return {pass: false, message: () => failMsg};
103
+ }
104
+
105
+ const fails: string[] = [];
106
+ for (const [name, value] of Object.entries(expected)) {
107
+ try {
108
+ const got = result.data.path(0, name).value;
109
+ const mustBe = value instanceof Date ? value.getTime() : value;
110
+ const actuallyGot = got instanceof Date ? got.getTime() : got;
111
+ if (actuallyGot !== mustBe) {
112
+ fails.push(`Expected {${name}: ${value}} Got: ${got}`);
113
+ }
114
+ } catch (e) {
115
+ fails.push(`Expected {${name}: ${value}} Error: ${e.message}`);
116
+ }
117
+ }
118
+ if (fails.length !== 0) {
119
+ const failMsg = `SQL: ${await query.getSQL()}\n${fails.join('\n')}`;
120
+ return {pass: false, message: () => failMsg};
121
+ }
122
+
123
+ return {
124
+ pass: true,
125
+ message: () => `First row matched ${JSON.stringify(expected)}`,
126
+ };
127
+ },
128
+ });