Fix for caching of nonexisted test results for case
Add timeout option for some TestRail commands
Fix python warnings
PRODX-38927
Change-Id: I1e85889318a1d19e4346b1aaf63a1cfcd6fb11be
diff --git a/testrail_bot/control/utils.py b/testrail_bot/control/utils.py
index 6c7738a..2038aba 100644
--- a/testrail_bot/control/utils.py
+++ b/testrail_bot/control/utils.py
@@ -1,8 +1,6 @@
from parse import parse
from typing import Dict, List, Callable, Any
-from functools import wraps
from django.core.cache import cache
-import difflib
def parse_title(test_name):
@@ -63,18 +61,33 @@
return r
-def cached(timeout: int = None) -> Callable:
+def cached(timeout: int = None,
+ condition_for_endless_cache: Callable = lambda x: False
+ ) -> Callable:
+ """
+ :param timeout: (in seconds) usage accordingly
+ https://docs.djangoproject.com/en/4.2/topics/cache/#basic-usage
+ :param condition_for_endless_cache: Callable should return boolean.
+ Checks a result of function. If Result meets requirements of condition
+ then the endless timeout will be set. Or it will use provided timeout
+ otherwise
+
+ :return: decorator
+ """
def decorator(func: Callable) -> Callable:
def wrapper(*args, **kwargs) -> Any:
cache_key = f'{func.__name__}_{args}_{kwargs}'
cache_key = replace_all(cache_key, "{}()\'\" .,:", "_")
- result = cache.get(cache_key)
- if result is None:
+ cached_value = cache.get(cache_key)
+ if cached_value is None:
print(f"{func.__name__} MISS")
result = func(*args, **kwargs)
- cache.set(cache_key, result, timeout=timeout)
- return result
+ _timeout = None \
+ if condition_for_endless_cache(result) \
+ else timeout
+ cache.set(cache_key, result, timeout=_timeout)
+ return result
print(f"{func.__name__} hit")
# # FIXME Assert to test the caching mechanism
@@ -85,7 +98,7 @@
# assert result == _result
# # ENDFIXME
- return result
+ return cached_value
return wrapper
return decorator