Skip to content

Commit

Permalink
Add a couple tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerdonek committed Sep 10, 2019
1 parent ed55cde commit ac8d3bd
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 7 deletions.
21 changes: 16 additions & 5 deletions tests/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ def create_file(path, contents=None):
f.write("\n")


def make_test_search_scope(
find_links=None, # type: Optional[List[str]]
index_urls=None, # type: Optional[List[str]]
):
if find_links is None:
find_links = []
if index_urls is None:
index_urls = []

return SearchScope.create(
find_links=find_links,
index_urls=index_urls,
)


def make_test_finder(
find_links=None, # type: Optional[List[str]]
index_urls=None, # type: Optional[List[str]]
Expand All @@ -91,14 +106,10 @@ def make_test_finder(
"""
Create a PackageFinder for testing purposes.
"""
if find_links is None:
find_links = []
if index_urls is None:
index_urls = []
if session is None:
session = PipSession()

search_scope = SearchScope.create(
search_scope = make_test_search_scope(
find_links=find_links,
index_urls=index_urls,
)
Expand Down
107 changes: 105 additions & 2 deletions tests/unit/test_index.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
import os.path
from textwrap import dedent

import pytest
from mock import Mock
from mock import Mock, patch
from pip._vendor import html5lib, requests
from pip._vendor.packaging.specifiers import SpecifierSet

Expand All @@ -25,13 +26,99 @@
group_locations,
)
from pip._internal.models.candidate import InstallationCandidate
from pip._internal.models.index import PyPI
from pip._internal.models.link import Link
from pip._internal.models.search_scope import SearchScope
from pip._internal.models.selection_prefs import SelectionPreferences
from pip._internal.models.target_python import TargetPython
from pip._internal.pep425tags import get_supported
from pip._internal.utils.hashes import Hashes
from tests.lib import CURRENT_PY_VERSION_INFO
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
from tests.lib import CURRENT_PY_VERSION_INFO, make_test_search_scope

if MYPY_CHECK_RUNNING:
from typing import List, Optional


def make_fake_html_page(url):
html = dedent(u"""\
<html><head><meta name="api-version" value="2" /></head>
<body>
<a href="/abc-1.0.tar.gz#md5=000000000">abc-1.0.tar.gz</a>
</body></html>
""")
content = html.encode('utf-8')
headers = {}
return HTMLPage(content, url=url, headers=headers)


def make_test_link_collector(
find_links=None, # type: Optional[List[str]]
):
# type: (...) -> LinkCollector
"""
Create a LinkCollector object for testing purposes.
"""
session = PipSession()
search_scope = make_test_search_scope(
find_links=find_links,
index_urls=[PyPI.simple_url],
)

return LinkCollector(
session=session,
search_scope=search_scope,
)


def check_links_include(links, names):
"""
Assert that the given list of Link objects includes, for each of the
given names, a link whose URL has a base name matching that name.
"""
for name in names:
for link in links:
if link.url.endswith(name):
# Then exit the inner loop and check the next name.
break
else:
raise RuntimeError(
'name {!r} not among links: {}'.format(name, links)
)


class TestLinkCollector(object):

@patch('pip._internal.index._get_html_response')
def test_collect_links(self, mock_get_html_response, data):
expected_url = 'https://pypi.org/simple/twine/'

fake_page = make_fake_html_page(expected_url)
mock_get_html_response.return_value = fake_page

link_collector = make_test_link_collector(
find_links=[data.find_links]
)
actual = link_collector.collect_links('twine')

mock_get_html_response.assert_called_once_with(
expected_url, session=link_collector.session,
)

# Spot-check the CollectedLinks return value.
assert len(actual.files) > 20
check_links_include(actual.files, names=['simple-1.0.tar.gz'])

assert len(actual.find_links) == 1
check_links_include(actual.find_links, names=['packages'])

actual_pages = actual.pages
assert list(actual_pages) == [expected_url]
actual_page_links = actual_pages[expected_url]
assert len(actual_page_links) == 1
assert actual_page_links[0].url == (
'https://pypi.org/abc-1.0.tar.gz#md5=000000000'
)


def make_mock_candidate(version, yanked_reason=None, hex_digest=None):
Expand Down Expand Up @@ -586,6 +673,22 @@ def test_create__candidate_prefs(
assert candidate_prefs.allow_all_prereleases == allow_all_prereleases
assert candidate_prefs.prefer_binary == prefer_binary

def test_create__link_collector(self):
"""
Test that the _link_collector attribute is set correctly.
"""
search_scope = SearchScope([], [])
session = PipSession()
finder = PackageFinder.create(
search_scope=search_scope,
selection_prefs=SelectionPreferences(allow_yanked=True),
session=session,
)

actual_link_collector = finder._link_collector
assert actual_link_collector.search_scope is search_scope
assert actual_link_collector.session is session

def test_create__target_python(self):
"""
Test that the _target_python attribute is set correctly.
Expand Down

0 comments on commit ac8d3bd

Please sign in to comment.