From 904a8d3fd416d1e7b1ca95492d65ed2091552e74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Paiva?= Date: Sun, 3 Mar 2024 19:58:40 -0300 Subject: [PATCH 1/4] ci: Adds CI workflow --- .github/workflows/integration.yml | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/integration.yml diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml new file mode 100644 index 0000000..71021de --- /dev/null +++ b/.github/workflows/integration.yml @@ -0,0 +1,38 @@ +--- +name: Integration + +on: + pull_request: + branches: + - main + +env: + DOCKER_IMAGE: alvarofpp/app:${{ github.sha }} + +jobs: + lint: + runs-on: ubuntu-latest + container: + image: alvarofpp/linter:latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - run: git config --global --add safe.directory $GITHUB_WORKSPACE + - run: lint-commit origin/main + - run: lint-markdown + - run: lint-dockerfile + - run: lint-yaml + - run: lint-shell-script + - run: lint-python + tests: + needs: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - run: docker build -t $DOCKER_IMAGE . + - run: | + docker run --rm -v $(pwd):/app $DOCKER_IMAGE /bin/bash -c \ + "pytest --cov=validate_docbr/" From 68d946d4b2b02059d4a46ddab999cd672f9ba032 Mon Sep 17 00:00:00 2001 From: DOUG Date: Mon, 27 May 2024 10:15:51 -0300 Subject: [PATCH 2/4] feat(issue-63): Complete cpf with zeros with not 11 digits and test it this commit allow validate cpf starts with zeros --- tests/test_CPF.py | 14 ++++++++++++++ validate_docbr/CPF.py | 11 +++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/tests/test_CPF.py b/tests/test_CPF.py index ffd9455..8de7a11 100644 --- a/tests/test_CPF.py +++ b/tests/test_CPF.py @@ -56,3 +56,17 @@ def test_special_case(self): ] for cpf, is_valid in cases: self.assertEqual(self.cpf.validate(cpf), is_valid) + + def test_add_leading_zeros(self): + """Verifica se o método de adicionar zeros à esquerda funciona corretamente.""" + cases = [ + ('123456789', '00123456789'), # 9 digits + ('12345678901', '12345678901'), # 11 digits + ('1234567', '00001234567'), # 7 digits + ] + for cpf_input, expected_output in cases: + self.assertEqual(self.cpf._complete_with_zeros(cpf_input), expected_output) + + # Test if the input is already correct length, it should not add zeros + self.assertEqual(self.cpf._complete_with_zeros('00123456789'), '00123456789') + self.assertEqual(self.cpf._complete_with_zeros('23456789012'), '23456789012') \ No newline at end of file diff --git a/validate_docbr/CPF.py b/validate_docbr/CPF.py index 152137f..3f87eae 100644 --- a/validate_docbr/CPF.py +++ b/validate_docbr/CPF.py @@ -19,13 +19,15 @@ def validate(self, doc: str = '') -> bool: doc = list(self._only_digits(doc)) if len(doc) != 11: - return False + doc = self._complete_with_zeros(doc) + if len(doc) != 11: + return False if not self.repeated_digits and self._check_repeated_digits(doc): return False return self._generate_first_digit(doc) == doc[9] \ - and self._generate_second_digit(doc) == doc[10] + and self._generate_second_digit(doc) == doc[10] def generate(self, mask: bool = False) -> str: """Gerar CPF.""" @@ -76,3 +78,8 @@ def _check_repeated_digits(self, doc: List[str]) -> bool: """Verifica se é um CPF com números repetidos. Exemplo: 111.111.111-11""" return len(set(doc)) == 1 + + def _complete_with_zeros(self, doc: list[str]) -> list[str]: + """Adiciona zeros a esquerda para completar o CPF.""" + + return '0' * (11 - len(doc)) + str(doc) From 9f267cb9655d327e84c4fee1a2844a23d233ef5e Mon Sep 17 00:00:00 2001 From: DOUG Date: Wed, 31 Jul 2024 14:54:19 -0300 Subject: [PATCH 3/4] feat(issue-63): Translate comments to portuguese and remove non used if --- tests/test_CPF.py | 2 +- validate_docbr/CPF.py | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_CPF.py b/tests/test_CPF.py index 8de7a11..410b8c6 100644 --- a/tests/test_CPF.py +++ b/tests/test_CPF.py @@ -67,6 +67,6 @@ def test_add_leading_zeros(self): for cpf_input, expected_output in cases: self.assertEqual(self.cpf._complete_with_zeros(cpf_input), expected_output) - # Test if the input is already correct length, it should not add zeros + # Testa se a entrada ja tem o tamanho correto, não deve adicionar zeros self.assertEqual(self.cpf._complete_with_zeros('00123456789'), '00123456789') self.assertEqual(self.cpf._complete_with_zeros('23456789012'), '23456789012') \ No newline at end of file diff --git a/validate_docbr/CPF.py b/validate_docbr/CPF.py index 3f87eae..b8591c3 100644 --- a/validate_docbr/CPF.py +++ b/validate_docbr/CPF.py @@ -20,8 +20,6 @@ def validate(self, doc: str = '') -> bool: if len(doc) != 11: doc = self._complete_with_zeros(doc) - if len(doc) != 11: - return False if not self.repeated_digits and self._check_repeated_digits(doc): return False @@ -81,5 +79,4 @@ def _check_repeated_digits(self, doc: List[str]) -> bool: def _complete_with_zeros(self, doc: list[str]) -> list[str]: """Adiciona zeros a esquerda para completar o CPF.""" - return '0' * (11 - len(doc)) + str(doc) From a3d25d13a89da42193c694bdcce11a07d5e86035 Mon Sep 17 00:00:00 2001 From: DOUG Date: Wed, 14 Aug 2024 10:07:08 -0300 Subject: [PATCH 4/4] feat(issue-63): Fix if len(doc) < 11 and cpf tests --- tests/test_CPF.py | 15 ++++++--------- validate_docbr/CPF.py | 9 +++++---- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/test_CPF.py b/tests/test_CPF.py index 410b8c6..4707d4d 100644 --- a/tests/test_CPF.py +++ b/tests/test_CPF.py @@ -60,13 +60,10 @@ def test_special_case(self): def test_add_leading_zeros(self): """Verifica se o método de adicionar zeros à esquerda funciona corretamente.""" cases = [ - ('123456789', '00123456789'), # 9 digits - ('12345678901', '12345678901'), # 11 digits - ('1234567', '00001234567'), # 7 digits + ('123456789', False), # 9 digitos + ('12345678901', False), # 11 digitos + ('1234567', False), # 7 digitos + ('9380826044', True) # cpf valido ] - for cpf_input, expected_output in cases: - self.assertEqual(self.cpf._complete_with_zeros(cpf_input), expected_output) - - # Testa se a entrada ja tem o tamanho correto, não deve adicionar zeros - self.assertEqual(self.cpf._complete_with_zeros('00123456789'), '00123456789') - self.assertEqual(self.cpf._complete_with_zeros('23456789012'), '23456789012') \ No newline at end of file + for cpf_input, is_valid in cases: + self.assertEqual(self.cpf.validate(cpf_input), is_valid) diff --git a/validate_docbr/CPF.py b/validate_docbr/CPF.py index b8591c3..458458f 100644 --- a/validate_docbr/CPF.py +++ b/validate_docbr/CPF.py @@ -18,7 +18,7 @@ def validate(self, doc: str = '') -> bool: doc = list(self._only_digits(doc)) - if len(doc) != 11: + if len(doc) < 11: doc = self._complete_with_zeros(doc) if not self.repeated_digits and self._check_repeated_digits(doc): @@ -77,6 +77,7 @@ def _check_repeated_digits(self, doc: List[str]) -> bool: Exemplo: 111.111.111-11""" return len(set(doc)) == 1 - def _complete_with_zeros(self, doc: list[str]) -> list[str]: - """Adiciona zeros a esquerda para completar o CPF.""" - return '0' * (11 - len(doc)) + str(doc) + def _complete_with_zeros(self, doc: str) -> list[str]: + """Adiciona zeros à esquerda para completar o CPF.""" + zeros_needed = 11 - len(doc) + return ['0'] * zeros_needed + doc