Skip to content

Commit 7d0e051

Browse files
committed
Fix some errors in cache tests
1 parent d6edd92 commit 7d0e051

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 cache
1314
from django.core.cache import CacheKeyWarning
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

@@ -725,7 +732,7 @@ def test_cache_write_unpicklable_object(self):
725732
fetch_middleware = FetchFromCacheMiddleware(empty_response)
726733

727734
request = self.factory.get("/cache/test")
728-
request._cache_update_cache = True
735+
request._cache_update_cache = True # type: ignore [attr-defined]
729736
get_cache_data = FetchFromCacheMiddleware(empty_response).process_request(
730737
request
731738
)
@@ -778,10 +785,10 @@ def test_get_or_set_version(self):
778785
cache.get_or_set("brian", 1979, version=2)
779786

780787
with pytest.raises(TypeError, match=msg_re):
781-
cache.get_or_set("brian")
788+
cache.get_or_set("brian") # type: ignore [call-arg]
782789

783790
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]
785792

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

916923
def test_base_set_bad_value(self):
924+
assert isinstance(cache, MySQLCache)
917925
with pytest.raises(ValueError) as excinfo:
918926
cache._base_set("foo", "key", "value")
919927
assert "'mode' should be" in str(excinfo.value)
@@ -996,7 +1004,9 @@ def test_cull_deletes_expired_first(self):
9961004
self._perform_cull_test(cull_cache, 30, 30)
9971005
assert cull_cache.get("key") is None
9981006

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

11371147
@override_cache_settings(KEY_FUNCTION=custom_key_func)
11381148
def test_keys_with_prefix_with_bad_cache(self):
1149+
assert isinstance(cache, MySQLCache)
11391150
with pytest.raises(ValueError) as excinfo:
11401151
cache.keys_with_prefix("")
11411152
assert str(excinfo.value).startswith("To use the _with_prefix commands")
@@ -1175,6 +1186,7 @@ def test_get_with_prefix_version(self, cache_name):
11751186

11761187
@override_cache_settings(KEY_FUNCTION=custom_key_func)
11771188
def test_get_with_prefix_with_bad_cache(self):
1189+
assert isinstance(cache, MySQLCache)
11781190
with pytest.raises(ValueError) as excinfo:
11791191
cache.get_with_prefix("")
11801192
assert str(excinfo.value).startswith("To use the _with_prefix commands")
@@ -1232,6 +1244,7 @@ def test_delete_with_prefix_version(self, cache_name):
12321244

12331245
@override_cache_settings(KEY_FUNCTION=custom_key_func)
12341246
def test_delete_with_prefix_with_no_reverse_works(self):
1247+
assert isinstance(cache, MySQLCache)
12351248
cache.set_many({"K1": "value", "K2": "value2", "B2": "Anothervalue"})
12361249
assert cache.delete_with_prefix("K") == 2
12371250
assert cache.get_many(["K1", "K2", "B2"]) == {"B2": "Anothervalue"}
@@ -1261,6 +1274,7 @@ def test_mysql_cache_migration_no_mysql_caches(self):
12611274
def test_cull_max_entries_minus_one(self):
12621275
# cull with MAX_ENTRIES = -1 should never clear anything that is not
12631276
# expired
1277+
assert isinstance(cache, MySQLCache)
12641278

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

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

0 commit comments

Comments
 (0)