From cae3ff95208344d3c1901cf34ed13c61eabf34dd Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 19 Oct 2022 09:41:27 +0100 Subject: [PATCH] Fix some errors in cache tests --- tests/testapp/test_cache.py | 39 ++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/tests/testapp/test_cache.py b/tests/testapp/test_cache.py index aad4f11b..0cd10eb4 100644 --- a/tests/testapp/test_cache.py +++ b/tests/testapp/test_cache.py @@ -9,7 +9,7 @@ from typing import Any import pytest -from django.core.cache import CacheKeyWarning, cache, caches +from django.core.cache import BaseCache, CacheKeyWarning, cache, caches from django.core.management import CommandError, call_command from django.db import IntegrityError, connection from django.db.migrations.state import ProjectState @@ -90,7 +90,10 @@ def reverse_custom_key_func(full_key): } -def caches_setting_for_tests(options=None, **params): +def caches_setting_for_tests( + options: dict[str, Any] | None = None, + **params: Any, +) -> dict[str, Any]: # `params` are test specific overrides and `_caches_settings_base` is the # base config for the tests. # This results in the following search order: @@ -107,8 +110,10 @@ def caches_setting_for_tests(options=None, **params): # Spaces are used in the table name to ensure quoting/escaping is working def override_cache_settings( - BACKEND="django_mysql.cache.MySQLCache", LOCATION="test cache table", **kwargs -): + BACKEND: str = "django_mysql.cache.MySQLCache", + LOCATION: str = "test cache table", + **kwargs: Any, +) -> override_settings: return override_settings( CACHES=caches_setting_for_tests(BACKEND=BACKEND, LOCATION=LOCATION, **kwargs) ) @@ -119,13 +124,13 @@ class MySQLCacheTableMixin(TransactionTestCase): table_name = "test cache table" @classmethod - def create_table(self): + def create_table(self) -> None: sql = MySQLCache.create_table_sql.format(table_name=self.table_name) with connection.cursor() as cursor: cursor.execute(sql) @classmethod - def drop_table(self): + def drop_table(self) -> None: with connection.cursor() as cursor: cursor.execute("DROP TABLE `%s`" % self.table_name) @@ -144,10 +149,11 @@ def tearDownClass(cls): super().tearDownClass() cls.drop_table() - def table_count(self): + def table_count(self) -> int: with connection.cursor() as cursor: cursor.execute("SELECT COUNT(*) FROM `%s`" % self.table_name) - return cursor.fetchone()[0] + count: int = cursor.fetchone()[0] + return count # These tests were copied from django's tests/cache/tests.py file @@ -717,7 +723,7 @@ def test_cache_write_unpicklable_object(self): fetch_middleware = FetchFromCacheMiddleware(empty_response) request = self.factory.get("/cache/test") - request._cache_update_cache = True + request._cache_update_cache = True # type: ignore [attr-defined] get_cache_data = FetchFromCacheMiddleware(empty_response).process_request( request ) @@ -770,10 +776,10 @@ def test_get_or_set_version(self): cache.get_or_set("brian", 1979, version=2) with pytest.raises(TypeError, match=msg_re): - cache.get_or_set("brian") + cache.get_or_set("brian") # type: ignore [call-arg] with pytest.raises(TypeError, match=msg_re): - cache.get_or_set("brian", version=1) + cache.get_or_set("brian", version=1) # type: ignore [call-arg] assert cache.get("brian", version=1) is None assert cache.get_or_set("brian", 42, version=1) == 42 @@ -906,6 +912,7 @@ def func(key, *args): # Original tests def test_base_set_bad_value(self): + assert isinstance(cache, MySQLCache) with pytest.raises(ValueError) as excinfo: cache._base_set("foo", "key", "value") assert "'mode' should be" in str(excinfo.value) @@ -988,7 +995,9 @@ def test_cull_deletes_expired_first(self): self._perform_cull_test(cull_cache, 30, 30) assert cull_cache.get("key") is None - def _perform_cull_test(self, cull_cache, initial_count, final_count): + def _perform_cull_test( + self, cull_cache: BaseCache, initial_count: int, final_count: int + ) -> None: # Create initial cache key entries. This will overflow the cache, # causing a cull. for i in range(1, initial_count + 1): @@ -1128,6 +1137,7 @@ def test_keys_with_prefix_version(self, cache_name): @override_cache_settings(KEY_FUNCTION=custom_key_func) def test_keys_with_prefix_with_bad_cache(self): + assert isinstance(cache, MySQLCache) with pytest.raises(ValueError) as excinfo: cache.keys_with_prefix("") assert str(excinfo.value).startswith("To use the _with_prefix commands") @@ -1167,6 +1177,7 @@ def test_get_with_prefix_version(self, cache_name): @override_cache_settings(KEY_FUNCTION=custom_key_func) def test_get_with_prefix_with_bad_cache(self): + assert isinstance(cache, MySQLCache) with pytest.raises(ValueError) as excinfo: cache.get_with_prefix("") assert str(excinfo.value).startswith("To use the _with_prefix commands") @@ -1224,6 +1235,7 @@ def test_delete_with_prefix_version(self, cache_name): @override_cache_settings(KEY_FUNCTION=custom_key_func) def test_delete_with_prefix_with_no_reverse_works(self): + assert isinstance(cache, MySQLCache) cache.set_many({"K1": "value", "K2": "value2", "B2": "Anothervalue"}) assert cache.delete_with_prefix("K") == 2 assert cache.get_many(["K1", "K2", "B2"]) == {"B2": "Anothervalue"} @@ -1253,6 +1265,7 @@ def test_mysql_cache_migration_no_mysql_caches(self): def test_cull_max_entries_minus_one(self): # cull with MAX_ENTRIES = -1 should never clear anything that is not # expired + assert isinstance(cache, MySQLCache) # one expired key cache.set("key", "value", 0.1) @@ -1332,7 +1345,7 @@ def test_mysql_cache_migration(self): operation.database_backwards("testapp", editor, new_state, state) assert not self.table_exists(self.table_name) - def table_exists(self, table_name): + def table_exists(self, table_name: str) -> bool: with connection.cursor() as cursor: cursor.execute( """SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES