From 861ba347b231cb4edf249e92f8adb7823c987b91 Mon Sep 17 00:00:00 2001 From: Alexander Maretskiy Date: Mon, 18 May 2015 14:28:39 +0300 Subject: [PATCH] [Clients] Add test for osclients.cached osclients.cached missed unit test, so this patch adds one. Also, there is a small refactoring in this function. Change-Id: I76301fbfb4abb8323b5d76888f93984d23981d5b --- rally/osclients.py | 6 ++---- tests/unit/test_osclients.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/rally/osclients.py b/rally/osclients.py index 4ab5076413..4a990b1e6e 100644 --- a/rally/osclients.py +++ b/rally/osclients.py @@ -46,10 +46,8 @@ def cached(func): key = "{0}{1}{2}".format(func.__name__, str(args) if args else "", str(kwargs) if kwargs else "") - - if key in self.cache: - return self.cache[key] - self.cache[key] = func(self, *args, **kwargs) + if key not in self.cache: + self.cache[key] = func(self, *args, **kwargs) return self.cache[key] return wrapper diff --git a/tests/unit/test_osclients.py b/tests/unit/test_osclients.py index 58bdf3ed87..4ef07eee03 100644 --- a/tests/unit/test_osclients.py +++ b/tests/unit/test_osclients.py @@ -26,6 +26,28 @@ from tests.unit import fakes from tests.unit import test +class CachedTestCase(test.TestCase): + + def test_cached(self): + foo_client = mock.Mock( + __name__="foo_client", + side_effect=lambda ins, *args, **kw: (args, kw)) + ins = mock.Mock(cache={}) + cached = osclients.cached(foo_client) + self.assertEqual(((), {}), cached(ins)) + self.assertEqual({"foo_client": ((), {})}, ins.cache) + self.assertEqual((("foo",), {"bar": "spam"}), + cached(ins, "foo", bar="spam")) + self.assertEqual( + {"foo_client": ((), {}), + "foo_client('foo',){'bar': 'spam'}": (("foo",), + {"bar": "spam"})}, + ins.cache) + ins.cache["foo_client('foo',){'bar': 'spam'}"] = "foo_cached" + self.assertEqual( + "foo_cached", cached(ins, "foo", bar="spam")) + + class TestCreateKeystoneClient(test.TestCase): def setUp(self):