horizon/horizon/test/unit/utils/test_memoized.py

117 lines
4.0 KiB
Python

# 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 horizon.test import helpers as test
from horizon.utils import memoized
class MemoizedTests(test.TestCase):
def test_memoized_decorator_cache_on_next_call(self):
values_list = []
@memoized.memoized
def cache_calls(remove_from):
values_list.append(remove_from)
return True
def non_cached_calls(remove_from):
values_list.append(remove_from)
return True
for x in range(0, 5):
non_cached_calls(1)
self.assertEqual(5, len(values_list))
values_list = []
for x in range(0, 5):
cache_calls(1)
self.assertEqual(1, len(values_list))
def test_memoized_with_request_call(self):
chorus = [
"I",
"Love",
"Rock 'n' Roll",
"put another coin",
"in the Jukebox Baby."
]
leader = 'Joan Jett'
group = 'Blackhearts'
for position, chorus_line in enumerate(chorus):
changed_args = False
def some_func(some_param):
if not changed_args:
self.assertEqual(some_param, chorus_line)
else:
self.assertNotEqual(some_param, chorus_line)
self.assertEqual(some_param, group)
return leader
@memoized.memoized_with_request(some_func, position)
def some_other_func(*args):
return args
# check chorus_copy[position] is replaced by some_func's
# output
output1 = some_other_func(*chorus)
self.assertEqual(output1[position], leader)
# Change args used to call the function
chorus_copy = list(chorus)
chorus_copy[position] = group
changed_args = True
# check that some_func is called with a different parameter, and
# that check chorus_copy[position] is replaced by some_func's
# output and some_other_func still called with the same parameters
output2 = some_other_func(*chorus_copy)
self.assertEqual(output2[position], leader)
# check that some_other_func returned a memoized list.
self.assertIs(output1, output2)
def test_memoized_with_argcnv(self):
value_list = []
def converter(*args, **kwargs):
new_args = tuple(reversed(args))
new_kwargs = dict((k, v + 1) for k, v in kwargs.items())
return new_args, new_kwargs
@memoized.memoized_with_argconv(converter)
def target_func(*args, **kwargs):
value_list.append(1)
return args, kwargs
for i in range(3):
ret_args, ret_kwargs = target_func(1, 2, 3)
self.assertEqual((3, 2, 1), ret_args)
self.assertEqual({}, ret_kwargs)
self.assertEqual(1, len(value_list))
value_list = []
for i in range(3):
ret_args, ret_kwargs = target_func(a=1, b=2, c=3)
self.assertEqual(tuple(), ret_args)
self.assertEqual({'a': 2, 'b': 3, 'c': 4}, ret_kwargs)
self.assertEqual(1, len(value_list))
value_list = []
for i in range(3):
ret_args, ret_kwargs = target_func(1, 2, a=3, b=4)
self.assertEqual((2, 1), ret_args)
self.assertEqual({'a': 4, 'b': 5}, ret_kwargs)
self.assertEqual(1, len(value_list))