Skip to content
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ check:
@poetry run black -l 80 --check . --exclude=.venv

test:
@poetry run python3 -m unittest discover tests/
@poetry run python3 -m unittest discover tests/ -v
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@ pip install brutils

## Usage

### Importing:
```
>>> from brutils import cpf, cnpj
```

### How do I validate a CPF or CNPJ?
```
# numbers only, formatted as strings

>>> cpf.validate('00011122233')
>>> from brutils import cnpj
>>> from brutils.cpf import is_valid_cpf
>>> is_valid_cpf('00011122233')
False
>>> cnpj.validate('00111222000133')
False
Expand Down
9 changes: 3 additions & 6 deletions README_PT_BR.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ pip install brutils

## Utilização

### Importando a Biblioteca:
```
>>> from brutils import cpf, cnpj
```

### Como faço para validar um CPF ou CNPJ?
```
# somente numeros, em formato string

>>> cpf.validate('00011122233')
>>> from brutils import cnpj
>>> from brutils.cpf import is_valid_cpf
>>> is_valid_cpf('00011122233')
False
>>> cnpj.validate('00111222000133')
False
Expand Down
11 changes: 11 additions & 0 deletions brutils/cpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ def validate(cpf): # type: (str) -> bool
return all(hashdigit(cpf, i + 10) == int(v) for i, v in enumerate(cpf[9:]))


def is_valid_cpf(cpf): # type: (str) -> bool
"""
Returns whether or not the verifying checksum digits of the
given `cpf` match it's base number. Input should be a digit
string of proper length.
Using this method name to match with the js library api.
Using the same method to ensure backwards compatibility.
"""
return validate(cpf)


def generate(): # type: () -> str
"""Generates a random valid CPF digit string."""
base = str(randint(1, 999999998)).zfill(9)
Expand Down
15 changes: 14 additions & 1 deletion tests/test_cpf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
path.insert(
1, abspath(join(dirname(abspath(getsourcefile(lambda: 0))), pardir))
)
from brutils.cpf import sieve, display, hashdigit, checksum, validate, generate
from brutils.cpf import (
sieve,
display,
hashdigit,
checksum,
validate,
generate,
is_valid_cpf,
)
from unittest import TestCase, main


Expand Down Expand Up @@ -42,6 +50,11 @@ def test_validate(self):
assert validate("52599927765")
assert not validate("00000000000")

def test_is_valid_cpf(self):
assert is_valid_cpf("52513127765")
assert is_valid_cpf("52599927765")
assert not is_valid_cpf("00000000000")

def test_generate(self):
for i in range(1000):
assert validate(generate())
Expand Down