Skip to content

Commit

Permalink
Add tests for Nested data type (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
auvred authored Feb 11, 2024
1 parent 7a14392 commit cbc1090
Showing 1 changed file with 192 additions and 18 deletions.
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

0 comments on commit cbc1090

Please sign in to comment.