def setUp(self) -> None: self.patch_read = patch.object(student_service, 'read', wraps=student_service.read).start() self.cached_read = Cached(key='{args[0]}')(self.patch_read) self.patch_update = patch.object(student_service, 'update', wraps=student_service.update).start() self.cached_update = CacheUpdate(key='{args[0]}')(self.patch_update)
def test_can_create_key_from_object(self): cached_key = None def set_cached_key(key: str, _): nonlocal cached_key cached_key = key cached_method = Cached( key='duration({started_at.day},{finished_at.day})', on_miss=set_cached_key, )(self.duration) cached_method(datetime(2020, 1, 1), datetime(2020, 1, 2)) self.assertEqual('duration(1,2)', cached_key)
def test_can_create_key_from_dict(self): cached_key = None def set_cached_key(key: str, _): nonlocal cached_key cached_key = key cached_method = Cached( key='join_name({a["name"]},{b["name"]})', on_miss=set_cached_key, )(self.join_name) cached_method({'id': 0, 'name': 'a'}, {'id': 1, 'name': 'b'}) self.assertEqual('join_name(a,b)', cached_key)
def test_can_create_key_from_primitive(self): cached_key = None def set_cached_key(key: str, _): nonlocal cached_key cached_key = key cached_method = Cached( key='sum({a},{b})', on_miss=set_cached_key, )(self.sum) cached_method(1, 2) self.assertEqual('sum(1,2)', cached_key)
def test_can_create_key_from_list_or_tuple(self): cached_key = None def set_cached_key(key: str, _): nonlocal cached_key cached_key = key cached_method = Cached( key='is_lowest({values[0]},{values[-1]})', on_miss=set_cached_key, )(self.is_lowest) cached_method([0, 1, 2, 3]) self.assertEqual('is_lowest(0,3)', cached_key) cached_method((0, 1, 2, 3, 4, 5, 6)) self.assertEqual('is_lowest(0,6)', cached_key)