Skip to content

Commit 6c14038

Browse files
committed
add expiration date field
1 parent 554d1c4 commit 6c14038

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

mati/types/enums.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import dataclass, field, fields
2+
from datetime import date
23
from enum import Enum
34
from typing import Any, BinaryIO, Dict, List, Optional, Union
45

@@ -184,6 +185,40 @@ def ocr_number(self) -> str:
184185
return self.fields['ocr_number']['value']
185186
return ''
186187

188+
@property
189+
def expiration_date(self) -> Optional[date]:
190+
"""
191+
This property fills the expiration date direct from the ocr
192+
fields `expiration_date`.
193+
194+
Returns a date object if the field is present and valid.
195+
Returns None if the field is missing, invalid, or fields is None.
196+
"""
197+
if self.fields is None:
198+
return None
199+
try:
200+
date_str = self.fields['expiration_date']['value']
201+
return date.fromisoformat(date_str)
202+
except (KeyError, TypeError, ValueError):
203+
return None
204+
205+
@property
206+
def emission_date(self) -> Optional[date]:
207+
"""
208+
This property fills the emission date direct from the ocr
209+
fields `emission_date`.
210+
211+
Returns a date object if the field is present and valid.
212+
Returns None if the field is missing, invalid, or fields is None.
213+
"""
214+
if self.fields is None:
215+
return None
216+
try:
217+
date_str = self.fields['emission_date']['value']
218+
return date.fromisoformat(date_str)
219+
except (KeyError, TypeError, ValueError):
220+
return None
221+
187222
def add_expired_step(self) -> None:
188223
'''
189224
Appends an expired error step to the document if missing.

mati/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.0.8'
1+
__version__ = '2.0.9.dev3'

tests/test_types.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from datetime import date
2+
from typing import Any, Dict, Optional
3+
14
import pytest
25
from pytest_lazyfixture import lazy_fixture
36

47
from mati.types import ValidationInputType
5-
from mati.types.enums import VerificationDocumentStep
8+
from mati.types.enums import VerificationDocument, VerificationDocumentStep
69

710

811
def test_type_to_str():
@@ -44,3 +47,67 @@ def test_excess_fields():
4447
data = {'some': 'data', 'aditional': 'data', 'id': 'foo', 'status': 10}
4548
VerificationDocumentStep._filter_excess_fields(data)
4649
assert 'some' not in data
50+
51+
52+
@pytest.mark.parametrize(
53+
('fields', 'expected'),
54+
[
55+
(
56+
{
57+
'expiration_date': {
58+
'value': '2030-12-31',
59+
'label': 'Date of Expiration',
60+
'format': 'date',
61+
}
62+
},
63+
date(2030, 12, 31),
64+
),
65+
(None, None),
66+
({'address': {'value': 'Test Address', 'label': 'Address'}}, None),
67+
({'expiration_date': {'label': 'Date of Expiration'}}, None),
68+
],
69+
)
70+
def test_expiration_date_property(
71+
fields: Optional[Dict[str, Any]], expected: Optional[date]
72+
) -> None:
73+
doc = VerificationDocument(
74+
country='MX',
75+
region='',
76+
photos=[],
77+
steps=[],
78+
type='national-id',
79+
fields=fields,
80+
)
81+
assert doc.expiration_date == expected
82+
83+
84+
@pytest.mark.parametrize(
85+
('fields', 'expected'),
86+
[
87+
(
88+
{
89+
'emission_date': {
90+
'value': '2023-01-15',
91+
'label': 'Emission Date',
92+
'format': 'date',
93+
}
94+
},
95+
date(2023, 1, 15),
96+
),
97+
(None, None),
98+
({'address': {'value': 'Test Address', 'label': 'Address'}}, None),
99+
({'emission_date': {'label': 'Emission Date'}}, None),
100+
],
101+
)
102+
def test_emission_date_property(
103+
fields: Optional[Dict[str, Any]], expected: Optional[date]
104+
) -> None:
105+
doc = VerificationDocument(
106+
country='MX',
107+
region='',
108+
photos=[],
109+
steps=[],
110+
type='proof-of-residency',
111+
fields=fields,
112+
)
113+
assert doc.emission_date == expected

0 commit comments

Comments
 (0)