|
1 | | -import type { Row } from '@clickhouse/client-common' |
| 1 | +import type { DataFormat, Row } from '@clickhouse/client-common' |
2 | 2 | import { guid } from '@test/utils' |
3 | 3 | import Stream, { Readable } from 'stream' |
4 | 4 | import { ResultSet } from '../../src' |
@@ -63,10 +63,81 @@ describe('[Node.js] ResultSet', () => { |
63 | 63 | expect(row.json()).toEqual({ foo: 'bar' }) |
64 | 64 | }) |
65 | 65 |
|
66 | | - function makeResultSet(stream: Stream.Readable) { |
| 66 | + describe('unhandled exceptions with streamable JSON formats', () => { |
| 67 | + const logAndQuit = (err: Error | unknown, prefix: string) => { |
| 68 | + console.error(prefix, err) |
| 69 | + process.exit(1) |
| 70 | + } |
| 71 | + const uncaughtExceptionListener = (err: Error) => |
| 72 | + logAndQuit(err, 'uncaughtException:') |
| 73 | + const unhandledRejectionListener = (err: unknown) => |
| 74 | + logAndQuit(err, 'unhandledRejection:') |
| 75 | + |
| 76 | + const invalidJSON = 'invalid":"foo"}\n' |
| 77 | + |
| 78 | + beforeAll(() => { |
| 79 | + process.on('uncaughtException', uncaughtExceptionListener) |
| 80 | + process.on('unhandledRejection', unhandledRejectionListener) |
| 81 | + }) |
| 82 | + afterAll(() => { |
| 83 | + process.off('uncaughtException', uncaughtExceptionListener) |
| 84 | + process.off('unhandledRejection', unhandledRejectionListener) |
| 85 | + }) |
| 86 | + |
| 87 | + describe('Streamable JSON formats - JSONEachRow', () => { |
| 88 | + it('should not be produced (ResultSet.text)', async () => { |
| 89 | + const rs = makeResultSet( |
| 90 | + Stream.Readable.from([Buffer.from(invalidJSON)]), |
| 91 | + ) |
| 92 | + const text = await rs.text() |
| 93 | + expect(text).toEqual(invalidJSON) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should not be produced (ResultSet.json)', async () => { |
| 97 | + const rs = makeResultSet( |
| 98 | + Stream.Readable.from([Buffer.from(invalidJSON)]), |
| 99 | + ) |
| 100 | + const jsonPromise = rs.json() |
| 101 | + await expectAsync(jsonPromise).toBeRejectedWith( |
| 102 | + jasmine.objectContaining({ |
| 103 | + name: 'SyntaxError', |
| 104 | + }), |
| 105 | + ) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + describe('Non-streamable JSON formats - JSON', () => { |
| 110 | + it('should not be produced (ResultSet.text)', async () => { |
| 111 | + const rs = makeResultSet( |
| 112 | + Stream.Readable.from([Buffer.from(invalidJSON)]), |
| 113 | + 'JSON', |
| 114 | + ) |
| 115 | + const text = await rs.text() |
| 116 | + expect(text).toEqual(invalidJSON) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should not be produced (ResultSet.json)', async () => { |
| 120 | + const rs = makeResultSet( |
| 121 | + Stream.Readable.from([Buffer.from(invalidJSON)]), |
| 122 | + 'JSON', |
| 123 | + ) |
| 124 | + const jsonPromise = rs.json() |
| 125 | + await expectAsync(jsonPromise).toBeRejectedWith( |
| 126 | + jasmine.objectContaining({ |
| 127 | + name: 'SyntaxError', |
| 128 | + }), |
| 129 | + ) |
| 130 | + }) |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + function makeResultSet( |
| 135 | + stream: Stream.Readable, |
| 136 | + format: DataFormat = 'JSONEachRow', |
| 137 | + ) { |
67 | 138 | return ResultSet.instance({ |
68 | 139 | stream, |
69 | | - format: 'JSONEachRow', |
| 140 | + format, |
70 | 141 | query_id: guid(), |
71 | 142 | log_error: (err) => { |
72 | 143 | console.error(err) |
|
0 commit comments