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 CacheKeyWarning
13
14
from django .core .cache import cache
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
@@ -726,7 +733,7 @@ def test_cache_write_unpicklable_object(self):
726
733
fetch_middleware = FetchFromCacheMiddleware (empty_response )
727
734
728
735
request = self .factory .get ("/cache/test" )
729
- request ._cache_update_cache = True
736
+ request ._cache_update_cache = True # type: ignore [attr-defined]
730
737
get_cache_data = FetchFromCacheMiddleware (empty_response ).process_request (
731
738
request
732
739
)
@@ -779,10 +786,10 @@ def test_get_or_set_version(self):
779
786
cache .get_or_set ("brian" , 1979 , version = 2 )
780
787
781
788
with pytest .raises (TypeError , match = msg_re ):
782
- cache .get_or_set ("brian" )
789
+ cache .get_or_set ("brian" ) # type: ignore [call-arg]
783
790
784
791
with pytest .raises (TypeError , match = msg_re ):
785
- cache .get_or_set ("brian" , version = 1 )
792
+ cache .get_or_set ("brian" , version = 1 ) # type: ignore [call-arg]
786
793
787
794
assert cache .get ("brian" , version = 1 ) is None
788
795
assert cache .get_or_set ("brian" , 42 , version = 1 ) == 42
@@ -915,6 +922,7 @@ def func(key, *args):
915
922
# Original tests
916
923
917
924
def test_base_set_bad_value (self ):
925
+ assert isinstance (cache , MySQLCache )
918
926
with pytest .raises (ValueError ) as excinfo :
919
927
cache ._base_set ("foo" , "key" , "value" )
920
928
assert "'mode' should be" in str (excinfo .value )
@@ -997,7 +1005,9 @@ def test_cull_deletes_expired_first(self):
997
1005
self ._perform_cull_test (cull_cache , 30 , 30 )
998
1006
assert cull_cache .get ("key" ) is None
999
1007
1000
- def _perform_cull_test (self , cull_cache , initial_count , final_count ):
1008
+ def _perform_cull_test (
1009
+ self , cull_cache : BaseCache , initial_count : int , final_count : int
1010
+ ) -> None :
1001
1011
# Create initial cache key entries. This will overflow the cache,
1002
1012
# causing a cull.
1003
1013
for i in range (1 , initial_count + 1 ):
@@ -1137,6 +1147,7 @@ def test_keys_with_prefix_version(self, cache_name):
1137
1147
1138
1148
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1139
1149
def test_keys_with_prefix_with_bad_cache (self ):
1150
+ assert isinstance (cache , MySQLCache )
1140
1151
with pytest .raises (ValueError ) as excinfo :
1141
1152
cache .keys_with_prefix ("" )
1142
1153
assert str (excinfo .value ).startswith ("To use the _with_prefix commands" )
@@ -1176,6 +1187,7 @@ def test_get_with_prefix_version(self, cache_name):
1176
1187
1177
1188
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1178
1189
def test_get_with_prefix_with_bad_cache (self ):
1190
+ assert isinstance (cache , MySQLCache )
1179
1191
with pytest .raises (ValueError ) as excinfo :
1180
1192
cache .get_with_prefix ("" )
1181
1193
assert str (excinfo .value ).startswith ("To use the _with_prefix commands" )
@@ -1233,6 +1245,7 @@ def test_delete_with_prefix_version(self, cache_name):
1233
1245
1234
1246
@override_cache_settings (KEY_FUNCTION = custom_key_func )
1235
1247
def test_delete_with_prefix_with_no_reverse_works (self ):
1248
+ assert isinstance (cache , MySQLCache )
1236
1249
cache .set_many ({"K1" : "value" , "K2" : "value2" , "B2" : "Anothervalue" })
1237
1250
assert cache .delete_with_prefix ("K" ) == 2
1238
1251
assert cache .get_many (["K1" , "K2" , "B2" ]) == {"B2" : "Anothervalue" }
@@ -1262,6 +1275,7 @@ def test_mysql_cache_migration_no_mysql_caches(self):
1262
1275
def test_cull_max_entries_minus_one (self ):
1263
1276
# cull with MAX_ENTRIES = -1 should never clear anything that is not
1264
1277
# expired
1278
+ assert isinstance (cache , MySQLCache )
1265
1279
1266
1280
# one expired key
1267
1281
cache .set ("key" , "value" , 0.1 )
@@ -1341,7 +1355,7 @@ def test_mysql_cache_migration(self):
1341
1355
operation .database_backwards ("testapp" , editor , new_state , state )
1342
1356
assert not self .table_exists (self .table_name )
1343
1357
1344
- def table_exists (self , table_name ) :
1358
+ def table_exists (self , table_name : str ) -> bool :
1345
1359
with connection .cursor () as cursor :
1346
1360
cursor .execute (
1347
1361
"""SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
0 commit comments