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

Add validators for 'python_major' and 'python_minor' arguments #402

Merged
merged 2 commits into from
Jul 25, 2023

Conversation

lnxpy
Copy link
Contributor

@lnxpy lnxpy commented Jul 19, 2023

  • argparse validators implementation
  • add cli tests

@lnxpy lnxpy changed the title Add validators for 'python_major' and 'python_minor Add validators for 'python_major' and 'python_minor' arguments Jul 19, 2023
@codecov
Copy link

codecov bot commented Jul 19, 2023

Codecov Report

Merging #402 (d71797f) into main (a5889af) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #402      +/-   ##
==========================================
+ Coverage   99.60%   99.62%   +0.01%     
==========================================
  Files           8        8              
  Lines         764      792      +28     
==========================================
+ Hits          761      789      +28     
  Misses          3        3              
Flag Coverage Δ
macos-latest 99.62% <100.00%> (+0.01%) ⬆️
ubuntu-latest 99.62% <100.00%> (+0.01%) ⬆️
windows-latest 99.62% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
src/pypistats/cli.py 98.98% <100.00%> (+0.16%) ⬆️
tests/test_cli.py 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@lnxpy lnxpy force-pushed the bugfix/invalid-option-values branch from e5ad05f to 37543b6 Compare July 19, 2023 17:03
@hugovk hugovk added the changelog: Added For new features label Jul 20, 2023
@hugovk
Copy link
Owner

hugovk commented Jul 20, 2023

Please could you add tests in https://github.com/hugovk/pypistats/blob/main/tests/test_cli.py?

@lnxpy
Copy link
Contributor Author

lnxpy commented Jul 23, 2023

Hi @hugovk. Would you work on the remaining tasks?! They look a bit confusing to me actually. Sorry for that.

@hugovk
Copy link
Owner

hugovk commented Jul 25, 2023

Sure! Let me explain the general idea:

@c("test_input", ["2", "3", "4"])
def test__python_major_version_valid(test_input: str) -> None:
    # Act / Assert
    assert cli._python_major_version(test_input) == test_input

assert cli._python_major_version(test_input) == test_input is the main bit. We're passing an input value to the function, and asserting the output is as expected. In this case, the input should be valid, so the output should be the same as the input.

@pytest.mark.parametrize("test_input", ["2", "3", "4"]) is a nice bit of magic from pytest.

It runs the test function three times for each input value there: "2", "3" and "4". Each time is like a fresh run, independent of any previous inputs. It's a nice way to repeat a test with the same logic but with different input values. It's basically a do-not-repeat-yourself of doing:

def test__python_major_version_valid2() -> None:
    # Act / Assert
    assert cli._python_major_version("2") == "2"

def test__python_major_version_valid3() -> None:
    # Act / Assert
    assert cli._python_major_version("3") == "3"

def test__python_major_version_valid4() -> None:
    # Act / Assert
    assert cli._python_major_version("4") == "4"

We could of course loop it:

def test__python_major_version_valid() -> None:
    # Act / Assert
    for test_input in ("2", "3", "4"):
        assert cli._python_major_version(test_input) == test_input

But if it failed for, say, "3", then it wouldn't run it for "3". pytest.mark.parametrize runs each one separately, so even if "3" fails, it will run "4".

@hugovk
Copy link
Owner

hugovk commented Jul 25, 2023

The invalid one is similar:

@pytest.mark.parametrize("test_input", ["2.7", "3.9", "3.11", "-5", "pillow"])
def test__python_major_version_invalid(test_input) -> None:
    # Act / Assert
    with pytest.raises(argparse.ArgumentTypeError):
        cli._python_major_version(test_input)

Except that _python_major_version won't return a value for invalid input, so we don't assert the output is a value, we use the pytest.raises context manager to check the mentioned exception is raised.

Then we have a similar pair for _python_minor_version.

@hugovk
Copy link
Owner

hugovk commented Jul 25, 2023

Would you to look at exception handling for empty data or shall I check that?

We could actually merge this PR as it is, it's nicely self-contained, and do that bit in another PR.

@lnxpy
Copy link
Contributor Author

lnxpy commented Jul 25, 2023

I actually thought that you want me to add tests for the actual CLI arguments like the args.python_major, args.python_minor, and args.system. I could add tests for that small validator piece. Thanks for your complete explanation though. :))

@lnxpy
Copy link
Contributor Author

lnxpy commented Jul 25, 2023

Yes indeed. We can have that exception handling in another PR. I unlinked this PR. By merging this PR, the issue won't get closed anymore. Would you please work on that part too?! I looked over the codebase days ago and that was a bit hard to figure out its parts. Keep in mind that I looked over it with the mindset that I talked about in #401.

For the sake of simplicity, we can have some refactoring and improvements next up. 🍻

@hugovk
Copy link
Owner

hugovk commented Jul 25, 2023

Sounds good 👍

@hugovk hugovk merged commit a56af06 into hugovk:main Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Added For new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants