mirror of
https://github.com/ansible/awx.git
synced 2026-03-01 08:48:46 -03:30
Refactor InventoryHostsList get_queryset to use getattrd
This commit is contained in:
@@ -1961,10 +1961,7 @@ class InventoryHostsList(SubListCreateAttachDetachAPIView):
|
|||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
inventory = self.get_parent_object()
|
inventory = self.get_parent_object()
|
||||||
if inventory.kind == 'smart':
|
return getattrd(inventory, self.relationship).all()
|
||||||
filter_qs = SmartFilter.query_from_string(inventory.host_filter)
|
|
||||||
return filter_qs.distinct()
|
|
||||||
return super(InventoryHostsList, self).get_queryset()
|
|
||||||
|
|
||||||
|
|
||||||
class HostGroupsList(ControlledByScmMixin, SubListCreateAttachDetachAPIView):
|
class HostGroupsList(ControlledByScmMixin, SubListCreateAttachDetachAPIView):
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ from awx.main.models import (
|
|||||||
Host,
|
Host,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from awx.main.managers import HostManager
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mock_response_new(mocker):
|
def mock_response_new(mocker):
|
||||||
@@ -210,10 +212,12 @@ class TestHostInsights():
|
|||||||
class TestInventoryHostsList(object):
|
class TestInventoryHostsList(object):
|
||||||
|
|
||||||
def test_host_list_smart_inventory(self, mocker):
|
def test_host_list_smart_inventory(self, mocker):
|
||||||
Inventory = namedtuple('Inventory', ['kind', 'host_filter'])
|
Inventory = namedtuple('Inventory', ['kind', 'host_filter', 'hosts'])
|
||||||
obj = Inventory(kind='smart', host_filter='localhost')
|
obj = Inventory(kind='smart', host_filter='localhost', hosts=HostManager())
|
||||||
|
obj.hosts.instance = obj
|
||||||
|
|
||||||
with mock.patch.object(InventoryHostsList, 'get_parent_object', return_value=obj):
|
with mock.patch.object(InventoryHostsList, 'get_parent_object', return_value=obj):
|
||||||
with mock.patch('awx.api.views.SmartFilter.query_from_string') as mock_query:
|
with mock.patch('awx.main.utils.filters.SmartFilter.query_from_string') as mock_query:
|
||||||
view = InventoryHostsList()
|
view = InventoryHostsList()
|
||||||
view.get_queryset()
|
view.get_queryset()
|
||||||
mock_query.assert_called_once_with('localhost')
|
mock_query.assert_called_once_with('localhost')
|
||||||
|
|||||||
Reference in New Issue
Block a user