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

66 lines
2.4 KiB
Python
Raw Normal View History

import pip.download
from pip.commands.search import (compare_versions,
highest_version,
transform_hits,
SearchCommand,)
2011-03-15 20:49:48 +01:00
from pip.backwardcompat import xmlrpclib, b
from mock import Mock
2011-03-15 20:49:48 +01:00
from tests.test_pip import run_pip, reset_env, pyversion
from tests.pypi_server import assert_equal
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 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'}]
2011-03-15 20:49:48 +01:00
assert_equal(expected, transform_hits(pypi_hits))
2010-02-24 11:24:55 +01:00
def test_search():
"""
End to end test of search command.
"""
2010-02-24 11:24:55 +01:00
reset_env()
output = run_pip('search', 'pip')
2010-02-24 11:24:55 +01:00
assert 'pip installs packages' in output.stdout
def test_searching_through_Search_class():
"""
Verify if ``pip.vcs.Search`` uses tests xmlrpclib.Transport class
"""
pip.download.xmlrpclib_transport = fake_transport = Mock()
query = 'mylittlequerythatdoesnotexists'
2011-03-15 20:49:48 +01:00
dumped_xmlrpc_request = b(xmlrpclib.dumps(({'name': query, 'summary': query}, 'or'), 'search'))
expected = [{'_pypi_ordering': 100, 'name': 'foo', 'summary': 'foo summary', 'version': '1.0'}]
fake_transport.request.return_value = (expected,)
pypi_searcher = SearchCommand()
result = pypi_searcher.search(query, 'http://pypi.python.org/pypi')
assert expected == result, result
2011-03-15 20:49:48 +01:00
fake_transport.request.assert_called_with('pypi.python.org', '/pypi', dumped_xmlrpc_request, verbose=VERBOSE_FALSE)