def test_cache(): ax = DummyA(1, 10) bx = DummyB(2, 2) cx = DummyC(3, 3) assert f(ax, bx, cx) == 50 assert h(bx, cx) == 5 assert score(ax) assert smache.is_fun_fresh(score, ax) == True assert smache.is_fun_fresh(f, ax, bx, cx) == True assert smache.is_fun_fresh(h, bx, cx) == True DummyB.update(0, {}) assert smache.is_fun_fresh(score, ax) == False assert smache.is_fun_fresh(f, ax, bx, cx) == True assert smache.is_fun_fresh(h, bx, cx) == True DummyA.update(1, {}) assert smache.is_fun_fresh(score, ax) == False assert smache.is_fun_fresh(f, ax, bx, cx) == False assert smache.is_fun_fresh(h, bx, cx) == True DummyB.update(2, {}) assert smache.is_fun_fresh(score, ax) == False assert smache.is_fun_fresh(f, ax, bx, cx) == False assert smache.is_fun_fresh(h, bx, cx) == False
def setup_module(module): global smache, score, h, f, with_raw DummyA.unsubscribe_all() DummyB.unsubscribe_all() DummyC.unsubscribe_all() smache = Smache(scheduler=InProcessScheduler(), write_through=False) @smache.sources(DummyB, DummyC) @smache.computed(DummyA) def score(a): return a.value + 5 + 10 @smache.computed(DummyB, DummyC) def h(b, c): return b.value + c.value @smache.computed(DummyA, DummyB, DummyC) def f(a, b, c): return a.value * h(b, c) @smache.computed(DummyA, DummyB, Raw) def with_raw(a, b, static_value): return (a.value + b.value) * static_value
def test_write_through_with_collection_wide_subscription(): assert slash() == 'hello/hihi' DummyA.update(2, {'value': 'lol'}) execute_all_jobs(worker_queue, redis_con) assert smache.function_cache_value(slash) == 'hello/lol'
def test_write_through(): ax = DummyA(1, 'hello') bx = DummyB(1, 'world') assert hyphen(ax, bx) == 'hello - world' assert slash() == 'hello/hihi' assert smache.is_fun_fresh(hyphen, ax, bx) == True DummyA.update(1, {'value': 'wtf'}) execute_all_jobs(worker_queue, redis_con) assert smache.is_fun_fresh(hyphen, ax, bx) == True assert smache.function_cache_value(hyphen, ax, bx) == 'wtf - world'
def test_serialization(): ax = DummyA(1, 10) bx = DummyB('2', 2) fun_serializer = FunctionSerializer() a = InMemoryDataSource(DummyA) b = InMemoryDataSource(DummyB) raw = RawDataSource() e = 'smache:functions:' \ '"tests.integration.test_function_serialization/score"' \ '~~~1~~~"2"~~~500' \ '***(dp0\nS\'resolved\'\np1\nI01\ns.' computed_fun = ComputedFunction(score, [a, b, raw]) key = fun_serializer.serialized_fun(computed_fun, ax, bx, 500, resolved=True) assert key == e expected_deserialization = ( "tests.integration.test_function_serialization/score", [1, '2', 500], {"resolved": True} ) assert fun_serializer.deserialized_fun(key) == expected_deserialization
def test_write_through(): ax = DummyA(1, 'hello') bx = DummyB(1, 'world') assert hyphen(ax, bx) == 'hello - world' assert slash() == 'hello/hihi' assert smache.is_fun_fresh(hyphen, ax, bx) == True DummyA.update(1, {'value': 'wtf'}) execute_all_jobs(worker_queue, redis_con) assert smache.is_fun_fresh(hyphen, ax, bx) == True assert smache.function_cache_value( hyphen, ax, bx ) == 'wtf - world'
def setup_module(module): global smache, f, h DummyA.unsubscribe_all() DummyB.unsubscribe_all() DummyC.unsubscribe_all() smache = Smache(scheduler=InProcessScheduler(), write_through=False) @smache.relations((DummyB, lambda b: DummyC.find('30'))) @smache.computed(DummyA, DummyC, Raw, Raw) def f(a, dummyc, c, d): b = DummyB.find('2') return a.value * b.value @smache.relations((DummyB, lambda b: [DummyA.find('1'), DummyA.find('2')])) @smache.computed(DummyA, Raw, Raw) def h(a, c, d): b = DummyB.find('2') return a.value * b.value
def test_with_raw_value(): ax = DummyA(1, 10) bx = DummyB(2, 2) assert with_raw(ax, bx, 1000) == 12000
def teardown_module(module): DummyA.unsubscribe_all() DummyB.unsubscribe_all() DummyC.unsubscribe_all()
def flush_before_each_test_case(): smache._set_globals() DummyA.set_data({'1': {'value': 'hello'}, '2': {'value': 'hihi'}}) DummyB.set_data({'1': {'value': 'world'}}) redis_con.flushall() yield
def slash(): return '/'.join([DummyA.find('1').value, DummyA.find('2').value])
def flush_before_each_test_case(): redis_con.flushall() DummyA.set_data({'1': {'value': 10}, '2': {'value': 500}}) DummyB.set_data({'2': {'value': 20}}) DummyC.set_data({'20': {'value': -1}, '30': {'value': -2}}) yield