Skip to content

Commit 2939cde

Browse files
authored
Add read-only users cleanup to the tests (#343)
1 parent 7fe20e4 commit 2939cde

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

packages/client-common/__tests__/integration/read_only_user.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import type { ClickHouseClient } from '@clickhouse/client-common'
2+
import { isCloudTestEnv } from '@test/utils/test_env'
23
import { createReadOnlyUser } from '../fixtures/read_only_user'
34
import { createSimpleTable } from '../fixtures/simple_table'
45
import { createTestClient, getTestDatabaseName, guid } from '../utils'
56

67
describe('read only user', () => {
8+
let defaultClient: ClickHouseClient
79
let client: ClickHouseClient
810
let tableName: string
11+
let userName: string
912

1013
beforeAll(async () => {
1114
const database = getTestDatabaseName()
12-
const defaultClient = createTestClient()
15+
defaultClient = createTestClient()
1316

14-
const { username, password } = await createReadOnlyUser(defaultClient)
17+
const credentials = await createReadOnlyUser(defaultClient)
18+
userName = credentials.username
1519

1620
// Populate some test table to select from
1721
tableName = `read_only_user_data_${guid()}`
@@ -20,13 +24,12 @@ describe('read only user', () => {
2024
table: tableName,
2125
values: [[42, 'hello', [0, 1]]],
2226
})
23-
await defaultClient.close()
2427

2528
// Create a client that connects read only user to the test database
2629
client = createTestClient({
27-
username,
28-
password,
2930
database,
31+
username: credentials.username,
32+
password: credentials.password,
3033
clickhouse_settings: {
3134
// readonly user cannot adjust settings. reset the default ones set by fixtures.
3235
// might be fixed by https://github.com/ClickHouse/ClickHouse/issues/40244
@@ -37,7 +40,13 @@ describe('read only user', () => {
3740
})
3841

3942
afterAll(async () => {
43+
if (isCloudTestEnv()) {
44+
await defaultClient.command({
45+
query: `DROP USER IF EXISTS ${userName}`,
46+
})
47+
}
4048
await client.close()
49+
await defaultClient.close()
4150
})
4251

4352
it('should select some data without issues', async () => {

packages/client-web/__tests__/integration/web_abort_request.test.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import type { ClickHouseClient, Row } from '@clickhouse/client-common'
1+
import type { Row } from '@clickhouse/client-common'
22
import { createTestClient } from '@test/utils'
3+
import type { WebClickHouseClient } from '../../src/client'
34

4-
describe('[Web] abort request', () => {
5-
let client: ClickHouseClient<ReadableStream>
5+
fdescribe('[Web] abort request', () => {
6+
let client: WebClickHouseClient
67

78
beforeEach(() => {
8-
client = createTestClient()
9+
client = createTestClient() as unknown as WebClickHouseClient
910
})
1011

1112
afterEach(async () => {
@@ -31,23 +32,27 @@ describe('[Web] abort request', () => {
3132

3233
it('cancels a select query while reading response', async () => {
3334
const controller = new AbortController()
35+
let rowCount = 0
3436
const selectPromise = client
3537
.query({
36-
query: 'SELECT * from system.numbers LIMIT 100000',
38+
query: 'SELECT number FROM system.numbers LIMIT 1000',
3739
format: 'JSONCompactEachRow',
3840
abort_signal: controller.signal,
41+
clickhouse_settings: {
42+
// low block size to force streaming 1 row at a time
43+
max_block_size: '1',
44+
},
3945
})
4046
.then(async (rs) => {
4147
const reader = rs.stream().getReader()
4248
while (true) {
4349
const { done, value: rows } = await reader.read()
4450
if (done) break
45-
;(rows as Row[]).forEach((row: Row) => {
46-
const [number] = row.json<[string]>()
47-
// abort when reach number 3
48-
if (number === '3') {
51+
;(rows as Row[]).forEach(() => {
52+
if (rowCount >= 1) {
4953
controller.abort()
5054
}
55+
rowCount++
5156
})
5257
}
5358
})
@@ -61,23 +66,27 @@ describe('[Web] abort request', () => {
6166
})
6267

6368
it('cancels a select query while reading response by closing response stream', async () => {
69+
let rowCount = 0
6470
const selectPromise = client
6571
.query({
66-
query: 'SELECT * from system.numbers LIMIT 100000',
72+
query: 'SELECT number FROM system.numbers LIMIT 3',
6773
format: 'JSONCompactEachRow',
74+
clickhouse_settings: {
75+
// low block size to force streaming 1 row at a time
76+
max_block_size: '1',
77+
},
6878
})
6979
.then(async function (rs) {
7080
const reader = rs.stream().getReader()
7181
while (true) {
7282
const { done, value: rows } = await reader.read()
7383
if (done) break
7484
for (const row of rows as Row[]) {
75-
const [number] = row.json<[string]>()
76-
// abort when reach number 3
77-
if (number === '3') {
78-
await reader.releaseLock()
79-
await rs.close()
85+
row.json()
86+
if (rowCount >= 1) {
87+
await rs.stream().cancel()
8088
}
89+
rowCount++
8190
}
8291
}
8392
})

0 commit comments

Comments
 (0)