1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
from django.db import transaction
from django.test import TestCase, override_settings
from cacheops import cached_as, no_invalidation, invalidate_obj, invalidate_model, invalidate_all
from cacheops.conf import settings
from cacheops.signals import cache_read, cache_invalidated
from .utils import BaseTestCase, make_inc
from .models import Post, Category, Local, DbAgnostic, DbBinded
class SettingsTests(TestCase):
def test_context_manager(self):
self.assertTrue(settings.CACHEOPS_ENABLED)
with self.settings(CACHEOPS_ENABLED=False):
self.assertFalse(settings.CACHEOPS_ENABLED)
@override_settings(CACHEOPS_ENABLED=False)
def test_decorator(self):
self.assertFalse(settings.CACHEOPS_ENABLED)
@override_settings(CACHEOPS_ENABLED=False)
class ClassOverrideSettingsTests(TestCase):
def test_class(self):
self.assertFalse(settings.CACHEOPS_ENABLED)
class SignalsTests(BaseTestCase):
def setUp(self):
super(SignalsTests, self).setUp()
def set_signal(signal=None, **kwargs):
self.signal_calls.append(kwargs)
self.signal_calls = []
cache_read.connect(set_signal, dispatch_uid=1, weak=False)
def tearDown(self):
super(SignalsTests, self).tearDown()
cache_read.disconnect(dispatch_uid=1)
def test_queryset(self):
# Miss
test_model = Category.objects.create(title="foo")
Category.objects.cache().get(id=test_model.id)
self.assertEqual(self.signal_calls, [{'sender': Category, 'func': None, 'hit': False}])
# Hit
self.signal_calls = []
Category.objects.cache().get(id=test_model.id) # hit
self.assertEqual(self.signal_calls, [{'sender': Category, 'func': None, 'hit': True}])
def test_queryset_empty(self):
list(Category.objects.cache().filter(pk__in=[]))
self.assertEqual(self.signal_calls, [{'sender': Category, 'func': None, 'hit': False}])
def test_cached_as(self):
get_calls = make_inc(cached_as(Category.objects.filter(title='test')))
func = get_calls.__wrapped__
# Miss
self.assertEqual(get_calls(), 1)
self.assertEqual(self.signal_calls, [{'sender': None, 'func': func, 'hit': False}])
# Hit
self.signal_calls = []
self.assertEqual(get_calls(), 1)
self.assertEqual(self.signal_calls, [{'sender': None, 'func': func, 'hit': True}])
def test_invalidation_signal(self):
def set_signal(signal=None, **kwargs):
signal_calls.append(kwargs)
signal_calls = []
cache_invalidated.connect(set_signal, dispatch_uid=1, weak=False)
invalidate_all()
invalidate_model(Post)
c = Category.objects.create(title='Hey')
self.assertEqual(signal_calls, [
{'sender': None, 'obj_dict': None},
{'sender': Post, 'obj_dict': None},
{'sender': Category, 'obj_dict': {'id': c.pk, 'title': 'Hey'}},
])
class LockingTests(BaseTestCase):
def test_lock(self):
import random
import threading
from .utils import ThreadWithReturnValue
from before_after import before
@cached_as(Post, lock=True, timeout=60)
def func():
return random.random()
results = []
locked = threading.Event()
thread = [None]
def second_thread():
def _target():
try:
with before('redis.Redis.brpoplpush', lambda *a, **kw: locked.set()):
results.append(func())
except Exception:
locked.set()
raise
thread[0] = ThreadWithReturnValue(target=_target)
thread[0].start()
assert locked.wait(1) # Wait until right before the block
with before('random.random', second_thread):
results.append(func())
thread[0].join()
self.assertEqual(results[0], results[1])
class NoInvalidationTests(BaseTestCase):
fixtures = ['basic']
def _template(self, invalidate):
post = Post.objects.cache().get(pk=1)
invalidate(post)
with self.assertNumQueries(0):
Post.objects.cache().get(pk=1)
def test_context_manager(self):
def invalidate(post):
with no_invalidation:
invalidate_obj(post)
self._template(invalidate)
def test_decorator(self):
self._template(no_invalidation(invalidate_obj))
def test_nested(self):
def invalidate(post):
with no_invalidation:
with no_invalidation:
pass
invalidate_obj(post)
self._template(invalidate)
def test_in_transaction(self):
with transaction.atomic():
post = Post.objects.cache().get(pk=1)
with no_invalidation:
post.save()
with self.assertNumQueries(0):
Post.objects.cache().get(pk=1)
class LocalGetTests(BaseTestCase):
def setUp(self):
Local.objects.create(pk=1)
super(LocalGetTests, self).setUp()
def test_unhashable_args(self):
Local.objects.cache().get(pk__in=[1, 2])
class DbAgnosticTests(BaseTestCase):
databases = ('default', 'slave')
def test_db_agnostic_by_default(self):
list(DbAgnostic.objects.cache())
with self.assertNumQueries(0, using='slave'):
list(DbAgnostic.objects.cache().using('slave'))
def test_db_agnostic_disabled(self):
list(DbBinded.objects.cache())
with self.assertNumQueries(1, using='slave'):
list(DbBinded.objects.cache().using('slave'))
def test_model_family():
from cacheops.utils import model_family
from .models import Abs, Concrete1, AbsChild, Concrete2
from .models import NoProfile, NoProfileProxy, AbsNoProfile, NoProfileChild
from .models import ParentId, ParentStr, Mess, MessChild
# Abstract models do not have family, children of an abstract model are not a family
assert model_family(Abs) == set()
assert model_family(Concrete1) == {Concrete1}
assert model_family(AbsChild) == set()
assert model_family(Concrete2) == {Concrete2}
# Everything in but an abstract model
assert model_family(NoProfile) == {NoProfile, NoProfileProxy, NoProfileChild}
assert model_family(NoProfileProxy) == {NoProfile, NoProfileProxy, NoProfileChild}
assert model_family(AbsNoProfile) == set()
assert model_family(NoProfileChild) == {NoProfile, NoProfileProxy, NoProfileChild}
# The worst of multiple inheritance
assert model_family(Mess) == {Mess, MessChild, ParentId, ParentStr}
|