Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Nested data type #227

Merged
merged 2 commits into from
Feb 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 192 additions & 18 deletions packages/client-common/__tests__/integration/data_types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,25 +519,133 @@ describe('data types', () => {
await insertAndAssert(table, values)
})

/** @see https://github.com/ClickHouse/clickhouse-js/issues/89 */
xit('should work with nested', async () => {
it('should work with nested', async () => {
const values = [
{
id: 1,
'n.id': [42],
'n.name': ['foo'],
'n.createdAt': ['2001-04-23 00:00:00'],
'n.roles': [['User']],
},
{
id: 2,
'n.id': [43],
'n.name': ['bar'],
'n.createdAt': ['2000-01-12 00:00:00'],
'n.roles': [['Admin']],
},
]
const table = await createTableWithFields(
client,
'n Nested(id UInt32, name String, createdAt DateTime, ' +
`roles Array(Enum('User', 'Admin')))`
)
await client.insert({
table,
values,
format: 'JSONEachRow',
})
const result = await client
.query({
query: `SELECT n.id, n.name, n.createdAt, n.roles FROM ${table} ORDER BY id ASC`,
format: 'JSONEachRow',
})
.then((r) => r.json())
expect(result).toEqual([
{
'n.id': [42],
'n.name': ['foo'],
'n.createdAt': ['2001-04-23 00:00:00'],
'n.roles': [['User']],
},
{
'n.id': [43],
'n.name': ['bar'],
'n.createdAt': ['2000-01-12 00:00:00'],
'n.roles': [['Admin']],
},
])
})

it('should work with nested (flatten_nested = 0)', async () => {
const values = [
{
id: 1,
n: [
{
id: 42,
name: 'foo',
createdAt: '2001-04-23 00:00:00',
roles: ['User'],
},
],
},
{
id: 2,
n: [
{
id: 43,
name: 'bar',
createdAt: '2000-01-12 00:00:00',
roles: ['Admin'],
},
],
},
]
const table = await createTableWithFields(
client,
'n Nested(id UInt32, name String, createdAt DateTime, ' +
`roles Array(Enum('User', 'Admin')))`,
{
flatten_nested: 0,
}
)
await client.insert({
table,
values,
format: 'JSONEachRow',
})
const result = await client
.query({
query: `SELECT n.id, n.name, n.createdAt, n.roles FROM ${table} ORDER BY id ASC`,
format: 'JSONEachRow',
})
.then((r) => r.json())
expect(result).toEqual([
{
'n.id': [42],
'n.name': ['foo'],
'n.createdAt': ['2001-04-23 00:00:00'],
'n.roles': [['User']],
},
{
'n.id': [43],
'n.name': ['bar'],
'n.createdAt': ['2000-01-12 00:00:00'],
'n.roles': [['Admin']],
},
])
})

it('should work with nested (input_format_import_nested_json = 1)', async () => {
const values = [
{
id: 1,
n: {
id: 42,
name: 'foo',
createdAt: '2001-04-23',
roles: ['User'],
id: [42],
name: ['foo'],
createdAt: ['2001-04-23 00:00:00'],
roles: [['User']],
},
},
{
id: 2,
n: {
id: 43,
name: 'bar',
createdAt: '2000-01-12',
roles: ['Admin'],
id: [43],
name: ['bar'],
createdAt: ['2000-01-12 00:00:00'],
roles: [['Admin']],
},
},
]
Expand All @@ -549,6 +657,72 @@ describe('data types', () => {
await client.insert({
table,
values,
clickhouse_settings: {
input_format_import_nested_json: 1,
},
format: 'JSONEachRow',
})
const result = await client
.query({
query: `SELECT n.id, n.name, n.createdAt, n.roles FROM ${table} ORDER BY id ASC`,
format: 'JSONEachRow',
})
.then((r) => r.json())
expect(result).toEqual([
{
'n.id': [42],
'n.name': ['foo'],
'n.createdAt': ['2001-04-23 00:00:00'],
'n.roles': [['User']],
},
{
'n.id': [43],
'n.name': ['bar'],
'n.createdAt': ['2000-01-12 00:00:00'],
'n.roles': [['Admin']],
},
])
})

it('should work with nested with (flatten_nested = 0 and input_format_import_nested_json = 1)', async () => {
const values = [
{
id: 1,
n: [
{
id: 42,
name: 'foo',
createdAt: '2001-04-23 00:00:00',
roles: ['User'],
},
],
},
{
id: 2,
n: [
{
id: 43,
name: 'bar',
createdAt: '2000-01-12 00:00:00',
roles: ['Admin'],
},
],
},
]
const table = await createTableWithFields(
client,
'n Nested(id UInt32, name String, createdAt DateTime, ' +
`roles Array(Enum('User', 'Admin')))`,
{
flatten_nested: 0,
}
)
await client.insert({
table,
values,
clickhouse_settings: {
input_format_import_nested_json: 1,
},
format: 'JSONEachRow',
})
const result = await client
Expand All @@ -559,16 +733,16 @@ describe('data types', () => {
.then((r) => r.json())
expect(result).toEqual([
{
'n.id': 42,
'n.name': 'foo',
'n.createdAt': '2001-04-23',
'n.roles': ['User'],
'n.id': [42],
'n.name': ['foo'],
'n.createdAt': ['2001-04-23 00:00:00'],
'n.roles': [['User']],
},
{
'n.id': 43,
'n.name': 'bar',
'n.createdAt': '2000-01-12',
'n.roles': ['Admin'],
'n.id': [43],
'n.name': ['bar'],
'n.createdAt': ['2000-01-12 00:00:00'],
'n.roles': [['Admin']],
},
])
})
Expand Down
Loading