From 5a07fb91b3a317d95ecd6e2b8400f8798308b843 Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Thu, 24 Sep 2026 04:23:12 +0000 Subject: [PATCH] Fix human_size dropping trailing zeros from kilobyte sizes The kilobyte branch stripped trailing zeros from an integer string, so 10240 bytes rendered as "1K" and 102400 bytes as "1K" in the STAC HTML templates. The rstrip is only needed for the decimal formats. --- pygeoapi/util.py | 2 +- tests/other/test_util.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pygeoapi/util.py b/pygeoapi/util.py index b60e187a08..4dce4e997b 100644 --- a/pygeoapi/util.py +++ b/pygeoapi/util.py @@ -342,7 +342,7 @@ def human_size(nbytes: int) -> str: i += 1 if suffixes[i] == 'K': - f = str(int(nbytes)).rstrip('0').rstrip('.') + f = str(int(nbytes)) elif suffixes[i] == 'B': return nbytes else: diff --git a/tests/other/test_util.py b/tests/other/test_util.py index 7cb321019d..2a471e21c8 100644 --- a/tests/other/test_util.py +++ b/tests/other/test_util.py @@ -170,6 +170,21 @@ def test_json_serial(): util.json_serial('foo') +@pytest.mark.parametrize('nbytes,expected', [ + (500, 500), + (1024, '1K'), + (1536, '1K'), + (10240, '10K'), + (102400, '100K'), + (1048576, '1M'), + (1572864, '1.5M'), + (10485760, '10M'), + (1073741824, '1G'), +]) +def test_human_size(nbytes, expected): + assert util.human_size(nbytes) == expected + + def test_mimetype(): assert util.get_mimetype('file.xml') == 'application/xml' assert util.get_mimetype('file.yml') == 'text/plain'