Skip to content
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

Complete Luhn algorithm #1167

Merged
merged 7 commits into from
Jan 20, 2020
Merged

Conversation

andreshndz
Copy link
Contributor

@andreshndz andreshndz commented Jan 15, 2020

Change Summary

Luhn algorithm was not complete. An important step of this algorithm was missing.

This PR would solve the problem described in the issue linked below: Luhn Algorithm is not working as expected.

Related issue number

#1166

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/1166-cuencandres.md file added describing change
    (see changes/README.md for details)

@samuelcolvin
Copy link
Member

Looks like build is failing due to an error with netlify, don't worry about that. I'm pretty pissed off with netlify anyway, so will probably move away from them anyway fairly soon, thus not that keen to look into debugging it.

@matin originally implemented PaymentCardNumber in #790, @matin can you confirm you agree with this?

@samuelcolvin
Copy link
Member

Looks good to me.

@codecov
Copy link

codecov bot commented Jan 15, 2020

Codecov Report

Merging #1167 into master will not change coverage.
The diff coverage is 100%.

@@          Coverage Diff           @@
##           master   #1167   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files          20      20           
  Lines        3490    3509   +19     
  Branches      679     684    +5     
======================================
+ Hits         3490    3509   +19
Impacted Files Coverage Δ
pydantic/types.py 100% <100%> (ø) ⬆️
pydantic/env_settings.py 100% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a9e8db3...bac496d. Read the comment docs.

@matin
Copy link
Contributor

matin commented Jan 15, 2020

@cuencandres my recommendation is for you to use the luhnmod10 library like this: https://github.com/cuenca-mx/stpmex-python/blob/master/stpmex/types.py#L156

I made a bad choice implementing luhn 10 from scratch vs using a library that already has a solid implementation.

@samuelcolvin
Copy link
Member

I'd rather not add another requirement for a single use. From a brief look on wikipedia, the luhn algorithm really doesn't look that complicated. Is there really enough complexity to require a separate library?

@matin
Copy link
Contributor

matin commented Jan 15, 2020

Nope. It's not complex. That's why I had implemented myself at first. I just missed one part. If you prefer not to have the dependency, I'm ok with the PR as it is now.

@dmontagu
Copy link
Contributor

+1 for not adding the dependency

@samuelcolvin
Copy link
Member

Surely for something with as wide-spread use as this algorithm there should be a pretty comprehensive set of tests which pretty much guarantee a correct algorithm?

@samuelcolvin
Copy link
Member

Update, let's include all of these test cases in this PR.

@matin
Copy link
Contributor

matin commented Jan 16, 2020

Works for me.

@matin
Copy link
Contributor

matin commented Jan 16, 2020

@cuencandres can you update the tests? Please make sure to use pytest.mark.parameterize vs a for loop.

@andreshndz
Copy link
Contributor Author

Sure, I'll work on it.

@andreshndz
Copy link
Contributor Author

I added the same tests @samuelcolvin suggested to include +6 suggested by myself.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've realised that the reason docs aren't building is because this is based off a very old version of master.

Therefore please also merge upstream master into this branch.

@@ -0,0 +1 @@
completing Luhn algorithm for ``PaymentCardNumber``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
completing Luhn algorithm for ``PaymentCardNumber``.
completing Luhn algorithm for `PaymentCardNumber`

VALID_OTHER = '2000000000000000008'
LUHN_INVALID = '4000000000000000'
LEN_INVALID = '40000000000000006'

luhn_tests = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please move this into the decorator, e.g.

@pytest.mark.parametrize('card_number, valid', [
    ...
])

@samuelcolvin
Copy link
Member

I'm afraid you've messed up the merge with master.

@samuelcolvin
Copy link
Member

Maybe easiest to start a new pr?

@andreshndz
Copy link
Contributor Author

Sorry, let me create another PR.

@andreshndz
Copy link
Contributor Author

I've fixed the merge.
I think it is done!

def test_validate_luhn_check_digit(card_number: str, valid: bool):
if valid:
assert PaymentCardNumber.validate_luhn_check_digit(card_number) == card_number
assert valid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert valid

else:
with pytest.raises(LuhnValidationError):
PaymentCardNumber.validate_luhn_check_digit(card_number)
assert not valid
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert not valid

@samuelcolvin
Copy link
Member

just one more tiny thing.

By the way, somehow you've made your PR so I can't edit it, hence I can't just fix small issues like this.

In future it might be worthwhile allowing project collaborators to edit your PRs.

@andreshndz
Copy link
Contributor Author

@samuelcolvin , tiny changes are done!

@samuelcolvin samuelcolvin merged commit 4f8f939 into pydantic:master Jan 20, 2020
RajatRajdeep pushed a commit to RajatRajdeep/pydantic that referenced this pull request May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants