Files
slic/tests/test_utils_tqdm_mod.py
T
tligui_y c35dc71372
Run CI Tests / test (push) Successful in 40s
Update tests/test_utils_tqdm_mod.py
2025-07-30 14:55:15 +02:00

96 lines
2.5 KiB
Python

# test_tqdm_mod.py
import io
import sys
from time import sleep
import pytest
import tqdm
from contextlib import redirect_stdout
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from slic.utils.tqdm_mod import *
def extract_last_line(output):
return output.strip().splitlines()[-1]
def get_bar_visual(line):
try:
return line.split("|")[1]
except IndexError:
return ""
# Tests
def test_complete_progress_bar():
f = io.StringIO()
with redirect_stdout(f):
for _ in tqdm_mod(range(3), desc="TestBar", file=f):
sleep(0.001)
lines = extract_lines(f.getvalue(), "TestBar")
last = lines[-1]
bar = get_bar_visual(last)
assert last.startswith("TestBar: 100%")
assert "3.00/3.00" in last
assert "Hz" in last
# Check that the bar us full
assert len(bar.replace("", "").replace("#", "").strip()) == 0, f"Bar not full: '{bar}'"
def test_set_progress_multiple_points():
f = io.StringIO()
with redirect_stdout(f):
bar = tqdm_mod(total=5, desc="SetBar", file=f)
bar.set(1.0)
bar.set(2.0)
bar.set(3.5)
bar.set(5.0)
bar.close()
lines = extract_lines(f.getvalue(), "SetBar")
for i, (expected_progress, expected_value) in enumerate([
("20%", "1.00/5.00"),
("40%", "2.00/5.00"),
("70%", "3.50/5.00"),
("100%", "5.00/5.00"),
]):
assert lines[i].startswith(f"SetBar:{expected_progress}")
assert expected_value in lines[i]
bar = get_bar_visual(lines[i])
assert len(bar.strip()) > 0
def test_custom_unit():
f = io.StringIO()
with redirect_stdout(f):
for _ in tqdm_mod(range(4), desc="StepBar", unit="step", file=f):
sleep(0.001)
lines = extract_lines(f.getvalue(), "StepBar")
last = lines[-1]
bar = get_bar_visual(last)
assert last.startswith("StepBar: 100%")
assert "4.00/4.00" in last
assert "step/s" in last
assert len(bar.strip()) > 0
def test_clamp_above_total():
f = io.StringIO()
with redirect_stdout(f):
bar = tqdm_mod(total=10, desc="ClampBar", file=f)
bar.set(12)
bar.close()
lines = extract_lines(f.getvalue(), "ClampBar")
last = lines[-1]
bar = get_bar_visual(last)
assert last.startswith("ClampBar: 100%")
assert "10.0/10.0" in last
assert "Hz" in last
# Check that the bar us full
assert len(bar.replace("", "").replace("#", "").strip()) == 0, f"Bar not full: '{bar}'"