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 87dbd66487de3fee0502004ce58b88fdc3d19103 Mon Sep 17 00:00:00 2001 From: Maria Antonia Date: Mon, 17 Jul 2023 07:06:40 -0300 Subject: [PATCH 3/3] =?UTF-8?q?Feature:=20implementando=20utilit=C3=A1rio?= =?UTF-8?q?=20format=5Fcep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 ++++++++++++- README_EN.md | 15 +++++++++++++-- brutils/__init__.py | 1 + brutils/cep.py | 12 ++++++++++++ tests/test_cep.py | 13 +++++++++++++ 5 files changed, 51 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 807b7e80..686d27d7 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ False - [generate_cnpj](#generate_cnpj) - [CEP](#cep) - [is_valid_cep](#is_valid_cep) + - [format_cep](#format_cep) - [generate_cep](#generate_cep) ## CPF @@ -139,10 +140,20 @@ Verifica se o CEP é valido. Apenas números, formatados como string. Não verif ```python >>> from brutils import is_valid_cep ->>> is_valid_cep('88390000') +>>> is_valid_cep('01310200') True ``` +### format_cep + +Formata o CEP. Retorna None se o CEP for inválido. + +```python +>>> from brutils import format_cep +>>> format_cep('01310200') +'01310-200' +``` + ### generate_cep Gera um CEP válido aleatório. diff --git a/README_EN.md b/README_EN.md index d4619437..9d9422fa 100644 --- a/README_EN.md +++ b/README_EN.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) + - [format_cep](#format_cep) - [generate_cep](#generate_cep) ## CPF @@ -144,10 +145,20 @@ Check if CEP is valid. Numbers only, formatted as strings. Does not check if CEP ```python >>> from brutils import is_valid_cep ->>> is_valid_cep('88390000') +>>> is_valid_cep('01310200') True ``` +### format_cep + +Format CEP. Returns None if CEP is invalid. + +```python +>>> from brutils import format_cep +>>> format_cpf('01310200') +'01310-200' +``` + ### generate_cep Generate a valid random CEP. diff --git a/brutils/__init__.py b/brutils/__init__.py index e2f1aeec..e51068cf 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -12,5 +12,6 @@ ) from brutils.cep import ( is_valid as is_valid_cep, + format_cep, generate as generate_cep, ) diff --git a/brutils/cep.py b/brutils/cep.py index 0b170127..cd864083 100644 --- a/brutils/cep.py +++ b/brutils/cep.py @@ -5,6 +5,18 @@ ############ +def format_cep(cep): # type: (str) -> str + """ + Will format an adequately formatted numbers-only CEP string, + adding in standard formatting visual aid symbols for display. + """ + + if not is_valid(cep): + return None + + return "{}-{}".format(cep[:5], cep[5:8]) + + # OPERATIONS ############ diff --git a/tests/test_cep.py b/tests/test_cep.py index e023061d..a2a2eca0 100644 --- a/tests/test_cep.py +++ b/tests/test_cep.py @@ -11,12 +11,25 @@ ) from brutils.cep import ( is_valid, + format_cep, generate, ) from unittest import TestCase, main class CEP(TestCase): + 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 + assert format_cep("01310200") == "01310-200" + + # Checks if function is_valid_cnpj is called + mock_is_valid.assert_called_once_with("01310200") + + with patch("brutils.cep.is_valid", return_value=False) as mock_is_valid: + # When cep isn't valid, returns None + assert format_cep("013102009") is None + def test_is_valid(self): # When CEP is not string, returns False assert not is_valid(1)