-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cli_utils.py
More file actions
378 lines (292 loc) · 17.6 KB
/
test_cli_utils.py
File metadata and controls
378 lines (292 loc) · 17.6 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from typing import Union
from datetime import datetime, timezone
import pytest
from zoneinfo import ZoneInfo
from together.lib.cli.api.utils import generate_progress_bar
from together.lib.types.fine_tuning import (
FinetuneProgress,
FinetuneResponse,
FinetuneJobStatus,
)
def create_finetune_response(
status: FinetuneJobStatus = FinetuneJobStatus.STATUS_RUNNING,
updated_at: datetime = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc),
progress: Union[FinetuneProgress, None] = None,
job_id: str = "ft-test-123",
) -> FinetuneResponse:
"""Helper function to create FinetuneResponse objects for testing.
Args:
status: The job status.
updated_at: The updated timestamp in ISO format.
progress: Optional FinetuneProgress object.
job_id: The fine-tune job ID.
Returns:
A FinetuneResponse object for testing.
"""
return FinetuneResponse(
id=job_id,
progress=progress,
updated_at=updated_at,
status=status,
created_at=datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc),
model_output_name="test_model",
adapter_output_name="test_adapter",
TrainingFileNumLines=0,
TrainingFileSize=0,
)
class TestGenerateProgressBarGeneral:
"""General test cases for normal operation."""
def test_progress_unavailable_when_none(self):
"""Test that progress shows unavailable when progress field is None."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
finetune_job = create_finetune_response(progress=None)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert result == "Progress: [bold red]unavailable[/bold red]"
def test_progress_unavailable_when_not_set(self):
"""Test that progress shows unavailable when field is not provided."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
finetune_job = create_finetune_response()
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert result == "Progress: [bold red]unavailable[/bold red]"
def test_progress_bar_at_start(self):
"""Test progress bar display when job just started (low percentage)."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=1000.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
# 10 seconds elapsed / 1000 seconds remaining = 0.01 ratio = 1% progress
# 0.01 * 40 = 0.4, ceil(0.4) = 1 filled bar
assert (
result
== "Progress: █░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ [bold] 1%[/bold] [yellow]16min 30s left[/yellow]"
)
def test_progress_bar_at_midpoint(self):
"""Test progress bar at approximately 50% completion."""
current_time = datetime(2024, 1, 1, 12, 1, 0, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
# 60 seconds elapsed / 60 seconds remaining = 1.0 ratio = 100% progress
# 1.0 * 40 = 40 filled bars
assert (
result == "Progress: ████████████████████████████████████████ [bold]100%[/bold] [yellow]N/A left[/yellow]"
)
def test_progress_bar_near_completion(self):
"""Test progress bar when job is almost complete."""
current_time = datetime(2024, 1, 1, 12, 5, 0, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=30.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
# 300 seconds elapsed / 30 seconds remaining = 10.0 ratio = 1000% progress
# 10.0 * 40 = 400, ceil(400) = 400, but width is 40 so all filled
assert (
result == "Progress: ████████████████████████████████████████ [bold]100%[/bold] [yellow]N/A left[/yellow]"
)
def test_progress_bar_contains_rich_formatting(self):
"""Test that progress bar includes expected Rich markup formatting."""
current_time = datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
# 30 seconds elapsed / 60 seconds remaining = 0.5 ratio = 50% progress
# 0.5 * 40 = 20 filled bars
assert (
result == "Progress: ████████████████████░░░░░░░░░░░░░░░░░░░░ [bold] 50%[/bold] [yellow]30s left[/yellow]"
)
class TestGenerateProgressBarRichFormatting:
"""Test cases for use_rich parameter."""
def test_rich_formatting_removed_when_use_rich_false(self):
"""Test that rich formatting tags are removed when use_rich=False."""
current_time = datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=False)
assert result == "Progress: ████████████████████░░░░░░░░░░░░░░░░░░░░ 50% 30s left"
def test_rich_formatting_preserved_when_use_rich_true(self):
"""Test that rich formatting tags are preserved when use_rich=True."""
current_time = datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert (
result == "Progress: ████████████████████░░░░░░░░░░░░░░░░░░░░ [bold] 50%[/bold] [yellow]30s left[/yellow]"
)
def test_completed_status_formatting_removed(self):
"""Test that completed status formatting is removed when use_rich=False."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
finetune_job = create_finetune_response(status=FinetuneJobStatus.STATUS_COMPLETED, progress=None)
result = generate_progress_bar(finetune_job, current_time, use_rich=False)
assert result == "Progress: completed"
def test_unavailable_status_formatting_removed(self):
"""Test that unavailable status formatting is removed when use_rich=False."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
finetune_job = create_finetune_response(progress=None)
result = generate_progress_bar(finetune_job, current_time, use_rich=False)
assert result == "Progress: unavailable"
def test_rich_formatting_removed_at_completion(self):
"""Test that rich formatting is removed at 100% when use_rich=False."""
current_time = datetime(2024, 1, 1, 12, 1, 0, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=False)
assert result == "Progress: ████████████████████████████████████████ 100% N/A left"
def test_default_behavior_strips_formatting(self):
"""Test that rich formatting is removed by default (use_rich not specified)."""
current_time = datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time)
assert result == "Progress: ████████████████████░░░░░░░░░░░░░░░░░░░░ 50% 30s left"
def test_content_consistency_between_modes(self):
"""Test that use_rich=True and use_rich=False have same content, just different formatting."""
import re
current_time = datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result_with_rich = generate_progress_bar(finetune_job, current_time, use_rich=True)
result_without_rich = generate_progress_bar(finetune_job, current_time, use_rich=False)
stripped_rich = re.sub(r"\[/?[^\]]+\]", "", result_with_rich)
assert stripped_rich == result_without_rich
def test_all_rich_tag_types_removed(self):
"""Test that all types of rich formatting tags are properly removed."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
# Test with completed status (has [bold green] tags)
completed_job = create_finetune_response(status=FinetuneJobStatus.STATUS_COMPLETED, progress=None)
result_completed = generate_progress_bar(completed_job, current_time, use_rich=False)
assert result_completed == "Progress: completed"
# Test with unavailable status (has [bold red] tags)
unavailable_job = create_finetune_response(progress=None)
result_unavailable = generate_progress_bar(unavailable_job, current_time, use_rich=False)
assert result_unavailable == "Progress: unavailable"
@pytest.mark.parametrize(
"use_rich,expected_completed,expected_running",
[
(
True,
"Progress: [bold green]completed[/bold green]",
"Progress: ███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ [bold] 17%[/bold] [yellow]50s left[/yellow]",
),
(
False,
"Progress: completed",
"Progress: ███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 17% 50s left",
),
],
)
def test_rich_parameter_with_different_statuses(
self, use_rich: bool, expected_completed: str, expected_running: str
):
"""Test use_rich parameter works correctly with different job statuses."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
# Test completed status
completed_job = create_finetune_response(status=FinetuneJobStatus.STATUS_COMPLETED, progress=None)
result = generate_progress_bar(completed_job, current_time, use_rich=use_rich)
assert result == expected_completed
# Test running status
running_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(running_job, current_time, use_rich=use_rich)
assert result == expected_running
def test_progress_percentage_1_percent(self):
"""Test progress bar at 1% completion."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=1000.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=False)
assert result == "Progress: █░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1% 16min 30s left"
def test_progress_percentage_75_percent(self):
"""Test progress bar at 75% completion."""
current_time = datetime(2024, 1, 1, 12, 0, 45, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=False)
assert result == "Progress: ██████████████████████████████░░░░░░░░░░ 75% 15s left"
class TestGenerateProgressBarCornerCases:
"""Corner cases and edge conditions."""
def test_zero_seconds_remaining(self):
"""Test handling of zero seconds remaining (potential division by zero)."""
current_time = datetime(2024, 1, 1, 12, 0, 10, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=0.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert result == "Progress: [bold red]unavailable[/bold red]"
def test_very_small_remaining_time(self):
"""Test with very small remaining time (< 1 second)."""
current_time = datetime(2024, 1, 1, 12, 0, 5, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=0.5)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert (
result == "Progress: ████████████████████████████████████████ [bold]100%[/bold] [yellow]N/A left[/yellow]"
)
def test_very_large_remaining_time(self):
"""Test with very large remaining time (hours)."""
current_time = datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=36000.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert (
result
== "Progress: █░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ [bold] 0%[/bold] [yellow]9h 59min 30s left[/yellow]"
)
def test_job_exceeding_estimate(self):
"""Test when elapsed time exceeds original estimate (>100% progress)."""
current_time = datetime(2024, 1, 1, 14, 0, 0, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert (
result == "Progress: ████████████████████████████████████████ [bold]100%[/bold] [yellow]N/A left[/yellow]"
)
def test_timezone_aware_datetime(self):
"""Test with different timezone for updated_at."""
current_time = datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
updated_at=datetime(2024, 1, 1, 7, 0, 0, tzinfo=ZoneInfo("EST")), # Same as 12:00:00 UTC (EST = UTC-5)
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0),
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert (
result == "Progress: ████████████████████░░░░░░░░░░░░░░░░░░░░ [bold] 50%[/bold] [yellow]30s left[/yellow]"
)
def test_estimate_unavailable_flag(self):
"""Test when estimate_available flag is False."""
current_time = datetime(2024, 1, 1, 12, 0, 50, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=False, seconds_remaining=100.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert result == "Progress: [bold red]unavailable[/bold red]"
def test_negative_elapsed_time_scenario(self):
"""Test unusual case where current time appears before updated_at."""
current_time = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
updated_at=datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc), # In the "future"
progress=FinetuneProgress(estimate_available=True, seconds_remaining=100.0),
)
result = generate_progress_bar(finetune_job, current_time, use_rich=True)
assert result == "Progress: [bold red]unavailable[/bold red]"
def test_unicode_progress_bars_preserved(self):
"""Test that unicode characters in progress bars are preserved after tag removal."""
current_time = datetime(2024, 1, 1, 12, 0, 30, tzinfo=timezone.utc)
finetune_job = create_finetune_response(
progress=FinetuneProgress(estimate_available=True, seconds_remaining=60.0)
)
result = generate_progress_bar(finetune_job, current_time, use_rich=False)
assert result == "Progress: ████████████████████░░░░░░░░░░░░░░░░░░░░ 50% 30s left"