Files
python-utils/tests/unit/readable_unit_test.py
T

47 lines
1.3 KiB
Python

"""Unittests for the readable function."""
import pytest
from typing import Any
from python_utils import readable_unit
TEST_CASES = [
(-1, "B", ValueError),
("abc", "B", ValueError),
(100, "", ValueError),
(100, 123, ValueError),
(100, " ", ValueError),
(0.5, "B", "0.50 B"),
(0, "B", "0.00 B"),
(1, "B", "1.00 B"),
(1024, "B", "1.00 KB"),
(500000, "bps", "488.28 Kbps"),
(1048576, "bps", "1.00 Mbps"),
(50000000, "bps", "47.68 Mbps"),
(1073741824, "Hz", "1.00 GHz"),
(5000000000, "Hz", "4.66 GHz"),
(1099511627776, "foo", "1.00 Tfoo"),
(5000000000000, "bar", "4.55 Tbar"),
(2**50, "foo", "1.00 Pfoo"),
(2**60, "B", "1.00 EB"),
(2**70, "B", "1.00 ZB"),
(2**80, "B", "1.00 YB"),
(2**90, "B", "1024.00 YB"),
]
@pytest.mark.parametrize("value, unit, expected", TEST_CASES)
def test_readable_unit(
value: Any, # Allow invalid types for testing
unit: Any, # Allow invalid types for testing
expected: Any, # Allow invalid types for testing
) -> None:
"""Test readable_unit function with valid inputs."""
if isinstance(expected, type) and issubclass(expected, Exception):
with pytest.raises(expected):
readable_unit(value, unit)
else:
result = readable_unit(value, unit)
assert result == expected