Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'list_packages' into 0.0-dev
- Loading branch information
Showing
5 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
Commands related to searching for packages in a Python repository. | ||
""" | ||
from gettext import gettext as _ | ||
|
||
from pulp.client.commands import options | ||
from pulp.client.commands.criteria import DisplayUnitAssociationsCommand | ||
|
||
from pulp_python.common import constants | ||
|
||
|
||
DESC_SEARCH = _('search for packages in a repository') | ||
|
||
|
||
class PackagesCommand(DisplayUnitAssociationsCommand): | ||
""" | ||
This command is used to search for existing Python packages in a repository. | ||
""" | ||
def __init__(self, context): | ||
""" | ||
Initialize the command. | ||
:param context: The CLI context | ||
:type context: pulp.client.extensions.core.ClientContext | ||
""" | ||
super(PackagesCommand, self).__init__(self.run, name='packages', | ||
description=DESC_SEARCH) | ||
self.context = context | ||
self.prompt = context.prompt | ||
|
||
def run(self, **kwargs): | ||
""" | ||
Query the server for the packages, and render them for the user. | ||
:param kwargs: The CLI options passed by the user | ||
:type kwargs: dict | ||
""" | ||
# Retrieve the packages | ||
repo_id = kwargs.pop(options.OPTION_REPO_ID.keyword) | ||
kwargs['type_ids'] = [constants.PACKAGE_TYPE_ID] | ||
packages = self.context.server.repo_unit.search(repo_id, **kwargs).response_body | ||
|
||
order = [] | ||
|
||
if not kwargs.get(self.ASSOCIATION_FLAG.keyword): | ||
packages = [m['metadata'] for m in packages] | ||
# Make sure the key info is at the top; the rest can be alpha | ||
order = ['name', 'version', 'author'] | ||
|
||
self.prompt.render_document_list(packages, order=order) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
""" | ||
This module contains tests for the pulp_python.extensions.admin.packages module. | ||
""" | ||
import unittest | ||
|
||
from pulp.client.commands import options | ||
import mock | ||
|
||
from pulp_python.common import constants | ||
from pulp_python.extensions.admin import packages | ||
|
||
|
||
class TestPackagesCommand(unittest.TestCase): | ||
""" | ||
This class contains tests for the PackagesCommand class. | ||
""" | ||
@mock.patch('pulp_python.extensions.admin.packages.DisplayUnitAssociationsCommand.__init__') | ||
def test___init__(self, super___init__): | ||
""" | ||
Assert correct behavior from __init__(). | ||
""" | ||
context = mock.MagicMock() | ||
|
||
c = packages.PackagesCommand(context) | ||
|
||
super___init__.assert_called_once_with(c.run, name='packages', | ||
description=packages.DESC_SEARCH) | ||
self.assertEqual(c.context, context) | ||
self.assertEqual(c.prompt, context.prompt) | ||
|
||
def test_run_with_details(self): | ||
""" | ||
Test the run() method with the details flag. | ||
""" | ||
_packages = ['package_1', 'package_2'] | ||
|
||
def search(_repo_id, **_kwargs): | ||
""" | ||
Fakes the server search. | ||
""" | ||
self.assertEqual(_repo_id, 'some_repo') | ||
self.assertEqual( | ||
_kwargs, | ||
{'type_ids': [constants.PACKAGE_TYPE_ID], c.ASSOCIATION_FLAG.keyword: True}) | ||
|
||
response = mock.MagicMock() | ||
response.response_body = _packages | ||
return response | ||
|
||
context = mock.MagicMock() | ||
context.server.repo_unit.search.side_effect = search | ||
c = packages.PackagesCommand(context) | ||
kwargs = {options.OPTION_REPO_ID.keyword: 'some_repo', c.ASSOCIATION_FLAG.keyword: True} | ||
|
||
c.run(**kwargs) | ||
|
||
c.prompt.render_document_list.assert_called_once_with(_packages, order=[]) | ||
|
||
def test_run_without_details(self): | ||
""" | ||
Test the run() method without the details flag. | ||
""" | ||
_packages = [{'metadata': 'package_1'}, {'metadata': 'package_2'}] | ||
|
||
def search(_repo_id, **_kwargs): | ||
""" | ||
Fakes the server search. | ||
""" | ||
self.assertEqual(_repo_id, 'some_repo') | ||
self.assertEqual( | ||
_kwargs, | ||
{'type_ids': [constants.PACKAGE_TYPE_ID]}) | ||
|
||
response = mock.MagicMock() | ||
response.response_body = _packages | ||
return response | ||
|
||
context = mock.MagicMock() | ||
context.server.repo_unit.search.side_effect = search | ||
c = packages.PackagesCommand(context) | ||
kwargs = {options.OPTION_REPO_ID.keyword: 'some_repo'} | ||
|
||
c.run(**kwargs) | ||
|
||
c.prompt.render_document_list.assert_called_once_with(['package_1', 'package_2'], | ||
order=['name', 'version', 'author']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters