Skip to content

Commit

Permalink
Merge pull request #29 from royopa/#26_pis_pasep_nis_nit
Browse files Browse the repository at this point in the history
adicionado validador para PIS/PASEP/NIS/NIT
  • Loading branch information
alvarofpp authored May 4, 2020
2 parents 6782b6c + c37e894 commit f0aa95c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Documentos que estão no pacote:
- [CNH](#): Carteira Nacional de Habilitação;
- [CNPJ](https://github.com/alvarofpp/validate-docbr/wiki/CNPJ): Cadastro Nacional da Pessoa Jurídica;
- [CNS](https://github.com/alvarofpp/validate-docbr/wiki/CNS): Cartão Nacional de Saúde;
- [PIS](#): PIS/NIS/PASEP/NIT;
- [Título eleitoral](#): Cadastro que permite cidadãos brasileiros votar.

Para entender melhor os documentos e suas respectivas classes, basta acessar a [Wiki do projeto](https://github.com/alvarofpp/validate-docbr/wiki).
Expand Down
2 changes: 2 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tests.test_cnh
import tests.test_cns
import tests.test_cnpj
import tests.test_pis
import tests.test_titulo_eleitor


Expand All @@ -15,6 +16,7 @@ def suite():
test_suite.addTests(loader.loadTestsFromModule(tests.test_cnh))
test_suite.addTests(loader.loadTestsFromModule(tests.test_cns))
test_suite.addTests(loader.loadTestsFromModule(tests.test_cnpj))
test_suite.addTests(loader.loadTestsFromModule(tests.test_pis))
test_suite.addTests(loader.loadTestsFromModule(tests.test_titulo_eleitor))

return test_suite
Expand Down
34 changes: 34 additions & 0 deletions tests/test_pis.py
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')
54 changes: 54 additions & 0 deletions validate_docbr/PIS.py
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
1 change: 1 addition & 0 deletions validate_docbr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
from .CNPJ import CNPJ
from .CNH import CNH
from .CNS import CNS
from .PIS import PIS
from .TituloEleitoral import TituloEleitoral

0 comments on commit f0aa95c

Please sign in to comment.