Skip to content

Commit

Permalink
exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
felipao-mx committed Apr 19, 2022
1 parent 299fd1c commit c5720aa
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
17 changes: 16 additions & 1 deletion cep/transferencia.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

import clabe
from lxml import etree
from requests import HTTPError

from .client import Client
from .cuenta import Cuenta
from .exc import CepError, MaxRequestError

MAX_REQUEST_ERROR_MESSAGE = (
b'Lo sentimos, pero ha excedido el número máximo '
b'de consultas en este portal'
)


@dataclass
Expand Down Expand Up @@ -36,7 +43,15 @@ def validar(
)
if not client:
return None
xml = cls._descargar(client, 'XML')

try:
xml = cls._descargar(client, 'XML')
except HTTPError as exc:
raise CepError from exc

if MAX_REQUEST_ERROR_MESSAGE in xml:
raise MaxRequestError

resp = etree.fromstring(xml)

ordenante = Cuenta.from_etree(resp.find('Ordenante'))
Expand Down
2 changes: 1 addition & 1 deletion cep/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.7'
__version__ = '0.2.0.dev0'
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pytest==6.2.5
pytest-vcr==1.0.2
pytest-cov==3.0.0
black==21.12b0
black==22.3.0
flake8==4.0.1
isort[pipfile]==5.10.1
mypy==0.790
35 changes: 35 additions & 0 deletions tests/test_transferencia.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import os

import pytest
from requests import HTTPError

from cep import Transferencia
from cep.exc import CepError, MaxRequestError


@pytest.mark.vcr
Expand Down Expand Up @@ -54,3 +56,36 @@ def test_descagar_transferencia_con_fecha_distinta(transferencia):
)
assert type(tr.to_dict()) is dict
tr.descargar()


@pytest.mark.vcr
def test_lanza_cep_error_para_errores_500():
try:
for i in range(10):
Transferencia.validar(
fecha=dt.date(2022, 4, 19),
clave_rastreo='CUENCA927820173168',
emisor='90646', # STP
receptor='40012', # BBVA
cuenta='012180000',
monto=0.01,
)
except CepError as exc:
assert type(exc.__cause__) is HTTPError
assert str(exc.__cause__) == (
'500 Server Error: Internal Server Error for url: '
'https://www.banxico.org.mx/cep/descarga.do?formato=XML'
)


@pytest.mark.vcr
def test_maximo_numero_de_requests():
with pytest.raises(MaxRequestError):
Transferencia.validar(
fecha=dt.date(2022, 4, 19),
clave_rastreo='CUENCA927820173168',
emisor='90646', # STP
receptor='40012', # BBVA
cuenta='012180000',
monto=0.01,
)

0 comments on commit c5720aa

Please sign in to comment.