From 817df51695a6f082e7da6a449f8b84fc3b25d8fd Mon Sep 17 00:00:00 2001 From: Pavel Tisnovsky Date: Wed, 5 Nov 2025 10:00:06 +0100 Subject: [PATCH] LCORE-888: Fix issues in cache unit tests found by Pyright linter --- tests/unit/cache/test_cache_factory.py | 10 +++++++--- tests/unit/cache/test_postgres_cache.py | 20 +++++++++++++++----- tests/unit/cache/test_sqlite_cache.py | 8 ++++++-- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/tests/unit/cache/test_cache_factory.py b/tests/unit/cache/test_cache_factory.py index 4461738be..7f5a11af0 100644 --- a/tests/unit/cache/test_cache_factory.py +++ b/tests/unit/cache/test_cache_factory.py @@ -4,6 +4,7 @@ import pytest from pytest_mock import MockerFixture +from pydantic import SecretStr from constants import ( CACHE_TYPE_NOOP, @@ -46,7 +47,7 @@ def postgres_cache_config() -> ConversationCacheConfiguration: return ConversationCacheConfiguration( type=CACHE_TYPE_POSTGRES, postgres=PostgreSQLDatabaseConfiguration( - db="database", user="user", password="password" + db="database", user="user", password=SecretStr("password") ), ) @@ -64,7 +65,8 @@ def sqlite_cache_config(tmpdir: Path) -> ConversationCacheConfiguration: def invalid_cache_type_config() -> ConversationCacheConfiguration: """Fixture containing instance of ConversationCacheConfiguration with improper settings.""" c = ConversationCacheConfiguration() - c.type = "foo bar baz" + # the conversation cache type name is incorrect in purpose + c.type = "foo bar baz" # pyright: ignore return c @@ -136,7 +138,9 @@ def test_conversation_cache_postgres_improper_config() -> None: """Check if PostgreSQL cache configuration is checked in cache factory.""" cc = ConversationCacheConfiguration( type=CACHE_TYPE_POSTGRES, - postgres=PostgreSQLDatabaseConfiguration(db="db", user="u", password="p"), + postgres=PostgreSQLDatabaseConfiguration( + db="db", user="u", password=SecretStr("p") + ), ) # simulate improper configuration (can not be done directly as model checks this) cc.postgres = None diff --git a/tests/unit/cache/test_postgres_cache.py b/tests/unit/cache/test_postgres_cache.py index 8a9bd2a44..0c161223a 100644 --- a/tests/unit/cache/test_postgres_cache.py +++ b/tests/unit/cache/test_postgres_cache.py @@ -6,6 +6,7 @@ import pytest from pytest_mock import MockerFixture +from pydantic import SecretStr, AnyUrl import psycopg2 @@ -71,7 +72,11 @@ def postgres_cache_config() -> PostgreSQLDatabaseConfiguration: # can be any configuration, becuase tests won't really try to # connect to database return PostgreSQLDatabaseConfiguration( - host="localhost", port=1234, db="database", user="user", password="password" + host="localhost", + port=1234, + db="database", + user="user", + password=SecretStr("password"), ) @@ -158,7 +163,8 @@ def test_connected_when_connection_error( mocker.patch("psycopg2.connect") # simulate connection error cache = PostgresCache(postgres_cache_config_fixture) - cache.connection = ConnectionMock() + # connection does not have to have proper type + cache.connection = ConnectionMock() # pyright: ignore assert cache.connection is not None assert cache.connected() is False @@ -282,7 +288,8 @@ def test_insert_or_append_operation_operation_error( # no operation for @connection decorator cache.connect = lambda: None - cache.connection = ConnectionMock() + # connection does not have to have proper type + cache.connection = ConnectionMock() # pyright: ignore with pytest.raises(CacheError, match="insert_or_append"): cache.insert_or_append(USER_ID_1, CONVERSATION_ID_1, cache_entry_1, False) @@ -335,7 +342,8 @@ def test_delete_operation_operation_error( # no operation for @connection decorator cache.connect = lambda: None - cache.connection = ConnectionMock() + # connection does not have to have proper type + cache.connection = ConnectionMock() # pyright: ignore with pytest.raises(CacheError, match="delete"): cache.delete(USER_ID_1, CONVERSATION_ID_1, False) @@ -458,7 +466,9 @@ def test_insert_and_get_with_referenced_documents( mock_cursor = mock_connection.cursor.return_value.__enter__.return_value # Create a CacheEntry with referenced documents - docs = [ReferencedDocument(doc_title="Test Doc", doc_url="http://example.com/")] + docs = [ + ReferencedDocument(doc_title="Test Doc", doc_url=AnyUrl("http://example.com/")) + ] entry_with_docs = CacheEntry( query="user message", response="AI message", diff --git a/tests/unit/cache/test_sqlite_cache.py b/tests/unit/cache/test_sqlite_cache.py index 907950a98..48617f7ea 100644 --- a/tests/unit/cache/test_sqlite_cache.py +++ b/tests/unit/cache/test_sqlite_cache.py @@ -6,6 +6,7 @@ import sqlite3 +from pydantic import AnyUrl import pytest from models.config import SQLiteDatabaseConfiguration @@ -101,7 +102,8 @@ def test_connected_when_connection_error(tmpdir: Path) -> None: """Test the connected() method.""" # simulate connection error cache = create_cache(tmpdir) - cache.connection = ConnectionMock() + # connection can have any type + cache.connection = ConnectionMock() # pyright: ignore assert cache.connection is not None assert cache.connected() is False @@ -370,7 +372,9 @@ def test_insert_and_get_with_referenced_documents(tmpdir: Path) -> None: cache = create_cache(tmpdir) # Create a CacheEntry with referenced documents - docs = [ReferencedDocument(doc_title="Test Doc", doc_url="http://example.com")] + docs = [ + ReferencedDocument(doc_title="Test Doc", doc_url=AnyUrl("http://example.com")) + ] entry_with_docs = CacheEntry( query="user message", response="AI message",