Skip to content

Commit 51856ed

Browse files
author
gabino
committed
feat: enhance configure_additional_bank with input validation
1 parent 49d22b8 commit 51856ed

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

clabe/validations.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import random
22
from typing import List, Union
33

4+
from pydantic.errors import NotDigitError
5+
46
from .banks import BANK_NAMES, BANKS
57
from .errors import (
68
BankCodeABMAlreadyExistsError,
@@ -83,12 +85,26 @@ def configure_additional_bank(
8385
already exists in the provided dictionaries.
8486
"""
8587

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
99+
100+
if not bank_name.strip():
101+
raise ValueError("bank_name cannot be empty")
102+
86103
if bank_code_abm in BANKS:
87104
raise BankCodeABMAlreadyExistsError
88105

89106
if bank_code_banxico in BANK_NAMES:
90107
raise BankCodeBanxicoAlreadyExistsError
91108

92-
BANKS.update({bank_code_abm: bank_code_banxico})
93-
94-
BANK_NAMES.update({bank_code_banxico: bank_name})
109+
BANKS[bank_code_abm] = bank_code_banxico
110+
BANK_NAMES[bank_code_banxico] = bank_name.strip()

tests/test_clabe.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,16 @@ def test_configure_additional_bank_existing_abm_code():
5656
def test_configure_additional_bank_existing_banxico_code():
5757
with pytest.raises(BankCodeBanxicoAlreadyExistsError):
5858
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", "")
70+
with pytest.raises(ValueError):
71+
configure_additional_bank("abc", "def", "Test Bank")

0 commit comments

Comments
 (0)