Skip to content

Commit e8e23b5

Browse files
committed
coverage
1 parent e691e9d commit e8e23b5

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

onvif/types.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def pythonvalue(self, value: str) -> datetime:
5353
# lazy hack ;-)
5454
if len(value) == 10:
5555
value += "T00:00:00"
56-
elif (len(value) == 19 or len(value) == 26) and value[10] == " ":
56+
elif (len(value) in (19, 20, 26)) and value[10] == " ":
5757
value = "T".join(value.split(" "))
5858

5959
if dt := _try_parse_datetime(value):
@@ -63,7 +63,11 @@ def pythonvalue(self, value: str) -> datetime:
6363
# For example, 2024-08-17T00:61:16Z so we need
6464
# to fix the overflow
6565
date, _, time = value.partition("T")
66-
fixed_time, offset = _try_fix_time_overflow(time)
66+
try:
67+
fixed_time, offset = _try_fix_time_overflow(time)
68+
except ValueError:
69+
return ciso8601.parse_datetime(value)
70+
6771
if dt := _try_parse_datetime(f"{date}T{fixed_time}"):
6872
return dt + timedelta(**offset)
6973

@@ -83,7 +87,10 @@ def pythonvalue(self, value: str) -> time:
8387
# Some cameras overflow the hours/minutes/seconds
8488
# For example, 00:61:16Z so we need
8589
# to fix the overflow
86-
fixed_time, offset = _try_fix_time_overflow(value)
90+
try:
91+
fixed_time, offset = _try_fix_time_overflow(value)
92+
except ValueError:
93+
return isodate.parse_time(value)
8794
if fixed_dt := _try_parse_datetime(f"2024-01-15T{fixed_time}Z"):
8895
return (fixed_dt + timedelta(**offset)).time()
8996
return isodate.parse_time(value)

tests/test_types.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
@pytest.mark.asyncio
18-
async def test_parse_invalid_time(caplog: pytest.LogCaptureFixture) -> None:
18+
async def test_parse_invalid_dt(caplog: pytest.LogCaptureFixture) -> None:
1919
device = ONVIFCamera("127.0.0.1", 80, "user", "pass", wsdl_dir=_WSDL_PATH)
2020
device.xaddrs = {
2121
"http://www.onvif.org/ver10/events/wsdl": "http://192.168.210.102:6688/onvif/event_service"
@@ -40,6 +40,31 @@ async def test_parse_invalid_time(caplog: pytest.LogCaptureFixture) -> None:
4040
assert "ValueError" not in caplog.text
4141

4242

43+
def test_parse_invalid_datetime() -> None:
44+
with pytest.raises(ValueError, match="Invalid character while parsing year"):
45+
FastDateTime().pythonvalue("aaaa-aa-aaTaa:aa:aaZ")
46+
47+
48+
def test_parse_invalid_time() -> None:
49+
with pytest.raises(ValueError, match="Unrecognised ISO 8601 time format"):
50+
ForgivingTime().pythonvalue("aa:aa:aa")
51+
52+
53+
def test_fix_datetime_missing_time() -> None:
54+
assert FastDateTime().pythonvalue("2024-08-17") == datetime.datetime(
55+
2024, 8, 17, 0, 0, 0
56+
)
57+
58+
59+
def test_fix_datetime_missing_t() -> None:
60+
assert FastDateTime().pythonvalue("2024-08-17 00:61:16Z") == datetime.datetime(
61+
2024, 8, 17, 1, 1, 16, tzinfo=datetime.timezone.utc
62+
)
63+
assert FastDateTime().pythonvalue("2024-08-17 00:61:16") == datetime.datetime(
64+
2024, 8, 17, 1, 1, 16
65+
)
66+
67+
4368
def test_fix_datetime_overflow() -> None:
4469
assert FastDateTime().pythonvalue("2024-08-17T00:61:16Z") == datetime.datetime(
4570
2024, 8, 17, 1, 1, 16, tzinfo=datetime.timezone.utc

0 commit comments

Comments
 (0)