66
77from bson import SON
88from django .conf import settings
9- from django .core import management
109from django .core .cache import DEFAULT_CACHE_ALIAS , CacheKeyWarning , cache , caches
1110from django .core .cache .backends .base import InvalidCacheBackendError
1211from django .http import (
1615 FetchFromCacheMiddleware ,
1716 UpdateCacheMiddleware ,
1817)
19- from django .test import RequestFactory , TestCase , override_settings
18+ from django .test import RequestFactory , TestCase , modify_settings , override_settings
19+
20+ from django_mongodb_backend .cache import MongoDBCache
2021
2122from .models import Poll , expensive_calculation
2223
@@ -848,7 +849,9 @@ def test_custom_key_func(self):
848849
849850 @override_settings (
850851 CACHE_MIDDLEWARE_ALIAS = DEFAULT_CACHE_ALIAS ,
851- INSTALLED_APPS = settings .INSTALLED_APPS + ["django_mongodb_backend" ], # noqa: RUF005
852+ )
853+ @modify_settings (
854+ INSTALLED_APPS = {"prepend" : "django_mongodb_backend" },
852855 )
853856 def test_cache_write_unpicklable_object (self ):
854857 fetch_middleware = FetchFromCacheMiddleware (empty_response )
@@ -948,7 +951,9 @@ def test_collection_has_indexes(self):
948951 # Spaces are used in the table name to ensure quoting/escaping is working
949952 LOCATION = "test cache table" ,
950953 ),
951- INSTALLED_APPS = settings .INSTALLED_APPS + ["django_mongodb_backend" ], # noqa: RUF005
954+ )
955+ @modify_settings (
956+ INSTALLED_APPS = {"prepend" : "django_mongodb_backend" },
952957)
953958class DBCacheTests (BaseCacheTests , TestCase ):
954959 def setUp (self ):
@@ -961,7 +966,13 @@ def drop_collection(self):
961966 cache .collection .drop ()
962967
963968 def create_cache_collection (self ):
964- management .call_command ("createcachecollection" , verbosity = 0 )
969+ for cache_alias in settings .CACHES :
970+ cache = caches [cache_alias ]
971+ connection = cache ._db
972+ if cache ._collection_name in connection .introspection .table_names ():
973+ return
974+ cache = MongoDBCache (cache ._collection_name , {})
975+ cache .create_indexes ()
965976
966977
967978@override_settings (USE_TZ = True )
0 commit comments