Skip to content

Commit

Permalink
[FIX] Add serialization methods and test fixtures for Cheques module
Browse files Browse the repository at this point in the history
- Add from_dict and to_dict methods to all Cheques models
- Add data validation in post_init for data integrity
- Add sample check data fixture for response tests
- Fix API field mapping from camelCase to snake_case
See also: #31
  • Loading branch information
PPeitsch committed Dec 20, 2024
1 parent af3a50c commit db7a4e9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/bcra_connector/cheques/cheques.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@ class Entidad:
codigo_entidad: int
denominacion: str

def __post_init__(self) -> None:
"""Validate instance after initialization."""
if self.codigo_entidad < 0:
raise ValueError("Entity code must be non-negative")
if not self.denominacion.strip():
raise ValueError("Entity name cannot be empty")

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Entidad":
"""Create an Entidad instance from a dictionary."""
return cls(
codigo_entidad=data["codigoEntidad"], denominacion=data["denominacion"]
)

def to_dict(self) -> Dict[str, Any]:
"""Convert the instance to a dictionary."""
return {"codigoEntidad": self.codigo_entidad, "denominacion": self.denominacion}


@dataclass
class ChequeDetalle:
Expand All @@ -51,6 +62,14 @@ def from_dict(cls, data: Dict[str, Any]) -> "ChequeDetalle":
causal=data["causal"],
)

def to_dict(self) -> Dict[str, Any]:
"""Convert the instance to a dictionary."""
return {
"sucursal": self.sucursal,
"numeroCuenta": self.numero_cuenta,
"causal": self.causal,
}


@dataclass
class Cheque:
Expand All @@ -70,6 +89,13 @@ class Cheque:
denominacion_entidad: str
detalles: List[ChequeDetalle]

def __post_init__(self) -> None:
"""Validate instance after initialization."""
if self.numero_cheque < 0:
raise ValueError("Check number must be non-negative")
if not self.denominacion_entidad.strip():
raise ValueError("Entity name cannot be empty")

@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Cheque":
"""Create a Cheque instance from a dictionary."""
Expand All @@ -82,20 +108,13 @@ def from_dict(cls, data: Dict[str, Any]) -> "Cheque":
)

def to_dict(self) -> Dict[str, Any]:
"""Convert the Cheque instance to a dictionary."""
"""Convert the instance to a dictionary."""
return {
"numeroCheque": self.numero_cheque,
"denunciado": self.denunciado,
"fechaProcesamiento": self.fecha_procesamiento.isoformat(),
"denominacionEntidad": self.denominacion_entidad,
"detalles": [
{
"sucursal": d.sucursal,
"numeroCuenta": d.numero_cuenta,
"causal": d.causal,
}
for d in self.detalles
],
"detalles": [d.to_dict() for d in self.detalles],
}


Expand Down
17 changes: 17 additions & 0 deletions tests/unit/models/test_cheques.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ def test_entidad_response(self) -> None:
assert len(response.results) == 1
assert isinstance(response.results[0], Entidad)

@pytest.fixture
def sample_cheque_data(self) -> Dict[str, Any]:
"""Fixture providing sample check data."""
return {
"numeroCheque": 20377516,
"denunciado": True,
"fechaProcesamiento": "2024-03-05",
"denominacionEntidad": "BANCO DE LA NACION ARGENTINA",
"detalles": [
{
"sucursal": 524,
"numeroCuenta": 5240055962,
"causal": "Denunciado por tercero",
}
],
}

def test_cheque_response(self, sample_cheque_data: Dict[str, Any]) -> None:
"""Test ChequeResponse model."""
data: Dict[str, Any] = {"status": 200, "results": sample_cheque_data}
Expand Down

0 comments on commit db7a4e9

Please sign in to comment.