| 1 |
from time import sleep |
| 2 |
from unittest import skipIf |
| 3 |
from unittest.mock import MagicMock, patch |
| 4 |
|
| 5 |
from django.conf import settings |
| 6 |
from django.contrib.auth.models import User |
| 7 |
from django.core.cache import DEFAULT_CACHE_ALIAS |
| 8 |
from django.core.checks import Error, Tags, Warning, run_checks |
| 9 |
from django.db import connection |
| 10 |
from django.test import TransactionTestCase |
| 11 |
from django.test.utils import override_settings |
| 12 |
|
| 13 |
from ..api import invalidate |
| 14 |
from ..settings import SUPPORTED_DATABASE_ENGINES, SUPPORTED_ONLY |
| 15 |
from ..utils import _get_tables |
| 16 |
from .models import Test, TestChild, TestParent, UnmanagedModel |
| 17 |
from .test_utils import TestUtilsMixin |
| 18 |
|
| 19 |
|
| 20 |
class SettingsTestCase(TestUtilsMixin, TransactionTestCase): |
| 21 |
@override_settings(CACHALOT_ENABLED=False) |
| 22 |
def test_decorator(self): |
| 23 |
self.assert_query_cached(Test.objects.all(), after=1) |
| 24 |
|
| 25 |
def test_django_override(self): |
| 26 |
with self.settings(CACHALOT_ENABLED=False): |
| 27 |
qs = Test.objects.all() |
| 28 |
self.assert_query_cached(qs, after=1) |
| 29 |
with self.settings(CACHALOT_ENABLED=True): |
| 30 |
self.assert_query_cached(qs) |
| 31 |
|
| 32 |
def test_enabled(self): |
| 33 |
qs = Test.objects.all() |
| 34 |
|
| 35 |
with self.settings(CACHALOT_ENABLED=True): |
| 36 |
self.assert_query_cached(qs) |
| 37 |
|
| 38 |
with self.settings(CACHALOT_ENABLED=False): |
| 39 |
self.assert_query_cached(qs, after=1) |
| 40 |
|
| 41 |
with self.assertNumQueries(0): |
| 42 |
list(Test.objects.all()) |
| 43 |
|
| 44 |
with self.settings(CACHALOT_ENABLED=False): |
| 45 |
with self.assertNumQueries(1): |
| 46 |
t = Test.objects.create(name='test') |
| 47 |
with self.assertNumQueries(1): |
| 48 |
data = list(Test.objects.all()) |
| 49 |
self.assertListEqual(data, [t]) |
| 50 |
|
| 51 |
@skipIf(len(settings.CACHES) == 1, 'We can’t change the cache used ' |
| 52 |
'since there’s only one configured.') |
| 53 |
def test_cache(self): |
| 54 |
other_cache_alias = next(alias for alias in settings.CACHES |
| 55 |
if alias != DEFAULT_CACHE_ALIAS) |
| 56 |
invalidate(Test, cache_alias=other_cache_alias) |
| 57 |
|
| 58 |
qs = Test.objects.all() |
| 59 |
|
| 60 |
with self.settings(CACHALOT_CACHE=DEFAULT_CACHE_ALIAS): |
| 61 |
self.assert_query_cached(qs) |
| 62 |
|
| 63 |
with self.settings(CACHALOT_CACHE=other_cache_alias): |
| 64 |
self.assert_query_cached(qs) |
| 65 |
|
| 66 |
Test.objects.create(name='test') |
| 67 |
|
| 68 |
# Only `CACHALOT_CACHE` is invalidated, so changing the database should |
| 69 |
# not invalidate all caches. |
| 70 |
with self.settings(CACHALOT_CACHE=other_cache_alias): |
| 71 |
self.assert_query_cached(qs, before=0) |
| 72 |
|
| 73 |
def test_databases(self): |
| 74 |
qs = Test.objects.all() |
| 75 |
with self.settings(CACHALOT_DATABASES=SUPPORTED_ONLY): |
| 76 |
self.assert_query_cached(qs) |
| 77 |
|
| 78 |
invalidate(Test) |
| 79 |
|
| 80 |
engine = connection.settings_dict['ENGINE'] |
| 81 |
SUPPORTED_DATABASE_ENGINES.remove(engine) |
| 82 |
with self.settings(CACHALOT_DATABASES=SUPPORTED_ONLY): |
| 83 |
self.assert_query_cached(qs, after=1) |
| 84 |
invalidate(Test) |
| 85 |
|
| 86 |
with self.settings(CACHALOT_USE_UNSUPPORTED_DATABASE=True): |
| 87 |
self.assert_query_cached(qs) |
| 88 |
invalidate(Test) |
| 89 |
|
| 90 |
with self.settings(CACHALOT_ADDITIONAL_SUPPORTED_DATABASES={engine}): |
| 91 |
self.assert_query_cached(qs) |
| 92 |
invalidate(Test) |
| 93 |
|
| 94 |
SUPPORTED_DATABASE_ENGINES.add(engine) |
| 95 |
with self.settings(CACHALOT_DATABASES=SUPPORTED_ONLY): |
| 96 |
self.assert_query_cached(qs) |
| 97 |
|
| 98 |
with self.settings(CACHALOT_DATABASES=[]): |
| 99 |
self.assert_query_cached(qs, after=1) |
| 100 |
|
| 101 |
def test_cache_timeout(self): |
| 102 |
qs = Test.objects.all() |
| 103 |
|
| 104 |
with self.assertNumQueries(1): |
| 105 |
list(qs.all()) |
| 106 |
sleep(1) |
| 107 |
with self.assertNumQueries(0): |
| 108 |
list(qs.all()) |
| 109 |
|
| 110 |
invalidate(Test) |
| 111 |
|
| 112 |
with self.settings(CACHALOT_TIMEOUT=0): |
| 113 |
with self.assertNumQueries(1): |
| 114 |
list(qs.all()) |
| 115 |
sleep(0.05) |
| 116 |
with self.assertNumQueries(1): |
| 117 |
list(qs.all()) |
| 118 |
|
| 119 |
# We have to test with a full second and not a shorter time because |
| 120 |
# memcached only takes the integer part of the timeout into account. |
| 121 |
with self.settings(CACHALOT_TIMEOUT=1): |
| 122 |
self.assert_query_cached(qs) |
| 123 |
sleep(1) |
| 124 |
with self.assertNumQueries(1): |
| 125 |
list(Test.objects.all()) |
| 126 |
|
| 127 |
def test_cache_random(self): |
| 128 |
qs = Test.objects.order_by('?') |
| 129 |
self.assert_query_cached(qs, after=1, compare_results=False) |
| 130 |
|
| 131 |
with self.settings(CACHALOT_CACHE_RANDOM=True): |
| 132 |
self.assert_query_cached(qs) |
| 133 |
|
| 134 |
def test_invalidate_raw(self): |
| 135 |
with self.assertNumQueries(1): |
| 136 |
list(Test.objects.all()) |
| 137 |
with self.settings(CACHALOT_INVALIDATE_RAW=False): |
| 138 |
with self.assertNumQueries(1): |
| 139 |
with connection.cursor() as cursor: |
| 140 |
cursor.execute("UPDATE %s SET name = 'new name';" |
| 141 |
% Test._meta.db_table) |
| 142 |
with self.assertNumQueries(0): |
| 143 |
list(Test.objects.all()) |
| 144 |
|
| 145 |
def test_only_cachable_tables(self): |
| 146 |
with self.settings(CACHALOT_ONLY_CACHABLE_TABLES=('cachalot_test',)): |
| 147 |
self.assert_query_cached(Test.objects.all()) |
| 148 |
self.assert_query_cached(TestParent.objects.all(), after=1) |
| 149 |
self.assert_query_cached(Test.objects.select_related('owner'), |
| 150 |
after=1) |
| 151 |
|
| 152 |
self.assert_query_cached(TestParent.objects.all()) |
| 153 |
|
| 154 |
with self.settings(CACHALOT_ONLY_CACHABLE_TABLES=( |
| 155 |
'cachalot_test', 'cachalot_testchild', 'auth_user')): |
| 156 |
self.assert_query_cached(Test.objects.select_related('owner')) |
| 157 |
|
| 158 |
# TestChild uses multi-table inheritance, and since its parent, |
| 159 |
# 'cachalot_testparent', is not cachable, a basic |
| 160 |
# TestChild query can’t be cached |
| 161 |
self.assert_query_cached(TestChild.objects.all(), after=1) |
| 162 |
|
| 163 |
# However, if we only fetch data from the 'cachalot_testchild' |
| 164 |
# table, it’s cachable. |
| 165 |
self.assert_query_cached(TestChild.objects.values('public')) |
| 166 |
|
| 167 |
@override_settings(CACHALOT_ONLY_CACHABLE_APPS=('cachalot',)) |
| 168 |
def test_only_cachable_apps(self): |
| 169 |
self.assert_query_cached(Test.objects.all()) |
| 170 |
self.assert_query_cached(TestParent.objects.all()) |
| 171 |
self.assert_query_cached(Test.objects.select_related('owner'), after=1) |
| 172 |
|
| 173 |
# Must use override_settings to get the correct effect. Using the cm doesn't |
| 174 |
# reload settings on cachalot's side |
| 175 |
@override_settings(CACHALOT_ONLY_CACHABLE_TABLES=('cachalot_test', 'auth_user'), |
| 176 |
CACHALOT_ONLY_CACHABLE_APPS=('cachalot',)) |
| 177 |
def test_only_cachable_apps_set_combo(self): |
| 178 |
self.assert_query_cached(Test.objects.all()) |
| 179 |
self.assert_query_cached(TestParent.objects.all()) |
| 180 |
self.assert_query_cached(Test.objects.select_related('owner')) |
| 181 |
|
| 182 |
def test_uncachable_tables(self): |
| 183 |
qs = Test.objects.all() |
| 184 |
|
| 185 |
with self.settings(CACHALOT_UNCACHABLE_TABLES=('cachalot_test',)): |
| 186 |
self.assert_query_cached(qs, after=1) |
| 187 |
|
| 188 |
self.assert_query_cached(qs) |
| 189 |
|
| 190 |
with self.settings(CACHALOT_UNCACHABLE_TABLES=('cachalot_test',)): |
| 191 |
self.assert_query_cached(qs, after=1) |
| 192 |
|
| 193 |
@override_settings(CACHALOT_UNCACHABLE_APPS=('cachalot',)) |
| 194 |
def test_uncachable_apps(self): |
| 195 |
self.assert_query_cached(Test.objects.all(), after=1) |
| 196 |
self.assert_query_cached(TestParent.objects.all(), after=1) |
| 197 |
|
| 198 |
@override_settings(CACHALOT_UNCACHABLE_TABLES=('cachalot_test',), |
| 199 |
CACHALOT_UNCACHABLE_APPS=('cachalot',)) |
| 200 |
def test_uncachable_apps_set_combo(self): |
| 201 |
self.assert_query_cached(Test.objects.all(), after=1) |
| 202 |
self.assert_query_cached(TestParent.objects.all(), after=1) |
| 203 |
|
| 204 |
def test_only_cachable_and_uncachable_table(self): |
| 205 |
with self.settings( |
| 206 |
CACHALOT_ONLY_CACHABLE_TABLES=('cachalot_test', |
| 207 |
'cachalot_testparent'), |
| 208 |
CACHALOT_UNCACHABLE_TABLES=('cachalot_test',)): |
| 209 |
self.assert_query_cached(Test.objects.all(), after=1) |
| 210 |
self.assert_query_cached(TestParent.objects.all()) |
| 211 |
self.assert_query_cached(User.objects.all(), after=1) |
| 212 |
|
| 213 |
def test_uncachable_unmanaged_table(self): |
| 214 |
qs = UnmanagedModel.objects.all() |
| 215 |
with self.settings( |
| 216 |
CACHALOT_UNCACHABLE_TABLES=("cachalot_unmanagedmodel",), |
| 217 |
CACHALOT_ADDITIONAL_TABLES=("cachalot_unmanagedmodel",) |
| 218 |
): |
| 219 |
self.assert_query_cached(qs, after=1) |
| 220 |
|
| 221 |
def test_cache_compatibility(self): |
| 222 |
compatible_cache = { |
| 223 |
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', |
| 224 |
} |
| 225 |
incompatible_cache = { |
| 226 |
'BACKEND': 'django.core.cache.backends.db.DatabaseCache', |
| 227 |
'LOCATION': 'cache_table' |
| 228 |
} |
| 229 |
|
| 230 |
with self.settings(CACHES={'default': compatible_cache, |
| 231 |
'secondary': incompatible_cache}): |
| 232 |
errors = run_checks(tags=[Tags.compatibility]) |
| 233 |
self.assertListEqual(errors, []) |
| 234 |
|
| 235 |
warning001 = Warning( |
| 236 |
'Cache backend %r is not supported by django-cachalot.' |
| 237 |
% 'django.core.cache.backends.db.DatabaseCache', |
| 238 |
hint='Switch to a supported cache backend ' |
| 239 |
'like Redis or Memcached.', |
| 240 |
id='cachalot.W001') |
| 241 |
with self.settings(CACHES={'default': incompatible_cache}): |
| 242 |
errors = run_checks(tags=[Tags.compatibility]) |
| 243 |
self.assertListEqual(errors, [warning001]) |
| 244 |
with self.settings(CACHES={'default': compatible_cache, |
| 245 |
'secondary': incompatible_cache}, |
| 246 |
CACHALOT_CACHE='secondary'): |
| 247 |
errors = run_checks(tags=[Tags.compatibility]) |
| 248 |
self.assertListEqual(errors, [warning001]) |
| 249 |
|
| 250 |
def test_database_compatibility(self): |
| 251 |
compatible_database = { |
| 252 |
'ENGINE': 'django.db.backends.sqlite3', |
| 253 |
'NAME': 'non_existent_db.sqlite3', |
| 254 |
} |
| 255 |
incompatible_database = { |
| 256 |
'ENGINE': 'django.db.backends.oracle', |
| 257 |
'NAME': 'non_existent_db', |
| 258 |
} |
| 259 |
|
| 260 |
warning002 = Warning( |
| 261 |
'None of the configured databases are supported ' |
| 262 |
'by django-cachalot.', |
| 263 |
hint='Use a supported database, or remove django-cachalot, or ' |
| 264 |
'put at least one database alias in `CACHALOT_DATABASES` ' |
| 265 |
'to force django-cachalot to use it.', |
| 266 |
id='cachalot.W002' |
| 267 |
) |
| 268 |
warning003 = Warning( |
| 269 |
'Database engine %r is not supported by django-cachalot.' |
| 270 |
% 'django.db.backends.oracle', |
| 271 |
hint='Switch to a supported database engine, ' |
| 272 |
'add an entry in `CACHALOT_ADDITIONAL_SUPPORTED_DATABASES`' |
| 273 |
', or set True to `CACHALOT_USE_UNSUPPORTED_DATABASE`.', |
| 274 |
id='cachalot.W003' |
| 275 |
) |
| 276 |
warning004 = Warning( |
| 277 |
'Django-cachalot is useless because no database ' |
| 278 |
'is configured in `CACHALOT_DATABASES`.', |
| 279 |
hint='Reconfigure django-cachalot or remove it.', |
| 280 |
id='cachalot.W004' |
| 281 |
) |
| 282 |
error001 = Error( |
| 283 |
'Database alias %r from `CACHALOT_DATABASES` ' |
| 284 |
'is not defined in `DATABASES`.' % 'secondary', |
| 285 |
hint='Change `CACHALOT_DATABASES` to be compliant with' |
| 286 |
'`CACHALOT_DATABASES`', |
| 287 |
id='cachalot.E001', |
| 288 |
) |
| 289 |
error002 = Error( |
| 290 |
"`CACHALOT_DATABASES` must be either %r or a list, tuple, " |
| 291 |
"frozenset or set of database aliases." % SUPPORTED_ONLY, |
| 292 |
hint='Remove `CACHALOT_DATABASES` or change it.', |
| 293 |
id='cachalot.E002', |
| 294 |
) |
| 295 |
|
| 296 |
with self.settings(DATABASES={'default': incompatible_database}): |
| 297 |
errors = run_checks(tags=[Tags.compatibility]) |
| 298 |
self.assertListEqual(errors, [warning002]) |
| 299 |
|
| 300 |
with self.settings(DATABASES={'default': compatible_database, |
| 301 |
'secondary': incompatible_database}): |
| 302 |
errors = run_checks(tags=[Tags.compatibility]) |
| 303 |
self.assertListEqual(errors, []) |
| 304 |
with self.settings(DATABASES={'default': incompatible_database, |
| 305 |
'secondary': compatible_database}): |
| 306 |
errors = run_checks(tags=[Tags.compatibility]) |
| 307 |
self.assertListEqual(errors, []) |
| 308 |
|
| 309 |
with self.settings(DATABASES={'default': incompatible_database}, |
| 310 |
CACHALOT_DATABASES=['default']): |
| 311 |
errors = run_checks(tags=[Tags.compatibility]) |
| 312 |
self.assertListEqual(errors, [warning003]) |
| 313 |
|
| 314 |
with self.settings(DATABASES={'default': incompatible_database}, |
| 315 |
CACHALOT_DATABASES=[]): |
| 316 |
errors = run_checks(tags=[Tags.compatibility]) |
| 317 |
self.assertListEqual(errors, [warning004]) |
| 318 |
|
| 319 |
with self.settings(DATABASES={'default': incompatible_database}, |
| 320 |
CACHALOT_DATABASES=['secondary']): |
| 321 |
errors = run_checks(tags=[Tags.compatibility]) |
| 322 |
self.assertListEqual(errors, [error001]) |
| 323 |
with self.settings(DATABASES={'default': compatible_database}, |
| 324 |
CACHALOT_DATABASES=['default', 'secondary']): |
| 325 |
errors = run_checks(tags=[Tags.compatibility]) |
| 326 |
self.assertListEqual(errors, [error001]) |
| 327 |
|
| 328 |
with self.settings(CACHALOT_DATABASES='invalid value'): |
| 329 |
errors = run_checks(tags=[Tags.compatibility]) |
| 330 |
self.assertListEqual(errors, [error002]) |
| 331 |
|
| 332 |
def call_get_tables(self): |
| 333 |
qs = Test.objects.all() |
| 334 |
compiler_mock = MagicMock() |
| 335 |
compiler_mock.__cachalot_generated_sql = '' |
| 336 |
tables = _get_tables(qs.db, qs.query, compiler_mock) |
| 337 |
self.assertTrue(tables) |
| 338 |
return tables |
| 339 |
|
| 340 |
@override_settings(CACHALOT_FINAL_SQL_CHECK=True) |
| 341 |
@patch('cachalot.utils._get_tables_from_sql') |
| 342 |
def test_cachalot_final_sql_check_when_true(self, _get_tables_from_sql): |
| 343 |
_get_tables_from_sql.return_value = {'patched'} |
| 344 |
tables = self.call_get_tables() |
| 345 |
_get_tables_from_sql.assert_called_once() |
| 346 |
self.assertIn('patched', tables) |
| 347 |
|
| 348 |
|
| 349 |
@override_settings(CACHALOT_FINAL_SQL_CHECK=False) |
| 350 |
@patch('cachalot.utils._get_tables_from_sql') |
| 351 |
def test_cachalot_final_sql_check_when_false(self, _get_tables_from_sql): |
| 352 |
_get_tables_from_sql.return_value = {'patched'} |
| 353 |
tables = self.call_get_tables() |
| 354 |
_get_tables_from_sql.assert_not_called() |
| 355 |
self.assertNotIn('patched', tables) |
| 356 |
|