-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Adiciona validação do RENAVAM - Issue 430 #440
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
def is_valid_renavam(renavam): # type: (str) -> bool | ||
""" | ||
Validates the Brazilian vehicle registration number (RENAVAM). | ||
|
||
This function takes a RENAVAM string and checks if it is valid. | ||
A valid RENAVAM consists of exactly 11 digits, with the last digit as | ||
a verification digit calculated from the previous 10 digits. | ||
|
||
Args: | ||
renavam (str): The RENAVAM string to be validated. | ||
|
||
Returns: | ||
bool: True if the RENAVAM is valid, False otherwise. | ||
|
||
Example: | ||
>>> is_valid_renavam('12345678900') | ||
True | ||
>>> is_valid_renavam('12345678901') | ||
False | ||
>>> is_valid_renavam('1234567890a') | ||
False | ||
>>> is_valid_renavam('12345678 901') | ||
False | ||
>>> is_valid_renavam('12345678') # Less than 11 digits | ||
False | ||
>>> is_valid_renavam('') # Empty string | ||
False | ||
>>> is_valid_renavam(None) # None | ||
False | ||
""" | ||
if renavam and len(renavam) == 11 and renavam.isnumeric(): | ||
check_digit = int(renavam[-1:]) | ||
renavam_digitis = [int(d) for d in renavam] | ||
renavam_digitis = renavam_digitis[:-1] | ||
multipliers = [2, 3, 4, 5, 6, 7, 8, 9, 2, 3] | ||
sum_digits = sum( | ||
digit * multipliers[i] | ||
for i, digit in enumerate(renavam_digitis[::-1]) | ||
) | ||
remainder_division = sum_digits % 11 | ||
if remainder_division <= 1: | ||
check_digit_calculated = 0 | ||
else: | ||
check_digit_calculated = 11 - remainder_division | ||
if check_digit == check_digit_calculated: | ||
return True | ||
return False | ||
Comment on lines
+34
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Na codebase existe essa funcção que está implementada no PIS: def _checksum(base_pis: str) -> int:
"""
Calculate the checksum digit of the given `base_pis` string.
Args:
base_pis (str): The first 10 digits of a PIS number as a string.
Returns:
int: The checksum digit.
"""
pis_digits = list(map(int, base_pis))
pis_sum = sum(digit * weight for digit, weight in zip(pis_digits, WEIGHTS))
check_digit = 11 - (pis_sum % 11)
return 0 if check_digit in [10, 11] else check_digit Me parece que todo documento utilizado no Brasil (PIS, PASEP, RENAVAM, CPF) utilizam esse código de maneira parecida. Então, o que eu sugiro é você utilizar essa função que já existes ajustando para o que você precisa, assim, a gente evita muita duplicação de código. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from unittest import TestCase | ||
|
||
from brutils.renavam import is_valid_renavam | ||
|
||
|
||
class TestRENAVAM(TestCase): | ||
def test_is_valid_renavam(self): | ||
# Testes para RENAVAM válidos | ||
self.assertFalse(is_valid_renavam("12345678901")) | ||
self.assertFalse(is_valid_renavam("09945438645")) | ||
self.assertFalse(is_valid_renavam("94521237655")) | ||
self.assertFalse(is_valid_renavam("45403264305")) | ||
self.assertFalse(is_valid_renavam("45403471665")) | ||
self.assertFalse(is_valid_renavam("34743721835")) | ||
self.assertFalse(is_valid_renavam("69277208515")) | ||
|
||
self.assertTrue(is_valid_renavam("12345678900")) | ||
self.assertTrue(is_valid_renavam("92876838150")) | ||
self.assertTrue(is_valid_renavam("65720648534")) | ||
self.assertTrue(is_valid_renavam("63601073019")) | ||
self.assertTrue(is_valid_renavam("09945438641")) | ||
self.assertTrue(is_valid_renavam("94521237651")) | ||
self.assertTrue(is_valid_renavam("45403264308")) | ||
self.assertTrue(is_valid_renavam("45403471664")) | ||
self.assertTrue(is_valid_renavam("34743721831")) | ||
self.assertTrue(is_valid_renavam("69277208510")) | ||
|
||
# Testes para entradas inválidas | ||
self.assertFalse(is_valid_renavam("1234567890a")) # Contém letra | ||
self.assertFalse(is_valid_renavam("12345678 901")) # Contém espaço | ||
self.assertFalse(is_valid_renavam("12345678")) # Menos de 11 dígitos | ||
self.assertFalse(is_valid_renavam("")) # String vazia | ||
self.assertFalse(is_valid_renavam("123456789012")) # Mais de 11 dígitos | ||
self.assertFalse(is_valid_renavam("abcdefghijk")) # Apenas letras | ||
self.assertFalse( | ||
is_valid_renavam("12345678901!") | ||
) # Contém caractere especial | ||
self.assertFalse(is_valid_renavam(None)) # Contém caractere especial |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ao invés de tipar os parâmetros e o retorno como comentário, utilize mesmo como se fosse uma tipagem forte em linguagens compiladas.