diff options
author | Ihar Hrachyshka <ihrachys@redhat.com> | 2016-05-18 19:19:48 +0200 |
---|---|---|
committer | Ihar Hrachyshka <ihrachys@redhat.com> | 2016-05-31 12:23:13 +0200 |
commit | 5decc850f126d74b89ec79593afcc68b9e8988e6 (patch) | |
tree | d2a14213dc65c63c02eeaacde02fa3292eb9696d /neutron/tests | |
parent | 067a5c2a47026fccd09ce10e45eb16f5f9310234 (diff) | |
download | neutron-5decc850f126d74b89ec79593afcc68b9e8988e6.tar.gz |
objects: support advanced criteria for get_objects
Those are needed to accommodate to API request needs without handling
sorting or pagination in Python.
Instead of adding four new arguments to get_objects interface, they are
consolidated using a single Pager object that is passed through the
_pager argument. The name uses underscore to avoid breaking objects that
want to pass 'pager' filter into SQLAlchemy. Hopefully, no objects will
ever have '_pager' attribute in their models.
Related-Bug: #1541928
Change-Id: I7dafc4dbd80f0ac35dbc2c2f30e56e441f5b1fc0
Diffstat (limited to 'neutron/tests')
-rw-r--r-- | neutron/tests/unit/objects/db/__init__.py | 0 | ||||
-rw-r--r-- | neutron/tests/unit/objects/db/test_api.py | 49 | ||||
-rw-r--r-- | neutron/tests/unit/objects/qos/test_policy.py | 4 | ||||
-rw-r--r-- | neutron/tests/unit/objects/test_base.py | 27 | ||||
-rw-r--r-- | neutron/tests/unit/plugins/ml2/test_plugin.py | 4 |
5 files changed, 78 insertions, 6 deletions
diff --git a/neutron/tests/unit/objects/db/__init__.py b/neutron/tests/unit/objects/db/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/neutron/tests/unit/objects/db/__init__.py diff --git a/neutron/tests/unit/objects/db/test_api.py b/neutron/tests/unit/objects/db/test_api.py new file mode 100644 index 0000000000..2f967d62b5 --- /dev/null +++ b/neutron/tests/unit/objects/db/test_api.py @@ -0,0 +1,49 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock + +from neutron import context +from neutron import manager +from neutron.objects import base +from neutron.objects.db import api +from neutron.tests import base as test_base + + +PLUGIN_NAME = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2' + + +class GetObjectsTestCase(test_base.BaseTestCase): + + def setUp(self): + super(GetObjectsTestCase, self).setUp() + # TODO(ihrachys): revisit plugin setup once we decouple + # objects.db.objects.api from core plugin instance + self.setup_coreplugin(PLUGIN_NAME) + + def test_get_objects_pass_marker_obj_when_limit_and_marker_passed(self): + ctxt = context.get_admin_context() + model = mock.sentinel.model + marker = mock.sentinel.marker + limit = mock.sentinel.limit + pager = base.Pager(marker=marker, limit=limit) + + plugin = manager.NeutronManager.get_plugin() + with mock.patch.object(plugin, '_get_collection') as get_collection: + with mock.patch.object(api, 'get_object') as get_object: + api.get_objects(ctxt, model, _pager=pager) + get_object.assert_called_with(ctxt, model, id=marker) + get_collection.assert_called_with( + ctxt, model, mock.ANY, + filters={}, + limit=limit, + marker_obj=get_object.return_value) diff --git a/neutron/tests/unit/objects/qos/test_policy.py b/neutron/tests/unit/objects/qos/test_policy.py index cb60901b8c..290e9c9e0c 100644 --- a/neutron/tests/unit/objects/qos/test_policy.py +++ b/neutron/tests/unit/objects/qos/test_policy.py @@ -65,7 +65,7 @@ class QosPolicyObjectTestCase(test_base.BaseObjectIfaceTestCase): objs = self._test_class.get_objects(self.context) context_mock.assert_called_once_with() self.get_objects.assert_any_call( - admin_context, self._test_class.db_model) + admin_context, self._test_class.db_model, _pager=None) self._validate_objects(self.db_objs, objs) def test_get_objects_valid_fields(self): @@ -85,7 +85,7 @@ class QosPolicyObjectTestCase(test_base.BaseObjectIfaceTestCase): **self.valid_field_filter) context_mock.assert_called_once_with() get_objects_mock.assert_any_call( - admin_context, self._test_class.db_model, + admin_context, self._test_class.db_model, _pager=None, **self.valid_field_filter) self._validate_objects([self.db_obj], objs) diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index 9ed3a2727e..5ffdaa448a 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -364,7 +364,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): field].objname)[0] mock_calls.append( mock.call( - self.context, obj_class.db_model, + self.context, obj_class.db_model, _pager=None, **{k: db_obj[v] for k, v in obj_class.foreign_keys.items()})) return mock_calls @@ -375,7 +375,9 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): side_effect=self.fake_get_objects) as get_objects_mock: objs = self._test_class.get_objects(self.context) self._validate_objects(self.db_objs, objs) - mock_calls = [mock.call(self.context, self._test_class.db_model)] + mock_calls = [ + mock.call(self.context, self._test_class.db_model, _pager=None) + ] mock_calls.extend(self._get_synthetic_fields_get_objects_calls( self.db_objs)) get_objects_mock.assert_has_calls(mock_calls) @@ -389,8 +391,10 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): **self.valid_field_filter) self._validate_objects(self.db_objs, objs) - mock_calls = [mock.call(self.context, self._test_class.db_model, - **self.valid_field_filter)] + mock_calls = [ + mock.call(self.context, self._test_class.db_model, _pager=None, + **self.valid_field_filter) + ] mock_calls.extend(self._get_synthetic_fields_get_objects_calls( [self.db_obj])) get_objects_mock.assert_has_calls(mock_calls) @@ -601,6 +605,13 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase): dict_ = obj.to_dict() self.assertEqual(child_dict, dict_[field]) + def test_get_objects_pager_is_passed_through(self): + with mock.patch.object(obj_db_api, 'get_objects') as get_objects: + pager = base.Pager() + self._test_class.get_objects(self.context, _pager=pager) + get_objects.assert_called_once_with( + mock.ANY, self._test_class.db_model, _pager=pager) + class BaseDbObjectNonStandardPrimaryKeyTestCase(BaseObjectIfaceTestCase): @@ -635,6 +646,14 @@ class BaseDbObjectMultipleForeignKeysTestCase(_BaseObjectTestCase, class BaseDbObjectTestCase(_BaseObjectTestCase): + CORE_PLUGIN = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2' + + def setUp(self): + super(BaseDbObjectTestCase, self).setUp() + # TODO(ihrachys): revisit plugin setup once we decouple + # neutron.objects.db.api from core plugin instance + self.setup_coreplugin(self.CORE_PLUGIN) + def _create_test_network(self): # TODO(ihrachys): replace with network.create() once we get an object # implementation for networks diff --git a/neutron/tests/unit/plugins/ml2/test_plugin.py b/neutron/tests/unit/plugins/ml2/test_plugin.py index 64241ae44f..60f9e3ea0b 100644 --- a/neutron/tests/unit/plugins/ml2/test_plugin.py +++ b/neutron/tests/unit/plugins/ml2/test_plugin.py @@ -2031,8 +2031,12 @@ class TestML2PluggableIPAM(test_ipam.UseIpamMixin, TestMl2SubnetsV2): class TestMl2PluginCreateUpdateDeletePort(base.BaseTestCase): + def setUp(self): super(TestMl2PluginCreateUpdateDeletePort, self).setUp() + # TODO(ihrachys): revisit plugin setup once we decouple + # neutron.objects.db.api from core plugin instance + self.setup_coreplugin(PLUGIN_NAME) self.context = mock.MagicMock() self.notify_p = mock.patch('neutron.callbacks.registry.notify') self.notify = self.notify_p.start() |