|
| 1 | +"""Tests for the toolbox module.""" |
| 2 | + |
| 3 | +from mdverse_scrapers.core.toolbox import ( |
| 4 | + convert_file_size_in_bytes_to_human_readable_format, |
| 5 | +) |
| 6 | + |
| 7 | + |
| 8 | +class TestConvertFileSizeInBytesToHumanReadableFormat: |
| 9 | + """Tests for convert_file_size_in_bytes_to_human_readable_format function.""" |
| 10 | + |
| 11 | + def test_bytes(self): |
| 12 | + """Test conversion for values in bytes range.""" |
| 13 | + assert convert_file_size_in_bytes_to_human_readable_format(0) == "0.00 B" |
| 14 | + assert convert_file_size_in_bytes_to_human_readable_format(1) == "1.00 B" |
| 15 | + assert convert_file_size_in_bytes_to_human_readable_format(512) == "512.00 B" |
| 16 | + assert convert_file_size_in_bytes_to_human_readable_format(1023) == "1023.00 B" |
| 17 | + |
| 18 | + def test_kilobytes(self): |
| 19 | + """Test conversion for values in kilobytes range.""" |
| 20 | + assert convert_file_size_in_bytes_to_human_readable_format(1024) == "1.00 KB" |
| 21 | + assert convert_file_size_in_bytes_to_human_readable_format(1536) == "1.50 KB" |
| 22 | + assert convert_file_size_in_bytes_to_human_readable_format(10240) == "10.00 KB" |
| 23 | + assert ( |
| 24 | + convert_file_size_in_bytes_to_human_readable_format(127560) == "124.57 KB" |
| 25 | + ) |
| 26 | + |
| 27 | + def test_megabytes(self): |
| 28 | + """Test conversion for values in megabytes range.""" |
| 29 | + assert convert_file_size_in_bytes_to_human_readable_format(1048576) == "1.00 MB" |
| 30 | + assert convert_file_size_in_bytes_to_human_readable_format(1289748) == "1.23 MB" |
| 31 | + assert ( |
| 32 | + convert_file_size_in_bytes_to_human_readable_format(10485760) == "10.00 MB" |
| 33 | + ) |
| 34 | + assert ( |
| 35 | + convert_file_size_in_bytes_to_human_readable_format(104857600) |
| 36 | + == "100.00 MB" |
| 37 | + ) |
| 38 | + |
| 39 | + def test_gigabytes(self): |
| 40 | + """Test conversion for values in gigabytes range.""" |
| 41 | + assert ( |
| 42 | + convert_file_size_in_bytes_to_human_readable_format(1073741824) == "1.00 GB" |
| 43 | + ) |
| 44 | + assert ( |
| 45 | + convert_file_size_in_bytes_to_human_readable_format(2147483648) == "2.00 GB" |
| 46 | + ) |
| 47 | + assert ( |
| 48 | + convert_file_size_in_bytes_to_human_readable_format(132553428173) |
| 49 | + == "123.45 GB" |
| 50 | + ) |
| 51 | + |
| 52 | + def test_terabytes(self): |
| 53 | + """Test conversion for values in terabytes range.""" |
| 54 | + assert ( |
| 55 | + convert_file_size_in_bytes_to_human_readable_format(1099511627776) |
| 56 | + == "1.00 TB" |
| 57 | + ) # 1 TB |
| 58 | + assert ( |
| 59 | + convert_file_size_in_bytes_to_human_readable_format(5497558138880) |
| 60 | + == "5.00 TB" |
| 61 | + ) # 5 TB |
| 62 | + |
| 63 | + def test_very_large_file(self): |
| 64 | + """Test conversion for files larger than terabytes.""" |
| 65 | + # 1 PB (petabyte) |
| 66 | + assert ( |
| 67 | + convert_file_size_in_bytes_to_human_readable_format(1125899906842624) |
| 68 | + == "File too big!" |
| 69 | + ) |
| 70 | + |
| 71 | + def test_edge_cases(self): |
| 72 | + """Test edge cases between unit boundaries.""" |
| 73 | + # Just at the boundary |
| 74 | + assert ( |
| 75 | + convert_file_size_in_bytes_to_human_readable_format(1024**2) == "1.00 MB" |
| 76 | + ) # Exactly 1 MB |
| 77 | + assert ( |
| 78 | + convert_file_size_in_bytes_to_human_readable_format(1024**3) == "1.00 GB" |
| 79 | + ) # Exactly 1 GB |
| 80 | + assert ( |
| 81 | + convert_file_size_in_bytes_to_human_readable_format(1024**4) == "1.00 TB" |
| 82 | + ) # Exactly 1 TB |
0 commit comments