Move InstallationCandidate to models + add tests

This commit is contained in:
Pradyun Gedam 2018-06-17 15:01:55 +05:30
parent 44d79d3700
commit f1cf9a06e9
No known key found for this signature in database
GPG Key ID: DA17C4B29CB32E4B
3 changed files with 68 additions and 42 deletions

View File

@ -28,6 +28,7 @@ from pip._internal.exceptions import (
UnsupportedWheel,
)
from pip._internal.models.index import PyPI
from pip._internal.models.candidate import InstallationCandidate
from pip._internal.pep425tags import get_supported
from pip._internal.utils.deprecation import RemovedInPip11Warning
from pip._internal.utils.logging import indent_log
@ -57,47 +58,6 @@ SECURE_ORIGINS = [
logger = logging.getLogger(__name__)
class InstallationCandidate(object):
def __init__(self, project, version, location):
self.project = project
self.version = parse_version(version)
self.location = location
self._key = (self.project, self.version, self.location)
def __repr__(self):
return "<InstallationCandidate({!r}, {!r}, {!r})>".format(
self.project, self.version, self.location,
)
def __hash__(self):
return hash(self._key)
def __lt__(self, other):
return self._compare(other, lambda s, o: s < o)
def __le__(self, other):
return self._compare(other, lambda s, o: s <= o)
def __eq__(self, other):
return self._compare(other, lambda s, o: s == o)
def __ge__(self, other):
return self._compare(other, lambda s, o: s >= o)
def __gt__(self, other):
return self._compare(other, lambda s, o: s > o)
def __ne__(self, other):
return self._compare(other, lambda s, o: s != o)
def _compare(self, other, method):
if not isinstance(other, InstallationCandidate):
return NotImplemented
return method(self._key, other._key)
class PackageFinder(object):
"""This finds packages.

View File

@ -0,0 +1,47 @@
from pip._vendor.packaging.version import parse as parse_version
class InstallationCandidate(object):
"""Represents a potential "candidate" for installation.
"""
def __init__(self, project, version, location):
self.project = project
self.version = parse_version(version)
self.location = location
self._key = (self.project, self.version, self.location)
def __repr__(self):
return "<InstallationCandidate({!r}, {!r}, {!r})>".format(
self.project, self.version, self.location,
)
# NOTE: pip._internal.index.Link does something similar.
# We could have a "key-based-compare" mixin that these both use.
def __hash__(self):
return hash(self._key)
def __lt__(self, other):
return self._compare(other, lambda s, o: s < o)
def __le__(self, other):
return self._compare(other, lambda s, o: s <= o)
def __eq__(self, other):
return self._compare(other, lambda s, o: s == o)
def __ge__(self, other):
return self._compare(other, lambda s, o: s >= o)
def __gt__(self, other):
return self._compare(other, lambda s, o: s > o)
def __ne__(self, other):
return self._compare(other, lambda s, o: s != o)
def _compare(self, other, method):
if not isinstance(other, InstallationCandidate):
return NotImplemented
return method(self._key, other._key)

View File

@ -3,7 +3,7 @@
from pip._vendor.packaging.version import parse as parse_version
from pip._internal.models import index
from pip._internal.models import index, candidate
class TestPackageIndex(object):
@ -26,3 +26,22 @@ class TestPackageIndex(object):
assert pack_index.url == "https://pypi.org/"
assert pack_index.simple_url == "https://pypi.org/simple"
assert pack_index.pypi_url == "https://pypi.org/pypi"
class TestInstallationCandidate(object):
def test_sets_correct_variables(self):
obj = candidate.InstallationCandidate(
"A", "1.0.0", "https://somewhere.com/path/A-1.0.0.tar.gz"
)
assert obj.project == "A"
assert obj.version == parse_version("1.0.0")
assert obj.location == "https://somewhere.com/path/A-1.0.0.tar.gz"
# NOTE: This isn't checking the ordering logic; only the data provided to
# it is correct.
def test_sets_the_right_key(self):
obj = candidate.InstallationCandidate(
"A", "1.0.0", "https://somewhere.com/path/A-1.0.0.tar.gz"
)
assert obj._key == (obj.project, obj.version, obj.location)