From 9bdea1707610ca594c0d7686a9b3055f3561a5f5 Mon Sep 17 00:00:00 2001 From: Serge Klochkov <3175289+slvrtrn@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:19:55 +0200 Subject: [PATCH] Change incorrect totals TS type (#280) --- CHANGELOG.md | 8 ++- .../__tests__/integration/totals.test.ts | 65 +++++++++++++++++++ .../client-common/src/clickhouse_types.ts | 2 +- .../integration/node_errors_parsing.test.ts | 2 +- .../__tests__/integration/node_ping.test.ts | 2 +- .../integration/web_error_parsing.test.ts | 2 +- .../__tests__/integration/web_ping.test.ts | 2 +- 7 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 packages/client-common/__tests__/integration/totals.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b6c3df4..6a906f76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,12 @@ ## New features -- Added an option to override the credentials for a particular `query`/`command`/`exec`/`insert` request via the `BaseQueryParams.auth` setting; when set, the credentials will be taken from there instead of the username/password provided during the client instantiation. -- Allow overriding `session_id` per query ([@holi0317](https://github.com/Holi0317), [#271](https://github.com/ClickHouse/clickhouse-js/issues/271)). +- Added an option to override the credentials for a particular `query`/`command`/`exec`/`insert` request via the `BaseQueryParams.auth` setting; when set, the credentials will be taken from there instead of the username/password provided during the client instantiation ([#278](https://github.com/ClickHouse/clickhouse-js/issues/278)). +- Added an option to override the `session_id` for a particular `query`/`command`/`exec`/`insert` request via the `BaseQueryParams.session_id` setting; when set, it will be used instead of the session id provided during the client instantiation ([@holi0317](https://github.com/Holi0317), [#271](https://github.com/ClickHouse/clickhouse-js/issues/271)). + +## Bug fixes + +- Fixed the incorrect `ResponseJSON.totals` TypeScript type. Now it correctly matches the shape of the data (`T`, default = `unknown`) instead of the former `Record` definition ([#274](https://github.com/ClickHouse/clickhouse-js/issues/274)). # 1.0.2 (Common, Node.js, Web) diff --git a/packages/client-common/__tests__/integration/totals.test.ts b/packages/client-common/__tests__/integration/totals.test.ts new file mode 100644 index 00000000..4d6f2074 --- /dev/null +++ b/packages/client-common/__tests__/integration/totals.test.ts @@ -0,0 +1,65 @@ +import type { ClickHouseClient } from '@clickhouse/client-common' +import { createSimpleTable } from '@test/fixtures/simple_table' +import { createTestClient, guid } from '@test/utils' + +describe('Queries with totals', () => { + let client: ClickHouseClient + let tableName: string + + beforeEach(async () => { + client = createTestClient() + tableName = `totals_test_${guid()}` + await createSimpleTable(client, tableName) + }) + afterEach(async () => { + await client.close() + }) + + it('should return the expected totals', async () => { + await client.insert({ + table: tableName, + values: [ + { id: '42', name: 'hello', sku: [0, 1] }, + { id: '43', name: 'hello', sku: [2, 3] }, + { id: '44', name: 'foo', sku: [3, 4] }, + { id: '45', name: 'foo', sku: [4, 5] }, + { id: '46', name: 'foo', sku: [6, 7] }, + ], + format: 'JSONEachRow', + }) + + const rs1 = await client.query({ + query: `SELECT *, count(*) AS count FROM ${tableName} GROUP BY id, name, sku WITH TOTALS ORDER BY id ASC`, + format: 'JSON', + }) + const result1 = await rs1.json() + expect(result1.data).toEqual([ + { id: '42', name: 'hello', sku: [0, 1], count: '1' }, + { id: '43', name: 'hello', sku: [2, 3], count: '1' }, + { id: '44', name: 'foo', sku: [3, 4], count: '1' }, + { id: '45', name: 'foo', sku: [4, 5], count: '1' }, + { id: '46', name: 'foo', sku: [6, 7], count: '1' }, + ]) + expect(result1.totals).toEqual({ + id: '0', + name: '', + sku: [], + count: '5', + }) + + const rs2 = await client.query({ + query: `SELECT 1 :: Int16 AS x, name, count(*) AS count FROM ${tableName} GROUP BY name WITH TOTALS ORDER BY name ASC`, + format: 'JSON', + }) + const result2 = await rs2.json() + expect(result2.data).toEqual([ + { x: 1, name: 'foo', count: '3' }, + { x: 1, name: 'hello', count: '2' }, + ]) + expect(result2.totals).toEqual({ + x: 1, + name: '', + count: '5', + }) + }) +}) diff --git a/packages/client-common/src/clickhouse_types.ts b/packages/client-common/src/clickhouse_types.ts index f2fd4e95..e98ccfb6 100644 --- a/packages/client-common/src/clickhouse_types.ts +++ b/packages/client-common/src/clickhouse_types.ts @@ -1,7 +1,7 @@ export interface ResponseJSON { data: Array query_id?: string - totals?: Record + totals?: T extremes?: Record // # Supported only by responses in JSON, XML. // # Otherwise, it can be read from x-clickhouse-summary header diff --git a/packages/client-node/__tests__/integration/node_errors_parsing.test.ts b/packages/client-node/__tests__/integration/node_errors_parsing.test.ts index 7254c45e..9f603730 100644 --- a/packages/client-node/__tests__/integration/node_errors_parsing.test.ts +++ b/packages/client-node/__tests__/integration/node_errors_parsing.test.ts @@ -3,7 +3,7 @@ import { createClient } from '../../src' describe('[Node.js] errors parsing', () => { it('should return an error when URL is unreachable', async () => { const client = createClient({ - host: 'http://localhost:1111', + url: 'http://localhost:1111', }) await expectAsync( client.query({ diff --git a/packages/client-node/__tests__/integration/node_ping.test.ts b/packages/client-node/__tests__/integration/node_ping.test.ts index 9207495d..a4f72e9d 100644 --- a/packages/client-node/__tests__/integration/node_ping.test.ts +++ b/packages/client-node/__tests__/integration/node_ping.test.ts @@ -8,7 +8,7 @@ describe('[Node.js] ping', () => { }) it('does not swallow a client error', async () => { client = createTestClient({ - host: 'http://localhost:3333', + url: 'http://localhost:3333', }) const result = await client.ping() diff --git a/packages/client-web/__tests__/integration/web_error_parsing.test.ts b/packages/client-web/__tests__/integration/web_error_parsing.test.ts index c79d1ed8..71417400 100644 --- a/packages/client-web/__tests__/integration/web_error_parsing.test.ts +++ b/packages/client-web/__tests__/integration/web_error_parsing.test.ts @@ -3,7 +3,7 @@ import { createClient } from '../../src' describe('[Web] errors parsing', () => { it('should return an error when URL is unreachable', async () => { const client = createClient({ - host: 'http://localhost:1111', + url: 'http://localhost:1111', }) await expectAsync( client.query({ diff --git a/packages/client-web/__tests__/integration/web_ping.test.ts b/packages/client-web/__tests__/integration/web_ping.test.ts index 0d172a01..79558eee 100644 --- a/packages/client-web/__tests__/integration/web_ping.test.ts +++ b/packages/client-web/__tests__/integration/web_ping.test.ts @@ -8,7 +8,7 @@ describe('[Web] ping', () => { }) it('does not swallow a client error', async () => { client = createTestClient({ - host: 'http://localhost:3333', + url: 'http://localhost:3333', }) const result = await client.ping()