|
| 1 | +import pytest |
| 2 | + |
| 3 | +from dstack._internal.cli.commands.metrics import _format_memory |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.parametrize( |
| 7 | + "bytes_value,decimal_places,expected", |
| 8 | + [ |
| 9 | + # Test MB values with different decimal places |
| 10 | + (512 * 1024 * 1024, 0, "512MB"), # exact MB, no decimals |
| 11 | + (512 * 1024 * 1024, 2, "512MB"), # exact MB, with decimals |
| 12 | + (512.5 * 1024 * 1024, 0, "512MB"), # decimal MB, no decimals |
| 13 | + (512.5 * 1024 * 1024, 2, "512.5MB"), # decimal MB, 2 decimals |
| 14 | + (512.5 * 1024 * 1024, 3, "512.5MB"), # decimal MB, 3 decimals |
| 15 | + (999 * 1024 * 1024, 0, "999MB"), # just under 1GB, no decimals |
| 16 | + (999 * 1024 * 1024, 2, "999MB"), # just under 1GB, with decimals |
| 17 | + # Test GB values with different decimal places |
| 18 | + (1.5 * 1024 * 1024 * 1024, 0, "2GB"), # decimal GB, no decimals |
| 19 | + (1.5 * 1024 * 1024 * 1024, 2, "1.5GB"), # decimal GB, 2 decimals |
| 20 | + (1.5 * 1024 * 1024 * 1024, 3, "1.5GB"), # decimal GB, 3 decimals |
| 21 | + (2 * 1024 * 1024 * 1024, 0, "2GB"), # exact GB, no decimals |
| 22 | + (2 * 1024 * 1024 * 1024, 2, "2GB"), # exact GB, with decimals |
| 23 | + # Test edge cases |
| 24 | + (0, 0, "0MB"), # zero bytes, no decimals |
| 25 | + (0, 2, "0MB"), # zero bytes, with decimals |
| 26 | + (1023 * 1024, 0, "1MB"), # just under 1MB, no decimals |
| 27 | + (1023 * 1024, 2, "1MB"), # just under 1MB, with decimals |
| 28 | + (1024 * 1024 * 1024 - 1, 0, "1024MB"), # just under 1GB, no decimals |
| 29 | + (1024 * 1024 * 1024 - 1, 2, "1024MB"), # just under 1GB, with decimals |
| 30 | + ], |
| 31 | +) |
| 32 | +def test_format_memory(bytes_value: int, decimal_places: int, expected: str): |
| 33 | + result = _format_memory(bytes_value, decimal_places) |
| 34 | + assert result == expected |
0 commit comments