Skip to content

Commit

Permalink
refactor: rename configure_additional_bank to add_bank and update val…
Browse files Browse the repository at this point in the history
…idation logic

- Refactored the add_bank function to improve clarity and validation.
- Updated tests to align with the new function name and validation rules.
  • Loading branch information
gabino committed Dec 24, 2024
1 parent acb4644 commit c94a35a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import clabe
clabe.generate_new_clabes(10, '002123456')
```

### Para agregar un nuevo banco
## Para agregar un nuevo banco

A partir de la versión 2.0.0, el paquete se actualizará a **Pydantic v2**, lo que significa que las versiones anteriores ya no recibirán soporte.

Expand Down
4 changes: 2 additions & 2 deletions clabe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
'generate_new_clabes',
'get_bank_name',
'validate_clabe',
'configure_additional_bank',
'add_bank',
'remove_bank',
]

from .banks import BANK_NAMES, BANKS
from .types import Clabe
from .validations import (
add_bank,
compute_control_digit,
configure_additional_bank,
generate_new_clabes,
get_bank_name,
remove_bank,
Expand Down
49 changes: 25 additions & 24 deletions clabe/validations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import random
import re
from typing import List, Union

from pydantic import BaseModel, Field, validator
from pydantic import BaseModel, Field

from .banks import BANK_NAMES, BANKS

Expand Down Expand Up @@ -67,31 +66,37 @@ def generate_new_clabes(number_of_clabes: int, prefix: str) -> List[str]:


class BankConfigRequest(BaseModel):
"""
Validates and processes bank configuration requests.
The class handles validation of bank names and codes, ensuring:
- Bank names are non-empty strings
- Banxico codes are exactly 5 digits
"""

bank_name: str = Field(
min_length=1,
strip_whitespace=True,
description="The name of the bank - cannot be empty",
description="Bank name must have at least 1 character.",
)

bank_code_banxico: str = Field(
min_length=5, max_length=5, description="The Banxico code for the bank"
regex=r"^\d{5}$", description="Banxico code must be a 5-digit string."
)

@validator("bank_code_banxico")
def validate_bank_code(cls, value):
if not re.fullmatch(r"\d{5}", value):
raise ValueError(
"bank_code_banxico must be a string of exactly 5 digits"
)
return value

@property
def bank_code_abm(self):
return self.bank_code_banxico[-3:]


def configure_additional_bank(bank_code_banxico: str, bank_name: str) -> None:
def add_bank(bank_code_banxico: str, bank_name: str) -> None:
"""
Add a bank configuration.
Args:
bank_code_banxico: 5-digit Banxico bank code
bank_name: Bank name
"""
request = BankConfigRequest(
bank_code_banxico=bank_code_banxico,
bank_name=bank_name,
Expand All @@ -101,15 +106,11 @@ def configure_additional_bank(bank_code_banxico: str, bank_name: str) -> None:


def remove_bank(bank_code_banxico: str) -> None:
bank_code_abm = next(
(
abm
for abm, banxico in BANKS.items()
if banxico == bank_code_banxico
),
None,
)
"""
Remove a bank configuration by its Banxico code.
if bank_code_abm:
BANKS.pop(bank_code_abm)
BANK_NAMES.pop(bank_code_banxico)
Args:
bank_code_banxico: 5-digit Banxico bank code
"""
BANKS.pop(bank_code_banxico[-3:], None)
BANK_NAMES.pop(bank_code_banxico, None)
35 changes: 27 additions & 8 deletions tests/test_clabe.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest

from clabe import (
BANK_NAMES,
BANKS,
add_bank,
compute_control_digit,
configure_additional_bank,
generate_new_clabes,
get_bank_name,
remove_bank,
Expand Down Expand Up @@ -48,8 +50,8 @@ def test_generate_new_clabes():
('715', '90715', 'Cuenca Gem Beta'),
],
)
def test_configure_additional_bank_success(abm_code, banxico_code, name):
configure_additional_bank(banxico_code, name)
def test_add_bank_success(abm_code, banxico_code, name):
add_bank(banxico_code, name)
assert get_bank_name(abm_code) == name


Expand All @@ -62,12 +64,29 @@ def test_configure_additional_bank_success(abm_code, banxico_code, name):
('123AT', 'Test Bank'), # Non-numeric codes
],
)
def test_configure_additional_bank_invalid_inputs(banxico_code, name):
def test_add_bank_invalid_inputs(banxico_code, name):
with pytest.raises(ValueError):
configure_additional_bank(banxico_code, name)
add_bank(banxico_code, name)


def test_remove_bank():
remove_bank('40138')
with pytest.raises(ValueError):
get_bank_name('138')
test_code = "90716"
test_name = "To Delete Bank"
add_bank(test_code, test_name)

assert test_code[-3:] in BANKS
assert test_code in BANK_NAMES

remove_bank(test_code)

assert test_code[-3:] not in BANKS
assert test_code not in BANK_NAMES


def test_remove_nonexistent_bank():
nonexistent_code = "99999"

remove_bank(nonexistent_code)

assert nonexistent_code[-3:] not in BANKS
assert nonexistent_code not in BANK_NAMES

0 comments on commit c94a35a

Please sign in to comment.