[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
This commit is contained in:
parent
b4417f5e67
commit
861ba347b2
@ -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
|
||||
|
@ -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):
|
||||
|
Loading…
Reference in New Issue
Block a user