-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adicionado validador para PIS/PASEP/NIS
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import unittest | ||
import validate_docbr as docbr | ||
|
||
|
||
class TestPis(unittest.TestCase): | ||
"""Testar a classe PIS/NIS/PASEP/NIT.""" | ||
|
||
def setUp(self): | ||
"""Inicia novo objeto em todo os testes.""" | ||
self.pis = docbr.PIS() | ||
|
||
def test_generate_validate(self): | ||
"""Verifica os métodos de geração e validação de documento.""" | ||
# generate_list | ||
piss = self.pis.generate_list(5000)\ | ||
+ self.pis.generate_list(5000, mask=True)\ | ||
+ self.pis.generate_list(5000, mask=True, repeat=True) | ||
self.assertIsInstance(piss, list) | ||
self.assertTrue(len(piss) == 15000) | ||
|
||
# validate_list | ||
piss_validates = self.pis.validate_list(piss) | ||
self.assertTrue(sum(piss_validates) == 15000) | ||
|
||
def test_mask(self): | ||
"""Verifica se o método mask funciona corretamente.""" | ||
masked_pis = self.pis.mask('23992734770') | ||
self.assertEqual(masked_pis, '239.92734.77-0') | ||
|
||
masked_pis = self.pis.mask('93999998770') | ||
self.assertEqual(masked_pis, '939.99998.77-0') | ||
|
||
masked_pis = self.pis.mask('03953333770') | ||
self.assertEqual(masked_pis, '039.53333.77-0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from .BaseDoc import BaseDoc | ||
from random import sample | ||
from typing import Union | ||
|
||
|
||
class PIS(BaseDoc): | ||
"""Classe referente ao PIS/NIS/PASEP/NIT.""" | ||
|
||
def __init__(self): | ||
self.digits = list(range(10)) | ||
|
||
def validate(self, doc: str = '') -> bool: | ||
"""Validar PIS/NIS/PASEP/NIT.""" | ||
doc = self._only_digits(doc) | ||
|
||
if len(doc) != 11 or self._is_repeated_digits(doc): | ||
return False | ||
|
||
digit = self._generate_digit(doc) | ||
|
||
return digit == doc[10] | ||
|
||
def generate(self, mask: bool = False) -> str: | ||
"""Gerar PIS/NIS/PASEP/NIT.""" | ||
pis = [str(sample(self.digits, 1)[0]) for i in range(10)] | ||
pis.append(self._generate_digit(pis)) | ||
|
||
pis = ''.join(pis) | ||
return self.mask(pis) if mask else pis | ||
|
||
def mask(self, doc: str = '') -> str: | ||
"""Coloca a máscara de PIS/NIS/PASEP/NIT na variável doc.""" | ||
return "{}.{}.{}-{}".format(doc[:3], doc[3:8], doc[8:10], doc[10:]) | ||
|
||
def _generate_digit(self, doc: Union[str, list]) -> str: | ||
"""Gerar o dígito verificador do PIS/NIS/PASEP/NIT.""" | ||
multipliers = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2] | ||
summation = 0 | ||
|
||
for position in range(0, 10): | ||
summation += int(doc[position]) * multipliers[position] | ||
|
||
module = summation % 11 | ||
digit = 0 | ||
|
||
if module >= 2: | ||
digit = 11 - module | ||
|
||
return str(digit) | ||
|
||
def _is_repeated_digits(self, doc: str) -> bool: | ||
"""Verifica se o PIS/NIS/PASEP/NIT contém com números repetidos. | ||
Exemplo: 11111111111""" | ||
return len(set(doc)) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters