def test_clear_cache_from_errors_with_kwargs(a, b, c): def partial_pow(p): if not isinstance(p, int): raise ValueError("should be int") def my_pow(i): return i**p return my_pow pow = Step(partial_pow, p=b) expr = Step(sorted, a, key=pow) * (c+1) do_eval(expr, a=[1, 3, -2], b='2', c=1) clear_cache(expr) to_dict(expr) assert do_eval(expr, a=[1, 3, -2], b=2, c=1) == [1, -2, 3, 1, -2, 3]
def test_caching_results(a): num_of_executions = 0 def echo_increase(x): nonlocal num_of_executions num_of_executions +=1 return x+1 s = Step(echo_increase, a) assert do_eval(s, a=1) == 2 assert do_eval(s, a=2) == 2 clear_cache(s, force=True) assert do_eval(s, a=2) == 3 assert num_of_executions == 2
def test_resect_part_of_the_DAG(a, b, c, d): r = a*d + b*c r2 = do_eval(r, a=1, c=3, d=4) r3 = 4+b*3 assert are_equal(r2, r3) def _replace_idx(obj, idx, value): obj = list(obj) obj[idx] = value return tuple(obj) assert do_eval(r3, b=1) == 7 clear_cache(r3, force=True) r3._args[1] = b*4 assert do_eval(r3, b=1) == 8
def test_clear_cache_from_errors(): a = Step('a') b = Step('b') expr = 1/b + 1/a do_eval(expr, a=0, b=1) expected_1 = {Tokens.FUNCTION_IDX: op.add, Tokens.CACHE_IDX: TypeError("unsupported operand type(s) for +: 'int' and 'ZeroDivisionError'"), 0: {Tokens.FUNCTION_IDX: op.truediv, Tokens.CACHE_IDX: 1, 0: 1, 1: {Tokens.FUNCTION_IDX: 'b', Tokens.CACHE_IDX: Tokens.NO_PREVIOUS_RESULT, } }, 1: {Tokens.FUNCTION_IDX: op.truediv, Tokens.CACHE_IDX: ZeroDivisionError('division by zero'), 0: 1, 1: {Tokens.FUNCTION_IDX: 'a', Tokens.CACHE_IDX: Tokens.NO_PREVIOUS_RESULT, } } } assert are_equal(from_dict(expected_1), from_dict(expected_1)) assert are_equal(expr, from_dict(expected_1)) clean_expr = clear_cache(expr) expected_2 = {Tokens.FUNCTION_IDX: op.add, Tokens.CACHE_IDX: Tokens.NO_PREVIOUS_RESULT, 0: {Tokens.FUNCTION_IDX: op.truediv, Tokens.CACHE_IDX: 1, 0: 1, 1: {Tokens.FUNCTION_IDX: 'b', Tokens.CACHE_IDX: Tokens.NO_PREVIOUS_RESULT, } }, 1: {Tokens.FUNCTION_IDX: op.truediv, Tokens.CACHE_IDX: Tokens.NO_PREVIOUS_RESULT, 0: 1, 1: {Tokens.FUNCTION_IDX: 'a', Tokens.CACHE_IDX: Tokens.NO_PREVIOUS_RESULT, } } } assert are_equal(clean_expr, from_dict(expected_2))
def test_clear_cache_not_action_without_errors(a): expr = a+1 res_1 = do_eval(expr, a=2) clear_cache(expr) assert expr._last_result is res_1