-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclickhouseClient.test.ts
More file actions
166 lines (145 loc) · 4.7 KB
/
clickhouseClient.test.ts
File metadata and controls
166 lines (145 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
import {
type ClickHouseClientLike,
type ClickHouseConfig,
createPullRequestToInferenceRecord,
getPullRequestToEpisodeRecords
} from './clickhouseClient.js'
const defaultConfig: ClickHouseConfig = {
url: 'https://clickhouse.example.com',
table: 'tensorzero.inference_records'
}
function createMockClient(): jest.Mocked<ClickHouseClientLike> {
return {
insert: jest.fn(),
query: jest.fn(),
close: jest.fn()
}
}
describe('clickhouseClient', () => {
let client: jest.Mocked<ClickHouseClientLike>
beforeEach(() => {
client = createMockClient()
})
it('writes inference records using structured inserts', async () => {
await createPullRequestToInferenceRecord(
{
inferenceId: 'abc-123',
episodeId: 'episode-123',
pullRequestId: 42
},
defaultConfig,
{ client }
)
expect(client.insert).toHaveBeenCalledWith({
table: 'tensorzero.inference_records',
values: [
{
episode_id: 'episode-123',
pull_request_id: 42,
inference_id: 'abc-123',
}
],
format: 'JSONEachRow'
})
expect(client.close).not.toHaveBeenCalled()
})
it('queries inference records with parameter binding', async () => {
const expectedRecords = [
{
inference_id: 'xyz',
pull_request_id: 77,
created_at: '2024-01-01T00:00:00Z',
original_pull_request_url: 'https://github.com/org/repo/pull/77'
}
]
const jsonMock = jest
.fn<() => Promise<unknown>>()
.mockResolvedValue(expectedRecords)
// @ts-expect-error(Mock type is inaccurate)
client.query.mockResolvedValueOnce({ json: jsonMock })
const records = await getPullRequestToEpisodeRecords(77, defaultConfig, {
client
})
expect(client.query).toHaveBeenCalledWith({
query:
'SELECT episode_id, pull_request_id, created_at, original_pull_request_url FROM tensorzero.inference_records WHERE pull_request_id = {pullRequestId:UInt64}',
query_params: { pullRequestId: 77 },
format: 'JSONEachRow'
})
expect(jsonMock).toHaveBeenCalledTimes(1)
expect(records).toEqual(expectedRecords)
})
it('throws when the table name fails validation', async () => {
await expect(
createPullRequestToInferenceRecord(
{
inferenceId: 'abc',
episodeId: 'episode-123',
pullRequestId: 1
},
{ ...defaultConfig, table: 'invalid-table!' }
)
).rejects.toThrow('ClickHouse table name must contain only')
})
it('validates missing URL', async () => {
await expect(
createPullRequestToInferenceRecord(
{
inferenceId: 'abc',
episodeId: 'episode-123',
pullRequestId: 1
},
{ ...defaultConfig, url: ' ' }
)
).rejects.toThrow('ClickHouse URL is required')
})
it('propagates insert failures from the injected client without closing it', async () => {
client.insert.mockRejectedValueOnce(new Error('insert failed'))
await expect(
createPullRequestToInferenceRecord(
{
inferenceId: 'abc',
episodeId: 'episode-123',
pullRequestId: 1
},
defaultConfig,
{ client }
)
).rejects.toThrow('insert failed')
expect(client.close).not.toHaveBeenCalled()
})
it('queries episode records with parameter binding', async () => {
const expectedRecords = [
{
episode_id: 'episode-789',
pull_request_id: 55,
created_at: '2024-02-01T00:00:00Z',
original_pull_request_url: 'https://github.com/org/repo/pull/55'
}
]
const jsonMock = jest
.fn<() => Promise<unknown>>()
.mockResolvedValue(expectedRecords)
// @ts-expect-error(Mock type is inaccurate)
client.query.mockResolvedValueOnce({ json: jsonMock })
const records = await getPullRequestToEpisodeRecords(55, defaultConfig, {
client
})
expect(client.query).toHaveBeenCalledWith({
query:
'SELECT episode_id, pull_request_id, created_at, original_pull_request_url FROM tensorzero.inference_records WHERE pull_request_id = {pullRequestId:UInt64}',
query_params: { pullRequestId: 55 },
format: 'JSONEachRow'
})
expect(jsonMock).toHaveBeenCalledTimes(1)
expect(records).toEqual(expectedRecords)
})
it('propagates query failures for episode records without closing the client', async () => {
client.query.mockRejectedValueOnce(new Error('query failed'))
await expect(
getPullRequestToEpisodeRecords(123, defaultConfig, { client })
).rejects.toThrow('query failed')
expect(client.close).not.toHaveBeenCalled()
})
})