Skip to content

Commit

Permalink
Reorganize the tests a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
slvrtrn committed Sep 30, 2024
1 parent d3f34bf commit 62dc9cd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 86 deletions.
37 changes: 37 additions & 0 deletions packages/client-common/__tests__/utils/datasets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { ClickHouseClient } from '@clickhouse/client-common'
import { fakerRU } from '@faker-js/faker'
import { createTableWithFields } from '@test/fixtures/table_with_fields'

export async function genLargeStringsDataset<Stream = unknown>(
client: ClickHouseClient<Stream>,
{
rows,
words,
}: {
rows: number
words: number
},
): Promise<{
table: string
values: { id: number; sentence: string; timestamp: string }[]
}> {
const table = await createTableWithFields(
client as ClickHouseClient,
`sentence String, timestamp String`,
)
const values = [...new Array(rows)].map((_, id) => ({
id,
// it seems that it is easier to trigger an incorrect behavior with non-ASCII symbols
sentence: fakerRU.lorem.sentence(words),
timestamp: new Date().toISOString(),
}))
await client.insert({
table,
values,
format: 'JSONEachRow',
})
return {
table,
values,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
type ClickHouseClient,
type ClickHouseSettings,
} from '@clickhouse/client-common'
import { fakerRU } from '@faker-js/faker'
import { createSimpleTable } from '@test/fixtures/simple_table'
import { createTableWithFields } from '@test/fixtures/table_with_fields'
import { createTestClient, guid } from '@test/utils'
import { genLargeStringsDataset } from '@test/utils/datasets'
import { tableFromIPC } from 'apache-arrow'
import { Buffer } from 'buffer'
import Fs from 'fs'
Expand Down Expand Up @@ -152,40 +151,9 @@ describe('[Node.js] streaming e2e', () => {
// Here we generate a large enough dataset to break into multiple chunks while streaming,
// effectively testing the implementation of incomplete rows handling
describe('should correctly process multiple chunks', () => {
async function generateData({
rows,
words,
}: {
rows: number
words: number
}): Promise<{
table: string
values: { id: number; sentence: string; timestamp: string }[]
}> {
const table = await createTableWithFields(
client as ClickHouseClient,
`sentence String, timestamp String`,
)
const values = [...new Array(rows)].map((_, id) => ({
id,
// it seems that it is easier to trigger an incorrect behavior with non-ASCII symbols
sentence: fakerRU.lorem.sentence(words),
timestamp: new Date().toISOString(),
}))
await client.insert({
table,
values,
format: 'JSONEachRow',
})
return {
table,
values,
}
}

describe('large amount of rows', () => {
it('should work with .json()', async () => {
const { table, values } = await generateData({
const { table, values } = await genLargeStringsDataset(client, {
rows: 10000,
words: 10,
})
Expand All @@ -199,7 +167,7 @@ describe('[Node.js] streaming e2e', () => {
})

it('should work with .stream()', async () => {
const { table, values } = await generateData({
const { table, values } = await genLargeStringsDataset(client, {
rows: 10000,
words: 10,
})
Expand All @@ -222,7 +190,7 @@ describe('[Node.js] streaming e2e', () => {

describe("rows that don't fit into a single chunk", () => {
it('should work with .json()', async () => {
const { table, values } = await generateData({
const { table, values } = await genLargeStringsDataset(client, {
rows: 5,
words: 10000,
})
Expand All @@ -236,7 +204,7 @@ describe('[Node.js] streaming e2e', () => {
})

it('should work with .stream()', async () => {
const { table, values } = await generateData({
const { table, values } = await genLargeStringsDataset(client, {
rows: 5,
words: 10000,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ClickHouseClient, Row } from '@clickhouse/client-common'
import { fakerRU } from '@faker-js/faker'
import { createTableWithFields } from '@test/fixtures/table_with_fields'
import { createTestClient } from '@test/utils'
import { genLargeStringsDataset } from '@test/utils/datasets'

describe('[Web] SELECT streaming', () => {
let client: ClickHouseClient<ReadableStream<Row[]>>
Expand Down Expand Up @@ -206,40 +205,9 @@ describe('[Web] SELECT streaming', () => {
// Here we generate a large enough dataset to break into multiple chunks while streaming,
// effectively testing the implementation of incomplete rows handling
describe('should correctly process multiple chunks', () => {
async function generateData({
rows,
words,
}: {
rows: number
words: number
}): Promise<{
table: string
values: { id: number; sentence: string; timestamp: string }[]
}> {
const table = await createTableWithFields(
client as ClickHouseClient,
`sentence String, timestamp String`,
)
const values = [...new Array(rows)].map((_, id) => ({
id,
// it seems that it is easier to trigger an incorrect behavior with non-ASCII symbols
sentence: fakerRU.lorem.sentence(words),
timestamp: new Date().toISOString(),
}))
await client.insert({
table,
values,
format: 'JSONEachRow',
})
return {
table,
values,
}
}

describe('large amount of rows', () => {
it('should work with .json()', async () => {
const { table, values } = await generateData({
const { table, values } = await genLargeStringsDataset(client, {
rows: 10000,
words: 10,
})
Expand All @@ -253,7 +221,7 @@ describe('[Web] SELECT streaming', () => {
})

it('should work with .stream()', async () => {
const { table, values } = await generateData({
const { table, values } = await genLargeStringsDataset(client, {
rows: 10000,
words: 10,
})
Expand All @@ -264,19 +232,14 @@ describe('[Web] SELECT streaming', () => {
})
.then((r) => r.stream())

const result = []
for await (const rows of stream) {
for (const row of rows) {
result.push(await row.json())
}
}
const result = await rowsJsonValues(stream)
expect(result).toEqual(values)
})
})

describe("rows that don't fit into a single chunk", () => {
it('should work with .json()', async () => {
const { table, values } = await generateData({
const { table, values } = await genLargeStringsDataset(client, {
rows: 5,
words: 10000,
})
Expand All @@ -290,7 +253,7 @@ describe('[Web] SELECT streaming', () => {
})

it('should work with .stream()', async () => {
const { table, values } = await generateData({
const { table, values } = await genLargeStringsDataset(client, {
rows: 5,
words: 10000,
})
Expand All @@ -301,12 +264,7 @@ describe('[Web] SELECT streaming', () => {
})
.then((r) => r.stream())

const result = []
for await (const rows of stream) {
for (const row of rows) {
result.push(await row.json())
}
}
const result = await rowsJsonValues(stream)
expect(result).toEqual(values)
})
})
Expand Down

0 comments on commit 62dc9cd

Please sign in to comment.