From e103e0b8534ba811cdfa2e298766067be1bad830 Mon Sep 17 00:00:00 2001 From: Maria Antonia Date: Thu, 29 Jun 2023 19:50:59 -0300 Subject: [PATCH 1/3] =?UTF-8?q?Melhorando=20os=20testes=20do=20m=C3=A9todo?= =?UTF-8?q?=20para=20o=20CNPJ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_cnpj.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/tests/test_cnpj.py b/tests/test_cnpj.py index 08b5cb84..517bed81 100644 --- a/tests/test_cnpj.py +++ b/tests/test_cnpj.py @@ -50,22 +50,37 @@ def test_format_cnpj(self): assert format_cnpj("0000000000000a") is None assert format_cnpj("0000000000000") is None - def test_hashdigit(self): - assert hashdigit("00000000000000", 13) == 0 - assert hashdigit("00000000000000", 14) == 0 - assert hashdigit("52513127000292", 13) == 9 - assert hashdigit("52513127000292", 14) == 9 - - def test_checksum(self): - assert checksum("00000000000000") == "00" - assert checksum("52513127000299") == "99" - def test_validate(self): assert validate("34665388000161") assert not validate("52599927000100") assert not validate("00000000000") def test_is_valid(self): + # When CNPJ is not string, returns False + assert not is_valid(1) + + # When CNPJ's len is different of 14, returns False + assert not is_valid("1") + + # When CNPJ does not contain only digits, returns False + assert not is_valid("1112223334445-") + + # When CNPJ has only the same digit, returns false + assert not is_valid("11111111111111") + + # When rest_1 is lt 2 and the 13th digit is not 0, returns False + assert not is_valid("1111111111315") + + # When rest_1 is gte 2 and the 13th digit is not (11 - rest), returns False + assert not is_valid("1111111111115") + + # When rest_2 is lt 2 and the 14th digit is not 0, returns False + assert not is_valid("11111111121205") + + # When rest_2 is gte 2 and the 14th digit is not (11 - rest), returns False + assert not is_valid("11111111113105") + + # When CNPJ is valid assert is_valid("34665388000161") assert not is_valid("52599927000100") assert not is_valid("00000000000") @@ -75,6 +90,16 @@ def test_generate(self): assert validate(generate()) assert display(generate()) is not None + def test_hashdigit(self): + assert hashdigit("00000000000000", 13) == 0 + assert hashdigit("00000000000000", 14) == 0 + assert hashdigit("52513127000292", 13) == 9 + assert hashdigit("52513127000292", 14) == 9 + + def test_checksum(self): + assert checksum("00000000000000") == "00" + assert checksum("52513127000299") == "99" + if __name__ == "__main__": main() From 114019c076374f8b6e075387962723394ffbe4ea Mon Sep 17 00:00:00 2001 From: Maria Antonia Date: Sat, 1 Jul 2023 06:33:44 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Ajustando=20a=20fun=C3=A7=C3=A3o=20is=5Fval?= =?UTF-8?q?id=20para=20melhorar=20a=20implementa=C3=A7=C3=A3o=20dos=20test?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- brutils/cnpj.py | 2 +- tests/test_cnpj.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/brutils/cnpj.py b/brutils/cnpj.py index efca1578..913a41ee 100644 --- a/brutils/cnpj.py +++ b/brutils/cnpj.py @@ -93,7 +93,7 @@ def is_valid(cnpj): # type: (str) -> bool Using this method name to match with the js library api. Using the same method to ensure backwards compatibility. """ - return validate(cnpj) + return isinstance(cnpj, str) and validate(cnpj) def generate(branch=1): # type: (int) -> str diff --git a/tests/test_cnpj.py b/tests/test_cnpj.py index 517bed81..9b262346 100644 --- a/tests/test_cnpj.py +++ b/tests/test_cnpj.py @@ -82,8 +82,7 @@ def test_is_valid(self): # When CNPJ is valid assert is_valid("34665388000161") - assert not is_valid("52599927000100") - assert not is_valid("00000000000") + assert is_valid("01838723000127") def test_generate(self): for i in range(1000): From 34db7e6b134786493280471d995e7cda4d1dcc61 Mon Sep 17 00:00:00 2001 From: Maria Antonia Date: Sat, 8 Jul 2023 08:33:52 -0300 Subject: [PATCH 3/3] Feature: generate_cep --- README.md | 14 +++++++++++++- README_EN.md | 11 +++++++++++ brutils/__init__.py | 1 + brutils/cep.py | 13 +++++++++++++ tests/test_cep.py | 6 ++++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f1c26c6b..807b7e80 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,8 @@ False - [remove_symbols_cnpj](#remove_symbols_cnpj) - [generate_cnpj](#generate_cnpj) - [CEP](#cep) - - [is_valid_cep](#is_valid_cep) + - [is_valid_cep](#is_valid_cep) + - [generate_cep](#generate_cep) ## CPF @@ -142,6 +143,17 @@ Verifica se o CEP é valido. Apenas números, formatados como string. Não verif True ``` +### generate_cep + +Gera um CEP válido aleatório. + +```python +>>> from brutils import generate_cep +>>> generate_cep() +'77520503' +``` + + ## Contributing Sua colaboração é sempre bem-vinda! Preparamos o [arquivo contributing][contributing] pra te ajudar nos primeiros passos. Toda ajuda conta! Sinta-se livre para criar novas [GitHub issues][github-issues] e interagir aqui. diff --git a/README_EN.md b/README_EN.md index 43334e84..d4619437 100644 --- a/README_EN.md +++ b/README_EN.md @@ -46,6 +46,7 @@ False - [generate_cnpj](#generate_cnpj) - [CEP](#cep) - [is_valid_cep](#is_valid_cep) + - [generate_cep](#generate_cep) ## CPF @@ -147,6 +148,16 @@ Check if CEP is valid. Numbers only, formatted as strings. Does not check if CEP True ``` +### generate_cep + +Generate a valid random CEP. + +```python +>>> from brutils import generate_cep +>>> generate_cep() +'77520503' +``` + ## Contributing Collaboration is super welcome! We prepared the [contributing file][contributing] to help you in the first steps. Every little bit of help counts! Feel free to create new [GitHub issues][github-issues] and interact here. diff --git a/brutils/__init__.py b/brutils/__init__.py index e5d69ce0..e2f1aeec 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -12,4 +12,5 @@ ) from brutils.cep import ( is_valid as is_valid_cep, + generate as generate_cep, ) diff --git a/brutils/cep.py b/brutils/cep.py index 1c49718a..0b170127 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -20,3 +20,16 @@ def is_valid(cep): # type: (str) -> bool """ return isinstance(cep, str) and len(cep) == 8 and cep.isdigit() + + +def generate(): # type: () -> str + """ + Generates a random valid CEP digit string. An optional branch + number parameter can be given, it defaults to 1. + """ + generated_number = "" + + for _ in range(0, 8): + generated_number = generated_number + str(randint(0, 9)) + + return generated_number diff --git a/tests/test_cep.py b/tests/test_cep.py index b3d18e65..e023061d 100644 --- a/tests/test_cep.py +++ b/tests/test_cep.py @@ -11,6 +11,7 @@ ) from brutils.cep import ( is_valid, + generate, ) from unittest import TestCase, main @@ -29,3 +30,8 @@ def test_is_valid(self): # When CEP is valid assert is_valid("99999999") assert is_valid("88390000") + + def test_generate(self): + for _ in range(10_000): + assert is_valid(generate()) + # assert format(generate()) is not None