Add find_unique base manager method

Allows to request a single matched item from list of resources,
raising NotFound if no match was found or Conflict if more than one
match was found.

Change-Id: Ib8b516d96de2b95ae20eb4545db12c0b82651941
Closes-Bug: #1414953
This commit is contained in:
Pavlo Shchelokovskyy
2015-01-27 12:31:36 +02:00
parent 7e1e609298
commit 0e56c7170c
4 changed files with 76 additions and 11 deletions

View File

@@ -69,6 +69,16 @@ class ResourceManager(object):
def find(self, **kwargs):
return [i for i in self.list() if _check_items(i, kwargs.items())]
def find_unique(self, **kwargs):
found = self.find(**kwargs)
if not found:
raise APIException(error_code=404,
error_message=_("No matches found."))
if len(found) > 1:
raise APIException(error_code=409,
error_message=_("Multiple matches found."))
return found[0]
def _copy_if_defined(self, data, **kwargs):
for var_name, var_value in six.iteritems(kwargs):
if var_value is not None:

View File

@@ -15,6 +15,7 @@
import six
import testtools
from saharaclient.api import base
from saharaclient.api import client
from requests_mock.contrib import fixture
@@ -34,3 +35,13 @@ class BaseTestCase(testtools.TestCase):
def assertFields(self, body, obj):
for key, value in six.iteritems(body):
self.assertEqual(value, getattr(obj, key))
class TestResource(base.Resource):
resource_name = 'Test Resource'
defaults = {'description': 'Test Description',
'extra': "extra"}
class TestManager(base.ResourceManager):
resource_class = TestResource

View File

@@ -0,0 +1,50 @@
# Copyright (c) 2015 Mirantis Inc.
#
# 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 saharaclient.api import base
from saharaclient.tests.unit import base as test_base
class ManagerTest(test_base.BaseTestCase):
def setUp(self):
super(ManagerTest, self).setUp()
self.man = test_base.TestManager(self.client)
def test_find(self):
self.man.list = mock.MagicMock(
return_value=[mock.Mock(test='foo'),
mock.Mock(test='bar')]
)
self.assertEqual(2, len(self.man.find()))
self.assertEqual(1, len(self.man.find(test='foo')))
self.assertEqual(0, len(self.man.find(test='baz')))
def test_find_unique(self):
expected = mock.Mock(test='foo')
self.man.list = mock.MagicMock(
return_value=[expected,
mock.Mock(test='bar')]
)
ex = self.assertRaises(base.APIException,
self.man.find_unique, test='baz')
self.assertEqual(404, ex.error_code)
ex = self.assertRaises(base.APIException, self.man.find_unique)
self.assertEqual(409, ex.error_code)
self.assertEqual(expected, self.man.find_unique(test='foo'))

View File

@@ -15,20 +15,20 @@
import testtools
from saharaclient.api import base
from saharaclient.tests.unit import base as test_base
class ResourceTest(testtools.TestCase):
def test_create_resource(self):
dict = {"name": "test"}
resource = TestResource(None, dict)
resource = test_base.TestResource(None, dict)
self.assertEqual("test", resource.name)
self.assertEqual("Test Description", resource.description)
def test_overwrite_default(self):
dict = {"name": "test",
"description": "Changed Description"}
resource = TestResource(None, dict)
resource = test_base.TestResource(None, dict)
self.assertEqual("test", resource.name)
self.assertEqual("Changed Description", resource.description)
self.assertEqual("extra", resource.extra)
@@ -37,14 +37,14 @@ class ResourceTest(testtools.TestCase):
dict = {"name": "test",
"description": "Changed Description"}
dict_copy = dict.copy()
resource = TestResource(None, dict)
resource = test_base.TestResource(None, dict)
self.assertIsNotNone(resource)
self.assertEqual(dict_copy, dict)
def test_resource_str(self):
dict = {"name": "test",
"description": "Changed Description"}
resource = TestResource(None, dict)
resource = test_base.TestResource(None, dict)
rstr = str(resource)
self.assertIn(resource.resource_name, rstr)
self.assertIn("name", rstr)
@@ -53,9 +53,3 @@ class ResourceTest(testtools.TestCase):
self.assertNotIn("Test Description", rstr)
self.assertIn("extra", rstr)
self.assertNotIn("manager", rstr)
class TestResource(base.Resource):
resource_name = 'Test Resource'
defaults = {'description': 'Test Description',
'extra': "extra"}