| 1 |
import os |
| 2 |
from time import time, sleep |
| 3 |
from unittest import skipIf |
| 4 |
|
| 5 |
from django.conf import settings |
| 6 |
from django.contrib.auth.models import Permission, User |
| 7 |
from django.core.cache import DEFAULT_CACHE_ALIAS, caches |
| 8 |
from django.core.management import call_command |
| 9 |
from django.db import connection, transaction, DEFAULT_DB_ALIAS |
| 10 |
from django.template import engines |
| 11 |
from django.test import TransactionTestCase |
| 12 |
from jinja2.exceptions import TemplateSyntaxError |
| 13 |
|
| 14 |
from ..api import * |
| 15 |
from .models import Test |
| 16 |
from .test_utils import TestUtilsMixin |
| 17 |
|
| 18 |
|
| 19 |
class APITestCase(TestUtilsMixin, TransactionTestCase): |
| 20 |
databases = set(settings.DATABASES.keys()) |
| 21 |
|
| 22 |
def setUp(self): |
| 23 |
super(APITestCase, self).setUp() |
| 24 |
self.t1 = Test.objects.create(name='test1') |
| 25 |
self.cache_alias2 = next(alias for alias in settings.CACHES |
| 26 |
if alias != DEFAULT_CACHE_ALIAS) |
| 27 |
# For cachalot_disabled test |
| 28 |
self.user = User.objects.create_user('user') |
| 29 |
self.t1__permission = (Permission.objects.order_by('?') |
| 30 |
.select_related('content_type')[0]) |
| 31 |
|
| 32 |
def test_invalidate_tables(self): |
| 33 |
with self.assertNumQueries(1): |
| 34 |
data1 = list(Test.objects.values_list('name', flat=True)) |
| 35 |
self.assertListEqual(data1, ['test1']) |
| 36 |
|
| 37 |
with self.settings(CACHALOT_INVALIDATE_RAW=False): |
| 38 |
with connection.cursor() as cursor: |
| 39 |
cursor.execute( |
| 40 |
"INSERT INTO cachalot_test (name, public) " |
| 41 |
"VALUES ('test2', %s);", [1 if self.is_sqlite else True]) |
| 42 |
|
| 43 |
with self.assertNumQueries(0): |
| 44 |
data2 = list(Test.objects.values_list('name', flat=True)) |
| 45 |
self.assertListEqual(data2, ['test1']) |
| 46 |
|
| 47 |
invalidate('cachalot_test') |
| 48 |
|
| 49 |
with self.assertNumQueries(1): |
| 50 |
data3 = list(Test.objects.values_list('name', flat=True)) |
| 51 |
self.assertListEqual(data3, ['test1', 'test2']) |
| 52 |
|
| 53 |
def test_invalidate_models_lookups(self): |
| 54 |
with self.assertNumQueries(1): |
| 55 |
data1 = list(Test.objects.values_list('name', flat=True)) |
| 56 |
self.assertListEqual(data1, ['test1']) |
| 57 |
|
| 58 |
with self.settings(CACHALOT_INVALIDATE_RAW=False): |
| 59 |
with connection.cursor() as cursor: |
| 60 |
cursor.execute( |
| 61 |
"INSERT INTO cachalot_test (name, public) " |
| 62 |
"VALUES ('test2', %s);", [1 if self.is_sqlite else True]) |
| 63 |
|
| 64 |
with self.assertNumQueries(0): |
| 65 |
data2 = list(Test.objects.values_list('name', flat=True)) |
| 66 |
self.assertListEqual(data2, ['test1']) |
| 67 |
|
| 68 |
invalidate('cachalot.Test') |
| 69 |
|
| 70 |
with self.assertNumQueries(1): |
| 71 |
data3 = list(Test.objects.values_list('name', flat=True)) |
| 72 |
self.assertListEqual(data3, ['test1', 'test2']) |
| 73 |
|
| 74 |
def test_invalidate_models(self): |
| 75 |
with self.assertNumQueries(1): |
| 76 |
data1 = list(Test.objects.values_list('name', flat=True)) |
| 77 |
self.assertListEqual(data1, ['test1']) |
| 78 |
|
| 79 |
with self.settings(CACHALOT_INVALIDATE_RAW=False): |
| 80 |
with connection.cursor() as cursor: |
| 81 |
cursor.execute( |
| 82 |
"INSERT INTO cachalot_test (name, public) " |
| 83 |
"VALUES ('test2', %s);", [1 if self.is_sqlite else True]) |
| 84 |
|
| 85 |
with self.assertNumQueries(0): |
| 86 |
data2 = list(Test.objects.values_list('name', flat=True)) |
| 87 |
self.assertListEqual(data2, ['test1']) |
| 88 |
|
| 89 |
invalidate(Test) |
| 90 |
|
| 91 |
with self.assertNumQueries(1): |
| 92 |
data3 = list(Test.objects.values_list('name', flat=True)) |
| 93 |
self.assertListEqual(data3, ['test1', 'test2']) |
| 94 |
|
| 95 |
def test_invalidate_all(self): |
| 96 |
with self.assertNumQueries(1): |
| 97 |
Test.objects.get() |
| 98 |
|
| 99 |
with self.assertNumQueries(0): |
| 100 |
Test.objects.get() |
| 101 |
|
| 102 |
invalidate() |
| 103 |
|
| 104 |
with self.assertNumQueries(1): |
| 105 |
Test.objects.get() |
| 106 |
|
| 107 |
def test_invalidate_all_in_atomic(self): |
| 108 |
with transaction.atomic(): |
| 109 |
with self.assertNumQueries(1): |
| 110 |
Test.objects.get() |
| 111 |
|
| 112 |
with self.assertNumQueries(0): |
| 113 |
Test.objects.get() |
| 114 |
|
| 115 |
invalidate() |
| 116 |
|
| 117 |
with self.assertNumQueries(1): |
| 118 |
Test.objects.get() |
| 119 |
|
| 120 |
with self.assertNumQueries(1): |
| 121 |
Test.objects.get() |
| 122 |
|
| 123 |
def test_get_last_invalidation(self): |
| 124 |
invalidate() |
| 125 |
timestamp = get_last_invalidation() |
| 126 |
delta = 0.15 if os.environ.get("CACHE_BACKEND") == "filebased" else 0.1 |
| 127 |
self.assertAlmostEqual(timestamp, time(), delta=delta) |
| 128 |
|
| 129 |
sleep(0.1) |
| 130 |
|
| 131 |
invalidate('cachalot_test') |
| 132 |
timestamp = get_last_invalidation('cachalot_test') |
| 133 |
self.assertAlmostEqual(timestamp, time(), delta=delta) |
| 134 |
same_timestamp = get_last_invalidation('cachalot.Test') |
| 135 |
self.assertEqual(same_timestamp, timestamp) |
| 136 |
same_timestamp = get_last_invalidation(Test) |
| 137 |
self.assertEqual(same_timestamp, timestamp) |
| 138 |
|
| 139 |
timestamp = get_last_invalidation('cachalot_testparent') |
| 140 |
self.assertNotAlmostEqual(timestamp, time(), delta=0.1) |
| 141 |
timestamp = get_last_invalidation('cachalot_testparent', 'cachalot_test') |
| 142 |
self.assertAlmostEqual(timestamp, time(), delta=delta) |
| 143 |
|
| 144 |
def test_get_last_invalidation_template_tag(self): |
| 145 |
# Without arguments |
| 146 |
original_timestamp = engines['django'].from_string( |
| 147 |
"{{ timestamp }}" |
| 148 |
).render({ |
| 149 |
'timestamp': get_last_invalidation(), |
| 150 |
}) |
| 151 |
|
| 152 |
template = engines['django'].from_string(""" |
| 153 |
{% load cachalot %} |
| 154 |
{% get_last_invalidation as timestamp %} |
| 155 |
{{ timestamp }} |
| 156 |
""") |
| 157 |
timestamp = template.render().strip() |
| 158 |
|
| 159 |
self.assertNotEqual(timestamp, '') |
| 160 |
self.assertNotEqual(timestamp, '0.0') |
| 161 |
self.assertAlmostEqual(float(timestamp), float(original_timestamp), |
| 162 |
delta=0.1) |
| 163 |
|
| 164 |
# With arguments |
| 165 |
original_timestamp = engines['django'].from_string( |
| 166 |
"{{ timestamp }}" |
| 167 |
).render({ |
| 168 |
'timestamp': get_last_invalidation('auth.Group', 'cachalot_test'), |
| 169 |
}) |
| 170 |
|
| 171 |
template = engines['django'].from_string(""" |
| 172 |
{% load cachalot %} |
| 173 |
{% get_last_invalidation 'auth.Group' 'cachalot_test' as timestamp %} |
| 174 |
{{ timestamp }} |
| 175 |
""") |
| 176 |
timestamp = template.render().strip() |
| 177 |
|
| 178 |
self.assertNotEqual(timestamp, '') |
| 179 |
self.assertNotEqual(timestamp, '0.0') |
| 180 |
self.assertAlmostEqual(float(timestamp), float(original_timestamp), |
| 181 |
delta=0.1) |
| 182 |
|
| 183 |
# While using the `cache` template tag, with invalidation |
| 184 |
template = engines['django'].from_string(""" |
| 185 |
{% load cachalot cache %} |
| 186 |
{% get_last_invalidation 'auth.Group' 'cachalot_test' as timestamp %} |
| 187 |
{% cache 10 cache_key_name timestamp %} |
| 188 |
{{ content }} |
| 189 |
{% endcache %} |
| 190 |
""") |
| 191 |
content = template.render({'content': 'something'}).strip() |
| 192 |
self.assertEqual(content, 'something') |
| 193 |
content = template.render({'content': 'anything'}).strip() |
| 194 |
self.assertEqual(content, 'something') |
| 195 |
invalidate('cachalot_test') |
| 196 |
content = template.render({'content': 'yet another'}).strip() |
| 197 |
self.assertEqual(content, 'yet another') |
| 198 |
|
| 199 |
def test_get_last_invalidation_jinja2(self): |
| 200 |
original_timestamp = engines['jinja2'].from_string( |
| 201 |
"{{ timestamp }}" |
| 202 |
).render({ |
| 203 |
'timestamp': get_last_invalidation('auth.Group', 'cachalot_test'), |
| 204 |
}) |
| 205 |
template = engines['jinja2'].from_string( |
| 206 |
"{{ get_last_invalidation('auth.Group', 'cachalot_test') }}") |
| 207 |
timestamp = template.render({}) |
| 208 |
|
| 209 |
self.assertNotEqual(timestamp, '') |
| 210 |
self.assertNotEqual(timestamp, '0.0') |
| 211 |
self.assertAlmostEqual(float(timestamp), float(original_timestamp), |
| 212 |
delta=0.1) |
| 213 |
|
| 214 |
def test_cache_jinja2(self): |
| 215 |
# Invalid arguments |
| 216 |
with self.assertRaises(TemplateSyntaxError, |
| 217 |
msg="'invalid' is not a valid keyword argument " |
| 218 |
"for {% cache %}"): |
| 219 |
engines['jinja2'].from_string(""" |
| 220 |
{% cache cache_key='anything', invalid='what?' %}{% endcache %} |
| 221 |
""") |
| 222 |
with self.assertRaises(ValueError, msg='You must set `cache_key` when ' |
| 223 |
'the template is not a file.'): |
| 224 |
engines['jinja2'].from_string( |
| 225 |
'{% cache %} broken {% endcache %}').render() |
| 226 |
|
| 227 |
# With the minimum number of arguments |
| 228 |
template = engines['jinja2'].from_string(""" |
| 229 |
{%- cache cache_key='first' -%} |
| 230 |
{{ content1 }} |
| 231 |
{%- endcache -%} |
| 232 |
{%- cache cache_key='second' -%} |
| 233 |
{{ content2 }} |
| 234 |
{%- endcache -%} |
| 235 |
""") |
| 236 |
content = template.render({'content1': 'abc', 'content2': 'def'}) |
| 237 |
self.assertEqual(content, 'abcdef') |
| 238 |
invalidate() |
| 239 |
content = template.render({'content1': 'ghi', 'content2': 'jkl'}) |
| 240 |
self.assertEqual(content, 'abcdef') |
| 241 |
|
| 242 |
# With the maximum number of arguments |
| 243 |
template = engines['jinja2'].from_string(""" |
| 244 |
{%- cache get_last_invalidation('auth.Group', 'cachalot_test', |
| 245 |
cache_alias=cache), |
| 246 |
timeout=10, cache_key='cache_key_name', cache_alias=cache -%} |
| 247 |
{{ content }} |
| 248 |
{%- endcache -%} |
| 249 |
""") |
| 250 |
content = template.render({'content': 'something', |
| 251 |
'cache': self.cache_alias2}) |
| 252 |
self.assertEqual(content, 'something') |
| 253 |
content = template.render({'content': 'anything', |
| 254 |
'cache': self.cache_alias2}) |
| 255 |
self.assertEqual(content, 'something') |
| 256 |
invalidate('cachalot_test', cache_alias=DEFAULT_CACHE_ALIAS) |
| 257 |
content = template.render({'content': 'yet another', |
| 258 |
'cache': self.cache_alias2}) |
| 259 |
self.assertEqual(content, 'something') |
| 260 |
invalidate('cachalot_test') |
| 261 |
content = template.render({'content': 'will you change?', |
| 262 |
'cache': self.cache_alias2}) |
| 263 |
self.assertEqual(content, 'will you change?') |
| 264 |
caches[self.cache_alias2].clear() |
| 265 |
content = template.render({'content': 'better!', |
| 266 |
'cache': self.cache_alias2}) |
| 267 |
self.assertEqual(content, 'better!') |
| 268 |
|
| 269 |
def test_cachalot_disabled_multiple_queries_ignoring_in_mem_cache(self): |
| 270 |
""" |
| 271 |
Test that when queries are given the `cachalot_disabled` context manager, |
| 272 |
the queries will not be cached. |
| 273 |
""" |
| 274 |
with cachalot_disabled(True): |
| 275 |
qs = Test.objects.all() |
| 276 |
with self.assertNumQueries(1): |
| 277 |
data1 = list(qs.all()) |
| 278 |
Test.objects.create( |
| 279 |
name='test3', owner=self.user, |
| 280 |
date='1789-07-14', datetime='1789-07-14T16:43:27', |
| 281 |
permission=self.t1__permission) |
| 282 |
with self.assertNumQueries(1): |
| 283 |
data2 = list(qs.all()) |
| 284 |
self.assertNotEqual(data1, data2) |
| 285 |
|
| 286 |
def test_query_cachalot_disabled_even_if_already_cached(self): |
| 287 |
""" |
| 288 |
Test that when a query is given the `cachalot_disabled` context manager, |
| 289 |
the query outside of the context manager will be cached. Any duplicated |
| 290 |
query will use the original query's cached result. |
| 291 |
""" |
| 292 |
qs = Test.objects.all() |
| 293 |
self.assert_query_cached(qs) |
| 294 |
with cachalot_disabled() and self.assertNumQueries(0): |
| 295 |
list(qs.all()) |
| 296 |
|
| 297 |
def test_duplicate_query_execute_anyways(self): |
| 298 |
"""After an object is created, a duplicate query should execute |
| 299 |
rather than use the cached result. |
| 300 |
""" |
| 301 |
qs = Test.objects.all() |
| 302 |
self.assert_query_cached(qs) |
| 303 |
Test.objects.create( |
| 304 |
name='test3', owner=self.user, |
| 305 |
date='1789-07-14', datetime='1789-07-14T16:43:27', |
| 306 |
permission=self.t1__permission) |
| 307 |
with cachalot_disabled() and self.assertNumQueries(1): |
| 308 |
list(qs.all()) |
| 309 |
|
| 310 |
|
| 311 |
class CommandTestCase(TransactionTestCase): |
| 312 |
multi_db = True |
| 313 |
databases = "__all__" |
| 314 |
|
| 315 |
def setUp(self): |
| 316 |
self.db_alias2 = next(alias for alias in settings.DATABASES |
| 317 |
if alias != DEFAULT_DB_ALIAS) |
| 318 |
|
| 319 |
self.cache_alias2 = next(alias for alias in settings.CACHES |
| 320 |
if alias != DEFAULT_CACHE_ALIAS) |
| 321 |
|
| 322 |
self.t1 = Test.objects.create(name='test1') |
| 323 |
self.t2 = Test.objects.using(self.db_alias2).create(name='test2') |
| 324 |
self.u = User.objects.create_user('test') |
| 325 |
|
| 326 |
def test_invalidate_cachalot(self): |
| 327 |
with self.assertNumQueries(1): |
| 328 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 329 |
call_command('invalidate_cachalot', verbosity=0) |
| 330 |
with self.assertNumQueries(1): |
| 331 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 332 |
|
| 333 |
call_command('invalidate_cachalot', 'auth', verbosity=0) |
| 334 |
with self.assertNumQueries(0): |
| 335 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 336 |
|
| 337 |
call_command('invalidate_cachalot', 'cachalot', verbosity=0) |
| 338 |
with self.assertNumQueries(1): |
| 339 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 340 |
|
| 341 |
call_command('invalidate_cachalot', 'cachalot.testchild', verbosity=0) |
| 342 |
with self.assertNumQueries(0): |
| 343 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 344 |
|
| 345 |
call_command('invalidate_cachalot', 'cachalot.test', verbosity=0) |
| 346 |
with self.assertNumQueries(1): |
| 347 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 348 |
|
| 349 |
with self.assertNumQueries(1): |
| 350 |
self.assertListEqual(list(User.objects.all()), [self.u]) |
| 351 |
call_command('invalidate_cachalot', 'cachalot.test', 'auth.user', |
| 352 |
verbosity=0) |
| 353 |
with self.assertNumQueries(1): |
| 354 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 355 |
with self.assertNumQueries(1): |
| 356 |
self.assertListEqual(list(User.objects.all()), [self.u]) |
| 357 |
|
| 358 |
@skipIf(len(settings.DATABASES) == 1, |
| 359 |
'We can’t change the DB used since there’s only one configured') |
| 360 |
def test_invalidate_cachalot_multi_db(self): |
| 361 |
with self.assertNumQueries(1): |
| 362 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 363 |
call_command('invalidate_cachalot', verbosity=0, |
| 364 |
db_alias=self.db_alias2) |
| 365 |
with self.assertNumQueries(0): |
| 366 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 367 |
|
| 368 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 369 |
self.assertListEqual(list(Test.objects.using(self.db_alias2)), |
| 370 |
[self.t2]) |
| 371 |
call_command('invalidate_cachalot', verbosity=0, |
| 372 |
db_alias=self.db_alias2) |
| 373 |
with self.assertNumQueries(1, using=self.db_alias2): |
| 374 |
self.assertListEqual(list(Test.objects.using(self.db_alias2)), |
| 375 |
[self.t2]) |
| 376 |
|
| 377 |
@skipIf(len(settings.CACHES) == 1, |
| 378 |
'We can’t change the cache used since there’s only one configured') |
| 379 |
def test_invalidate_cachalot_multi_cache(self): |
| 380 |
with self.assertNumQueries(1): |
| 381 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 382 |
call_command('invalidate_cachalot', verbosity=0, |
| 383 |
cache_alias=self.cache_alias2) |
| 384 |
with self.assertNumQueries(0): |
| 385 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 386 |
|
| 387 |
with self.assertNumQueries(1): |
| 388 |
with self.settings(CACHALOT_CACHE=self.cache_alias2): |
| 389 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |
| 390 |
call_command('invalidate_cachalot', verbosity=0, |
| 391 |
cache_alias=self.cache_alias2) |
| 392 |
with self.assertNumQueries(1): |
| 393 |
with self.settings(CACHALOT_CACHE=self.cache_alias2): |
| 394 |
self.assertListEqual(list(Test.objects.all()), [self.t1]) |