Update tests/test_utils_tqdm_mod.py
Run CI Tests / test (push) Successful in 37s

This commit is contained in:
2025-07-30 15:54:26 +02:00
parent b043fdaf53
commit a6bfc0aaf1
+10 -11
View File
@@ -82,22 +82,21 @@ def test_format_sizeof_alignment():
# test format_sizeof rounding using tqdm # test format_sizeof rounding using tqdm
def test_float_alignment_in_bar(): def test_float_alignment_in_bar():
# Capture the tqdm output into a string buffer # Capture the tqdm output into a string buffer
f = io.StringIO() f = io.StringIO()
with redirect_stdout(f): with redirect_stdout(f):
bar = tqdm_mod(total=100.12, desc="AlignBar", file=f, miniters=1, mininterval=0) bar = tqdm_mod(total=100.12, desc="AlignBar", file=f, miniters=1, mininterval=0)
bar.set(1.3333) bar.set(1.3333)
bar.set(12.5) bar.set(12.5)
bar.set(99.98) bar.set(99.89)
bar.set(100.0) bar.set(100.0)
bar.set(100.12) bar.set(100.12)
bar.close() bar.close()
# Extract lines containing the label # Extract lines containing the label
lines = extract_lines(f.getvalue(), "AlignBar") lines = extract_lines(f.getvalue(), "AlignBar")
# Expected strings that should appear in the output, rounded with format_sizeof # Expected formatted values using format_sizeof
expected_values = [ expected_values = [
" 1.3/100.1", " 1.3/100.1",
" 12.5/100.1", " 12.5/100.1",
@@ -106,21 +105,21 @@ def test_float_alignment_in_bar():
"100.1/100.1", "100.1/100.1",
] ]
# Extract progress values matching the format "xx.x/100.1" # Extract the actual padded float/total strings from the full lines
values = [] values = []
for line in lines: for line in lines:
match = re.search(r"(\d{1,3}\.\d)/100\.1", line) match = re.search(r"(\s*\d{1,3}\.\d)/100\.1", line)
if match: if match:
values.append(match.group(0)) values.append(match.group(0))
# Ensure raw 100.12 never appears : format_sizeof must have truncated it # Ensure raw 100.12 never appears : format_sizeof must have truncated it
assert all("100.12" not in line for line in lines), "Unrounded value '100.12' found in output!" assert all("100.12" not in line for line in lines), "Unrounded value '100.12' found in output!"
# Ensure all expected formatted values are found in the output # Check all expected values appear rounded as expected by format_sizeof
for expected in expected_values: for expected in expected_values:
assert expected in values, f"Missing expected value: {expected}" assert expected in values, f"Missing expected value: {expected}"
# Ensure all values are visually aligned thanks to format_sizeof : all outouts with same length # Check that all values are visually aligned, output with same length, to ensure that format_sizeof add the good number avec spaces
lengths = [len(v) for v in values] lengths = [len(v) for v in values]
assert len(set(lengths)) == 1, f"Misaligned values: {values}" assert len(set(lengths)) == 1, f"Misaligned values: {values}"