From ce8244df854665689e54127d5cc82ec813105b8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Ant=C3=B4nia=20Maia?= Date: Sat, 11 Mar 2023 09:42:36 -0300 Subject: [PATCH 1/3] feat: is_valid_cnpj --- README.md | 4 ++-- README_PT_BR.md | 4 ++-- brutils/cnpj.py | 12 ++++++++++++ tests/test_cnpj.py | 7 ++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ff1d2672..76318c7e 100644 --- a/README.md +++ b/README.md @@ -30,11 +30,11 @@ pip install brutils ``` # numbers only, formatted as strings ->>> from brutils import cnpj +>>> from brutils.cnpj import is_valid_cnpj >>> from brutils.cpf import is_valid_cpf >>> is_valid_cpf('00011122233') False ->>> cnpj.validate('00111222000133') +>>> is_valid_cnpj('00111222000133') False ``` diff --git a/README_PT_BR.md b/README_PT_BR.md index 8a7d3945..ef1fa574 100644 --- a/README_PT_BR.md +++ b/README_PT_BR.md @@ -32,11 +32,11 @@ pip install brutils ``` # somente numeros, em formato string ->>> from brutils import cnpj +>>> from brutils.cnpj import is_valid_cnpj >>> from brutils.cpf import is_valid_cpf >>> is_valid_cpf('00011122233') False ->>> cnpj.validate('00111222000133') +>>> is_valid_cnpj('00111222000133') False ``` diff --git a/brutils/cnpj.py b/brutils/cnpj.py index b106bff7..1a526afd 100644 --- a/brutils/cnpj.py +++ b/brutils/cnpj.py @@ -71,6 +71,18 @@ def validate(cnpj): # type: (str) -> bool hashdigit(cnpj, i + 13) == int(v) for i, v in enumerate(cnpj[12:]) ) +def is_valid_cnpj(cnpj): # type: (str) -> bool + """ + Returns whether or not the verifying checksum digits of the + given `cnpj` 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(cnpj) + + + def generate(branch=1): # type: (int) -> str """ diff --git a/tests/test_cnpj.py b/tests/test_cnpj.py index 1f2ad920..61270f7f 100644 --- a/tests/test_cnpj.py +++ b/tests/test_cnpj.py @@ -8,7 +8,7 @@ path.insert( 1, abspath(join(dirname(abspath(getsourcefile(lambda: 0))), pardir)) ) -from brutils.cnpj import sieve, display, hashdigit, checksum, validate, generate +from brutils.cnpj import sieve, display, hashdigit, checksum, validate, generate, is_valid_cnpj from unittest import TestCase, main @@ -42,6 +42,11 @@ def test_validate(self): assert not validate("52599927000100") assert not validate("00000000000") + def test_is_valid_cnpj(self): + assert is_valid_cnpj("34665388000161") + assert not is_valid_cnpj("52599927000100") + assert not is_valid_cnpj("00000000000") + def test_generate(self): for i in range(1000): assert validate(generate()) From 7fa94e6b3fe0d2594ee64316e7fb38b1d78f71a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Ant=C3=B4nia=20Maia?= Date: Sat, 11 Mar 2023 09:51:26 -0300 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Camila Maia --- README.md | 2 +- README_PT_BR.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 76318c7e..e2afed4d 100644 --- a/README.md +++ b/README.md @@ -30,10 +30,10 @@ pip install brutils ``` # numbers only, formatted as strings ->>> from brutils.cnpj import is_valid_cnpj >>> from brutils.cpf import is_valid_cpf >>> is_valid_cpf('00011122233') False +>>> from brutils.cnpj import is_valid_cnpj >>> is_valid_cnpj('00111222000133') False ``` diff --git a/README_PT_BR.md b/README_PT_BR.md index ef1fa574..dd772090 100644 --- a/README_PT_BR.md +++ b/README_PT_BR.md @@ -32,10 +32,10 @@ pip install brutils ``` # somente numeros, em formato string ->>> from brutils.cnpj import is_valid_cnpj >>> from brutils.cpf import is_valid_cpf >>> is_valid_cpf('00011122233') False +>>> from brutils.cnpj import is_valid_cnpj >>> is_valid_cnpj('00111222000133') False ``` From c8fef6258a27d8744131e11ad68d6244e2f83ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Ant=C3=B4nia=20Maia?= Date: Sat, 11 Mar 2023 09:52:44 -0300 Subject: [PATCH 3/3] fix format --- brutils/cnpj.py | 5 ++--- tests/test_cnpj.py | 10 +++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/brutils/cnpj.py b/brutils/cnpj.py index 1a526afd..69f80df8 100644 --- a/brutils/cnpj.py +++ b/brutils/cnpj.py @@ -71,7 +71,8 @@ def validate(cnpj): # type: (str) -> bool hashdigit(cnpj, i + 13) == int(v) for i, v in enumerate(cnpj[12:]) ) -def is_valid_cnpj(cnpj): # type: (str) -> bool + +def is_valid_cnpj(cnpj): # type: (str) -> bool """ Returns whether or not the verifying checksum digits of the given `cnpj` match it's base number. Input should be a digit @@ -80,8 +81,6 @@ def is_valid_cnpj(cnpj): # type: (str) -> bool Using the same method to ensure backwards compatibility. """ return validate(cnpj) - - def generate(branch=1): # type: (int) -> str diff --git a/tests/test_cnpj.py b/tests/test_cnpj.py index 61270f7f..d6874fce 100644 --- a/tests/test_cnpj.py +++ b/tests/test_cnpj.py @@ -8,7 +8,15 @@ path.insert( 1, abspath(join(dirname(abspath(getsourcefile(lambda: 0))), pardir)) ) -from brutils.cnpj import sieve, display, hashdigit, checksum, validate, generate, is_valid_cnpj +from brutils.cnpj import ( + sieve, + display, + hashdigit, + checksum, + validate, + generate, + is_valid_cnpj, +) from unittest import TestCase, main