Skip to content

Commit 1db917f

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

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

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: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from datetime import date
2+
13
import pytest
24
from pytest_lazyfixture import lazy_fixture
35

46
from mati.types import ValidationInputType
5-
from mati.types.enums import VerificationDocumentStep
7+
from mati.types.enums import VerificationDocument, VerificationDocumentStep
68

79

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

0 commit comments

Comments
 (0)