diff --git a/src/tribler/core/components/libtorrent/download_manager/download_manager.py b/src/tribler/core/components/libtorrent/download_manager/download_manager.py index 0884f6967a4..d8d744ebe42 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download_manager.py +++ b/src/tribler/core/components/libtorrent/download_manager/download_manager.py @@ -140,10 +140,9 @@ def convert_rate(rate: int) -> int: """ if rate == 0: return -1 - elif rate == -1: + if rate == -1: return 1 - else: - return rate * 1024 + return rate * 1024 @staticmethod def reverse_convert_rate(rate: int) -> int: @@ -153,10 +152,9 @@ def reverse_convert_rate(rate: int) -> int: """ if rate == -1: return 0 - elif rate == 1: + if rate == 1: return -1 - else: - return rate // 1024 + return rate // 1024 async def _check_dht_ready(self, min_dht_peers=60): """ diff --git a/src/tribler/core/components/libtorrent/tests/test_download_manager.py b/src/tribler/core/components/libtorrent/tests/test_download_manager.py index 18e3b625b84..db9aeddb4a5 100644 --- a/src/tribler/core/components/libtorrent/tests/test_download_manager.py +++ b/src/tribler/core/components/libtorrent/tests/test_download_manager.py @@ -274,6 +274,20 @@ async def test_start_download_existing_handle(fake_dlmgr): await download.shutdown() +def test_convert_rate(): + assert DownloadManager.convert_rate(0) == -1 # 0 is a special value + assert DownloadManager.convert_rate(-1) == 1 # -1 is a special value + for test_num in [1, 2, -2]: + assert DownloadManager.convert_rate(test_num) == test_num * 1024 + + +def test_reverse_convert_rate(): + assert DownloadManager.reverse_convert_rate(-1) == 0 # -1 is a special value + assert DownloadManager.reverse_convert_rate(1) == -1 # 1 is a special value + for test_num in [0, 2, -2]: + assert DownloadManager.reverse_convert_rate(test_num) == test_num // 1024 + + def test_start_download_existing_download(fake_dlmgr): """ Testing the addition of a torrent to the libtorrent manager, if there is a pre-existing download.