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 8f248fe34b9a3d3617d4d1b9833155c9a8a1a587 Mon Sep 17 00:00:00 2001 From: Maria Antonia Date: Sun, 23 Jul 2023 07:36:17 -0300 Subject: [PATCH 3/3] =?UTF-8?q?Feature:=20implementando=20a=20fun=C3=A7?= =?UTF-8?q?=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++++++ README_EN.md | 15 ++++++++++++++- brutils/__init__.py | 1 + brutils/cep.py | 10 ++++++++++ tests/test_cep.py | 11 +++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 686d27d7..b546bba9 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ False - [CEP](#cep) - [is_valid_cep](#is_valid_cep) - [format_cep](#format_cep) + - [remove_symbols_cep](#remove_symbols_cep) - [generate_cep](#generate_cep) ## CPF @@ -80,6 +81,7 @@ Remove os símbolos de formatação do CPF e retorna somente números. Filtra ap >>> remove_symbols_cpf('000.111.222-33') '00011122233' ``` + ### generate_cpf Gera um CPF válido aleatório. @@ -154,6 +156,16 @@ Formata o CEP. Retorna None se o CEP for inválido. '01310-200' ``` +### remove_symbols_cep + +Remove os símbolos de formatação do CEP e retorna somente números. Filtra apenas os símbolos utilizados para a validação do CEP. Propositalmente não remove outros símbolos. + +```python +>>> from brutils import remove_symbols_cep +>>> remove_symbols_cep('01310-200') +'01310200' +``` + ### generate_cep Gera um CEP válido aleatório. diff --git a/README_EN.md b/README_EN.md index 9d9422fa..f1d56216 100644 --- a/README_EN.md +++ b/README_EN.md @@ -46,7 +46,8 @@ False - [generate_cnpj](#generate_cnpj) - [CEP](#cep) - [is_valid_cep](#is_valid_cep) - - [format_cep](#format_cep) + - [format_cep](#format_cep) + - [remove_symbols_cep](#remove_symbols_cep) - [generate_cep](#generate_cep) ## CPF @@ -159,6 +160,18 @@ Format CEP. Returns None if CEP is invalid. '01310-200' ``` +### remove_symbols_cep + +Remove formatting symbols from CEP and return only digits. +It only filters out the symbols used for CEP validation. +It purposefully doesn't remove other symbols. + +```python +>>> from brutils import remove_symbols_cep +>>> remove_symbols_cep('01310-200') +'01310200' +``` + ### generate_cep Generate a valid random CEP. diff --git a/brutils/__init__.py b/brutils/__init__.py index e51068cf..839548a0 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -14,4 +14,5 @@ is_valid as is_valid_cep, format_cep, generate as generate_cep, + remove_symbols as remove_symbols_cep, ) diff --git a/brutils/cep.py b/brutils/cep.py index cd864083..ecf67a02 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -5,6 +5,16 @@ ############ +def remove_symbols(dirty): # type: (str) -> str + """ + Filters out CEP formatting symbols. Symbols that are not used + in the CEP formatting are left unfiltered on purpose so that + if fails other tests, because their presence indicate that the + input was somehow corrupted. + """ + return "".join(filter(lambda char: char not in ".-", dirty)) + + def format_cep(cep): # type: (str) -> str """ Will format an adequately formatted numbers-only CEP string, diff --git a/tests/test_cep.py b/tests/test_cep.py index a2a2eca0..56b12a61 100644 --- a/tests/test_cep.py +++ b/tests/test_cep.py @@ -13,11 +13,22 @@ is_valid, format_cep, generate, + remove_symbols, ) from unittest import TestCase, main class CEP(TestCase): + def test_remove_symbols(self): + assert remove_symbols("00000000") == "00000000" + assert remove_symbols("01310-200") == "01310200" + assert remove_symbols("01..310.-200.-") == "01310200" + assert remove_symbols("abc01310200*!*&#") == "abc01310200*!*&#" + assert ( + remove_symbols("ab.c1.--.3-102.-0-.0-.*.-!*&#") == "abc1310200*!*&#" + ) + assert remove_symbols("...---...") == "" + def test_format_cep(self): with patch("brutils.cep.is_valid", return_value=True) as mock_is_valid: # When cep is_valid, returns formatted cep