From 38e4bf8af96e8ff3ecbe25c396c8977ba9cc30a7 Mon Sep 17 00:00:00 2001 From: TheeCoderAhmed Date: Wed, 4 Nov 2026 05:05:08 +0300 Subject: [PATCH] Fixed uptime_string_to_seconds for uptime strings without seconds --- changes/615.fixed | 1 + netutils/constants.py | 4 ++++ tests/unit/test_time.py | 10 ++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 changes/615.fixed diff --git a/changes/615.fixed b/changes/615.fixed new file mode 100644 index 00000000..1a7d1fcd --- /dev/null +++ b/changes/615.fixed @@ -0,0 +1 @@ +Fixed uptime_string_to_seconds raising ValueError for uptime strings without a seconds value, such as "2w1d". diff --git a/netutils/constants.py b/netutils/constants.py index 1052867c..5de9f05d 100644 --- a/netutils/constants.py +++ b/netutils/constants.py @@ -420,6 +420,10 @@ r"(((?P\d+)y)?(?P\d+)w)?((?P\d+)d)?((?P\d+)h)?" r"((?P\d+)m)?((?P\d+)s)" ), # pylint: disable=implicit-str-concat + ( + r"^(?=\d)(((?P\d+)y)?(?P\d+)w)?((?P\d+)d)?((?P\d+)h)?" + r"((?P\d+)m)?$" + ), # pylint: disable=implicit-str-concat ] # Do not modify list, see UPGRADE_PATHS documentation on the versioning of this diff --git a/tests/unit/test_time.py b/tests/unit/test_time.py index b836d5ff..7f8ed1bc 100644 --- a/tests/unit/test_time.py +++ b/tests/unit/test_time.py @@ -45,6 +45,11 @@ {"sent": "4m15s", "received": 255}, {"sent": "6d10m1s", "received": 519001}, {"sent": "1y3w10m2s", "received": 33351002}, + {"sent": "2w1d", "received": 1296000}, + {"sent": "1y3w", "received": 33350400}, + {"sent": "3d02h", "received": 266400}, + {"sent": "5h10m", "received": 18600}, + {"sent": "10m", "received": 600}, ] @@ -58,6 +63,7 @@ def test_uptime_string_to_seconds(data): assert uptime_string_to_seconds(data["sent"]) == data["received"] -def test_fail_uptime_string_to_seconds(): +@pytest.mark.parametrize("data", ["thisismytime", "", "123", "1mo", "1h 5m", "up for 2w1d"]) +def test_fail_uptime_string_to_seconds(data): with pytest.raises(Exception, match=r"Unable to parse uptime string."): - uptime_string_to_seconds("thisismytime") + uptime_string_to_seconds(data)