Repository: joke2k/faker

Issue:
Adding ABA Routing Number to Bank Provider
* Faker version:8.1.2


Is there any interest in adding ABA routing numbers to the Bank provider? https://en.wikipedia.org/wiki/ABA_routing_transit_number

This would be helpful for my use case and have code to generate but wanted to check before putting more effort in.

Discussion:
mstellon: Fixed linting issues. Thanks!
fcurella: Thank you!

Patch:
diff --git a/faker/providers/bank/__init__.py b/faker/providers/bank/__init__.py
index ee46e800..9c7407b3 100644
--- a/faker/providers/bank/__init__.py
+++ b/faker/providers/bank/__init__.py
@@ -1,6 +1,7 @@
 import re
 import string
 
+from math import ceil
 from string import ascii_uppercase
 
 from .. import BaseProvider
@@ -29,6 +30,19 @@ class Provider(BaseProvider):
     bban_format = '????#############'
     country_code = 'GB'
 
+    def aba(self):
+        """Generate an ABA routing transit number."""
+        fed_num = self.random_int(min=1, max=12)
+        rand = self.numerify('######')
+        aba = f"{fed_num:02}{rand}"
+
+        # calculate check digit
+        d = [int(n) for n in aba]
+        chk_digit = 3*(d[0] + d[3] + d[6]) + 7*(d[1] + d[4] + d[7]) + d[2] + d[5]
+        chk_digit = ceil(chk_digit/10)*10 - chk_digit
+
+        return f"{aba}{chk_digit}"
+
     def bank_country(self):
         """Generate the bank provider's ISO 3166-1 alpha-2 country code."""
         return self.country_code


Test patch:
diff --git a/tests/providers/test_bank.py b/tests/providers/test_bank.py
index 9a3276ec..cd6760ae 100644
--- a/tests/providers/test_bank.py
+++ b/tests/providers/test_bank.py
@@ -23,8 +23,21 @@ def is_valid_iban(iban):
     return check % 97 == 1
 
 
+def is_valid_aba(aba):
+    d = [int(n) for n in aba]
+    chkdgt = (3*(d[0]+d[3]+d[6]) + 7*(d[1]+d[4]+d[7]) + (d[2]+d[5]+d[8]))
+    if chkdgt % 10 == 0:
+        return True
+    return False
+
+
 class TestNoNo:
     """Test no_NO bank provider"""
+    def test_aba(self, faker, num_samples):
+        for _ in range(num_samples):
+            aba = faker.aba()
+            assert len(aba) == 9
+            assert is_valid_aba(aba)
 
     def test_bban(self, faker, num_samples):
         for _ in range(num_samples):