Skip to content

Commit

Permalink
Improve type hint coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Jul 8, 2022
1 parent a9f1380 commit 4694acc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
14 changes: 7 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# stdlib
from typing import Callable, Type, TypeVar
from typing import Callable, Iterator, Type, TypeVar

# 3rd party
import pytest
from _pytest.fixtures import FixtureRequest
from apeye.url import URL
from betamax import Betamax # type: ignore
from betamax import Betamax # type: ignore[import]
from domdf_python_tools.paths import PathPlus
from packaging.tags import Tag
from pytest_regressions.data_regression import RegressionYamlDumper # type: ignore
from pytest_regressions.data_regression import RegressionYamlDumper # type: ignore[import]

# this package
from pypi_json import PyPIJSON
Expand All @@ -22,7 +22,7 @@


@pytest.fixture()
def cassette(request: FixtureRequest):
def cassette(request: FixtureRequest) -> Iterator[PyPIJSON]:
"""
Provides a Betamax cassette scoped to the test function
which record and plays back interactions with the PyPI API.
Expand All @@ -35,7 +35,7 @@ def cassette(request: FixtureRequest):


@pytest.fixture()
def module_cassette(request: FixtureRequest):
def module_cassette(request: FixtureRequest) -> Iterator[PyPIJSON]:
"""
Provides a Betamax cassette scoped to the test module
which record and plays back interactions with the PyPI API.
Expand All @@ -50,7 +50,7 @@ def module_cassette(request: FixtureRequest):
yield client


def _representer_for(*data_type: Type):
def _representer_for(*data_type: Type): # noqa: MAN002

def deco(representer_fn: _C) -> _C:
for dtype in data_type:
Expand All @@ -62,5 +62,5 @@ def deco(representer_fn: _C) -> _C:


@_representer_for(URL, Tag)
def _represent_sequences(dumper: RegressionYamlDumper, data):
def _represent_sequences(dumper: RegressionYamlDumper, data): # noqa: MAN001,MAN002
return dumper.represent_str(str(data))
45 changes: 29 additions & 16 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,28 @@
from pypi_json import PyPIJSON


def uri_validator(x):
def uri_validator(x) -> bool: # noqa: MAN001
# Based on https://stackoverflow.com/a/38020041
# By https://stackoverflow.com/users/1668293/alemol and https://stackoverflow.com/users/953553/andilabs
result = urlparse(x)
return all([result.scheme, result.netloc, result.path])


def test_get_metadata(advanced_data_regression: AdvancedDataRegressionFixture, module_cassette):
def test_get_metadata(
advanced_data_regression: AdvancedDataRegressionFixture,
module_cassette: PyPIJSON,
):
metadata = module_cassette.get_metadata("OctoCheese")
advanced_data_regression.check(metadata)

assert metadata.name == "octocheese"
assert metadata.version == Version("0.3.0")


def test_get_latest_version(advanced_data_regression: AdvancedDataRegressionFixture, cassette):
def test_get_latest_version(
advanced_data_regression: AdvancedDataRegressionFixture,
cassette: PyPIJSON,
):
metadata = cassette.get_metadata("OctoCheese", "0.1.0")
advanced_data_regression.check(metadata)

