def _generate_search_criteria(**kwargs): """ This composes arguments to a UnitAssociationCriteria, which is different and more complex than a normal Criteria. :param kwargs: options input by the user on the CLI and passed through by okaara :type kwargs: dict :return: dict of options that can be sent to the REST API representing a UnitAssociationCriteria :rtype: dict """ criteria = { 'filters': {'unit': SearchAPI.compose_filters(**kwargs)}, 'type_ids' : kwargs['type_ids'], } # build the association filters association_fake_kwargs = {} for arg, operator in (('after', 'str-gte'), ('before', 'str-lte')): value = kwargs.pop(arg, None) if value: association_fake_kwargs[operator] = [('created', value)] # use compose_filters() to create the actual mongo spec, which requires # simulating the **kwargs that okaara would pass in. if association_fake_kwargs: criteria['filters']['association'] = SearchAPI.compose_filters(**association_fake_kwargs) return criteria
class TestSearchAPI(unittest.TestCase): def setUp(self): super(TestSearchAPI, self).setUp() self.api = SearchAPI(mock.MagicMock()) self.api.PATH = '/some/path' def test_calls_post(self): self.api.search(limit=12) self.assertEqual(self.api.server.POST.call_count, 1) self.assertEqual(self.api.server.POST.call_args[0][0], '/some/path') self.assertEqual(self.api.server.POST.call_args[0][1], {'criteria': { 'limit': 12 }}) def test_returns_response_body(self): ret = self.api.search() self.assertEqual(ret, self.api.server.POST.return_value.response_body) def test_invalid_kwargs(self): self.assertRaises(ValueError, self.api.search, foo=True) @mock.patch('pulp.bindings.search.SearchAPI.compose_filters') def test_calls_compose(self, mock_compose): self.api.search(limit=20) mock_compose.assert_called_once_with(limit=20) def test_remove_non_criteria(self): self.api.search(gt=[('count', 20)]) spec = self.api.server.POST.call_args[0][1]['criteria'] self.assertTrue('gt' not in spec) self.assertTrue('filters' in spec) def test_compose_prefers_filters(self): kwargs = {'filters': '{}', 'gt': ['count=20']} ret = self.api.compose_filters(**kwargs) self.assertTrue('gt' not in ret) self.assertEqual(ret, '{}') @mock.patch('pulp.bindings.search.Operator.compose_filters') def test_compose_calls_operator(self, mock_compose): self.api.compose_filters(gt=['count=20']) mock_compose.assert_called_once_with(['count=20'])
class TestSearchAPI(unittest.TestCase): def setUp(self): super(TestSearchAPI, self).setUp() self.api = SearchAPI(mock.MagicMock()) self.api.PATH = '/some/path' def test_calls_post(self): self.api.search(limit=12) self.assertEqual(self.api.server.POST.call_count, 1) self.assertEqual(self.api.server.POST.call_args[0][0], '/some/path') self.assertEqual(self.api.server.POST.call_args[0][1], {'criteria': {'limit': 12}}) def test_returns_response_body(self): ret = self.api.search() self.assertEqual(ret, self.api.server.POST.return_value.response_body) def test_invalid_kwargs(self): self.assertRaises(ValueError, self.api.search, foo=True) @mock.patch('pulp.bindings.search.SearchAPI.compose_filters') def test_calls_compose(self, mock_compose): self.api.search(limit=20) mock_compose.assert_called_once_with(limit=20) def test_remove_non_criteria(self): self.api.search(gt=[('count', 20)]) spec = self.api.server.POST.call_args[0][1]['criteria'] self.assertTrue('gt' not in spec) self.assertTrue('filters' in spec) def test_compose_prefers_filters(self): kwargs = {'filters': '{}', 'gt': ['count=20']} ret = self.api.compose_filters(**kwargs) self.assertTrue('gt' not in ret) self.assertEqual(ret, '{}') @mock.patch('pulp.bindings.search.Operator.compose_filters') def test_compose_calls_operator(self, mock_compose): self.api.compose_filters(gt=['count=20']) mock_compose.assert_called_once_with(['count=20'])
def _generate_search_criteria(**kwargs): """ This composes arguments to a UnitAssociationCriteria, which is different and more complex than a normal Criteria. :param kwargs: options input by the user on the CLI and passed through by okaara :type kwargs: dict :return: dict of options that can be sent to the REST API representing a UnitAssociationCriteria :rtype: dict """ criteria = { 'filters': { 'unit': SearchAPI.compose_filters(**kwargs) }, 'type_ids': kwargs.get('type_ids', None), } # allow a caller with type-specific knowledge to limit the fields that # are retrieved. for copy purposes, we probably don't need all of the # unit's attributes, and limiting which fields are retrieved can save # a lot of RAM if kwargs.get('fields'): criteria['fields'] = {'unit': kwargs.pop('fields')} # build the association filters association_fake_kwargs = {} for arg, operator in (('after', 'str-gte'), ('before', 'str-lte')): value = kwargs.pop(arg, None) if value: association_fake_kwargs[operator] = [('created', value)] # use compose_filters() to create the actual mongo spec, which requires # simulating the **kwargs that okaara would pass in. if association_fake_kwargs: criteria['filters']['association'] = SearchAPI.compose_filters( **association_fake_kwargs) return criteria
def _generate_search_criteria(**kwargs): """ This composes arguments to a UnitAssociationCriteria, which is different and more complex than a normal Criteria. :param kwargs: options input by the user on the CLI and passed through by okaara :type kwargs: dict :return: dict of options that can be sent to the REST API representing a UnitAssociationCriteria :rtype: dict """ criteria = { 'filters': {'unit': SearchAPI.compose_filters(**kwargs)}, 'type_ids': kwargs.get('type_ids', None), } # allow a caller with type-specific knowledge to limit the fields that # are retrieved. for copy purposes, we probably don't need all of the # unit's attributes, and limiting which fields are retrieved can save # a lot of RAM if kwargs.get('fields'): criteria['fields'] = {'unit': kwargs.pop('fields')} # build the association filters association_fake_kwargs = {} for arg, operator in (('after', 'str-gte'), ('before', 'str-lte')): value = kwargs.pop(arg, None) if value: association_fake_kwargs[operator] = [('created', value)] # use compose_filters() to create the actual mongo spec, which requires # simulating the **kwargs that okaara would pass in. if association_fake_kwargs: criteria['filters']['association'] = SearchAPI.compose_filters( **association_fake_kwargs) return criteria