From be3dc4ee6a0f55d5b48384c9b8118f7f4e52eb80 Mon Sep 17 00:00:00 2001 From: Jiri Stransky Date: Wed, 10 Jul 2013 16:30:17 +0200 Subject: [PATCH 1/3] Add _get function to base.Manager This is useful so that we don't have to use _list in each Manager subclass for getting a single resource and then fetching first item of the array and checking for index error. This method wraps it for us. --- tuskarclient/common/base.py | 7 ++++ tuskarclient/tests/common/__init__.py | 0 tuskarclient/tests/common/test_base.py | 44 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 tuskarclient/tests/common/__init__.py create mode 100644 tuskarclient/tests/common/test_base.py diff --git a/tuskarclient/common/base.py b/tuskarclient/common/base.py index 144023d..beb0c4a 100644 --- a/tuskarclient/common/base.py +++ b/tuskarclient/common/base.py @@ -52,6 +52,13 @@ class Manager(object): if body: return self.resource_class(self, body) + def _get(self, url, **kwargs): + kwargs.setdefault('expect_single', True) + try: + return self._list(url, **kwargs)[0] + except IndexError: + return None + def _list(self, url, response_key=None, obj_class=None, body=None, expect_single=False): resp, body = self.api.json_request('GET', url) diff --git a/tuskarclient/tests/common/__init__.py b/tuskarclient/tests/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tuskarclient/tests/common/test_base.py b/tuskarclient/tests/common/test_base.py new file mode 100644 index 0000000..dd35939 --- /dev/null +++ b/tuskarclient/tests/common/test_base.py @@ -0,0 +1,44 @@ +# 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 tuskarclient.tests import utils as tutils +from tuskarclient.common import base + + +class ManagerTest(tutils.TestCase): + + def setUp(self): + super(ManagerTest, self).setUp() + self.api = mock.Mock() + self.m = base.Manager(self.api) + + def test_get(self): + self.m._list = mock.Mock(return_value=['fake_resource']) + got = self.m._get('url', response_key='response_key', + obj_class='obj_class', body='body') + + self.assertEqual('fake_resource', got) + self.m._list.assert_called_with('url', response_key='response_key', + obj_class='obj_class', + body='body', expect_single=True) + + def test_get_nonexistent(self): + self.m._list = mock.Mock(return_value=[]) + got = self.m._get('url', response_key='response_key', + obj_class='obj_class', body='body') + + self.assertEqual(None, got) + self.m._list.assert_called_with('url', response_key='response_key', + obj_class='obj_class', + body='body', expect_single=True) From 9dc11442f1db883426dcdbfcd63424708c24ebbd Mon Sep 17 00:00:00 2001 From: Jiri Stransky Date: Tue, 9 Jul 2013 13:56:25 +0200 Subject: [PATCH 2/3] Rack class Currently just inherits from Resource and doesn't contain anything testable really. --- tuskarclient/v1_0/racks.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tuskarclient/v1_0/racks.py diff --git a/tuskarclient/v1_0/racks.py b/tuskarclient/v1_0/racks.py new file mode 100644 index 0000000..5df6ec1 --- /dev/null +++ b/tuskarclient/v1_0/racks.py @@ -0,0 +1,19 @@ +# 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. + +from tuskarclient.common import base + + +class Rack(base.Resource): + + def __repr__(self): + return "".format(self._info) From 04bf5f409dd9b6a941a6859d343ecaf4f1761b5f Mon Sep 17 00:00:00 2001 From: Jiri Stransky Date: Wed, 10 Jul 2013 15:15:24 +0200 Subject: [PATCH 3/3] RackManager class --- tuskarclient/tests/v1_0/__init__.py | 0 tuskarclient/tests/v1_0/test_racks.py | 36 +++++++++++++++++++++++++++ tuskarclient/v1_0/racks.py | 15 +++++++++++ 3 files changed, 51 insertions(+) create mode 100644 tuskarclient/tests/v1_0/__init__.py create mode 100644 tuskarclient/tests/v1_0/test_racks.py diff --git a/tuskarclient/tests/v1_0/__init__.py b/tuskarclient/tests/v1_0/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tuskarclient/tests/v1_0/test_racks.py b/tuskarclient/tests/v1_0/test_racks.py new file mode 100644 index 0000000..edc5da7 --- /dev/null +++ b/tuskarclient/tests/v1_0/test_racks.py @@ -0,0 +1,36 @@ +# 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 tuskarclient.v1_0 import racks +import tuskarclient.tests.utils as tutils + + +class RackManagerTest(tutils.TestCase): + + def setUp(self): + super(RackManagerTest, self).setUp() + self.api = mock.Mock() + self.rm = racks.RackManager(self.api) + + def test_get(self): + self.rm._get = mock.Mock(return_value='fake_rack') + + self.assertEqual(self.rm.get(42), 'fake_rack') + self.rm._get.assert_called_with('/v1/racks/42') + + def test_list(self): + self.rm._list = mock.Mock(return_value=['fake_rack']) + + self.assertEqual(self.rm.list(), ['fake_rack']) + self.rm._list.assert_called_with('/v1/racks') diff --git a/tuskarclient/v1_0/racks.py b/tuskarclient/v1_0/racks.py index 5df6ec1..14e06d7 100644 --- a/tuskarclient/v1_0/racks.py +++ b/tuskarclient/v1_0/racks.py @@ -17,3 +17,18 @@ class Rack(base.Resource): def __repr__(self): return "".format(self._info) + + +class RackManager(base.Manager): + + resource_class = Rack + + @staticmethod + def _path(id=None): + return '/v1/racks/%s' % id if id else '/v1/racks' + + def get(self, rack_id): + return self._get(self._path(rack_id)) + + def list(self): + return self._list(self._path())