From 1965a6fcb373a64548fd54ac93250c030b61298b Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Thu, 27 Oct 2016 22:48:27 -0200 Subject: [PATCH 1/3] Add Brazilian "company ID" aka CNPJ --- faker/providers/company/pt_BR/__init__.py | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/faker/providers/company/pt_BR/__init__.py b/faker/providers/company/pt_BR/__init__.py index f301f0ee24..17e06026bd 100644 --- a/faker/providers/company/pt_BR/__init__.py +++ b/faker/providers/company/pt_BR/__init__.py @@ -1,8 +1,26 @@ # coding=utf-8 from __future__ import unicode_literals +import random from .. import Provider as CompanyProvider +def company_id_checksum(digits): + digits = list(digits) + weights = 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 + + dv = sum(w * d for w, d in zip(weights[1:], digits)) + dv = (11 - dv) % 11 + dv = 0 if dv >= 10 else dv + digits.append(dv) + + dv2 = sum(w * d for w, d in zip(weights, digits)) + dv2 = (11 - dv2) % 11 + dv2 = 0 if dv2 >= 10 else dv2 + digits.append(dv2) + + return digits[-2:] + + class Provider(CompanyProvider): formats = ( '{{last_name}} {{company_suffix}}', @@ -62,3 +80,15 @@ def catch_phrase(self): catch_phrase = self.generator.parse(pattern) catch_phrase = catch_phrase[0].upper() + catch_phrase[1:] return catch_phrase + + @classmethod + def company_id(cls): + digits = random.sample(range(10), 8) + [0, 0, 0, 1] + digits += company_id_checksum(digits) + return ''.join(str(d) for d in digits) + + @classmethod + def cnpj(cls): + digits = cls.company_id() + return '{}.{}.{}/{}-{}'.format(digits[:2], digits[2:5], digits[5:8], + digits[8:12], digits[12:]) From f3db26775a2eda50f9ddc2d5d73eec623af51168 Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Thu, 27 Oct 2016 23:04:28 -0200 Subject: [PATCH 2/3] Test pt_BR company id (CNPJ) --- faker/tests/pt_BR/__init__.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/faker/tests/pt_BR/__init__.py b/faker/tests/pt_BR/__init__.py index 68e0d2546f..c11146ca00 100644 --- a/faker/tests/pt_BR/__init__.py +++ b/faker/tests/pt_BR/__init__.py @@ -6,6 +6,7 @@ import re from faker import Factory +from faker.providers.company.pt_BR import Provider as CompanyProvider, company_id_checksum from faker.providers.ssn.pt_BR import Provider, checksum @@ -18,9 +19,22 @@ def test_pt_BR_ssn_checksum(self): self.assertEqual(checksum([8, 8, 2, 8, 2, 1, 6, 5, 2, 2]), 1) def test_pt_BR_ssn(self): - for i in range(100): + for _ in range(100): self.assertTrue(re.search(r'^\d{11}$', Provider.ssn())) def test_pt_BR_cpf(self): - for i in range(100): + for _ in range(100): self.assertTrue(re.search(r'\d{3}\.\d{3}\.\d{3}\-\d{2}', Provider.cpf())) + + def test_pt_BR_company_id_checksum(self): + self.assertEqual(company_id_checksum([9, 4, 9, 5, 3, 4, 4, 1, 0, 0, 0, 1]), [5, 1]) + self.assertEqual(company_id_checksum([1, 6, 0, 0, 4, 6, 3, 9, 0, 0, 0, 1]), [8, 5]) + + def test_pt_BR_company_id(self): + for _ in range(100): + self.assertTrue(re.search(r'^\d{14}$', CompanyProvider.company_id())) + + def test_pt_BR_cnpj(self): + for _ in range(100): + cnpj = CompanyProvider.cnpj() + self.assertTrue(re.search(r'\d{2}\.\d{3}\.\d{3}/0001-\d{2}', cnpj)) From c1a75315e94637c016736d9b083c25eac97afd0b Mon Sep 17 00:00:00 2001 From: Luiz Menezes Date: Fri, 28 Oct 2016 13:54:32 -0200 Subject: [PATCH 3/3] Use cls.random_sample instead of random.sample on pt-br company id generation --- faker/providers/company/pt_BR/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/faker/providers/company/pt_BR/__init__.py b/faker/providers/company/pt_BR/__init__.py index 17e06026bd..9bf4e519db 100644 --- a/faker/providers/company/pt_BR/__init__.py +++ b/faker/providers/company/pt_BR/__init__.py @@ -1,6 +1,5 @@ # coding=utf-8 from __future__ import unicode_literals -import random from .. import Provider as CompanyProvider @@ -83,7 +82,7 @@ def catch_phrase(self): @classmethod def company_id(cls): - digits = random.sample(range(10), 8) + [0, 0, 0, 1] + digits = cls.random_sample(range(10), 8) + [0, 0, 0, 1] digits += company_id_checksum(digits) return ''.join(str(d) for d in digits)