Skip to content

Commit 5c6327b

Browse files
committed
test: Add integration tests for common-utils/metadata
1 parent 21641d9 commit 5c6327b

File tree

7 files changed

+141
-2
lines changed

7 files changed

+141
-2
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,16 @@ dev-int:
4545
npx nx run @hyperdx/api:dev:int $(FILE)
4646
docker compose -p int -f ./docker-compose.ci.yml down
4747

48+
.PHONY: dev-int-common-utils
49+
dev-int-common-utils:
50+
docker compose -p int -f ./docker-compose.ci.yml up -d
51+
npx nx run @hyperdx/common-utils:dev:int $(FILE)
52+
docker compose -p int -f ./docker-compose.ci.yml down
53+
4854
.PHONY: ci-int
4955
ci-int:
5056
docker compose -p int -f ./docker-compose.ci.yml up -d
51-
npx nx run @hyperdx/api:ci:int
57+
npx nx run-many -t ci:int --parallel=false
5258
docker compose -p int -f ./docker-compose.ci.yml down
5359

5460
.PHONY: dev-unit

packages/common-utils/.env.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CLICKHOUSE_HOST=http://localhost:8123
2+
CLICKHOUSE_PASSWORD=
3+
CLICKHOUSE_USER=default
4+
NODE_ENV=test

packages/common-utils/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
verbose: true,
66
rootDir: './src',
77
testMatch: ['**/__tests__/*.test.ts?(x)'],
8+
testPathIgnorePatterns: ['.*\\.int\\.test\\.ts$'],
89
testTimeout: 30000,
910
moduleNameMapper: {
1011
'@/(.*)$': '<rootDir>/$1',
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
setupFiles: ['dotenv/config'],
4+
preset: 'ts-jest',
5+
testEnvironment: 'node',
6+
verbose: true,
7+
rootDir: './src',
8+
testMatch: ['**/__tests__/*.int.test.ts?(x)'],
9+
testTimeout: 30000,
10+
moduleNameMapper: {
11+
'@/(.*)$': '<rootDir>/$1',
12+
},
13+
};

