-
Notifications
You must be signed in to change notification settings - Fork 21
/
node_query_format_types.test.ts
632 lines (587 loc) · 19.8 KB
/
node_query_format_types.test.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import type { ResultSet } from '../../src'
import type {
ClickHouseClient as BaseClickHouseClient,
DataFormat,
} from '@clickhouse/client-common'
import { createTableWithFields } from '@test/fixtures/table_with_fields'
import { guid } from '@test/utils'
import type { ClickHouseClient } from '../../src'
import { createNodeTestClient } from '../utils/node_client'
// Ignored and used only as a source for ESLint checks with $ExpectType
// See also: https://www.npmjs.com/package/eslint-plugin-expect-type
xdescribe('[Node.js] Query and ResultSet types', () => {
let client: ClickHouseClient
const tableName = `node_query_format_types_test_${guid()}`
const query = `SELECT * FROM ${tableName} ORDER BY id ASC`
beforeAll(async () => {
client = createNodeTestClient()
await createTableWithFields(
client as BaseClickHouseClient,
'name String, sku Array(UInt32)',
{},
tableName,
)
await client.insert({
table: tableName,
values: [
{ id: 42, name: 'foo', sku: [1, 2, 3] },
{ id: 43, name: 'bar', sku: [4, 5, 6] },
],
format: 'JSONEachRow',
})
})
afterAll(async () => {
await client.close()
})
describe('Streamable JSON formats', () => {
it('should infer types for JSONEachRow', async () => {
// $ExpectType ResultSet<"JSONEachRow">
const rs = await client.query({
query,
format: 'JSONEachRow',
})
// $ExpectType unknown[]
await rs.json()
// $ExpectType Data[]
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType StreamReadable<Row<unknown, "JSONEachRow">[]>
const stream = rs.stream()
// stream + on('data')
await new Promise((resolve, reject) => {
stream
.on(
'data',
// $ExpectType (rows: Row<unknown, "JSONEachRow">[]) => void
(rows) => {
rows.forEach(
// $ExpectType (row: Row<unknown, "JSONEachRow">) => void
(row) => {
// $ExpectType unknown
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
},
)
.on('end', resolve)
.on('error', reject)
})
// stream + async iterator
for await (const _rows of stream) {
// $ExpectType Row<unknown, "JSONEachRow">[]
const rows = _rows
rows.length // avoid unused variable warning (rows reassigned for type assertion)
rows.forEach(
// $ExpectType (row: Row<unknown, "JSONEachRow">) => void
(row) => {
// $ExpectType unknown
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
}
// stream + T hint + on('data')
const streamTyped = rs.stream<Data>()
await new Promise((resolve, reject) => {
streamTyped
.on(
'data',
// $ExpectType (rows: Row<Data, "JSONEachRow">[]) => void
(rows) => {
rows.forEach(
// $ExpectType (row: Row<Data, "JSONEachRow">) => void
(row) => {
// $ExpectType Data
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
},
)
.on('end', resolve)
.on('error', reject)
})
// stream + T hint + async iterator
for await (const _rows of streamTyped) {
// $ExpectType Row<Data, "JSONEachRow">[]
const rows = _rows
rows.length // avoid unused variable warning (rows reassigned for type assertion)
rows.forEach(
// $ExpectType (row: Row<Data, "JSONEachRow">) => void
(row) => {
// $ExpectType Data
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
}
})
it('should infer ResultSet features when similar JSON formats are used in a function call', async () => {
// $ExpectType (format: "JSONEachRow" | "JSONCompactEachRow") => Promise<ResultSet<"JSONEachRow" | "JSONCompactEachRow">>
function runQuery(format: 'JSONEachRow' | 'JSONCompactEachRow') {
return client.query({
query,
format,
})
}
// ResultSet cannot infer the type from the literal, so it falls back to both possible formats.
// However, these are both streamable, both can use JSON features, and both have the same data layout.
//// JSONCompactEachRow
// $ExpectType ResultSet<"JSONEachRow" | "JSONCompactEachRow">
const rs = await runQuery('JSONCompactEachRow')
// $ExpectType unknown[]
await rs.json()
// $ExpectType Data[]
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType StreamReadable<Row<unknown, "JSONEachRow" | "JSONCompactEachRow">[]>
const stream = rs.stream()
// stream + on('data')
await new Promise((resolve, reject) => {
stream
.on(
'data',
// $ExpectType (rows: Row<unknown, "JSONEachRow" | "JSONCompactEachRow">[]) => void
(rows) => {
rows.forEach(
// $ExpectType (row: Row<unknown, "JSONEachRow" | "JSONCompactEachRow">) => void
(row) => {
// $ExpectType unknown
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
},
)
.on('end', resolve)
.on('error', reject)
})
// stream + async iterator
for await (const _rows of stream) {
// $ExpectType Row<unknown, "JSONEachRow" | "JSONCompactEachRow">[]
const rows = _rows
rows.length // avoid unused variable warning (rows reassigned for type assertion)
rows.forEach(
// $ExpectType (row: Row<unknown, "JSONEachRow" | "JSONCompactEachRow">) => void
(row) => {
// $ExpectType unknown
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
}
//// JSONEachRow
// $ExpectType ResultSet<"JSONEachRow" | "JSONCompactEachRow">
const rs2 = await runQuery('JSONEachRow')
// $ExpectType unknown[]
await rs2.json()
// $ExpectType Data[]
await rs2.json<Data>()
// $ExpectType string
await rs2.text()
// $ExpectType StreamReadable<Row<unknown, "JSONEachRow" | "JSONCompactEachRow">[]>
const stream2 = rs2.stream()
// stream + on('data')
await new Promise((resolve, reject) => {
stream2
.on(
'data',
// $ExpectType (rows: Row<unknown, "JSONEachRow" | "JSONCompactEachRow">[]) => void
(rows) => {
rows.forEach(
// $ExpectType (row: Row<unknown, "JSONEachRow" | "JSONCompactEachRow">) => void
(row) => {
// $ExpectType unknown
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
},
)
.on('end', resolve)
.on('error', reject)
})
// stream + async iterator
for await (const _rows of stream2) {
// $ExpectType Row<unknown, "JSONEachRow" | "JSONCompactEachRow">[]
const rows = _rows
rows.length // avoid unused variable warning (rows reassigned for type assertion)
rows.forEach(
// $ExpectType (row: Row<unknown, "JSONEachRow" | "JSONCompactEachRow">) => void
(row) => {
// $ExpectType unknown
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
}
})
/**
* Not covered, but should behave similarly:
* 'JSONStringsEachRow',
* 'JSONCompactStringsEachRow',
* 'JSONCompactEachRowWithNames',
* 'JSONCompactEachRowWithNamesAndTypes',
* 'JSONCompactStringsEachRowWithNames',
* 'JSONCompactStringsEachRowWithNamesAndTypes'
*/
})
describe('Single document JSON formats', () => {
it('should infer types when the format is omitted (JSON)', async () => {
// $ExpectType ResultSet<"JSON">
const rs = await client.query({
query,
})
// $ExpectType ResponseJSON<unknown>
await rs.json()
// $ExpectType ResponseJSON<Data>
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType never
rs.stream()
})
it('should infer types for JSON', async () => {
// $ExpectType ResultSet<"JSON">
const rs = await client.query({
query,
format: 'JSON',
})
// $ExpectType ResponseJSON<unknown>
await rs.json()
// $ExpectType ResponseJSON<Data>
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType never
rs.stream()
})
it('should infer types for JSONObjectEachRow', async () => {
// $ExpectType ResultSet<"JSONObjectEachRow">
const rs = await client.query({
query,
format: 'JSONObjectEachRow',
})
// $ExpectType Record<string, unknown>
await rs.json()
// $ExpectType Record<string, Data>
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType never
rs.stream()
})
/**
* Not covered, but should behave similarly:
* 'JSONStrings',
* 'JSONCompact',
* 'JSONCompactStrings',
* 'JSONColumnsWithMetadata',
*/
})
describe('Raw formats', () => {
it('should infer types for CSV', async () => {
// $ExpectType ResultSet<"CSV">
const rs = await client.query({
query,
format: 'CSV',
})
// $ExpectType never
await rs.json()
// $ExpectType never
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType StreamReadable<Row<unknown, "CSV">[]>
const stream = rs.stream()
// stream + on('data')
await new Promise((resolve, reject) => {
stream
.on(
'data',
// $ExpectType (rows: Row<unknown, "CSV">[]) => void
(rows) => {
rows.forEach(
// $ExpectType (row: Row<unknown, "CSV">) => void
(row) => {
// $ExpectType never
row.json()
// $ExpectType never
row.json<Data>()
// $ExpectType string
row.text
},
)
},
)
.on('end', resolve)
.on('error', reject)
})
// stream + async iterator
for await (const _rows of stream) {
// $ExpectType Row<unknown, "CSV">[]
const rows = _rows
rows.length // avoid unused variable warning (rows reassigned for type assertion)
rows.forEach(
// $ExpectType (row: Row<unknown, "CSV">) => void
(row) => {
// $ExpectType never
row.json()
// $ExpectType never
row.json<Data>()
// $ExpectType string
row.text
},
)
}
})
it('should infer ResultSet features when similar raw formats are used in a function call', async () => {
// $ExpectType (format: "CSV" | "TabSeparated") => Promise<ResultSet<"CSV" | "TabSeparated">>
function runQuery(format: 'CSV' | 'TabSeparated') {
return client.query({
query,
format,
})
}
// ResultSet cannot infer the type from the literal, so it falls back to both possible formats.
// However, these are both streamable, and both cannot use JSON features.
//// CSV
// $ExpectType ResultSet<"CSV" | "TabSeparated">
const rs = await runQuery('CSV')
// $ExpectType never
await rs.json()
// $ExpectType never
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType StreamReadable<Row<unknown, "CSV" | "TabSeparated">[]>
const stream = rs.stream()
// stream + on('data')
await new Promise((resolve, reject) => {
stream
.on(
'data',
// $ExpectType (rows: Row<unknown, "CSV" | "TabSeparated">[]) => void
(rows) => {
rows.forEach(
// $ExpectType (row: Row<unknown, "CSV" | "TabSeparated">) => void
(row) => {
// $ExpectType never
row.json()
// $ExpectType never
row.json<Data>()
// $ExpectType string
row.text
},
)
},
)
.on('end', resolve)
.on('error', reject)
})
// stream + async iterator
for await (const _rows of stream) {
// $ExpectType Row<unknown, "CSV" | "TabSeparated">[]
const rows = _rows
rows.length // avoid unused variable warning (rows reassigned for type assertion)
rows.forEach(
// $ExpectType (row: Row<unknown, "CSV" | "TabSeparated">) => void
(row) => {
// $ExpectType never
row.json()
// $ExpectType never
row.json<Data>()
// $ExpectType string
row.text
},
)
}
//// TabSeparated
// $ExpectType ResultSet<"CSV" | "TabSeparated">
const rs2 = await runQuery('TabSeparated')
// $ExpectType never
await rs2.json()
// $ExpectType never
await rs2.json<Data>()
// $ExpectType string
await rs2.text()
// $ExpectType StreamReadable<Row<unknown, "CSV" | "TabSeparated">[]>
const stream2 = rs2.stream()
// stream + on('data')
await new Promise((resolve, reject) => {
stream2
.on(
'data',
// $ExpectType (rows: Row<unknown, "CSV" | "TabSeparated">[]) => void
(rows) => {
rows.forEach(
// $ExpectType (row: Row<unknown, "CSV" | "TabSeparated">) => void
(row) => {
// $ExpectType never
row.json()
// $ExpectType never
row.json<Data>()
// $ExpectType string
row.text
},
)
},
)
.on('end', resolve)
.on('error', reject)
})
// stream + async iterator
for await (const _rows of stream2) {
// $ExpectType Row<unknown, "CSV" | "TabSeparated">[]
const rows = _rows
rows.length // avoid unused variable warning (rows reassigned for type assertion)
rows.forEach(
// $ExpectType (row: Row<unknown, "CSV" | "TabSeparated">) => void
(row) => {
// $ExpectType never
row.json()
// $ExpectType never
row.json<Data>()
// $ExpectType string
row.text
},
)
}
})
/**
* Not covered, but should behave similarly:
* 'CSVWithNames',
* 'CSVWithNamesAndTypes',
* 'TabSeparatedRaw',
* 'TabSeparatedWithNames',
* 'TabSeparatedWithNamesAndTypes',
* 'CustomSeparated',
* 'CustomSeparatedWithNames',
* 'CustomSeparatedWithNamesAndTypes',
* 'Parquet',
*/
})
describe('Type inference with ambiguous format variants', () => {
// expect-type itself fails a bit here sometimes. It can get a wrong order of the variants = flaky ESLint run.
type JSONFormat = 'JSON' | 'JSONEachRow'
type ResultSetJSONFormat = ResultSet<JSONFormat>
// TODO: Maybe there is a way to infer the format without an extra type parameter?
it('should infer types for JSON or JSONEachRow (no extra type params)', async () => {
function runQuery(format: JSONFormat): Promise<ResultSetJSONFormat> {
return client.query({
query,
format,
})
}
// ResultSet falls back to both possible formats (both JSON and JSONEachRow); 'JSON' string provided to `runQuery`
// cannot be used to narrow down the literal type, since the function argument is just DataFormat.
// $ExpectType ResultSetJSONFormat
const rs = await runQuery('JSON')
// $ExpectType unknown[] | ResponseJSON<unknown>
await rs.json()
// $ExpectType Data[] | ResponseJSON<Data>
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType StreamReadable<Row<unknown, JSONFormat>[]>
rs.stream()
})
it('should infer types for JSON or JSONEachRow (with extra type parameter)', async () => {
// $ExpectType <F extends JSONFormat>(format: F) => Promise<QueryResult<F>>
function runQuery<F extends JSONFormat>(format: F) {
return client.query({
query,
format,
})
}
// $ExpectType ResultSet<"JSON">
const rs = await runQuery('JSON')
// $ExpectType ResponseJSON<unknown>
await rs.json()
// $ExpectType ResponseJSON<Data>
await rs.json<Data>()
// $ExpectType string
await rs.text()
// $ExpectType never
rs.stream()
// $ExpectType ResultSet<"JSONEachRow">
const rs2 = await runQuery('JSONEachRow')
// $ExpectType unknown[]
await rs2.json()
// $ExpectType Data[]
await rs2.json<Data>()
// $ExpectType string
await rs2.text()
// $ExpectType StreamReadable<Row<unknown, "JSONEachRow">[]>
rs2.stream()
})
it('should fail to infer the types when the format is any', async () => {
// In a separate function, which breaks the format inference from the literal (due to "generic" DataFormat usage)
// $ExpectType (format: DataFormat) => Promise<ResultSet<unknown>>
function runQuery(format: DataFormat) {
return client.query({
query,
format,
})
}
// ResultSet falls back to all possible formats; 'JSON' string provided as an argument to `runQuery`
// cannot be used to narrow down the literal type, since the function argument is just DataFormat.
// $ExpectType ResultSet<unknown>
const rs = await runQuery('JSON')
// All possible JSON variants are now allowed
// $ExpectType unknown[] | Record<string, unknown> | ResponseJSON<unknown>
await rs.json() // IDE error here, different type order
// $ExpectType Data[] | ResponseJSON<Data> | Record<string, Data>
await rs.json<Data>()
// $ExpectType string
await rs.text()
// Stream is still allowed (can't be inferred, so it is not "never")
// $ExpectType StreamReadable<Row<unknown, unknown>[]>
const stream = rs.stream()
for await (const _rows of stream) {
// $ExpectType Row<unknown, unknown>[]
const rows = _rows
rows.length // avoid unused variable warning (rows reassigned for type assertion)
rows.forEach(
// $ExpectType (row: Row<unknown, unknown>) => void
(row) => {
// $ExpectType unknown
row.json()
// $ExpectType Data
row.json<Data>()
// $ExpectType string
row.text
},
)
}
})
})
})
type Data = { id: number; name: string; sku: number[] }