Skip to content

Commit 460fffa

Browse files
committed
feat: Add maximum due date validation
1 parent 99c0ed2 commit 460fffa

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

netsgiro/objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
TransmissionStart,
3131
)
3232
from netsgiro.records import parse as records_parse
33-
from netsgiro.validators import str_of_length, validate_minimum_date
33+
from netsgiro.validators import str_of_length, validate_due_date
3434

3535
if TYPE_CHECKING:
3636
from netsgiro.enums import AvtaleGiroRegistrationType
@@ -382,7 +382,7 @@ def add_payment_request(
382382

383383
if validate_due_date:
384384
# Make sure we're not passing invalid due dates
385-
validate_minimum_date(due_date)
385+
validate_due_date(due_date)
386386

387387
if bank_notification:
388388
transaction_type = TransactionType.AVTALEGIRO_WITH_BANK_NOTIFICATION

netsgiro/validators.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Custom validators for :mod:`attrs`."""
2-
from datetime import datetime
2+
from datetime import datetime, timedelta
33
from typing import TYPE_CHECKING, Any, Callable
44

55
from attr import Attribute
@@ -40,11 +40,20 @@ def validator(instance: object, attribute: Attribute, value: Any) -> None:
4040
return validator
4141

4242

43-
def validate_minimum_date(value: 'date') -> None:
43+
def validate_due_date(value: 'date') -> None:
4444
"""Make sure payment request dates are gt the minimum allowed date."""
45-
if value < get_minimum_due_date(now=datetime.now(tz=OSLO_TZ)):
45+
now = datetime.now(tz=OSLO_TZ)
46+
47+
if value < get_minimum_due_date(now=now):
4648
raise ValueError(
4749
'The minimum due date of a transaction is today + 4 calendar days.'
4850
' OCR files with due dates earlier than this will be rejected when'
4951
' submitted.'
5052
)
53+
54+
if value > (now + timedelta(days=365)).date():
55+
raise ValueError(
56+
'The maximum due date of a transaction is 12 months in the future.'
57+
' OCR files with due dates later than this will be rejected when'
58+
' submitted.'
59+
)

tests/test_utils.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from datetime import datetime, timedelta
22

33
import holidays
4+
import pytest
45

56
from netsgiro.utils import OSLO_TZ, get_minimum_due_date
7+
from netsgiro.validators import validate_due_date
68

79
monday = datetime(2022, 3, 28, 13, 59, tzinfo=OSLO_TZ)
810

@@ -24,8 +26,8 @@ def test_minimum_due_date_after_cutoff():
2426
generated after 14:00 Norwegian time should be adjusted by 5 days.
2527
"""
2628
assert (
27-
get_minimum_due_date(monday_after_cutoff)
28-
== (monday_after_cutoff + timedelta(days=5)).date()
29+
get_minimum_due_date(monday_after_cutoff)
30+
== (monday_after_cutoff + timedelta(days=5)).date()
2931
)
3032

3133

@@ -49,8 +51,8 @@ def test_minimum_due_date_with_holidays_after_cutoff():
4951
holidays is upped to 3, so we expect 2 more days of offsetting.
5052
"""
5153
assert (
52-
get_minimum_due_date(day_before_easter_after_cutoff)
53-
== (day_before_easter_after_cutoff + timedelta(days=8)).date()
54+
get_minimum_due_date(day_before_easter_after_cutoff)
55+
== (day_before_easter_after_cutoff + timedelta(days=8)).date()
5456
)
5557

5658

@@ -70,3 +72,11 @@ def test_minimum_due_date_without_holiday_dependency():
7072
sys.modules['holidays'] = None
7173
assert get_minimum_due_date(monday) == (monday + timedelta(days=4)).date()
7274
sys.modules['holidays'] = holidays
75+
76+
77+
def test_validate_due_date():
78+
with pytest.raises(ValueError, match='The minimum due date of a transaction'):
79+
validate_due_date(datetime.now().date())
80+
81+
with pytest.raises(ValueError, match='The maximum due date of a transaction'):
82+
validate_due_date((datetime.now() + timedelta(days=366)).date())

0 commit comments

Comments
 (0)