2010-07-02 14:27:22 +02:00
|
|
|
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
|
2010-07-02 14:27:22 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2010-02-25 00:16:43 +01:00
|
|
|
def test_version_compare():
|
|
|
|
"""
|
|
|
|
Test version comparison.
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2010-02-25 00:16:43 +01:00
|
|
|
"""
|
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
|
Work around failing test due to distutils.version issue
In py3k the following fails, handle this and use string comparison in worst
case.
Tested on 2.6 and 3.2
>> from distutils.version import LooseVersion as LV
>>> v1, v2, v3 = (LV('1.1'), LV('1.1.2'), LV('1.1a1'))
>>> v1 > v2
False
>>> v1 > v3
False
>>> v2 > v3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "distutils/version.py", line 70, in __gt__
c = self._cmp(other)
File "distutils/version.py", line 343, in _cmp
if self.version < other.version:
TypeError: unorderable types: int() < str()
2011-04-16 22:57:22 +02:00
|
|
|
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'
|
|
|
|
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2010-02-25 00:16:43 +01:00
|
|
|
def test_pypi_xml_transformation():
|
|
|
|
"""
|
|
|
|
Test transformation of data structures (pypi xmlrpc to custom list).
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2010-02-25 00:16:43 +01:00
|
|
|
"""
|
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'}]
|
2012-03-18 11:33:13 +01:00
|
|
|
assert_equal(transform_hits(pypi_hits), expected)
|
2010-02-24 11:24:55 +01:00
|
|
|
|
2012-05-14 05:13:50 +02:00
|
|
|
|
2012-03-18 11:33:13 +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_equal(transform_hits(pypi_hits), expected)
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2012-05-14 05:13:50 +02:00
|
|
|
|
2010-02-25 00:16:43 +01:00
|
|
|
def test_search():
|
|
|
|
"""
|
|
|
|
End to end test of search command.
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2010-02-25 00:16:43 +01:00
|
|
|
"""
|
2010-02-24 11:24:55 +01:00
|
|
|
reset_env()
|
2010-07-06 19:26:43 +02:00
|
|
|
output = run_pip('search', 'pip')
|
2010-02-24 11:24:55 +01:00
|
|
|
assert 'pip installs packages' in output.stdout
|
|
|
|
|
2010-07-02 14:27:22 +02:00
|
|
|
|
2011-04-16 18:00:10 +02:00
|
|
|
def test_multiple_search():
|
|
|
|
"""
|
2011-04-16 22:03:03 +02:00
|
|
|
Test searching for multiple packages at once.
|
2011-04-16 18:00:10 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
reset_env()
|
2011-04-16 22:03:03 +02:00
|
|
|
output = run_pip('search', 'pip', 'INITools')
|
2011-04-16 18:00:10 +02:00
|
|
|
assert 'pip installs packages' in output.stdout
|
2011-04-16 22:03:03 +02:00
|
|
|
assert 'Tools for parsing and using INI-style files' in output.stdout
|
2011-04-16 18:00:10 +02:00
|
|
|
|
|
|
|
|
2010-07-02 14:27:22 +02:00
|
|
|
def test_searching_through_Search_class():
|
|
|
|
"""
|
|
|
|
Verify if ``pip.vcs.Search`` uses tests xmlrpclib.Transport class
|
|
|
|
"""
|
2011-08-02 19:16:18 +02:00
|
|
|
original_xmlrpclib_transport = pip.download.xmlrpclib_transport
|
2010-07-02 14:27:22 +02:00
|
|
|
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'))
|
2010-07-02 14:27:22 +02:00
|
|
|
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')
|
2011-08-02 19:16:18 +02:00
|
|
|
try:
|
|
|
|
assert expected == result, result
|
|
|
|
fake_transport.request.assert_called_with('pypi.python.org', '/pypi', dumped_xmlrpc_request, verbose=VERBOSE_FALSE)
|
|
|
|
finally:
|
|
|
|
pip.download.xmlrpclib_transport = original_xmlrpclib_transport
|
2011-07-20 20:46:16 +02:00
|
|
|
|
2011-08-04 17:09:38 +02:00
|
|
|
|
2011-07-20 20:46:16 +02:00
|
|
|
def test_search_missing_argument():
|
|
|
|
"""
|
|
|
|
Test missing required argument for search
|
|
|
|
"""
|
|
|
|
env = reset_env(use_distribute=True)
|
2011-07-20 21:04:11 +02:00
|
|
|
result = run_pip('search', expect_error=True)
|
|
|
|
assert 'ERROR: Missing required argument (search query).' in result.stdout
|
2011-08-02 19:16:18 +02:00
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2011-08-02 19:16:18 +02:00
|
|
|
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()
|
|
|
|
status = search_cmd.run(options_mock, ('pip',))
|
|
|
|
assert status == SUCCESS
|
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2011-08-02 19:16:18 +02:00
|
|
|
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 = 'http://pypi.python.org/pypi'
|
|
|
|
search_cmd = SearchCommand()
|
|
|
|
status = search_cmd.run(options_mock, ('non-existant-package',))
|
|
|
|
assert status == NO_MATCHES_FOUND, status
|
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2011-08-02 19:16:18 +02:00
|
|
|
def test_search_should_exit_status_code_zero_when_find_packages():
|
|
|
|
"""
|
|
|
|
Test search exit status code for package found
|
|
|
|
"""
|
|
|
|
env = reset_env(use_distribute=True)
|
|
|
|
result = run_pip('search', 'pip')
|
|
|
|
assert result.returncode == SUCCESS
|
|
|
|
|
2012-01-30 07:26:12 +01:00
|
|
|
|
2011-08-02 19:16:18 +02:00
|
|
|
def test_search_exit_status_code_when_finds_no_package():
|
|
|
|
"""
|
|
|
|
Test search exit status code for no matches
|
|
|
|
"""
|
|
|
|
env = reset_env(use_distribute=True)
|
|
|
|
result = run_pip('search', 'non-existant-package', expect_error=True)
|
|
|
|
assert result.returncode == NO_MATCHES_FOUND
|