Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Melhorando os testes do método is_valid para o CNPJ #114

Merged
merged 2 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()