9
9
from typing import Any
10
10
11
11
import pytest
12
+ from django .core .cache import BaseCache
12
13
from django .core .cache import cache
13
14
from django .core .cache import CacheKeyWarning
14
15
from django .core .cache import caches
@@ -100,7 +101,10 @@ def reverse_custom_key_func(full_key):
100
101
}
101
102
102
103
103
- def caches_setting_for_tests (options = None , ** params ):
104
+ def caches_setting_for_tests (
105
+ options : dict [str , Any ] | None = None ,
106
+ ** params : Any ,
107
+ ) -> dict [str , Any ]:
104
108
# `params` are test specific overrides and `_caches_settings_base` is the
105
109
# base config for the tests.
106
110
# This results in the following search order:
@@ -117,8 +121,10 @@ def caches_setting_for_tests(options=None, **params):
117
121
118
122
# Spaces are used in the table name to ensure quoting/escaping is working
119
123
def override_cache_settings (
120
- BACKEND = "django_mysql.cache.MySQLCache" , LOCATION = "test cache table" , ** kwargs
121
- ):
124
+ BACKEND : str = "django_mysql.cache.MySQLCache" ,
125
+ LOCATION : str = "test cache table" ,
126
+ ** kwargs : Any ,
127
+ ) -> override_settings :
122
128
return override_settings (
123
129
CACHES = caches_setting_for_tests (BACKEND = BACKEND , LOCATION = LOCATION , ** kwargs )
124
130
)
@@ -129,13 +135,13 @@ class MySQLCacheTableMixin(TransactionTestCase):
129
135
table_name = "test cache table"
130
136
131
137
@classmethod
132
- def create_table (self ):
138
+ def create_table (self ) -> None :
133
139
sql = MySQLCache .create_table_sql .format (table_name = self .table_name )
134
140
with connection .cursor () as cursor :
135
141
cursor .execute (sql )
136
142
137
143
@classmethod
138
- def drop_table (self ):
144
+ def drop_table (self ) -> None :
139
145
with connection .cursor () as cursor :
140
146
cursor .execute ("DROP TABLE `%s`" % self .table_name )
141
147
@@ -154,10 +160,11 @@ def tearDownClass(cls):
154
160
super ().tearDownClass ()
155
161
cls .drop_table ()
156
162
157
- def table_count (self ):
163
+ def table_count (self ) -> int :
158
164
with connection .cursor () as cursor :
159
165
cursor .execute ("SELECT COUNT(*) FROM `%s`" % self .table_name )
160
- return cursor .fetchone ()[0 ]
166
+ count : int = cursor .fetchone ()[0 ]
167
+ return count
161
168
162
169
# These tests were copied from django's tests/cache/tests.py file
163
170
@@ -727,7 +734,7 @@ def test_cache_write_unpicklable_object(self):
727
734
fetch_middleware = FetchFromCacheMiddleware (empty_response )
728
735
729
736
request = self .factory .get ("/cache/test" )
730
- request ._cache_update_cache = True
737
+ request ._cache_update_cache = True # type: ignore [attr-defined]
731
738
get_cache_data = FetchFromCacheMiddleware (empty_response ).process_request (
732
739
request
733
740
)
@@ -780,10 +787,10 @@ def test_get_or_set_version(self):
780
787
cache .get_or_set ("brian" , 1979 , version = 2 )
781
788
782
789
with pytest .raises (TypeError , match = msg_re ):
783
- cache .get_or_set ("brian" )
790
+ cache .get_or_set ("brian" ) # type: ignore [call-arg]
784
791
785
792
with pytest .raises (TypeError , match = msg_re ):
786
- cache .get_or_set ("brian" , version = 1 )
793
+ cache .get_or_set ("brian" , version = 1 ) # type: ignore [call-arg]
787
794
788
795
assert cache .get ("brian" , version = 1 ) is None
789
796
assert cache .get_or_set ("brian" , 42 , version = 1 ) == 42
@@ -916,6 +923,7 @@ def func(key, *args):
916
923
# Original tests
917
924
918
925
def test_base_set_bad_value (self ):
926
+ assert isinstance (cache , MySQLCache )
919
927
with pytest .raises (ValueError ) as excinfo :
920
928
cache ._base_set ("foo" , "key" , "value" )
921
929
assert "'mode' should be" in str (excinfo .value )
@@ -998,7 +1006,9 @@ def test_cull_deletes_expired_first(self):
998
1006
self ._perform_cull_test (cull_cache , 30 , 30 )
999
1007
assert cull_cache .get ("key" ) is None
1000
1008
1001
- def _perform_cull_test (self , cull_cache , initial_count , final_count ):
1009
+ def _perform_cull_test (
1010
+ self , cull_cache : BaseCache , initial_count : int , final_count : int
1011
+ ) -> None :
1002
1012
# Create initial cache key entries. This will overflow the cache,
1003
1013
# causing a cull.
1004
1014
for i in range (1 , initial_count + 1 ):
@@ -1138,6 +1148,7 @@ def test_keys_with_prefix_version(self, cache_name):
1138
1148
1139
1149
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1140
1150
def test_keys_with_prefix_with_bad_cache (self ):
1151
+ assert isinstance (cache , MySQLCache )
1141
1152
with pytest .raises (ValueError ) as excinfo :
1142
1153
cache .keys_with_prefix ("" )
1143
1154
assert str (excinfo .value ).startswith ("To use the _with_prefix commands" )
@@ -1177,6 +1188,7 @@ def test_get_with_prefix_version(self, cache_name):
1177
1188
1178
1189
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1179
1190
def test_get_with_prefix_with_bad_cache (self ):
1191
+ assert isinstance (cache , MySQLCache )
1180
1192
with pytest .raises (ValueError ) as excinfo :
1181
1193
cache .get_with_prefix ("" )
1182
1194
assert str (excinfo .value ).startswith ("To use the _with_prefix commands" )
@@ -1234,6 +1246,7 @@ def test_delete_with_prefix_version(self, cache_name):
1234
1246
1235
1247
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1236
1248
def test_delete_with_prefix_with_no_reverse_works (self ):
1249
+ assert isinstance (cache , MySQLCache )
1237
1250
cache .set_many ({"K1" : "value" , "K2" : "value2" , "B2" : "Anothervalue" })
1238
1251
assert cache .delete_with_prefix ("K" ) == 2
1239
1252
assert cache .get_many (["K1" , "K2" , "B2" ]) == {"B2" : "Anothervalue" }
@@ -1263,6 +1276,7 @@ def test_mysql_cache_migration_no_mysql_caches(self):
1263
1276
def test_cull_max_entries_minus_one (self ):
1264
1277
# cull with MAX_ENTRIES = -1 should never clear anything that is not
1265
1278
# expired
1279
+ assert isinstance (cache , MySQLCache )
1266
1280
1267
1281
# one expired key
1268
1282
cache .set ("key" , "value" , 0.1 )
@@ -1342,7 +1356,7 @@ def test_mysql_cache_migration(self):
1342
1356
operation .database_backwards ("testapp" , editor , new_state , state )
1343
1357
assert not self .table_exists (self .table_name )
1344
1358
1345
- def table_exists (self , table_name ) :
1359
+ def table_exists (self , table_name : str ) -> bool :
1346
1360
with connection .cursor () as cursor :
1347
1361
cursor .execute (
1348
1362
"""SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
0 commit comments