Expand All @@ -43,7 +49,7 @@ def test_get_latest_version(advanced_data_regression: AdvancedDataRegressionFixt
assert metadata.get_latest_version() == Version("0.3.0")


def test_changes_to_api_july_2022(advanced_data_regression: AdvancedDataRegressionFixture):
def test_changes_to_api_july_2022():
with PyPIJSON() as client:
metadata = client.get_metadata("OctoCheese", "0.1.0")

Expand All @@ -66,8 +72,8 @@ def test_changes_to_api_july_2022(advanced_data_regression: AdvancedDataRegressi
@pytest.mark.parametrize("version", [None, "0.1.0"])
def test_get_pypi_releases(
advanced_data_regression: AdvancedDataRegressionFixture,
version,
module_cassette,
version: str,
module_cassette: PyPIJSON,
):
metadata = module_cassette.get_metadata("OctoCheese", version)

Expand All @@ -87,7 +93,9 @@ def test_get_pypi_releases(

@pytest.mark.parametrize("version", [None, "0.1.0"])
def test_get_releases_with_digests(
advanced_data_regression: AdvancedDataRegressionFixture, version, module_cassette
advanced_data_regression: AdvancedDataRegressionFixture,
version: str,
module_cassette: PyPIJSON,
):
metadata = module_cassette.get_metadata("OctoCheese", version)

Expand Down Expand Up @@ -163,7 +171,7 @@ def test_download_file(
advanced_data_regression.check(sorted({f.name for f in tar.getmembers()}))


def test_metadata_nonexistant(cassette):
def test_metadata_nonexistant(cassette: PyPIJSON):
with pytest.raises(InvalidRequirement, match="No such project 'FizzBuzz'"):
cassette.get_metadata("FizzBuzz")

Expand All @@ -181,31 +189,36 @@ def test_metadata_nonexistant(cassette):
param("coverage", None, idx=0),
]
)
def test_get_wheel_tag_mapping(name, version, advanced_data_regression: AdvancedDataRegressionFixture, cassette):
def test_get_wheel_tag_mapping(
name: str,
version: str,
advanced_data_regression: AdvancedDataRegressionFixture,
cassette: PyPIJSON,
):
metadata = cassette.get_metadata(name, version)

tag_url_map, non_wheel_urls = metadata.get_wheel_tag_mapping(version)
tag_url_map = dict(sorted((str(k), v) for k, v in tag_url_map.items()))
advanced_data_regression.check((tag_url_map, non_wheel_urls))
tag_url_map_str = dict(sorted((str(k), v) for k, v in tag_url_map.items()))
advanced_data_regression.check((tag_url_map_str, non_wheel_urls))


@pytest.mark.parametrize("name, version", [param("microsoft", "0.0.1", idx=0)])
def test_get_wheel_tag_mapping_no_files(name, version, cassette):
def test_get_wheel_tag_mapping_no_files(name: str, version: str, cassette: PyPIJSON):
metadata = cassette.get_metadata(name, version)

with pytest.raises(ValueError, match=f"Version {version} has no files on PyPI."):
metadata.get_wheel_tag_mapping(version)


@pytest.mark.parametrize("name, version", [param("microsoft", "0.0.1", idx=0)])
def test_get_wheel_tag_mapping_no_version(name, version, cassette):
def test_get_wheel_tag_mapping_no_version(name: str, version: str, cassette: PyPIJSON):
metadata = cassette.get_metadata(name, version)

with pytest.raises(ValueError, match="Cannot find version 1.2.3 on PyPI."):
metadata.get_wheel_tag_mapping(Version("1.2.3"))


def test_get_metadata_not_found(cassette):
def test_get_metadata_not_found(cassette: PyPIJSON):
with pytest.raises(InvalidRequirement, match="No such project 'pypi-json'"):
cassette.get_metadata("pypi-json", None)

Expand Down Expand Up @@ -256,7 +269,7 @@ def test_custom_endpoint():

my_endpoint = RequestsURL("http://my.pypi")
fake_session = object()
my_endpoint.session = fake_session # type: ignore
my_endpoint.session = fake_session # type: ignore[assignment]

client = PyPIJSON(my_endpoint)
assert client.endpoint == my_endpoint
Expand All @@ -265,6 +278,6 @@ def test_custom_endpoint():
my_endpoint = RequestsURL("http://my.pypi")
fake_session = object()

client = PyPIJSON(my_endpoint, session=fake_session) # type: ignore
client = PyPIJSON(my_endpoint, session=fake_session) # type: ignore[arg-type]
assert client.endpoint == my_endpoint
assert client.endpoint.session is fake_session

0 comments on commit 4694acc

Please sign in to comment.