Skip to content

Commit

Permalink
fixing excess fields on DocumentStep (#119)
Browse files Browse the repository at this point in the history
* fixing excess fields on DocumentStep

* version

* remove pragma

* fixed

* tested

* eofl
  • Loading branch information
alexviquez authored Jan 25, 2024
1 parent 001eec7 commit ffca913
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion mati/resources/verifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def __post_init__(self):
]
for doc in self.documents:
doc['steps'] = [
VerificationDocumentStep(**step) for step in doc['steps']
VerificationDocumentStep._from_dict(step)
for step in doc['steps']
]
docs.append(VerificationDocument(**doc))
self.documents = docs
Expand Down
17 changes: 15 additions & 2 deletions mati/types/enums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass, field
from dataclasses import dataclass, field, fields
from enum import Enum
from typing import BinaryIO, Dict, List, Optional, Union
from typing import Any, BinaryIO, Dict, List, Optional, Union


class SerializableEnum(str, Enum):
Expand Down Expand Up @@ -34,6 +34,19 @@ class VerificationDocumentStep:
error: Optional[Dict] = None
data: Optional[Dict] = field(default_factory=dict)

@classmethod
def _filter_excess_fields(cls, obj_dict: Dict) -> None:
excess = set(obj_dict.keys()) - {f.name for f in fields(cls)}
for f in excess:
del obj_dict[f]

@classmethod
def _from_dict(
cls, obj_dict: Dict[str, Any]
) -> 'VerificationDocumentStep':
cls._filter_excess_fields(obj_dict)
return cls(**obj_dict)


@dataclass
class Errors:
Expand Down
2 changes: 1 addition & 1 deletion mati/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.4' # pragma: no cover
__version__ = '2.0.5'
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ interactions:
"steps": [{"status": 200, "id": "document-reading", "data": {"fullName": {"required":
true, "label": "Name", "value": "FIRST NAME"}, "address": {"label": "Address",
"value": "Varsovia 36, 06600 CDMX"}, "emissionDate": {"format": "date", "label":
"Emission Date", "value": "1880-01-01"}}, "error": null}, {"status": 200,
"Emission Date", "value": "1880-01-01"}}, "error": null, "reused": false, "cacheHit": false}, {"status": 200,
"id": "watchlists", "error": null}], "fields": {"address": {"value": "Varsovia
36, 06600 CDMX"}, "emissionDate": {"value": "1880-01-01"}, "fullName": {"value":
"FIRST LASTNAME"}}, "photos": ["https://media.getmati.com/file?location=xyc"]}],
Expand Down
13 changes: 13 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pytest_lazyfixture import lazy_fixture

from mati.types import ValidationInputType
from mati.types.enums import VerificationDocumentStep


def test_type_to_str():
Expand Down Expand Up @@ -31,3 +32,15 @@ def test_type_to_str():
)
def test_document_type(verification_document, expected_type):
assert verification_document.document_type == expected_type


def test_from_dict():
data = {'some': 'data', 'aditional': 'data', 'id': 'foo', 'status': 10}
step = VerificationDocumentStep._from_dict(data)
assert step


def test_excess_fields():
data = {'some': 'data', 'aditional': 'data', 'id': 'foo', 'status': 10}
VerificationDocumentStep._filter_excess_fields(data)
assert 'some' not in data

0 comments on commit ffca913

Please sign in to comment.