/ tests / test_utils.py
test_utils.py
  1  """Tests for the `utils` module."""
  2  
  3  from __future__ import annotations
  4  
  5  from datetime import timedelta
  6  from typing import Any
  7  
  8  import pytest
  9  
 10  from aria2p.utils import bool_or_value, bool_to_str, human_readable_bytes, human_readable_timedelta
 11  
 12  
 13  @pytest.mark.parametrize(
 14      ("value", "kwargs", "expected"),
 15      [
 16          (0, {}, "0.00B"),
 17          (0, {"digits": 0}, "0B"),
 18          (0, {"digits": 0, "delim": " "}, "0 B"),
 19          (0, {"digits": 0, "delim": " ", "postfix": "/s"}, "0 B/s"),
 20          (1024, {"digits": 0, "delim": " ", "postfix": "/s"}, "1 KiB/s"),
 21          (2048, {}, "2.00KiB"),
 22          (2048 * 1024, {}, "2.00MiB"),
 23          (2048 * 1024 * 1024, {}, "2.00GiB"),
 24          (2048 * 1024 * 1024 * 1024, {}, "2.00TiB"),
 25      ],
 26  )
 27  def test_human_readable_bytes(value: int, kwargs: Any, expected: str) -> None:
 28      """Test the `human_readable_bytes` function.
 29  
 30      Parameters:
 31          args: Positional arguments passed to the function.
 32          kwargs: Keyword arguments passed to the function.
 33          expected: The expected result.
 34      """
 35      assert human_readable_bytes(value, **kwargs) == expected
 36  
 37  
 38  @pytest.mark.parametrize(
 39      ("td", "expected"),
 40      [
 41          (timedelta(seconds=0), "0s"),
 42          (timedelta(seconds=1), "1s"),
 43          (timedelta(seconds=75), "1m15s"),
 44          (timedelta(seconds=3601), "1h1s"),
 45          (timedelta(seconds=3600 * 24 + 2), "1d2s"),
 46          (timedelta(days=1, seconds=3600 * 24 + 2), "2d2s"),
 47          (timedelta(days=3, seconds=3), "3d3s"),
 48          (timedelta(days=3), "3d"),
 49          (timedelta(seconds=3600 * 3), "3h"),
 50          (timedelta(seconds=60 * 3), "3m"),
 51      ],
 52  )
 53  def test_human_readable_timedelta_force_print_0_seconds(td: timedelta, expected: str) -> None:
 54      """Test the `human_readable_timedelta` function.
 55  
 56      Parameters:
 57          td: A timedelta.
 58          expected: The expected result.
 59      """
 60      assert human_readable_timedelta(td) == expected
 61  
 62  
 63  @pytest.mark.parametrize(
 64      ("value", "expected"),
 65      [
 66          ("true", True),
 67          ("false", False),
 68          ("mem", "mem"),
 69          (None, None),
 70          (0, 0),
 71          (1, 1),
 72      ],
 73  )
 74  def test_bool_or_value_true_is_true(value: str | int | None, expected: bool | str | int | None) -> None:
 75      """Test the `bool_or_value` function.
 76  
 77      Parameters:
 78          value: Value passed to the function.
 79          expected: The expected result.
 80      """
 81      assert bool_or_value(value) == expected
 82  
 83  
 84  @pytest.mark.parametrize(
 85      ("value", "expected"),
 86      [
 87          (True, "true"),
 88          (False, "false"),
 89          (None, None),
 90          (0, 0),
 91          (1, 1),
 92      ],
 93  )
 94  def test_bool_to_str_true_gives_true(value: bool | int | None, expected: str | int | None) -> None:
 95      """Test the `bool_to_str` function.
 96  
 97      Parameters:
 98          value: Value passed to the function.
 99          expected: The expected result.
100      """
101      assert bool_to_str(value) == expected