Skip to content

Commit

Permalink
Melhorando os testes do método is_valid para o CNPJ (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniamaia committed Jul 1, 2023
1 parent 1b12ebf commit 756f126
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion brutils/cnpj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 36 additions & 12 deletions tests/test_cnpj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 756f126

Please sign in to comment.