Skip to content

Commit 1ce99b7

Browse files
committed
Fix some errors in cache tests
1 parent 0d70001 commit 1ce99b7

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

tests/testapp/test_cache.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import Any
1010

1111
import pytest
12+
from django.core.cache import BaseCache
1213
from django.core.cache import CacheKeyWarning
1314
from django.core.cache import cache
1415
from django.core.cache import caches
@@ -100,7 +101,10 @@ def reverse_custom_key_func(full_key):
100101
}
101102

102103

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]:
104108
# `params` are test specific overrides and `_caches_settings_base` is the
105109
# base config for the tests.
106110
# This results in the following search order:
@@ -117,8 +121,10 @@ def caches_setting_for_tests(options=None, **params):
117121

118122
# Spaces are used in the table name to ensure quoting/escaping is working
119123
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:
122128
return override_settings(
123129
CACHES=caches_setting_for_tests(BACKEND=BACKEND, LOCATION=LOCATION, **kwargs)
124130
)
@@ -128,13 +134,13 @@ class MySQLCacheTableMixin(TransactionTestCase):
128134
table_name = "test cache table"
129135

130136
@classmethod
131-
def create_table(self):
137+
def create_table(self) -> None:
132138
sql = MySQLCache.create_table_sql.format(table_name=self.table_name)
133139
with connection.cursor() as cursor:
134140
cursor.execute(sql)
135141

136142
@classmethod
137-
def drop_table(self):
143+
def drop_table(self) -> None:
138144
with connection.cursor() as cursor:
139145
cursor.execute("DROP TABLE `%s`" % self.table_name)
140146

@@ -153,10 +159,11 @@ def tearDownClass(cls):
153159
super().tearDownClass()
154160
cls.drop_table()
155161

156-
def table_count(self):
162+
def table_count(self) -> int:
157163
with connection.cursor() as cursor:
158164
cursor.execute("SELECT COUNT(*) FROM `%s`" % self.table_name)
159-
return cursor.fetchone()[0]
165+
count: int = cursor.fetchone()[0]
166+
return count
160167

161168
# These tests were copied from django's tests/cache/tests.py file
162169

@@ -726,7 +733,7 @@ def test_cache_write_unpicklable_object(self):
726733
fetch_middleware = FetchFromCacheMiddleware(empty_response)
727734

728735
request = self.factory.get("/cache/test")
729-
request._cache_update_cache = True
736+
request._cache_update_cache = True # type: ignore [attr-defined]
730737
get_cache_data = FetchFromCacheMiddleware(empty_response).process_request(
731738
request
732739
)
@@ -779,10 +786,10 @@ def test_get_or_set_version(self):
779786
cache.get_or_set("brian", 1979, version=2)
780787

781788
with pytest.raises(TypeError, match=msg_re):
782-
cache.get_or_set("brian")
789+
cache.get_or_set("brian") # type: ignore [call-arg]
783790

784791
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]
786793

787794
assert cache.get("brian", version=1) is None
788795
assert cache.get_or_set("brian", 42, version=1) == 42
@@ -915,6 +922,7 @@ def func(key, *args):
915922
# Original tests
916923

917924
def test_base_set_bad_value(self):
925+
assert isinstance(cache, MySQLCache)
918926
with pytest.raises(ValueError) as excinfo:
919927
cache._base_set("foo", "key", "value")
920928
assert "'mode' should be" in str(excinfo.value)
@@ -997,7 +1005,9 @@ def test_cull_deletes_expired_first(self):
9971005
self._perform_cull_test(cull_cache, 30, 30)
9981006
assert cull_cache.get("key") is None
9991007

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:
10011011
# Create initial cache key entries. This will overflow the cache,
10021012
# causing a cull.
10031013
for i in range(1, initial_count + 1):
@@ -1137,6 +1147,7 @@ def test_keys_with_prefix_version(self, cache_name):
11371147

11381148
@override_cache_settings(KEY_FUNCTION=custom_key_func)
11391149
def test_keys_with_prefix_with_bad_cache(self):
1150+
assert isinstance(cache, MySQLCache)
11401151
with pytest.raises(ValueError) as excinfo:
11411152
cache.keys_with_prefix("")
11421153
assert str(excinfo.value).startswith("To use the _with_prefix commands")
@@ -1176,6 +1187,7 @@ def test_get_with_prefix_version(self, cache_name):
11761187

11771188
@override_cache_settings(KEY_FUNCTION=custom_key_func)
11781189
def test_get_with_prefix_with_bad_cache(self):
1190+
assert isinstance(cache, MySQLCache)
11791191
with pytest.raises(ValueError) as excinfo:
11801192
cache.get_with_prefix("")
11811193
assert str(excinfo.value).startswith("To use the _with_prefix commands")
@@ -1233,6 +1245,7 @@ def test_delete_with_prefix_version(self, cache_name):
12331245

12341246
@override_cache_settings(KEY_FUNCTION=custom_key_func)
12351247
def test_delete_with_prefix_with_no_reverse_works(self):
1248+
assert isinstance(cache, MySQLCache)
12361249
cache.set_many({"K1": "value", "K2": "value2", "B2": "Anothervalue"})
12371250
assert cache.delete_with_prefix("K") == 2
12381251
assert cache.get_many(["K1", "K2", "B2"]) == {"B2": "Anothervalue"}
@@ -1262,6 +1275,7 @@ def test_mysql_cache_migration_no_mysql_caches(self):
12621275
def test_cull_max_entries_minus_one(self):
12631276
# cull with MAX_ENTRIES = -1 should never clear anything that is not
12641277
# expired
1278+
assert isinstance(cache, MySQLCache)
12651279

12661280
# one expired key
12671281
cache.set("key", "value", 0.1)
@@ -1341,7 +1355,7 @@ def test_mysql_cache_migration(self):
13411355
operation.database_backwards("testapp", editor, new_state, state)
13421356
assert not self.table_exists(self.table_name)
13431357

1344-
def table_exists(self, table_name):
1358+
def table_exists(self, table_name: str) -> bool:
13451359
with connection.cursor() as cursor:
13461360
cursor.execute(
13471361
"""SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES

0 commit comments

Comments
 (0)