aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-05-12 23:45:25 +0200
committerDaniel Schadt <kingdread@gmx.de>2023-05-12 23:45:25 +0200
commitdca030a075d2a1b12f8ca902ffb44fa1eeb6b718 (patch)
tree6f713d4821e0a3b34cf067f6be4f81c945e33705
parent27963e61e0626733f3b85b4ddde373fb1b17e13a (diff)
downloadfietsboek-dca030a075d2a1b12f8ca902ffb44fa1eeb6b718.tar.gz
fietsboek-dca030a075d2a1b12f8ca902ffb44fa1eeb6b718.tar.bz2
fietsboek-dca030a075d2a1b12f8ca902ffb44fa1eeb6b718.zip
add unit tests for util.human_size
-rw-r--r--fietsboek/util.py8
-rw-r--r--tests/unit/test_util.py16
2 files changed, 22 insertions, 2 deletions
diff --git a/fietsboek/util.py b/fietsboek/util.py
index ecd2f31..acbb45d 100644
--- a/fietsboek/util.py
+++ b/fietsboek/util.py
@@ -210,12 +210,16 @@ def human_size(num_bytes: int) -> str:
"""
num_bytes = float(num_bytes)
suffixes = ["B", "KiB", "MiB", "GiB"]
+ prefix = ""
+ if num_bytes < 0:
+ prefix = "-"
+ num_bytes = -num_bytes
for suffix in suffixes:
if num_bytes < 1024 or suffix == suffixes[-1]:
if suffix == "B":
# Don't do the decimal point for bytes
- return f"{int(num_bytes)} {suffix}"
- return f"{num_bytes:.1f} {suffix}"
+ return f"{prefix}{int(num_bytes)} {suffix}"
+ return f"{prefix}{num_bytes:.1f} {suffix}"
num_bytes /= 1024
# Unreachable:
return ""
diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py
index 1a56911..6dc8e7d 100644
--- a/tests/unit/test_util.py
+++ b/tests/unit/test_util.py
@@ -88,6 +88,22 @@ def test_tour_metadata(gpx_file):
def test_mps_to_kph(mps, kph):
assert util.mps_to_kph(mps) == pytest.approx(kph, 0.1)
+@pytest.mark.parametrize('num_bytes, expected', [
+ (1, '1 B'),
+ (1023, '1023 B'),
+ (1024, '1.0 KiB'),
+ (1536, '1.5 KiB'),
+ (1024 ** 2, '1.0 MiB'),
+ (1024 ** 3, '1.0 GiB'),
+ (0, '0 B'),
+ # Negative sizes in itself are a bit weird, but they make sense as the
+ # difference between two size values, so they should still work.
+ (-1, '-1 B'),
+ (-1024, '-1.0 KiB'),
+])
+def test_human_size(num_bytes, expected):
+ assert util.human_size(num_bytes) == expected
+
def test_tile_url(app_request):
route_url = util.tile_url(app_request, "tile-proxy", provider="bobby")