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
)
@@ -128,13 +134,13 @@ class MySQLCacheTableMixin(TransactionTestCase):
128
134
table_name = "test cache table"
129
135
130
136
@classmethod
131
- def create_table (self ):
137
+ def create_table (self ) -> None :
132
138
sql = MySQLCache .create_table_sql .format (table_name = self .table_name )
133
139
with connection .cursor () as cursor :
134
140
cursor .execute (sql )
135
141
136
142
@classmethod
137
- def drop_table (self ):
143
+ def drop_table (self ) -> None :
138
144
with connection .cursor () as cursor :
139
145
cursor .execute ("DROP TABLE `%s`" % self .table_name )
140
146
@@ -153,10 +159,11 @@ def tearDownClass(cls):
153
159
super ().tearDownClass ()
154
160
cls .drop_table ()
155
161
156
- def table_count (self ):
162
+ def table_count (self ) -> int :
157
163
with connection .cursor () as cursor :
158
164
cursor .execute ("SELECT COUNT(*) FROM `%s`" % self .table_name )
159
- return cursor .fetchone ()[0 ]
165
+ count : int = cursor .fetchone ()[0 ]
166
+ return count
160
167
161
168
# These tests were copied from django's tests/cache/tests.py file
162
169
@@ -725,7 +732,7 @@ def test_cache_write_unpicklable_object(self):
725
732
fetch_middleware = FetchFromCacheMiddleware (empty_response )
726
733
727
734
request = self .factory .get ("/cache/test" )
728
- request ._cache_update_cache = True
735
+ request ._cache_update_cache = True # type: ignore [attr-defined]
729
736
get_cache_data = FetchFromCacheMiddleware (empty_response ).process_request (
730
737
request
731
738
)
@@ -778,10 +785,10 @@ def test_get_or_set_version(self):
778
785
cache .get_or_set ("brian" , 1979 , version = 2 )
779
786
780
787
with pytest .raises (TypeError , match = msg_re ):
781
- cache .get_or_set ("brian" )
788
+ cache .get_or_set ("brian" ) # type: ignore [call-arg]
782
789
783
790
with pytest .raises (TypeError , match = msg_re ):
784
- cache .get_or_set ("brian" , version = 1 )
791
+ cache .get_or_set ("brian" , version = 1 ) # type: ignore [call-arg]
785
792
786
793
assert cache .get ("brian" , version = 1 ) is None
787
794
assert cache .get_or_set ("brian" , 42 , version = 1 ) == 42
@@ -914,6 +921,7 @@ def func(key, *args):
914
921
# Original tests
915
922
916
923
def test_base_set_bad_value (self ):
924
+ assert isinstance (cache , MySQLCache )
917
925
with pytest .raises (ValueError ) as excinfo :
918
926
cache ._base_set ("foo" , "key" , "value" )
919
927
assert "'mode' should be" in str (excinfo .value )
@@ -996,7 +1004,9 @@ def test_cull_deletes_expired_first(self):
996
1004
self ._perform_cull_test (cull_cache , 30 , 30 )
997
1005
assert cull_cache .get ("key" ) is None
998
1006
999
- def _perform_cull_test (self , cull_cache , initial_count , final_count ):
1007
+ def _perform_cull_test (
1008
+ self , cull_cache : BaseCache , initial_count : int , final_count : int
1009
+ ) -> None :
1000
1010
# Create initial cache key entries. This will overflow the cache,
1001
1011
# causing a cull.
1002
1012
for i in range (1 , initial_count + 1 ):
@@ -1136,6 +1146,7 @@ def test_keys_with_prefix_version(self, cache_name):
1136
1146
1137
1147
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1138
1148
def test_keys_with_prefix_with_bad_cache (self ):
1149
+ assert isinstance (cache , MySQLCache )
1139
1150
with pytest .raises (ValueError ) as excinfo :
1140
1151
cache .keys_with_prefix ("" )
1141
1152
assert str (excinfo .value ).startswith ("To use the _with_prefix commands" )
@@ -1175,6 +1186,7 @@ def test_get_with_prefix_version(self, cache_name):
1175
1186
1176
1187
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1177
1188
def test_get_with_prefix_with_bad_cache (self ):
1189
+ assert isinstance (cache , MySQLCache )
1178
1190
with pytest .raises (ValueError ) as excinfo :
1179
1191
cache .get_with_prefix ("" )
1180
1192
assert str (excinfo .value ).startswith ("To use the _with_prefix commands" )
@@ -1232,6 +1244,7 @@ def test_delete_with_prefix_version(self, cache_name):
1232
1244
1233
1245
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1234
1246
def test_delete_with_prefix_with_no_reverse_works (self ):
1247
+ assert isinstance (cache , MySQLCache )
1235
1248
cache .set_many ({"K1" : "value" , "K2" : "value2" , "B2" : "Anothervalue" })
1236
1249
assert cache .delete_with_prefix ("K" ) == 2
1237
1250
assert cache .get_many (["K1" , "K2" , "B2" ]) == {"B2" : "Anothervalue" }
@@ -1261,6 +1274,7 @@ def test_mysql_cache_migration_no_mysql_caches(self):
1261
1274
def test_cull_max_entries_minus_one (self ):
1262
1275
# cull with MAX_ENTRIES = -1 should never clear anything that is not
1263
1276
# expired
1277
+ assert isinstance (cache , MySQLCache )
1264
1278
1265
1279
# one expired key
1266
1280
cache .set ("key" , "value" , 0.1 )
@@ -1340,7 +1354,7 @@ def test_mysql_cache_migration(self):
1340
1354
operation .database_backwards ("testapp" , editor , new_state , state )
1341
1355
assert not self .table_exists (self .table_name )
1342
1356
1343
- def table_exists (self , table_name ) :
1357
+ def table_exists (self , table_name : str ) -> bool :
1344
1358
with connection .cursor () as cursor :
1345
1359
cursor .execute (
1346
1360
"""SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
0 commit comments