|
| 1 | +import pytest |
| 2 | +from holmes.plugins.toolsets.prometheus.utils import parse_duration_to_seconds |
| 3 | + |
| 4 | + |
| 5 | +class TestParseDurationToSeconds: |
| 6 | + @pytest.mark.parametrize( |
| 7 | + "input_value,expected", |
| 8 | + [ |
| 9 | + # None case |
| 10 | + (None, None), |
| 11 | + # Numeric inputs (int and float) |
| 12 | + (42, 42.0), |
| 13 | + (3.14, 3.14), |
| 14 | + (0, 0.0), |
| 15 | + # String numeric inputs |
| 16 | + ("123", 123.0), |
| 17 | + ("0", 0.0), |
| 18 | + (" 456 ", 456.0), # with whitespace |
| 19 | + # Time unit strings |
| 20 | + ("30s", 30.0), |
| 21 | + ("5m", 300.0), |
| 22 | + ("2h", 7200.0), |
| 23 | + ("1d", 86400.0), |
| 24 | + # Decimal values with units |
| 25 | + ("2.5s", 2.0), # int(float(2.5) * 1) = 2 |
| 26 | + ("1.5m", 90.0), # int(float(1.5) * 60) = 90 |
| 27 | + ("0.5h", 1800.0), # int(float(0.5) * 3600) = 1800 |
| 28 | + ("0.25d", 21600.0), # int(float(0.25) * 86400) = 21600 |
| 29 | + # Case insensitive and whitespace handling |
| 30 | + ("30S", 30.0), |
| 31 | + ("5M", 300.0), |
| 32 | + ("2H", 7200.0), |
| 33 | + ("1D", 86400.0), |
| 34 | + (" 30s ", 30.0), |
| 35 | + # Fallback to float seconds |
| 36 | + ("123.45", 123.45), |
| 37 | + ("0.5", 0.5), |
| 38 | + # Edge cases |
| 39 | + ("10", 10.0), # pure digit string |
| 40 | + ("0s", 0.0), |
| 41 | + ("0m", 0.0), |
| 42 | + ("0h", 0.0), |
| 43 | + ("0d", 0.0), |
| 44 | + # Partial time formats |
| 45 | + ("1h30m", 5400.0), # 1*3600 + 30*60 = 5400 |
| 46 | + ("2h45m", 9900.0), # 2*3600 + 45*60 = 9900 |
| 47 | + ("5m12s", 312.0), # 5*60 + 12 = 312 |
| 48 | + ("1m30s", 90.0), # 1*60 + 30 = 90 |
| 49 | + ("3h15m45s", 11745.0), # 3*3600 + 15*60 + 45 = 11745 |
| 50 | + ("1d2h30m", 95400.0), # 1*86400 + 2*3600 + 30*60 = 95400 |
| 51 | + ("2d1h", 176400.0), # 2*86400 + 1*3600 = 176400 |
| 52 | + ("30m15s", 1815.0), # 30*60 + 15 = 1815 |
| 53 | + ("1h0m30s", 3630.0), # 1*3600 + 0*60 + 30 = 3630 |
| 54 | + ("0h5m", 300.0), # 0*3600 + 5*60 = 300 |
| 55 | + # Case insensitive partial times |
| 56 | + ("1H30M", 5400.0), |
| 57 | + ("5M12S", 312.0), |
| 58 | + (" 1h30m ", 5400.0), # with whitespace |
| 59 | + ], |
| 60 | + ) |
| 61 | + def test_parse_duration_to_seconds(self, input_value, expected): |
| 62 | + result = parse_duration_to_seconds(input_value) |
| 63 | + assert result == expected |
| 64 | + |
| 65 | + @pytest.mark.parametrize( |
| 66 | + "invalid_input", |
| 67 | + [ |
| 68 | + "invalid", |
| 69 | + "10x", # unsupported unit |
| 70 | + "abc123", |
| 71 | + "", |
| 72 | + ], |
| 73 | + ) |
| 74 | + def test_parse_duration_to_seconds_invalid_fallback(self, invalid_input): |
| 75 | + # These should raise ValueError when trying to convert to float |
| 76 | + with pytest.raises(ValueError): |
| 77 | + parse_duration_to_seconds(invalid_input) |
0 commit comments