|
15 | 15 | # |
16 | 16 | import uuid |
17 | 17 | import hashlib |
18 | | -from common.misc_utils import get_uuid, download_img, hash_str2int |
| 18 | +from common.misc_utils import get_uuid, download_img, hash_str2int, convert_bytes |
19 | 19 |
|
20 | 20 |
|
21 | 21 | class TestGetUuid: |
@@ -270,3 +270,86 @@ def test_whitespace_strings(self): |
270 | 270 | result = hash_str2int(test_str) |
271 | 271 | assert isinstance(result, int) |
272 | 272 | assert 0 <= result < 10 ** 8 |
| 273 | + |
| 274 | + |
| 275 | +class TestConvertBytes: |
| 276 | + """Test suite for convert_bytes function""" |
| 277 | + |
| 278 | + def test_zero_bytes(self): |
| 279 | + """Test that 0 bytes returns '0 B'""" |
| 280 | + assert convert_bytes(0) == "0 B" |
| 281 | + |
| 282 | + def test_single_byte(self): |
| 283 | + """Test single byte values""" |
| 284 | + assert convert_bytes(1) == "1 B" |
| 285 | + assert convert_bytes(999) == "999 B" |
| 286 | + |
| 287 | + def test_kilobyte_range(self): |
| 288 | + """Test values in kilobyte range with different precisions""" |
| 289 | + # Exactly 1 KB |
| 290 | + assert convert_bytes(1024) == "1.00 KB" |
| 291 | + |
| 292 | + # Values that should show 1 decimal place (10-99.9 range) |
| 293 | + assert convert_bytes(15360) == "15.0 KB" # 15 KB exactly |
| 294 | + assert convert_bytes(10752) == "10.5 KB" # 10.5 KB |
| 295 | + |
| 296 | + # Values that should show 2 decimal places (1-9.99 range) |
| 297 | + assert convert_bytes(2048) == "2.00 KB" # 2 KB exactly |
| 298 | + assert convert_bytes(3072) == "3.00 KB" # 3 KB exactly |
| 299 | + assert convert_bytes(5120) == "5.00 KB" # 5 KB exactly |
| 300 | + |
| 301 | + def test_megabyte_range(self): |
| 302 | + """Test values in megabyte range""" |
| 303 | + # Exactly 1 MB |
| 304 | + assert convert_bytes(1048576) == "1.00 MB" |
| 305 | + |
| 306 | + # Values with different precision requirements |
| 307 | + assert convert_bytes(15728640) == "15.0 MB" # 15.0 MB |
| 308 | + assert convert_bytes(11010048) == "10.5 MB" # 10.5 MB |
| 309 | + |
| 310 | + def test_gigabyte_range(self): |
| 311 | + """Test values in gigabyte range""" |
| 312 | + # Exactly 1 GB |
| 313 | + assert convert_bytes(1073741824) == "1.00 GB" |
| 314 | + |
| 315 | + # Large value that should show 0 decimal places |
| 316 | + assert convert_bytes(3221225472) == "3.00 GB" # 3 GB exactly |
| 317 | + |
| 318 | + def test_terabyte_range(self): |
| 319 | + """Test values in terabyte range""" |
| 320 | + assert convert_bytes(1099511627776) == "1.00 TB" # 1 TB |
| 321 | + |
| 322 | + def test_petabyte_range(self): |
| 323 | + """Test values in petabyte range""" |
| 324 | + assert convert_bytes(1125899906842624) == "1.00 PB" # 1 PB |
| 325 | + |
| 326 | + def test_boundary_values(self): |
| 327 | + """Test values at unit boundaries""" |
| 328 | + # Just below 1 KB |
| 329 | + assert convert_bytes(1023) == "1023 B" |
| 330 | + |
| 331 | + # Just above 1 KB |
| 332 | + assert convert_bytes(1025) == "1.00 KB" |
| 333 | + |
| 334 | + # At 100 KB boundary (should switch to 0 decimal places) |
| 335 | + assert convert_bytes(102400) == "100 KB" |
| 336 | + assert convert_bytes(102300) == "99.9 KB" |
| 337 | + |
| 338 | + def test_precision_transitions(self): |
| 339 | + """Test the precision formatting transitions""" |
| 340 | + # Test transition from 2 decimal places to 1 decimal place |
| 341 | + assert convert_bytes(9216) == "9.00 KB" # 9.00 KB (2 decimal places) |
| 342 | + assert convert_bytes(10240) == "10.0 KB" # 10.0 KB (1 decimal place) |
| 343 | + |
| 344 | + # Test transition from 1 decimal place to 0 decimal places |
| 345 | + assert convert_bytes(102400) == "100 KB" # 100 KB (0 decimal places) |
| 346 | + |
| 347 | + def test_large_values_no_overflow(self): |
| 348 | + """Test that very large values don't cause issues""" |
| 349 | + # Very large value that should use PB |
| 350 | + large_value = 10 * 1125899906842624 # 10 PB |
| 351 | + assert "PB" in convert_bytes(large_value) |
| 352 | + |
| 353 | + # Ensure we don't exceed available units |
| 354 | + huge_value = 100 * 1125899906842624 # 100 PB (still within PB range) |
| 355 | + assert "PB" in convert_bytes(huge_value) |
0 commit comments