Skip to content

Commit

Permalink
refactor: update BankConfigRequest model and validation logic for ban…
Browse files Browse the repository at this point in the history
…k codes

- Replaced bank_code_abm with a derived property from bank_code_banxico.
- Enhanced validation for bank_code_banxico to ensure it is exactly 5 digits.
- Updated tests to reflect changes in bank configuration logic and validation rules.
  • Loading branch information
gabino committed Dec 23, 2024
1 parent e4c5146 commit 4eb15b9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
38 changes: 20 additions & 18 deletions clabe/validations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random
import re
from typing import List, Union

from pydantic import BaseModel, Field, validator
from pydantic.errors import NotDigitError

from .banks import BANK_NAMES, BANKS

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


class BankConfigRequest(BaseModel):
bank_code_abm: str = Field(..., description="The ABM code for the bank")
bank_name: str = Field(
min_length=1,
strip_whitespace=True,
description="The name of the bank - cannot be empty",
)

bank_code_banxico: str = Field(
..., description="The Banxico code for the bank"
min_length=5, max_length=5, description="The Banxico code for the bank"
)
bank_name: str = Field(..., description="The name of the bank")

@validator('bank_code_abm', 'bank_code_banxico')
def validate_numeric_codes(cls, v: str) -> str:
if not v.isdigit():
raise NotDigitError
return v
@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:]

@validator('bank_name')
def validate_bank_name(cls, v: str) -> str:
if not v.strip():
raise ValueError("bank_name cannot be empty")
return v.strip()

def configure_additional_bank(bank_code_banxico: str, bank_name: str) -> None:

def configure_additional_bank(
bank_code_abm: str, bank_code_banxico: str, bank_name: str
) -> None:
request = BankConfigRequest(
bank_code_abm=bank_code_abm,
bank_code_banxico=bank_code_banxico,
bank_name=bank_name,
)
Expand Down
22 changes: 10 additions & 12 deletions tests/test_clabe.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,24 @@ def test_generate_new_clabes():
'abm_code, banxico_code, name',
[
('713', '90713', 'Cuenca DMZ'),
('777', '713', 'Cuenca Gem DMZ'),
('666', '723', 'Cuenca Gem Beta'),
('714', '90714', 'Cuenca Gem DMZ'),
('715', '90715', 'Cuenca Gem Beta'),
],
)
def test_configure_additional_bank_success(abm_code, banxico_code, name):
configure_additional_bank(abm_code, banxico_code, name)
configure_additional_bank(banxico_code, name)
assert get_bank_name(abm_code) == name


@pytest.mark.parametrize(
'abm_code, banxico_code, name',
'banxico_code, name',
[
('A', 'B', 'C'), # Invalid format for both codes
('666', 'B', 'Test Bank'), # Valid ABM code, invalid Banxico code
('777', '713', ''), # Valid codes, empty name
('abc', 'def', 'Test Bank'), # Non-numeric codes
('1234', 'Test Bank'), # invalid Banxico code 4 digits
('123456', 'Test Bank'), # invalid Banxico code 6 digits
('12345', ''), # Valid code, empty name
('123AT', 'Test Bank'), # Non-numeric codes
],
)
def test_configure_additional_bank_invalid_inputs(
abm_code, banxico_code, name
):
def test_configure_additional_bank_invalid_inputs(banxico_code, name):
with pytest.raises(ValueError):
configure_additional_bank(abm_code, banxico_code, name)
configure_additional_bank(banxico_code, name)

0 comments on commit 4eb15b9

Please sign in to comment.