Skip to content

Commit e4c5146

Browse files
author
gabino
committed
refactor: streamline bank configuration by replacing error classes with Pydantic model validation
1 parent 51856ed commit e4c5146

3 files changed

Lines changed: 53 additions & 78 deletions

File tree

clabe/errors.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,3 @@ class BankCodeValidationError(PydanticValueError):
99
class ClabeControlDigitValidationError(PydanticValueError):
1010
code = 'clabe.control_digit'
1111
msg_template = 'clabe dígito de control no es válido'
12-
13-
14-
class BankCodeABMAlreadyExistsError(PydanticValueError):
15-
code = 'clabe.bank_code_abm_already_exists'
16-
msg_template = 'código de banco ABM ya existe'
17-
18-
19-
class BankCodeBanxicoAlreadyExistsError(PydanticValueError):
20-
code = 'clabe.bank_code_banxico_already_exists'
21-
msg_template = 'código de banco banxico ya existe'

clabe/validations.py

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import random
22
from typing import List, Union
33

4+
from pydantic import BaseModel, Field, validator
45
from pydantic.errors import NotDigitError
56

67
from .banks import BANK_NAMES, BANKS
7-
from .errors import (
8-
BankCodeABMAlreadyExistsError,
9-
BankCodeBanxicoAlreadyExistsError,
10-
)
118

129
CLABE_LENGTH = 18
1310
CLABE_WEIGHTS = [3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7]
@@ -69,42 +66,33 @@ def generate_new_clabes(number_of_clabes: int, prefix: str) -> List[str]:
6966
return clabes
7067

7168

72-
def configure_additional_bank(
73-
bank_code_abm: str, bank_code_banxico: str, bank_name: str
74-
) -> None:
75-
"""
76-
Configures an additional bank.
77-
78-
Args:
79-
bank_code_abm (str): The ABM code for the bank.
80-
bank_code_banxico (str): The Banxico code for the bank.
81-
bank_name (str): The name of the bank.
82-
83-
Raises:
84-
ValueError: If the bank_code_abm or bank_code_banxico
85-
already exists in the provided dictionaries.
86-
"""
87-
88-
if not all(
89-
isinstance(x, str)
90-
for x in [bank_code_abm, bank_code_banxico, bank_name]
91-
):
92-
raise TypeError("All parameters must be strings")
93-
94-
if not bank_code_abm.isdigit():
95-
raise NotDigitError
96-
97-
if not bank_code_banxico.isdigit():
98-
raise NotDigitError
69+
class BankConfigRequest(BaseModel):
70+
bank_code_abm: str = Field(..., description="The ABM code for the bank")
71+
bank_code_banxico: str = Field(
72+
..., description="The Banxico code for the bank"
73+
)
74+
bank_name: str = Field(..., description="The name of the bank")
9975

100-
if not bank_name.strip():
101-
raise ValueError("bank_name cannot be empty")
76+
@validator('bank_code_abm', 'bank_code_banxico')
77+
def validate_numeric_codes(cls, v: str) -> str:
78+
if not v.isdigit():
79+
raise NotDigitError
80+
return v
10281

103-
if bank_code_abm in BANKS:
104-
raise BankCodeABMAlreadyExistsError
82+
@validator('bank_name')
83+
def validate_bank_name(cls, v: str) -> str:
84+
if not v.strip():
85+
raise ValueError("bank_name cannot be empty")
86+
return v.strip()
10587

106-
if bank_code_banxico in BANK_NAMES:
107-
raise BankCodeBanxicoAlreadyExistsError
10888

109-
BANKS[bank_code_abm] = bank_code_banxico
110-
BANK_NAMES[bank_code_banxico] = bank_name.strip()
89+
def configure_additional_bank(
90+
bank_code_abm: str, bank_code_banxico: str, bank_name: str
91+
) -> None:
92+
request = BankConfigRequest(
93+
bank_code_abm=bank_code_abm,
94+
bank_code_banxico=bank_code_banxico,
95+
bank_name=bank_name,
96+
)
97+
BANKS[request.bank_code_abm] = request.bank_code_banxico
98+
BANK_NAMES[request.bank_code_banxico] = request.bank_name

tests/test_clabe.py

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
get_bank_name,
88
validate_clabe,
99
)
10-
from clabe.errors import (
11-
BankCodeABMAlreadyExistsError,
12-
BankCodeBanxicoAlreadyExistsError,
13-
)
1410

1511
VALID_CLABE = '002000000000000008'
1612
INVALID_CLABE_CONTROL_DIGIT = '002000000000000007'
@@ -43,29 +39,30 @@ def test_generate_new_clabes():
4339
assert validate_clabe(clabe)
4440

4541

46-
def test_configure_additional_bank_success():
47-
configure_additional_bank("777", "713", "New Bank")
48-
assert get_bank_name('777') == 'New Bank'
49-
50-
51-
def test_configure_additional_bank_existing_abm_code():
52-
with pytest.raises(BankCodeABMAlreadyExistsError):
53-
configure_additional_bank("002", "40002", "Banamex")
54-
55-
56-
def test_configure_additional_bank_existing_banxico_code():
57-
with pytest.raises(BankCodeBanxicoAlreadyExistsError):
58-
configure_additional_bank("666", "40137", "New Bank")
59-
60-
61-
def test_configure_additional_bank_invalid_inputs():
62-
with pytest.raises(TypeError):
63-
configure_additional_bank(3, 3, 3)
64-
with pytest.raises(ValueError):
65-
configure_additional_bank("A", "B", "C")
66-
with pytest.raises(ValueError):
67-
configure_additional_bank("666", "B", "C")
68-
with pytest.raises(ValueError):
69-
configure_additional_bank("777", "713", "")
42+
@pytest.mark.parametrize(
43+
'abm_code, banxico_code, name',
44+
[
45+
('713', '90713', 'Cuenca DMZ'),
46+
('777', '713', 'Cuenca Gem DMZ'),
47+
('666', '723', 'Cuenca Gem Beta'),
48+
],
49+
)
50+
def test_configure_additional_bank_success(abm_code, banxico_code, name):
51+
configure_additional_bank(abm_code, banxico_code, name)
52+
assert get_bank_name(abm_code) == name
53+
54+
55+
@pytest.mark.parametrize(
56+
'abm_code, banxico_code, name',
57+
[
58+
('A', 'B', 'C'), # Invalid format for both codes
59+
('666', 'B', 'Test Bank'), # Valid ABM code, invalid Banxico code
60+
('777', '713', ''), # Valid codes, empty name
61+
('abc', 'def', 'Test Bank'), # Non-numeric codes
62+
],
63+
)
64+
def test_configure_additional_bank_invalid_inputs(
65+
abm_code, banxico_code, name
66+
):
7067
with pytest.raises(ValueError):
71-
configure_additional_bank("abc", "def", "Test Bank")
68+
configure_additional_bank(abm_code, banxico_code, name)

0 commit comments

Comments
 (0)