Skip to content

Commit c6ebfbc

Browse files
committed
Fix some errors in cache tests
1 parent aa656f5 commit c6ebfbc

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
)
@@ -129,13 +135,13 @@ class MySQLCacheTableMixin(TransactionTestCase):
129135
table_name = "test cache table"
130136

131137
@classmethod
132-
def create_table(self):
138+
def create_table(self) -> None:
133139
sql = MySQLCache.create_table_sql.format(table_name=self.table_name)
134140
with connection.cursor() as cursor:
135141
cursor.execute(sql)
136142

137143
@classmethod
138-
def drop_table(self):
144+
def drop_table(self) -> None:
139145
with connection.cursor() as cursor:
140146
cursor.execute("DROP TABLE `%s`" % self.table_name)
141147

@@ -154,10 +160,11 @@ def tearDownClass(cls):
154160
super().tearDownClass()
155161
cls.drop_table()
156162

157-
def table_count(self):
163+
def table_count(self) -> int:
158164
with connection.cursor() as cursor:
159165
cursor.execute("SELECT COUNT(*) FROM `%s`" % self.table_name)
160-
return cursor.fetchone()[0]
166+
count: int = cursor.fetchone()[0]
167+
return count
161168

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

@@ -727,7 +734,7 @@ def test_cache_write_unpicklable_object(self):
727734
fetch_middleware = FetchFromCacheMiddleware(empty_response)
728735

729736
request = self.factory.get("/cache/test")
730-
request._cache_update_cache = True
737+
request._cache_update_cache = True # type: ignore [attr-defined]
731738
get_cache_data = FetchFromCacheMiddleware(empty_response).process_request(
732739
request
733740
)
@@ -780,10 +787,10 @@ def test_get_or_set_version(self):
780787
cache.get_or_set("brian", 1979, version=2)
781788

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

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

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

918925
def test_base_set_bad_value(self):
926+
assert isinstance(cache, MySQLCache)
919927
with pytest.raises(ValueError) as excinfo:
920928
cache._base_set("foo", "key", "value")
921929
assert "'mode' should be" in str(excinfo.value)
@@ -998,7 +1006,9 @@ def test_cull_deletes_expired_first(self):
9981006
self._perform_cull_test(cull_cache, 30, 30)
9991007
assert cull_cache.get("key") is None
10001008

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

11391149
@override_cache_settings(KEY_FUNCTION=custom_key_func)
11401150
def test_keys_with_prefix_with_bad_cache(self):
1151+
assert isinstance(cache, MySQLCache)
11411152
with pytest.raises(ValueError) as excinfo:
11421153
cache.keys_with_prefix("")
11431154
assert str(excinfo.value).startswith("To use the _with_prefix commands")
@@ -1177,6 +1188,7 @@ def test_get_with_prefix_version(self, cache_name):
11771188

11781189
@override_cache_settings(KEY_FUNCTION=custom_key_func)
11791190
def test_get_with_prefix_with_bad_cache(self):
1191+
assert isinstance(cache, MySQLCache)
11801192
with pytest.raises(ValueError) as excinfo:
11811193
cache.get_with_prefix("")
11821194
assert str(excinfo.value).startswith("To use the _with_prefix commands")
@@ -1234,6 +1246,7 @@ def test_delete_with_prefix_version(self, cache_name):
12341246

12351247
@override_cache_settings(KEY_FUNCTION=custom_key_func)
12361248
def test_delete_with_prefix_with_no_reverse_works(self):
1249+
assert isinstance(cache, MySQLCache)
12371250
cache.set_many({"K1": "value", "K2": "value2", "B2": "Anothervalue"})
12381251
assert cache.delete_with_prefix("K") == 2
12391252
assert cache.get_many(["K1", "K2", "B2"]) == {"B2": "Anothervalue"}
@@ -1263,6 +1276,7 @@ def test_mysql_cache_migration_no_mysql_caches(self):
12631276
def test_cull_max_entries_minus_one(self):
12641277
# cull with MAX_ENTRIES = -1 should never clear anything that is not
12651278
# expired
1279+
assert isinstance(cache, MySQLCache)
12661280

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

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

0 commit comments

Comments
 (0)