Skip to content

Commit 9de4114

Browse files
committed
Refactor createRun example
1 parent c9c86f1 commit 9de4114

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

mlflow/tests/RunClient.test.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,42 @@
11
import { describe, test, expect, beforeAll } from '@jest/globals';
22
import RunClient from '../src/tracking/RunClient';
3+
import ExperimentClient from '../src/tracking/ExperimentClient';
34
import { Run } from '../src/utils/interface';
45
import { ApiError } from '../src/utils/apiError';
56

67
describe('RunClient', () => {
7-
let client: RunClient;
8+
let runClient: RunClient;
9+
let experimentClient: ExperimentClient;
10+
let experimentId: string;
811

912
beforeAll(async () => {
1013
// Add a small delay to ensure MLflow is fully ready
1114
await new Promise((resolve) => setTimeout(resolve, 2000));
12-
client = new RunClient('http://127.0.0.1:5001');
15+
runClient = new RunClient('http://127.0.0.1:5001');
16+
experimentClient = new ExperimentClient('http://127.0.0.1:5001');
17+
18+
// Generate the experiment ID for test runs
19+
const timestamp = Date.now();
20+
experimentId = await experimentClient.createExperiment(
21+
`Testing ${timestamp}`
22+
);
1323
});
1424

1525
describe('createRun', () => {
1626
// POST - Create a new run within an experiment
1727
test('- Should create a run with experiment_id', async () => {
18-
const experiment_id = '876374673578277025';
28+
const response = (await runClient.createRun(experimentId)) as Run;
1929

20-
const response = (await client.createRun(experiment_id)) as Run;
21-
22-
expect(response.info.experiment_id).toBe(experiment_id);
30+
expect(response.info.experiment_id).toBe(experimentId);
2331
});
2432

2533
test('- Should create a run with optional run_name', async () => {
26-
const experiment_id = '876374673578277025';
2734
const run_name = 'Test Run 1';
2835

29-
const response = (await client.createRun(experiment_id, run_name)) as Run;
36+
const response = (await runClient.createRun(
37+
experimentId,
38+
run_name
39+
)) as Run;
3040

3141
expect(response.info.run_name).toBe(run_name);
3242
expect(response.data.tags).toContainEqual({
@@ -36,14 +46,13 @@ describe('RunClient', () => {
3646
});
3747

3848
test('- Should create a run with optional tags', async () => {
39-
const experiment_id = '876374673578277025';
4049
const tags = [
4150
{ key: 'test_key1', value: 'test_value1' },
4251
{ key: 'test_key2', value: 'test_value2' },
4352
];
4453

45-
const response = (await client.createRun(
46-
experiment_id,
54+
const response = (await runClient.createRun(
55+
experimentId,
4756
undefined,
4857
undefined,
4958
tags
@@ -56,32 +65,40 @@ describe('RunClient', () => {
5665
});
5766

5867
test('- Should create a run with all parameters', async () => {
59-
const experiment_id = '876374673578277025';
6068
const run_name = 'Test Run 2';
6169
const start_time = Date.now();
6270
const tags = [{ key: 'test_key', value: 'test_value' }];
6371

64-
const response = (await client.createRun(
65-
experiment_id,
72+
const response = (await runClient.createRun(
73+
experimentId,
6674
run_name,
6775
start_time,
6876
tags
6977
)) as Run;
7078

71-
expect(response.info.experiment_id).toBe(experiment_id);
79+
expect(response.info.experiment_id).toBe(experimentId);
7280
expect(response.info.run_name).toBe(run_name);
7381
expect(response.info.start_time).toBe(start_time);
7482
expect(response.data.tags).toContainEqual(tags[0]);
7583
});
7684

85+
test('- Should throw errors if experiment_id is missing', async () => {
86+
// @ts-expect-error: testing for missing arguments
87+
await expect(runClient.createRun()).rejects.toThrow(ApiError);
88+
// @ts-expect-error: testing for missing arguments
89+
await expect(runClient.createRun()).rejects.toThrow(
90+
/Error creating run:/
91+
);
92+
});
93+
7794
test('- Should handle API errors', async () => {
7895
const invalid_id = 'invalid_id';
7996

8097
// the thrown error is specifically an instance of 'ApiError'
81-
await expect(client.createRun(invalid_id)).rejects.toThrow(ApiError);
98+
await expect(runClient.createRun(invalid_id)).rejects.toThrow(ApiError);
8299

83100
// the thrown error message with invalid_id somewhere in the message
84-
await expect(client.createRun(invalid_id)).rejects.toThrow(
101+
await expect(runClient.createRun(invalid_id)).rejects.toThrow(
85102
/Error creating run:.+invalid_id/
86103
);
87104
});

0 commit comments

Comments
 (0)