Skip to content

Commit

Permalink
Implements BE BBAN checksum check
Browse files Browse the repository at this point in the history
  • Loading branch information
mhemeryck committed May 21, 2021
1 parent ff099de commit 301f747
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions schwifty/checksum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ def register(algorithm_cls: Type[Algorithm], prefix: Optional[str] = None) -> Ty

from schwifty.checksum import germany # noqa
from schwifty.checksum import italy # noqa
from schwifty.checksum import belgium # noqa
18 changes: 18 additions & 0 deletions schwifty/checksum/belgium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import functools

from schwifty import checksum


register = functools.partial(checksum.register, prefix="BE")


@register
class DefaultAlgorithm(checksum.Algorithm):
name = "default"
accepts = checksum.InputType.BBAN

def compute(self, bban: str) -> str:
return str(int(bban[:-2]) % 97)

def validate(self, bban: str) -> bool:
return bban[-2:] == self.compute(bban)
3 changes: 3 additions & 0 deletions schwifty/iban.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def add_bban_checksum(country_code: str, bban: str) -> str:
if country_code == "IT":
checksum = algorithms["IT:default"].compute(bban[1:])
bban = checksum + bban[1:]
elif country_code == "BE":
checksum = algorithms["BE:default"].compute(bban)
bban = bban[:-2] + checksum
return bban


Expand Down
8 changes: 8 additions & 0 deletions tests/test_checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ def test_german_checksum_success(account_code, algorithm_name):
)
def test_german_checksum_failure(account_code, algorithm_name):
assert algorithms[algorithm_name].validate(account_code) is False


def test_belgium_checksum():
assert algorithms["BE:default"].validate("539007547034") is True


def test_belgium_checksum_failure():
assert algorithms["BE:default"].validate("050000123456") is False
7 changes: 6 additions & 1 deletion tests/test_iban.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from pycountry import countries

from schwifty import IBAN
from schwifty.exceptions import SchwiftyException

Expand Down Expand Up @@ -152,6 +151,7 @@ def test_iban_properties():
(("GB", "NWBK", "31926819", "601613"), "GB29NWBK60161331926819"),
(("GB", "NWBK", "31926819"), "GB66NWBK00000031926819"),
(("GB", "NWBK601613", "31926819"), "GB29NWBK60161331926819"),
(("BE", "050", "123456"), "BE67050000123487"),
],
)
def test_generate_iban(components, compact):
Expand Down Expand Up @@ -206,3 +206,8 @@ def test_bic_from_iban(iban, bic):

def test_unknown_bic_from_iban():
assert IBAN("SI72000001234567892").bic is None


def test_be_generated_iban_valid():
iban = IBAN.generate("BE", bank_code="050", account_code="123456")
assert iban.validate(validate_bban=True)

0 comments on commit 301f747

Please sign in to comment.