From e103e0b8534ba811cdfa2e298766067be1bad830 Mon Sep 17 00:00:00 2001 From: Maria Antonia Date: Thu, 29 Jun 2023 19:50:59 -0300 Subject: [PATCH 1/4] =?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/4] =?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 85e4d99a073412190a2eb23ca198e8d61dab677c Mon Sep 17 00:00:00 2001 From: Maria Antonia Date: Wed, 5 Jul 2023 06:58:21 -0300 Subject: [PATCH 3/4] =?UTF-8?q?Feature:=20criando=20a=20valida=C3=A7=C3=A3?= =?UTF-8?q?o=20para=20CEP,=20criando=20arquivos=20e=20atualizando=20a=20do?= =?UTF-8?q?cumenta=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 14 ++++++++++++++ README_EN.md | 14 ++++++++++++++ brutils/__init__.py | 3 +++ brutils/cep.py | 25 +++++++++++++++++++++++++ tests/test_cep.py | 31 +++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 brutils/cep.py create mode 100644 tests/test_cep.py diff --git a/README.md b/README.md index 39db492e..b919d8c0 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ False - [format_cnpj](#format_cnpj) - [remove_symbols_cnpj](#remove_symbols_cnpj) - [generate_cnpj](#generate_cnpj) +- [CEP](#cep) + - [is_valid_cep](#is_valid_cep) ## CPF @@ -128,6 +130,18 @@ Gera um CNPJ válido aleatório. '34665388000161' ``` +## CEP + +### is_valid_cep + +Verifica se o CEP é válido. + +```python +>>> from brutils import is_valid_cep +>>> is_valid_cep('88390000') +True +``` + ## 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 59d7119b..0dfc9712 100644 --- a/README_EN.md +++ b/README_EN.md @@ -44,6 +44,8 @@ False - [format_cnpj](#format_cnpj) - [remove_symbols_cnpj](#remove_symbols_cnpj) - [generate_cnpj](#generate_cnpj) +- [CEP](#cep) + - [is_valid_cep](#is_valid_cep) ## CPF @@ -133,6 +135,18 @@ Generate a valid random CNPJ. '34665388000161' ``` +## CEP + +### is_valid_cep + +Check if CEP is valid. + +```python +>>> from brutils import is_valid_cep +>>> is_valid_cep('88390000') +True +``` + ## 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 9f5de8d4..e5d69ce0 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -10,3 +10,6 @@ remove_symbols as remove_symbols_cnpj, generate as generate_cnpj, ) +from brutils.cep import ( + is_valid as is_valid_cep, +) diff --git a/brutils/cep.py b/brutils/cep.py new file mode 100644 index 00000000..8bb60dda --- /dev/null +++ b/brutils/cep.py @@ -0,0 +1,25 @@ +from random import randint + + +# FORMATTING +############ + + +# OPERATIONS +############ + + +def is_valid(cep): # type: (str) -> bool + """ + Returns when CEP is valid, input should be a digit + string of proper length. Doesn't validate if it's real, + 'cause only the "Correios" Base does. + + Source: https://en.wikipedia.org/wiki/C%C3%B3digo_de_Endere%C3%A7amento_Postal + + """ + + if not cep.isdigit() or len(cep) != 8 or len(set(cep)) == 1: + return False + + return isinstance(cep, str) diff --git a/tests/test_cep.py b/tests/test_cep.py new file mode 100644 index 00000000..b3d18e65 --- /dev/null +++ b/tests/test_cep.py @@ -0,0 +1,31 @@ +from os import pardir +from os.path import abspath, join, dirname +from sys import path, version_info, dont_write_bytecode +from inspect import getsourcefile +from unittest.mock import patch + +dont_write_bytecode = True +range = range if version_info.major >= 3 else xrange +path.insert( + 1, abspath(join(dirname(abspath(getsourcefile(lambda: 0))), pardir)) +) +from brutils.cep import ( + is_valid, +) +from unittest import TestCase, main + + +class CEP(TestCase): + def test_is_valid(self): + # When CEP is not string, returns False + assert not is_valid(1) + + # When CEP's len is different of 8, returns False + assert not is_valid("1") + + # When CEP does not contain only digits, returns False + assert not is_valid("1234567-") + + # When CEP is valid + assert is_valid("99999999") + assert is_valid("88390000") From 5bcac9165a6bae2a686edf234ad3bec97fa81066 Mon Sep 17 00:00:00 2001 From: Maria Antonia Date: Sat, 8 Jul 2023 06:47:55 -0300 Subject: [PATCH 4/4] =?UTF-8?q?Ajustando=20a=20fun=C3=A7=C3=A3o=20is=5Fval?= =?UTF-8?q?id=20cep=20e=20a=20documenta=C3=A7=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++--- README_EN.md | 6 +++--- brutils/cep.py | 5 +---- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index b919d8c0..f1c26c6b 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ False ### is_valid_cpf -Verifica se o CPF é valido. +Verifica se o CPF é valido. Apenas números, formatados como string. Não verifica se o CPF existe. ```python >>> from brutils import is_valid_cpf @@ -92,7 +92,7 @@ Gera um CPF válido aleatório. ### is_valid_cnpj -Verifica se o CNPJ é valido. Apenas números, formatados como string. +Verifica se o CNPJ é valido. Apenas números, formatados como string. Não verifica se o CNPJ existe. ```python >>> from brutils import is_valid_cnpj @@ -134,7 +134,7 @@ Gera um CNPJ válido aleatório. ### is_valid_cep -Verifica se o CEP é válido. +Verifica se o CEP é valido. Apenas números, formatados como string. Não verifica se o CEP existe. ```python >>> from brutils import is_valid_cep diff --git a/README_EN.md b/README_EN.md index 0dfc9712..43334e84 100644 --- a/README_EN.md +++ b/README_EN.md @@ -51,7 +51,7 @@ False ### is_valid_cpf -Check if CPF is valid. +Check if CPF is valid. Numbers only, formatted as strings. Does not check if CPF exists. ```python >>> from brutils import is_valid_cpf @@ -95,7 +95,7 @@ Generate a valid random CPF. ### is_valid_cnpj -Check if CNPJ is valid. Numbers only, formatted as strings. +Check if CNPJ is valid. Numbers only, formatted as strings. Does not check if CNPJ exists. ```python >>> from brutils import is_valid_cnpj @@ -139,7 +139,7 @@ Generate a valid random CNPJ. ### is_valid_cep -Check if CEP is valid. +Check if CEP is valid. Numbers only, formatted as strings. Does not check if CEP exists. ```python >>> from brutils import is_valid_cep diff --git a/brutils/cep.py b/brutils/cep.py index 8bb60dda..1c49718a 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -19,7 +19,4 @@ def is_valid(cep): # type: (str) -> bool """ - if not cep.isdigit() or len(cep) != 8 or len(set(cep)) == 1: - return False - - return isinstance(cep, str) + return isinstance(cep, str) and len(cep) == 8 and cep.isdigit()