def setup_module(module): global smache, hyphen, slash, worker_queue worker_queue = Queue('test_queue', connection=redis_con) smache = Smache(worker_queue=worker_queue) @smache.computed(DummyA, DummyB) def hyphen(a, b): return ' - '.join([a.value, b.value]) @smache.sources(DummyA) @smache.computed() def slash(): return '/'.join([DummyA.find('1').value, DummyA.find('2').value])
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
from smache import Smache from smache.data_sources.in_memory_data_source import InMemoryEntity import pytest import redis import os class DummyA(InMemoryEntity): pass smache = Smache() @smache.computed(DummyA) def score(a): return a.value # Tests redis_con = redis.StrictRedis(host='localhost', port=6379, db=0) @pytest.yield_fixture(autouse=True) def flush_before_each_test_case(): redis_con.flushall() yield def test_draw(): execution_path = os.getcwd()
import time from mongoengine import Document, StringField, connect from smache import Smache connect('smache_test_db') class User(Document): name = StringField() mysmachecache = Smache() @mysmachecache.computed(User) def computed_value(user): time.sleep(2) # To simulate slow computation return user.name
from smache import Smache from smache.schedulers import InProcessScheduler import pytest import redis import time smache = Smache(scheduler=InProcessScheduler()) redis_con = redis.StrictRedis(host='localhost', port=6379, db=0) @smache.computed(ttl=500) def fun_with_ttl(): return 50 @pytest.yield_fixture(autouse=True) def flush_before_each_test_case(): smache._set_globals() redis_con.flushall() yield def test_cached_values_are_not_updated_when_below_ttl(monkeypatch): key = smache.computed_key(fun_with_ttl, tuple()) now = time.time() monkeypatch.setattr(time, 'time', lambda: now) smache._store.store(key, 100, 0)
from smache import Smache from smache.data_sources import MongoDataSource from smache.schedulers import InProcessScheduler from tests.mongo_helper import User, Handin, test_connect from tests.helper import relation_detector, RelationMissingError # NOQA import pytest import redis # Definitions test_connect() smache = Smache(scheduler=InProcessScheduler(), write_through=False) @smache.computed() def score_with_missing_relations(): handin = Handin.objects().first() handin.users[0] return 50 @smache.sources(User, Handin) @smache.computed() def score_with_sources(): handin = Handin.objects().first() handin.users[0] return 50 @smache.relations( (User, lambda user: user),
import redis from rq import Queue from smache import Smache, Raw from benchmark.db_helper import clean_dbs, connect_db, connect_db_setup, User, Handin import time # Setup redis_con = redis.StrictRedis(host='localhost', port=6379, db=0) worker_queue = Queue('test_queue', connection=redis_con) smache = Smache(worker_queue=worker_queue) @smache.relations((Handin, lambda handin: handin.users)) @smache.computed(User, Raw) def score(user, sleep_time): handins = Handin.objects(users=user) time.sleep(sleep_time) total_score = sum(handin.score for handin in handins) avg_score = total_score / len(handins) if len(handins) > 0 else 0 return { 'name': user.name, 'age': user.age, 'total_score': total_score, 'avg_score': avg_score } @smache.computed(User, Raw) def simple_slow_fun(user, variation): time.sleep(0.4) return "SIMPLE SLOW FUN"