-
-
Notifications
You must be signed in to change notification settings - Fork 113
feat: add is_valid_pis #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
antoniamaia
merged 2 commits into
brazilian-utils:main
from
kaioduarte:feat/validate-pis
Oct 5, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| WEIGHTS = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2] | ||
|
|
||
|
|
||
| # OPERATIONS | ||
| ############ | ||
|
|
||
|
|
||
| def validate(pis: str) -> bool: | ||
| """ | ||
| Validate a Brazilian PIS number. | ||
|
|
||
| The PIS is valid if: | ||
| - It has 11 digits | ||
| - All characters are digits | ||
| - It passes the weight calculation check | ||
|
|
||
| Args: | ||
| pis[str]: PIS number as a string. | ||
|
|
||
| Returns: | ||
| value[bool]: True if PIS is valid, False otherwise. | ||
| """ | ||
| if len(pis) != 11 or not pis.isdigit(): | ||
| return False | ||
|
|
||
| return pis[-1] == str(_checksum(pis[:-1])) | ||
|
|
||
|
|
||
| def is_valid(pis: str) -> bool: | ||
| """ | ||
| Returns whether or not the verifying checksum digit of the | ||
| given `pis` match its base number. | ||
|
|
||
| Args: | ||
| pis[str]: PIS number as a string of proper length. | ||
|
|
||
| Returns: | ||
| value[bool]: True if PIS is valid, False otherwise. | ||
| """ | ||
| return isinstance(pis, str) and validate(pis) | ||
|
|
||
|
|
||
| def _checksum(base_pis: str) -> int: | ||
| """ | ||
| Calculates the checksum digit of the given `base_pis` string. | ||
|
|
||
| Args: | ||
| base_pis [str]: 10 first digits of PIS number as a string. | ||
|
|
||
| Returns: | ||
| value [int]: Checksum digit. | ||
| """ | ||
| pis_digits = list(map(int, base_pis)) | ||
| pis_sum = sum(digit * weight for digit, weight in zip(pis_digits, WEIGHTS)) | ||
| check_digit = 11 - (pis_sum % 11) | ||
|
|
||
| return 0 if check_digit in [10, 11] else check_digit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from os import pardir | ||
| from os.path import abspath, join, dirname | ||
| from sys import path, version_info, dont_write_bytecode | ||
| from inspect import getsourcefile | ||
| from unittest.mock import patch | ||
|
|
||
| dont_write_bytecode = True | ||
| range = range if version_info.major >= 3 else xrange | ||
| path.insert( | ||
| 1, abspath(join(dirname(abspath(getsourcefile(lambda: 0))), pardir)) | ||
| ) | ||
| from brutils.pis import validate, is_valid, _checksum | ||
| from unittest import TestCase, main | ||
|
|
||
|
|
||
| class TestPIS(TestCase): | ||
| def test_validate(self): | ||
| self.assertIs(validate("123456789ab"), False) | ||
| self.assertIs(validate("901.85930.32-3"), False) | ||
| self.assertIs(validate("90185930323"), True) | ||
| self.assertIs(validate("93616217820"), True) | ||
|
|
||
| def test_is_valid(self): | ||
| # When PIS is not string, returns False | ||
| self.assertIs(is_valid(1), False) | ||
| self.assertIs(is_valid([]), False) | ||
| self.assertIs(is_valid({}), False) | ||
| self.assertIs(is_valid(None), False) | ||
|
|
||
| # When PIS's len is different of 11, returns False | ||
| self.assertIs(is_valid("123456789"), False) | ||
|
|
||
| # When PIS does not contain only digits, returns False | ||
| self.assertIs(is_valid("123pis"), False) | ||
|
|
||
| # When checksum digit doesn't match last digit, returns False | ||
| self.assertIs(is_valid("11111111111"), False) | ||
| self.assertIs(is_valid("11111111215"), False) | ||
| self.assertIs(is_valid("12038619493"), False) | ||
|
|
||
| # When PIS is valid | ||
| self.assertIs(is_valid("12038619494"), True) | ||
| self.assertIs(is_valid("12016784018"), True) | ||
| self.assertIs(is_valid("12083210826"), True) | ||
|
|
||
| def test_checksum(self): | ||
| # Checksum digit is 0 when the subtracted number is 10 or 11 | ||
| self.assertEqual(_checksum("1204152015"), 0) | ||
| self.assertEqual(_checksum("1204433157"), 0) | ||
|
|
||
| # Checksum digit is equal the subtracted number | ||
| self.assertEqual(_checksum("1204917738"), 2) | ||
| self.assertEqual(_checksum("1203861949"), 4) | ||
| self.assertEqual(_checksum("1208321082"), 6) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.