diff --git a/Makefile b/Makefile index d095d4f5..47a18705 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 8dd208d9..ff1d2672 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/README_PT_BR.md b/README_PT_BR.md index 20de35f9..8a7d3945 100644 --- a/README_PT_BR.md +++ b/README_PT_BR.md @@ -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 diff --git a/brutils/cpf.py b/brutils/cpf.py index df2d098e..9b4486db 100644 --- a/brutils/cpf.py +++ b/brutils/cpf.py @@ -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) diff --git a/tests/test_cpf.py b/tests/test_cpf.py index d339b16c..322f08da 100644 --- a/tests/test_cpf.py +++ b/tests/test_cpf.py @@ -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 @@ -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())