packages/common-utils/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@types/sqlstring": "^2.3.0",
3737
"@types/supertest": "^2.0.12",
3838
"@types/uuid": "^8.3.4",
39+
"dotenv": "^17.2.3",
3940
"jest": "^28.1.1",
4041
"nodemon": "^2.0.20",
4142
"rimraf": "^4.4.1",
@@ -56,6 +57,8 @@
5657
"lint:fix": "npx eslint . --ext .ts --fix",
5758
"ci:lint": "yarn lint && yarn tsc --noEmit",
5859
"ci:unit": "jest --runInBand --ci --forceExit --coverage",
59-
"dev:unit": "jest --watchAll --runInBand --detectOpenHandles"
60+
"dev:unit": "jest --watchAll --runInBand --detectOpenHandles",
61+
"ci:int": "DOTENV_CONFIG_PATH=.env.test jest --config jest.int.config.js --runInBand --ci --forceExit --coverage",
62+
"dev:int": "DOTENV_CONFIG_PATH=.env.test jest --config jest.int.config.js --watchAll --runInBand --detectOpenHandles"
6063
}
6164
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { createClient } from '@clickhouse/client';
2+
import { ClickHouseClient } from '@clickhouse/client-common';
3+
import { before } from 'lodash';
4+
5+
import { ClickhouseClient as HdxClickhouseClient } from '@/clickhouse/node';
6+
import { Metadata, MetadataCache } from '@/metadata';
7+
import { ChartConfigWithDateRange } from '@/types';
8+
9+
describe('ClickHouse Integration Tests', () => {
10+
let client: ClickHouseClient;
11+
let hdxClient: HdxClickhouseClient;
12+
13+
beforeAll(() => {
14+
const host = process.env.CLICKHOUSE_HOST || 'http://localhost:8123';
15+
const username = process.env.CLICKHOUSE_USER || 'default';
16+
const password = process.env.CLICKHOUSE_PASSWORD || '';
17+
18+
client = createClient({
19+
host,
20+
username,
21+
password,
22+
});
23+
24+
hdxClient = new HdxClickhouseClient({
25+
host,
26+
username,
27+
password,
28+
});
29+
});
30+
31+
describe('getKeyValues', () => {
32+
let metadata: Metadata;
33+
34+
beforeEach(async () => {
35+
metadata = new Metadata(hdxClient, new MetadataCache());
36+
37+
await client.command({
38+
query: `CREATE OR REPLACE TABLE default.test_table (
39+
Timestamp DateTime,
40+
col1 String,
41+
col2 LowCardinality(String)
42+
)
43+
ENGINE = MergeTree()
44+
ORDER BY (col1)
45+
`,
46+
});
47+
});
48+
49+
afterEach(async () => {
50+
await client.command({
51+
query: 'DROP TABLE IF EXISTS default.test_table',
52+
});
53+
});
54+
55+
it('should return key-value pairs for a given metadata key', async () => {
56+
await client.command({
57+
query: `INSERT INTO default.test_table (Timestamp, col1, col2) VALUES
58+
('2023-06-01 12:00:00', 'test_value1', 'test_value3'),
59+
('2024-06-01 12:00:00', 'test_value2', 'test_value4')
60+
`,
61+
});
62+
63+
const chartConfig: ChartConfigWithDateRange = {
64+
connection: 'test_connection',
65+
from: {
66+
databaseName: 'default',
67+
tableName: 'test_table',
68+
},
69+
dateRange: [new Date('2023-01-01'), new Date('2025-01-01')],
70+
select: 'col1, col2',
71+
timestampValueExpression: 'Timestamp',
72+
where: '',
73+
};
74+
75+
const resultCol1 = await metadata.getKeyValues({
76+
chartConfig,
77+
keys: ['col1'],
78+
});
79+
80+
expect(resultCol1).toEqual([
81+
{ key: 'col1', value: ['test_value1', 'test_value2'] },
82+
]);
83+
84+
const resultCol2 = await metadata.getKeyValues({
85+
chartConfig,
86+
keys: ['col2'],
87+
});
88+
89+
expect(resultCol2).toEqual([
90+
{ key: 'col2', value: ['test_value3', 'test_value4'] },
91+
]);
92+
93+
const resultBoth = await metadata.getKeyValues({
94+
chartConfig,
95+
keys: ['col1', 'col2'],
96+
});
97+
98+
expect(resultBoth).toEqual([
99+
{ key: 'col1', value: ['test_value1', 'test_value2'] },
100+
{ key: 'col2', value: ['test_value3', 'test_value4'] },
101+
]);
102+
});
103+
});
104+
});

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4536,6 +4536,7 @@ __metadata:
45364536
"@types/uuid": "npm:^8.3.4"
45374537
date-fns: "npm:^2.28.0"
45384538
date-fns-tz: "npm:^2.0.0"
4539+
dotenv: "npm:^17.2.3"
45394540
jest: "npm:^28.1.1"
45404541
lodash: "npm:^4.17.21"
45414542
node-sql-parser: "npm:^5.3.5"
@@ -14691,6 +14692,13 @@ __metadata:
1469114692
languageName: node
1469214693
linkType: hard
1469314694

14695+
"dotenv@npm:^17.2.3":
14696+
version: 17.2.3
14697+
resolution: "dotenv@npm:17.2.3"
14698+
checksum: 10c0/c884403209f713214a1b64d4d1defa4934c2aa5b0002f5a670ae298a51e3c3ad3ba79dfee2f8df49f01ae74290fcd9acdb1ab1d09c7bfb42b539036108bb2ba0
14699+
languageName: node
14700+
linkType: hard
14701+
1469414702
"dunder-proto@npm:^1.0.1":
1469514703
version: 1.0.1
1469614704
resolution: "dunder-proto@npm:1.0.1"

0 commit comments

Comments
 (0)