Coverage for value_object_pattern/usables/dates/negative_duration_value_object.py: 61.54%
11 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-11-21 14:31 +0000
1"""
2Provide a strictly negative duration value object.
3"""
5from datetime import timedelta 1abcdefghij
6from typing import NoReturn 1abcdefghij
8from value_object_pattern.decorators import validation 1abcdefghij
10from .duration_value_object import DurationValueObject 1abcdefghij
13class NegativeDurationValueObject(DurationValueObject): 1abcdefghij
14 """
15 Validate and store an exact, strictly negative `datetime.timedelta` value.
17 Example:
18 ```python
19 from datetime import timedelta
21 from value_object_pattern.usables.dates import NegativeDurationValueObject
23 offset = NegativeDurationValueObject(value=timedelta(seconds=-30))
24 print(repr(offset))
25 # >>> NegativeDurationValueObject(value=datetime.timedelta(days=-1, seconds=86370))
26 ```
27 """
29 @validation(order=1) 1abcdefghij
30 def _ensure_value_is_negative(self, value: timedelta) -> None: 1abcdefghij
31 """
32 Ensure the duration is less than zero.
34 Args:
35 value (timedelta): The provided duration.
37 Raises:
38 ValueError: If the duration is zero or positive.
39 """
40 if value >= timedelta():
41 self._raise_value_is_not_negative(value=value)
43 def _raise_value_is_not_negative(self, value: timedelta) -> NoReturn: 1abcdefghij
44 """
45 Raise an error for a duration that is not negative.
47 Args:
48 value (timedelta): The invalid duration.
50 Raises:
51 ValueError: Always raised with the invalid value.
52 """
53 raise ValueError(f'DurationValueObject value <<<{value}>>> must be a negative duration.')