From e408c3141511eb878958da9e20dd6c4da7cc9a8f Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Sat, 17 May 2025 19:26:18 +0800 Subject: [PATCH] Fix: it is impossible to have 0.1 byte as byte is the smallest unit --- Whatsapp_Chat_Exporter/utility.py | 4 ++-- tests/test_utility.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py index 5d52d2d..268247a 100644 --- a/Whatsapp_Chat_Exporter/utility.py +++ b/Whatsapp_Chat_Exporter/utility.py @@ -85,8 +85,8 @@ def bytes_to_readable(size_bytes: int) -> str: Returns: A human-readable string representing the file size. """ - if size_bytes == 0: - return "0B" + if size_bytes < 1024: + return f"{size_bytes} B" size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") i = int(math.floor(math.log(size_bytes, 1024))) p = math.pow(1024, i) diff --git a/tests/test_utility.py b/tests/test_utility.py index 1e9012d..6a7c326 100644 --- a/tests/test_utility.py +++ b/tests/test_utility.py @@ -23,8 +23,8 @@ def test_convert_time_unit(): class TestBytesToReadable: - assert bytes_to_readable(0) == "0B" - assert bytes_to_readable(500) == "500.0 B" + assert bytes_to_readable(0) == "0 B" + assert bytes_to_readable(500) == "500 B" assert bytes_to_readable(1024) == "1.0 KB" assert bytes_to_readable(2048) == "2.0 KB" assert bytes_to_readable(1536) == "1.5 KB" @@ -40,6 +40,7 @@ class TestBytesToReadable: class TestReadableToBytes: def test_conversion(self): + assert readable_to_bytes("0B") == 0 assert readable_to_bytes("100B") == 100 assert readable_to_bytes("50 B") == 50 assert readable_to_bytes("1KB") == 1024