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

complete cpf with zeros with not 11 digits and test it #64

Merged
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
11 changes: 11 additions & 0 deletions tests/test_CPF.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,14 @@ def test_special_case(self):
]
for cpf, is_valid in cases:
self.assertEqual(self.cpf.validate(cpf), is_valid)

def test_add_leading_zeros(self):
"""Verifica se o método de adicionar zeros à esquerda funciona corretamente."""
cases = [
('123456789', False), # 9 digitos
('12345678901', False), # 11 digitos
('1234567', False), # 7 digitos
('9380826044', True) # cpf valido
]
for cpf_input, is_valid in cases:
self.assertEqual(self.cpf.validate(cpf_input), is_valid)
11 changes: 8 additions & 3 deletions validate_docbr/CPF.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def validate(self, doc: str = '') -> bool:

doc = list(self._only_digits(doc))

if len(doc) != 11:
return False
if len(doc) < 11:
doc = self._complete_with_zeros(doc)

if not self.repeated_digits and self._check_repeated_digits(doc):
return False

return self._generate_first_digit(doc) == doc[9] \
and self._generate_second_digit(doc) == doc[10]
and self._generate_second_digit(doc) == doc[10]

def generate(self, mask: bool = False) -> str:
"""Gerar CPF."""
Expand Down Expand Up @@ -76,3 +76,8 @@ def _check_repeated_digits(self, doc: List[str]) -> bool:
"""Verifica se é um CPF com números repetidos.
Exemplo: 111.111.111-11"""
return len(set(doc)) == 1

def _complete_with_zeros(self, doc: str) -> list[str]:
"""Adiciona zeros à esquerda para completar o CPF."""
zeros_needed = 11 - len(doc)
return ['0'] * zeros_needed + doc