From 756f1267b78ed597a7b65fd344a8ae2c6bf6a0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Ant=C3=B4nia=20Maia?= Date: Sat, 1 Jul 2023 06:37:45 -0300 Subject: [PATCH] =?UTF-8?q?Melhorando=20os=20testes=20do=20m=C3=A9todo=20`?= =?UTF-8?q?is=5Fvalid`=20para=20o=20CNPJ=20(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- brutils/cnpj.py | 2 +- tests/test_cnpj.py | 48 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/brutils/cnpj.py b/brutils/cnpj.py index efca157..913a41e 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 08b5cb8..9b26234 100644 --- a/tests/test_cnpj.py +++ b/tests/test_cnpj.py @@ -50,31 +50,55 @@ 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") + assert is_valid("01838723000127") def test_generate(self): for i in range(1000): 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()