Skip to content

Commit

Permalink
Change incorrect totals TS type (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
slvrtrn committed Jun 6, 2024
1 parent 37aa43d commit 9bdea17
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>.totals` TypeScript type. Now it correctly matches the shape of the data (`T`, default = `unknown`) instead of the former `Record<string, number>` definition ([#274](https://github.com/ClickHouse/clickhouse-js/issues/274)).

# 1.0.2 (Common, Node.js, Web)

Expand Down
65 changes: 65 additions & 0 deletions packages/client-common/__tests__/integration/totals.test.ts
Original file line number Diff line number Diff line change
@@ -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',
})
})
})
2 changes: 1 addition & 1 deletion packages/client-common/src/clickhouse_types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface ResponseJSON<T = unknown> {
data: Array<T>
query_id?: string
totals?: Record<string, number>
totals?: T
extremes?: Record<string, any>
// # Supported only by responses in JSON, XML.
// # Otherwise, it can be read from x-clickhouse-summary header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion packages/client-web/__tests__/integration/web_ping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9bdea17

Please sign in to comment.