diff --git a/mati/resources/verifications.py b/mati/resources/verifications.py index 03512cc..730ef19 100644 --- a/mati/resources/verifications.py +++ b/mati/resources/verifications.py @@ -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 diff --git a/mati/types/enums.py b/mati/types/enums.py index 773fc55..58d0c43 100644 --- a/mati/types/enums.py +++ b/mati/types/enums.py @@ -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): @@ -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: diff --git a/mati/version.py b/mati/version.py index dc2f47f..4c354e0 100644 --- a/mati/version.py +++ b/mati/version.py @@ -1 +1 @@ -__version__ = '2.0.4' # pragma: no cover +__version__ = '2.0.5' diff --git a/tests/resources/cassettes/test_retrieve_full_verification.yaml b/tests/resources/cassettes/test_retrieve_full_verification.yaml index b8e9e5c..c272a75 100644 --- a/tests/resources/cassettes/test_retrieve_full_verification.yaml +++ b/tests/resources/cassettes/test_retrieve_full_verification.yaml @@ -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"]}], diff --git a/tests/test_types.py b/tests/test_types.py index 055ac9d..f5b2639 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -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(): @@ -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