Repository: joke2k/faker
Pull request #546 (2017-07-01 08:55:08)

Adding an implementation of `regon` for pl_PL company provider.
REGON is the National Business Registry in Poland.


Patch:
diff --git a/faker/providers/company/pl_PL/__init__.py b/faker/providers/company/pl_PL/__init__.py
new file mode 100644
index 00000000..af99b27b
--- /dev/null
+++ b/faker/providers/company/pl_PL/__init__.py
@@ -0,0 +1,41 @@
+# coding=utf-8
+from .. import Provider as CompanyProvider
+
+
+def regon_checksum(digits):
+    """
+    Calculates and returns a control digit for given list of digits basing on REGON standard.
+    """
+    weights_for_check_digit = [8, 9, 2, 3, 4, 5, 6, 7]
+    check_digit = 0
+
+    for i in range(0, 8):
+        check_digit += weights_for_check_digit[i] * digits[i]
+
+    check_digit %= 11
+
+    if check_digit == 10:
+        check_digit = 0
+
+    return check_digit
+
+
+class Provider(CompanyProvider):
+
+    @classmethod
+    def regon(cls):
+        """
+        Returns 9 character Polish National Business Registry Number,
+        Polish: Rejestr Gospodarki Narodowej - REGON.
+
+        https://pl.wikipedia.org/wiki/REGON
+        """
+        voivodeship_number = cls.random_int(0, 49) * 2 + 1
+        regon_digits = [int(voivodeship_number / 10), voivodeship_number % 10]
+
+        for _ in range(6):
+            regon_digits.append(cls.random_digit())
+
+        regon_digits.append(regon_checksum(regon_digits))
+
+        return ''.join(str(digit) for digit in regon_digits)


Test patch:
diff --git a/tests/providers/address.py b/tests/providers/address.py
index 78824c32..c73cca76 100644
--- a/tests/providers/address.py
+++ b/tests/providers/address.py
@@ -21,7 +21,7 @@ class TestEnGB(unittest.TestCase):
         self.factory = Factory.create('en_GB')
 
     def test_postcode(self):
-        for i in range(100):
+        for _ in range(100):
             assert isinstance(parse_uk_postcode(self.factory.postcode()), tuple)
 
 
@@ -34,7 +34,7 @@ class TestHuHU(unittest.TestCase):
     def test_postcode_first_digit(self):
         # Hungarian postcodes begin with 'H-' followed by 4 digits.
         # The first digit may not begin with a zero.
-        for i in range(100):
+        for _ in range(100):
             pcd = HuProvider.postcode()
             assert pcd[2] > "0"
 
@@ -119,5 +119,5 @@ class TestNoNO(unittest.TestCase):
         self.factory = Factory.create('no_NO')
 
     def test_postcode(self):
-        for i in range(100):
+        for _ in range(100):
             self.assertTrue(re.match(r'^[0-9]{4}$', self.factory.postcode()))
diff --git a/tests/providers/company.py b/tests/providers/company.py
index 1cc87ddf..9ec0ca29 100644
--- a/tests/providers/company.py
+++ b/tests/providers/company.py
@@ -9,6 +9,7 @@ from faker import Factory
 from faker.providers.company.hu_HU import Provider as HuProvider
 from faker.providers.company.ja_JP import Provider as JaProvider
 from faker.providers.company.pt_BR import Provider as PtProvider, company_id_checksum
+from faker.providers.company.pl_PL import Provider as PlProvider, regon_checksum
 from .. import string_types
 
 
@@ -50,6 +51,7 @@ class TestPtBR(unittest.TestCase):
             cnpj = PtProvider.cnpj()
             self.assertTrue(re.search(r'\d{2}\.\d{3}\.\d{3}/0001-\d{2}', cnpj))
 
+
 class TestHuHU(unittest.TestCase):
     """ Tests company in the hu_HU locale """
 
@@ -65,3 +67,21 @@ class TestHuHU(unittest.TestCase):
     def test_company(self):
         company = self.factory.company()
         assert isinstance(company, string_types)
+
+
+class TestPlPL(unittest.TestCase):
+    """ Tests company in the pl_PL locale """
+
+    def setUp(self):
+        self.factory = Factory.create('pl_PL')
+
+    def test_regon_checksum(self):
+        self.assertEqual(regon_checksum([1, 2, 3, 4, 5, 6, 7, 8]), 5)
+        self.assertEqual(regon_checksum([8, 9, 1, 9, 5, 7, 8, 8]), 3)
+        self.assertEqual(regon_checksum([2, 1, 7, 1, 5, 4, 8, 3]), 8)
+        self.assertEqual(regon_checksum([7, 9, 3, 5, 4, 7, 9, 3]), 9)
+        self.assertEqual(regon_checksum([9, 1, 5, 9, 6, 9, 4, 7]), 7)
+
+    def test_regon(self):
+        for _ in range(100):
+            self.assertTrue(re.search(r'^\d{9}$', PlProvider.regon()))
diff --git a/tests/providers/ssn.py b/tests/providers/ssn.py
index 9e9e6627..f4e5ad16 100644
--- a/tests/providers/ssn.py
+++ b/tests/providers/ssn.py
@@ -27,7 +27,7 @@ class TestEtEE(unittest.TestCase):
         self.assertEqual(et_checksum([3, 9, 7, 0, 3, 0, 4, 3, 3, 6]), 0)
 
     def test_ssn(self):
-        for i in range(100):
+        for _ in range(100):
             self.assertTrue(re.search(r'^\d{11}$', EtProvider.ssn()))
 
 
@@ -46,7 +46,7 @@ class TestHrHR(unittest.TestCase):
         self.assertEqual(hr_checksum([7, 1, 1, 4, 9, 9, 1, 2, 4, 1]), 6)
 
     def test_ssn(self):
-        for i in range(100):
+        for _ in range(100):
             self.assertTrue(re.search(r'^\d{11}$', HrProvider.ssn()))
 
 
@@ -98,5 +98,5 @@ class TestPlPL(unittest.TestCase):
         self.assertEqual(pl_calculate_mouth(datetime.strptime('1 1 2299', '%m %d %Y')), 61)
 
     def test_ssn(self):
-        for i in range(100):
+        for _ in range(100):
             self.assertTrue(re.search(r'^\d{11}$', PlProvider.ssn()))