1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/tests/functional/test_search.py

125 lines
4.1 KiB
Python
Raw Normal View History

import pip.download
from pip.commands.search import (compare_versions,
highest_version,
transform_hits,
2011-12-04 23:33:03 +01:00
SearchCommand)
from pip.status_codes import NO_MATCHES_FOUND, SUCCESS
2011-03-15 20:49:48 +01:00
from pip.backwardcompat import xmlrpclib, b
from pip.baseparser import create_main_parser
from mock import Mock
from tests.lib import reset_env, pyversion
2011-03-15 20:49:48 +01:00
if pyversion >= '3':
VERBOSE_FALSE = False
else:
VERBOSE_FALSE = 0
2010-02-24 11:24:55 +01:00
def test_version_compare():
"""
Test version comparison.
"""
2010-02-24 11:24:55 +01:00
assert compare_versions('1.0', '1.1') == -1
assert compare_versions('1.1', '1.0') == 1
assert compare_versions('1.1a1', '1.1') == -1
assert compare_versions('1.1.1', '1.1a') == -1
2010-02-24 11:24:55 +01:00
assert highest_version(['1.0', '2.0', '0.1']) == '2.0'
assert highest_version(['1.0a1', '1.0']) == '1.0'
def test_pypi_xml_transformation():
"""
Test transformation of data structures (pypi xmlrpc to custom list).
"""
2010-02-24 11:24:55 +01:00
pypi_hits = [{'_pypi_ordering': 100, 'name': 'foo', 'summary': 'foo summary', 'version': '1.0'},
{'_pypi_ordering': 200, 'name': 'foo', 'summary': 'foo summary v2', 'version': '2.0'},
{'_pypi_ordering': 50, 'name': 'bar', 'summary': 'bar summary', 'version': '1.0'}]
expected = [{'score': 200, 'versions': ['1.0', '2.0'], 'name': 'foo', 'summary': 'foo summary v2'},
{'score': 50, 'versions': ['1.0'], 'name': 'bar', 'summary': 'bar summary'}]
assert transform_hits(pypi_hits) == expected
2010-02-24 11:24:55 +01:00
def test_invalid_pypi_transformation():
"""
Test transformation of pypi when ordering None
"""
pypi_hits = [{'_pypi_ordering': None, 'name': 'bar', 'summary': 'bar summary', 'version': '1.0'},
{'_pypi_ordering': 100, 'name': 'foo', 'summary': 'foo summary', 'version': '1.0'}]
expected = [{'score': 100, 'versions': ['1.0'], 'name': 'foo', 'summary': 'foo summary'},
{'score': 0, 'versions': ['1.0'], 'name': 'bar', 'summary': 'bar summary'}]
assert transform_hits(pypi_hits) == expected
def test_search():
"""
End to end test of search command.
"""
script = reset_env()
output = script.pip('search', 'pip')
assert 'A tool for installing and managing Python packages' in output.stdout
2010-02-24 11:24:55 +01:00
def test_multiple_search():
"""
Test searching for multiple packages at once.
"""
script = reset_env()
output = script.pip('search', 'pip', 'INITools')
assert 'A tool for installing and managing Python packages' in output.stdout
assert 'Tools for parsing and using INI-style files' in output.stdout
2011-07-20 20:46:16 +02:00
def test_search_missing_argument():
"""
Test missing required argument for search
"""
script = reset_env()
result = script.pip('search', expect_error=True)
assert 'ERROR: Missing required argument (search query).' in result.stdout
def test_run_method_should_return_sucess_when_find_packages():
"""
Test SearchCommand.run for found package
"""
options_mock = Mock()
options_mock.index = 'http://pypi.python.org/pypi'
search_cmd = SearchCommand(create_main_parser())
status = search_cmd.run(options_mock, ('pip',))
assert status == SUCCESS
def test_run_method_should_return_no_matches_found_when_does_not_find_packages():
"""
Test SearchCommand.run for no matches
"""
options_mock = Mock()
options_mock.index = 'https://pypi.python.org/pypi'
search_cmd = SearchCommand(create_main_parser())
status = search_cmd.run(options_mock, ('non-existant-package',))
assert status == NO_MATCHES_FOUND, status
def test_search_should_exit_status_code_zero_when_find_packages():
"""
Test search exit status code for package found
"""
script = reset_env()
result = script.pip('search', 'pip')
assert result.returncode == SUCCESS
def test_search_exit_status_code_when_finds_no_package():
"""
Test search exit status code for no matches
"""
script = reset_env()
result = script.pip('search', 'non-existant-package', expect_error=True)
assert result.returncode == NO_MATCHES_FOUND, result.returncode