-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathtest_widgets.py
More file actions
428 lines (374 loc) · 13.4 KB
/
Copy pathtest_widgets.py
File metadata and controls
428 lines (374 loc) · 13.4 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from __future__ import annotations
import io
import time
from datetime import timedelta
import pytest
import progressbar
max_values: list[type[progressbar.base.UnknownLength] | int | None] = [
None,
10,
progressbar.UnknownLength,
]
def test_create_wrapper() -> None:
# F4: user-facing validation must raise ValueError (not a bare assert that
# vanishes under ``python -O``).
with pytest.raises(ValueError):
progressbar.widgets.create_wrapper('ab')
with pytest.raises(RuntimeError):
progressbar.widgets.create_wrapper(123)
def test_create_marker_rejects_multichar_marker() -> None:
# F4: markers must be a single visible character.
with pytest.raises(ValueError):
progressbar.widgets.create_marker('ab')
def test_multi_range_bar_rejects_multichar_marker() -> None:
# F4: the render path validates marker width; a 2-char marker must raise
# ValueError rather than a stripped-under-O assert.
widget = progressbar.MultiRangeBar('amounts', markers=['ab', ' '])
bar = progressbar.ProgressBar(
widgets=[widget],
variables={'amounts': []},
max_value=10,
fd=io.StringIO(),
term_width=60,
)
bar.start()
data = bar.data()
data['variables'] = {'amounts': [1, 0]}
with pytest.raises(ValueError):
widget(bar, data, width=20)
bar.finish(dirty=True)
def test_multi_range_bar_rejects_multichar_fill() -> None:
# Item 4: the fill path validates the fill width; a 2-char fill must raise
# ValueError rather than a stripped-under-O assert. Non-empty amounts keep
# the initial render on the marker branch; emptying them forces the
# zero-sum ``else`` (fill) branch on the direct call.
widget = progressbar.MultiRangeBar(
'amounts', markers=[' ', '#'], fill='xx'
)
bar = progressbar.ProgressBar(
widgets=[widget],
variables={'amounts': [1, 0]},
max_value=10,
fd=io.StringIO(),
term_width=60,
)
bar.start()
data = bar.data()
data['variables'] = {'amounts': []}
with pytest.raises(ValueError):
widget(bar, data, width=20)
bar.finish(dirty=True)
def test_widgets_small_values() -> None:
widgets = [
'Test: ',
progressbar.Percentage(),
' ',
progressbar.Bar(marker=progressbar.RotatingMarker()),
' ',
progressbar.ETA(),
' ',
progressbar.AbsoluteETA(),
' ',
progressbar.FileTransferSpeed(),
]
p = progressbar.ProgressBar(widgets=widgets, max_value=10).start()
p.update(0)
for i in range(10):
time.sleep(1)
p.update(i + 1)
p.finish()
@pytest.mark.parametrize('max_value', [10**6, 10**8])
def test_widgets_large_values(max_value) -> None:
widgets = [
'Test: ',
progressbar.Percentage(),
' ',
progressbar.Bar(marker=progressbar.RotatingMarker()),
' ',
progressbar.ETA(),
' ',
progressbar.AbsoluteETA(),
' ',
progressbar.FileTransferSpeed(),
]
p = progressbar.ProgressBar(widgets=widgets, max_value=max_value).start()
for i in range(0, 10**6, 10**4):
time.sleep(1)
p.update(i + 1)
p.finish()
def test_postfix_widget_renders_mapping_sorted() -> None:
bar = progressbar.ProgressBar(
widgets=[progressbar.Postfix()],
variables={'postfix': {'z': 2, 'a': 1}},
fd=io.StringIO(),
term_width=80,
)
bar.start()
output = ''.join(bar._format_widgets())
assert output == ' a=1, z=2'
def test_postfix_widget_renders_string() -> None:
bar = progressbar.ProgressBar(
widgets=[progressbar.Postfix()],
variables={'postfix': 'loss=0.25'},
fd=io.StringIO(),
term_width=80,
)
bar.start()
output = ''.join(bar._format_widgets())
assert output == ' loss=0.25'
def test_postfix_widget_renders_other_values() -> None:
bar = progressbar.ProgressBar(
widgets=[progressbar.Postfix()],
variables={'postfix': 3},
fd=io.StringIO(),
term_width=80,
)
bar.start()
output = ''.join(bar._format_widgets())
assert output == ' 3'
@pytest.mark.parametrize(
('value', 'expected'),
[
(0, ' 0'),
(0.0, ' 0.0'),
(False, ' False'),
],
)
def test_postfix_widget_renders_falsy_values(value, expected) -> None:
bar = progressbar.ProgressBar(
widgets=[progressbar.Postfix()],
variables={'postfix': value},
fd=io.StringIO(),
term_width=80,
)
bar.start()
output = ''.join(bar._format_widgets())
assert output == expected
def test_postfix_widget_omits_empty_values() -> None:
bar = progressbar.ProgressBar(
widgets=[progressbar.Postfix()],
variables={'postfix': None},
fd=io.StringIO(),
term_width=80,
)
bar.start()
output = ''.join(bar._format_widgets())
assert output == ''
def test_format_unit_value_special_cases() -> None:
assert progressbar.widgets.format_unit_value(None) == 'N/A'
unknown_value = progressbar.widgets.format_unit_value(
progressbar.UnknownLength,
)
assert unknown_value == 'N/A'
assert progressbar.widgets.format_unit_value(1.5, unit='B') == '1.5 B'
assert progressbar.widgets.format_unit_value(2, unit='B') == '2 B'
def test_unit_progress_scales_values() -> None:
bar = progressbar.ProgressBar(
max_value=2048,
widgets=[progressbar.UnitProgress(unit='B', unit_scale=True)],
fd=io.StringIO(),
term_width=80,
)
bar.start()
bar.update(1024, force=True)
output = ''.join(bar._format_widgets())
assert output == '1.0 KiB of 2.0 KiB'
def test_unit_progress_uses_progress_units_by_default() -> None:
bar = progressbar.ProgressBar(
max_value=2048,
widgets=[progressbar.UnitProgress()],
unit='B',
unit_scale=True,
fd=io.StringIO(),
term_width=80,
)
bar.start()
bar.update(1024, force=True)
output = ''.join(bar._format_widgets())
assert output == '1.0 KiB of 2.0 KiB'
def test_format_widget() -> None:
widgets = [
progressbar.FormatLabel(f'%({mapping})r')
for mapping in progressbar.FormatLabel.mapping
]
p = progressbar.ProgressBar(widgets=widgets)
for _ in p(range(10)):
time.sleep(1)
@pytest.mark.parametrize('max_value', [None, 10])
def test_all_widgets_small_values(max_value) -> None:
widgets = [
progressbar.Timer(),
progressbar.ETA(),
progressbar.AdaptiveETA(),
progressbar.AbsoluteETA(),
progressbar.DataSize(),
progressbar.FileTransferSpeed(),
progressbar.AdaptiveTransferSpeed(),
progressbar.AnimatedMarker(),
progressbar.Counter(),
progressbar.Percentage(),
progressbar.FormatLabel('%(value)d'),
progressbar.SimpleProgress(),
progressbar.Bar(),
progressbar.ReverseBar(),
progressbar.BouncingBar(),
progressbar.CurrentTime(),
progressbar.CurrentTime(microseconds=False),
progressbar.CurrentTime(microseconds=True),
]
p = progressbar.ProgressBar(widgets=widgets, max_value=max_value)
for i in range(10):
time.sleep(1)
p.update(i + 1)
p.finish()
@pytest.mark.parametrize('max_value', [10**6, 10**7])
def test_all_widgets_large_values(max_value) -> None:
widgets = [
progressbar.Timer(),
progressbar.ETA(),
progressbar.AdaptiveETA(),
progressbar.AbsoluteETA(),
progressbar.DataSize(),
progressbar.FileTransferSpeed(),
progressbar.AdaptiveTransferSpeed(),
progressbar.AnimatedMarker(),
progressbar.Counter(),
progressbar.Percentage(),
progressbar.FormatLabel('%(value)d/%(max_value)d'),
progressbar.SimpleProgress(),
progressbar.Bar(fill=lambda progress, data, width: '#'),
progressbar.ReverseBar(),
progressbar.BouncingBar(),
progressbar.FormatCustomText('Custom %(text)s', dict(text='text')),
]
p = progressbar.ProgressBar(widgets=widgets, max_value=max_value)
p.update()
time.sleep(1)
p.update()
for i in range(0, 10**6, 10**4):
time.sleep(1)
p.update(i)
@pytest.mark.parametrize('min_width', [None, 1, 2, 80, 120])
@pytest.mark.parametrize('term_width', [1, 2, 80, 120])
def test_all_widgets_min_width(min_width, term_width) -> None:
widgets = [
progressbar.Timer(min_width=min_width),
progressbar.ETA(min_width=min_width),
progressbar.AdaptiveETA(min_width=min_width),
progressbar.AbsoluteETA(min_width=min_width),
progressbar.DataSize(min_width=min_width),
progressbar.FileTransferSpeed(min_width=min_width),
progressbar.AdaptiveTransferSpeed(min_width=min_width),
progressbar.AnimatedMarker(min_width=min_width),
progressbar.Counter(min_width=min_width),
progressbar.Percentage(min_width=min_width),
progressbar.FormatLabel('%(value)d', min_width=min_width),
progressbar.SimpleProgress(min_width=min_width),
progressbar.Bar(min_width=min_width),
progressbar.ReverseBar(min_width=min_width),
progressbar.BouncingBar(min_width=min_width),
progressbar.FormatCustomText(
'Custom %(text)s',
dict(text='text'),
min_width=min_width,
),
progressbar.DynamicMessage('custom', min_width=min_width),
progressbar.CurrentTime(min_width=min_width),
]
p = progressbar.ProgressBar(widgets=widgets, term_width=term_width)
p.update(0)
p.update()
for widget in p._format_widgets():
if min_width and min_width > term_width:
assert widget == ''
else:
assert widget != ''
@pytest.mark.parametrize('max_width', [None, 1, 2, 80, 120])
@pytest.mark.parametrize('term_width', [1, 2, 80, 120])
def test_all_widgets_max_width(max_width, term_width) -> None:
widgets = [
progressbar.Timer(max_width=max_width),
progressbar.ETA(max_width=max_width),
progressbar.AdaptiveETA(max_width=max_width),
progressbar.AbsoluteETA(max_width=max_width),
progressbar.DataSize(max_width=max_width),
progressbar.FileTransferSpeed(max_width=max_width),
progressbar.AdaptiveTransferSpeed(max_width=max_width),
progressbar.AnimatedMarker(max_width=max_width),
progressbar.Counter(max_width=max_width),
progressbar.Percentage(max_width=max_width),
progressbar.FormatLabel('%(value)d', max_width=max_width),
progressbar.SimpleProgress(max_width=max_width),
progressbar.Bar(max_width=max_width),
progressbar.ReverseBar(max_width=max_width),
progressbar.BouncingBar(max_width=max_width),
progressbar.FormatCustomText(
'Custom %(text)s',
dict(text='text'),
max_width=max_width,
),
progressbar.DynamicMessage('custom', max_width=max_width),
progressbar.CurrentTime(max_width=max_width),
]
p = progressbar.ProgressBar(widgets=widgets, term_width=term_width)
p.update(0)
p.update()
for widget in p._format_widgets():
if max_width and max_width < term_width:
assert widget == ''
else:
assert widget != ''
def test_eta_respects_min_value() -> None:
# Regression: B3 - the items/second rate divided by the raw value
# instead of the progress relative to min_value.
bar = progressbar.ProgressBar(
min_value=50, max_value=100, fd=io.StringIO(), term_width=60
)
bar.start()
bar.update(75)
bar.start_time -= timedelta(seconds=30)
data = bar.data()
progressbar.ETA()(bar, data)
# 25 of 50 items done in 30 seconds -> 30 seconds remaining
assert data['eta_seconds'] == pytest.approx(30, rel=0.05)
def test_multi_progress_bar_zero_total() -> None:
# Regression: B5 - a (value, 0) tuple raised ZeroDivisionError.
widget = progressbar.MultiProgressBar('jobs')
bar = progressbar.ProgressBar(
widgets=[widget], max_value=10, fd=io.StringIO(), term_width=60
)
ranges = widget.get_values(bar, {'variables': {'jobs': [(3, 0)]}})
assert sum(ranges) > 0
def test_bar_widget_respects_min_value() -> None:
# Regression: B9 - the fill width was computed from the raw value, so
# a bar at 0% progress with min_value > 0 rendered partially full.
bar = progressbar.ProgressBar(
min_value=50,
max_value=100,
widgets=[progressbar.Bar()],
fd=io.StringIO(),
term_width=60,
)
bar.start()
assert '#' not in bar.fd.getvalue()
bar.finish(dirty=True)
def test_animated_marker_fill_stays_full_when_finished() -> None:
# Regression: a Bar filled by an AnimatedMarker(fill=...) collapsed to a
# single marker character at finish() because the end_time branch
# short-circuited before applying the fill. The finished bar must stay
# full instead of emptying out at 100%.
bar = progressbar.ProgressBar(
widgets=[progressbar.Bar(marker=progressbar.AnimatedMarker(fill='#'))],
max_value=10,
fd=io.StringIO(),
term_width=60,
)
bar.start()
for i in range(11):
bar.update(i)
bar.finish()
last_line = [
line for line in bar.fd.getvalue().split('\n') if line.strip()
][-1]
# term_width 60 leaves ~58 fill characters; the collapse bug left ~1
assert last_line.count('#') > 40, repr(last_line)