diff --git a/tests/unit/cache/test_cache_factory.py b/tests/unit/cache/test_cache_factory.py index 05054290..4461738b 100644 --- a/tests/unit/cache/test_cache_factory.py +++ b/tests/unit/cache/test_cache_factory.py @@ -1,5 +1,7 @@ """Unit tests for CacheFactory class.""" +from pathlib import Path + import pytest from pytest_mock import MockerFixture @@ -25,13 +27,13 @@ @pytest.fixture(scope="module", name="noop_cache_config_fixture") -def noop_cache_config(): +def noop_cache_config() -> ConversationCacheConfiguration: """Fixture containing initialized instance of ConversationCacheConfiguration.""" return ConversationCacheConfiguration(type=CACHE_TYPE_NOOP) @pytest.fixture(scope="module", name="memory_cache_config_fixture") -def memory_cache_config(): +def memory_cache_config() -> ConversationCacheConfiguration: """Fixture containing initialized instance of InMemory cache.""" return ConversationCacheConfiguration( type=CACHE_TYPE_MEMORY, memory=InMemoryCacheConfig(max_entries=10) @@ -39,7 +41,7 @@ def memory_cache_config(): @pytest.fixture(scope="module", name="postgres_cache_config_fixture") -def postgres_cache_config(): +def postgres_cache_config() -> ConversationCacheConfiguration: """Fixture containing initialized instance of PostgreSQL cache.""" return ConversationCacheConfiguration( type=CACHE_TYPE_POSTGRES, @@ -50,7 +52,7 @@ def postgres_cache_config(): @pytest.fixture(name="sqlite_cache_config_fixture") -def sqlite_cache_config(tmpdir): +def sqlite_cache_config(tmpdir: Path) -> ConversationCacheConfiguration: """Fixture containing initialized instance of SQLite cache.""" db_path = str(tmpdir / "test.sqlite") return ConversationCacheConfiguration( @@ -59,14 +61,16 @@ def sqlite_cache_config(tmpdir): @pytest.fixture(scope="module", name="invalid_cache_type_config_fixture") -def invalid_cache_type_config(): +def invalid_cache_type_config() -> ConversationCacheConfiguration: """Fixture containing instance of ConversationCacheConfiguration with improper settings.""" c = ConversationCacheConfiguration() c.type = "foo bar baz" return c -def test_conversation_cache_noop(noop_cache_config_fixture): +def test_conversation_cache_noop( + noop_cache_config_fixture: ConversationCacheConfiguration, +) -> None: """Check if NoopCache is returned by factory with proper configuration.""" cache = CacheFactory.conversation_cache(noop_cache_config_fixture) assert cache is not None @@ -74,7 +78,9 @@ def test_conversation_cache_noop(noop_cache_config_fixture): assert isinstance(cache, NoopCache) -def test_conversation_cache_in_memory(memory_cache_config_fixture): +def test_conversation_cache_in_memory( + memory_cache_config_fixture: ConversationCacheConfiguration, +) -> None: """Check if InMemoryCache is returned by factory with proper configuration.""" cache = CacheFactory.conversation_cache(memory_cache_config_fixture) assert cache is not None @@ -82,7 +88,7 @@ def test_conversation_cache_in_memory(memory_cache_config_fixture): assert isinstance(cache, InMemoryCache) -def test_conversation_cache_in_memory_improper_config(): +def test_conversation_cache_in_memory_improper_config() -> None: """Check if memory cache configuration is checked in cache factory.""" cc = ConversationCacheConfiguration( type=CACHE_TYPE_MEMORY, memory=InMemoryCacheConfig(max_entries=10) @@ -93,7 +99,9 @@ def test_conversation_cache_in_memory_improper_config(): _ = CacheFactory.conversation_cache(cc) -def test_conversation_cache_sqlite(sqlite_cache_config_fixture): +def test_conversation_cache_sqlite( + sqlite_cache_config_fixture: ConversationCacheConfiguration, +) -> None: """Check if SQLiteCache is returned by factory with proper configuration.""" cache = CacheFactory.conversation_cache(sqlite_cache_config_fixture) assert cache is not None @@ -101,7 +109,7 @@ def test_conversation_cache_sqlite(sqlite_cache_config_fixture): assert isinstance(cache, SQLiteCache) -def test_conversation_cache_sqlite_improper_config(tmpdir): +def test_conversation_cache_sqlite_improper_config(tmpdir: Path) -> None: """Check if memory cache configuration is checked in cache factory.""" db_path = str(tmpdir / "test.sqlite") cc = ConversationCacheConfiguration( @@ -114,8 +122,8 @@ def test_conversation_cache_sqlite_improper_config(tmpdir): def test_conversation_cache_postgres( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: ConversationCacheConfiguration, mocker: MockerFixture +) -> None: """Check if PostgreSQL is returned by factory with proper configuration.""" mocker.patch("psycopg2.connect") cache = CacheFactory.conversation_cache(postgres_cache_config_fixture) @@ -124,7 +132,7 @@ def test_conversation_cache_postgres( assert isinstance(cache, PostgresCache) -def test_conversation_cache_postgres_improper_config(): +def test_conversation_cache_postgres_improper_config() -> None: """Check if PostgreSQL cache configuration is checked in cache factory.""" cc = ConversationCacheConfiguration( type=CACHE_TYPE_POSTGRES, @@ -138,7 +146,7 @@ def test_conversation_cache_postgres_improper_config(): _ = CacheFactory.conversation_cache(cc) -def test_conversation_cache_no_type(): +def test_conversation_cache_no_type() -> None: """Check if wrong cache configuration is detected properly.""" cc = ConversationCacheConfiguration(type=CACHE_TYPE_NOOP) # simulate improper configuration (can not be done directly as model checks this) @@ -147,7 +155,9 @@ def test_conversation_cache_no_type(): CacheFactory.conversation_cache(cc) -def test_conversation_cache_wrong_cache(invalid_cache_type_config_fixture): +def test_conversation_cache_wrong_cache( + invalid_cache_type_config_fixture: ConversationCacheConfiguration, +) -> None: """Check if wrong cache configuration is detected properly.""" with pytest.raises(ValueError, match="Invalid cache type"): CacheFactory.conversation_cache(invalid_cache_type_config_fixture) diff --git a/tests/unit/cache/test_noop_cache.py b/tests/unit/cache/test_noop_cache.py index aad543d2..0ad3ac9d 100644 --- a/tests/unit/cache/test_noop_cache.py +++ b/tests/unit/cache/test_noop_cache.py @@ -3,8 +3,8 @@ import pytest from models.cache_entry import CacheEntry -from cache.noop_cache import NoopCache from utils import suid +from cache.noop_cache import NoopCache USER_ID = suid.get_suid() CONVERSATION_ID = suid.get_suid() @@ -28,19 +28,19 @@ @pytest.fixture(name="cache_fixture") -def cache(): +def cache() -> NoopCache: """Fixture with constucted and initialized in memory cache object.""" c = NoopCache() c.initialize_cache() return c -def test_connect(cache_fixture): +def test_connect(cache_fixture: NoopCache) -> None: """Test the behavior of connect method.""" cache_fixture.connect() -def test_insert_or_append(cache_fixture): +def test_insert_or_append(cache_fixture: NoopCache) -> None: """Test the behavior of insert_or_append method.""" cache_fixture.insert_or_append( USER_ID, @@ -49,7 +49,7 @@ def test_insert_or_append(cache_fixture): ) -def test_insert_or_append_skip_user_id_check(cache_fixture): +def test_insert_or_append_skip_user_id_check(cache_fixture: NoopCache) -> None: """Test the behavior of insert_or_append method.""" skip_user_id_check = True cache_fixture.insert_or_append( @@ -57,7 +57,7 @@ def test_insert_or_append_skip_user_id_check(cache_fixture): ) -def test_insert_or_append_existing_key(cache_fixture): +def test_insert_or_append_existing_key(cache_fixture: NoopCache) -> None: """Test the behavior of insert_or_append method for existing item.""" cache_fixture.insert_or_append( USER_ID, @@ -71,7 +71,7 @@ def test_insert_or_append_existing_key(cache_fixture): ) -def test_get_nonexistent_user(cache_fixture): +def test_get_nonexistent_user(cache_fixture: NoopCache) -> None: """Test how non-existent items are handled by the cache.""" # this UUID is different from DEFAULT_USER_UID assert ( @@ -79,7 +79,7 @@ def test_get_nonexistent_user(cache_fixture): ) -def test_delete_existing_conversation(cache_fixture): +def test_delete_existing_conversation(cache_fixture: NoopCache) -> None: """Test deleting an existing conversation.""" cache_fixture.insert_or_append(USER_ID, CONVERSATION_ID, cache_entry_1) @@ -88,19 +88,19 @@ def test_delete_existing_conversation(cache_fixture): assert result is True -def test_delete_nonexistent_conversation(cache_fixture): +def test_delete_nonexistent_conversation(cache_fixture: NoopCache) -> None: """Test deleting a conversation that doesn't exist.""" result = cache_fixture.delete(USER_ID, CONVERSATION_ID) assert result is True -def test_delete_improper_conversation_id(cache_fixture): +def test_delete_improper_conversation_id(cache_fixture: NoopCache) -> None: """Test delete with invalid conversation ID.""" with pytest.raises(ValueError, match="Invalid conversation ID"): cache_fixture.delete(USER_ID, "invalid-id") -def test_delete_skip_user_id_check(cache_fixture): +def test_delete_skip_user_id_check(cache_fixture: NoopCache) -> None: """Test deleting an existing conversation.""" skip_user_id_check = True cache_fixture.insert_or_append( @@ -114,7 +114,7 @@ def test_delete_skip_user_id_check(cache_fixture): assert result is True -def test_list_conversations(cache_fixture): +def test_list_conversations(cache_fixture: NoopCache) -> None: """Test listing conversations for a user.""" # Create multiple conversations conversation_id_1 = suid.get_suid() @@ -128,7 +128,7 @@ def test_list_conversations(cache_fixture): assert len(conversations) == 0 -def test_list_conversations_skip_user_id_check(cache_fixture): +def test_list_conversations_skip_user_id_check(cache_fixture: NoopCache) -> None: """Test listing conversations for a user.""" # Create multiple conversations conversation_id_1 = suid.get_suid() @@ -147,13 +147,13 @@ def test_list_conversations_skip_user_id_check(cache_fixture): assert len(conversations) == 0 -def test_list_no_conversations(cache_fixture): +def test_list_no_conversations(cache_fixture: NoopCache) -> None: """Test listing conversations for a user with no conversations.""" conversations = cache_fixture.list(USER_ID) assert len(conversations) == 0 -def test_ready(cache_fixture): +def test_ready(cache_fixture: NoopCache) -> None: """Test if in memory cache always report ready.""" assert cache_fixture.ready() @@ -172,27 +172,27 @@ def test_ready(cache_fixture): @pytest.mark.parametrize("uuid", improper_user_uuids) -def test_list_improper_user_id(cache_fixture, uuid): +def test_list_improper_user_id(cache_fixture: NoopCache, uuid: str | None) -> None: """Test list with invalid user ID.""" with pytest.raises(ValueError, match=f"Invalid user ID {uuid}"): cache_fixture.list(uuid) @pytest.mark.parametrize("uuid", improper_user_uuids) -def test_delete_improper_user_id(cache_fixture, uuid): +def test_delete_improper_user_id(cache_fixture: NoopCache, uuid: str | None) -> None: """Test delete with invalid user ID.""" with pytest.raises(ValueError, match=f"Invalid user ID {uuid}"): cache_fixture.delete(uuid, CONVERSATION_ID) @pytest.mark.parametrize("uuid", improper_user_uuids) -def test_get_improper_user_id(cache_fixture, uuid): +def test_get_improper_user_id(cache_fixture: NoopCache, uuid: str | None) -> None: """Test how improper user ID is handled.""" with pytest.raises(ValueError, match=f"Invalid user ID {uuid}"): cache_fixture.get(uuid, CONVERSATION_ID) -def test_get_improper_conversation_id(cache_fixture): +def test_get_improper_conversation_id(cache_fixture: NoopCache) -> None: """Test how improper conversation ID is handled.""" with pytest.raises(ValueError, match="Invalid conversation ID"): cache_fixture.get(USER_ID, "this-is-not-valid-uuid") diff --git a/tests/unit/cache/test_postgres_cache.py b/tests/unit/cache/test_postgres_cache.py index afdb3baa..8a9bd2a4 100644 --- a/tests/unit/cache/test_postgres_cache.py +++ b/tests/unit/cache/test_postgres_cache.py @@ -2,17 +2,19 @@ import json +from typing import Any + import pytest from pytest_mock import MockerFixture import psycopg2 -from cache.cache_error import CacheError -from cache.postgres_cache import PostgresCache from models.config import PostgreSQLDatabaseConfiguration from models.cache_entry import CacheEntry from models.responses import ConversationData, ReferencedDocument from utils import suid +from cache.cache_error import CacheError +from cache.postgres_cache import PostgresCache USER_ID_1 = suid.get_suid() @@ -43,10 +45,10 @@ class CursorMock: """Mock class for simulating DB cursor exceptions.""" - def __init__(self): + def __init__(self) -> None: """Construct the mock cursor class.""" - def execute(self, command): + def execute(self, command: Any) -> None: """Execute any SQL command.""" raise psycopg2.DatabaseError("can not INSERT") @@ -55,16 +57,16 @@ def execute(self, command): class ConnectionMock: """Mock class for connection.""" - def __init__(self): + def __init__(self) -> None: """Construct the connection mock class.""" - def cursor(self): + def cursor(self) -> None: """Getter for mock cursor.""" raise psycopg2.OperationalError("can not SELECT") @pytest.fixture(scope="module", name="postgres_cache_config_fixture") -def postgres_cache_config(): +def postgres_cache_config() -> PostgreSQLDatabaseConfiguration: """Fixture containing initialized instance of PostgreSQL cache.""" # can be any configuration, becuase tests won't really try to # connect to database @@ -73,7 +75,10 @@ def postgres_cache_config(): ) -def test_cache_initialization(postgres_cache_config_fixture, mocker: MockerFixture): +def test_cache_initialization( + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the get operation when DB is connected.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -85,8 +90,9 @@ def test_cache_initialization(postgres_cache_config_fixture, mocker: MockerFixtu def test_cache_initialization_on_error( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the get operation when DB is not connected.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect", side_effect=Exception("foo")) @@ -97,8 +103,9 @@ def test_cache_initialization_on_error( def test_cache_initialization_connect_finalizer( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the get operation when DB is not connected.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -114,7 +121,10 @@ def test_cache_initialization_connect_finalizer( _ = PostgresCache(postgres_cache_config_fixture) -def test_connected_when_connected(postgres_cache_config_fixture, mocker: MockerFixture): +def test_connected_when_connected( + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the connected() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -125,8 +135,9 @@ def test_connected_when_connected(postgres_cache_config_fixture, mocker: MockerF def test_connected_when_disconnected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the connected() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -139,8 +150,9 @@ def test_connected_when_disconnected( def test_connected_when_connection_error( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the connected() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -152,8 +164,9 @@ def test_connected_when_connection_error( def test_initialize_cache_when_connected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the initialize_cache().""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -163,8 +176,9 @@ def test_initialize_cache_when_connected( def test_initialize_cache_when_disconnected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the initialize_cache().""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -175,7 +189,10 @@ def test_initialize_cache_when_disconnected( cache.initialize_cache() -def test_ready_method(postgres_cache_config_fixture, mocker: MockerFixture): +def test_ready_method( + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the ready() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -187,8 +204,9 @@ def test_ready_method(postgres_cache_config_fixture, mocker: MockerFixture): def test_get_operation_when_disconnected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the get() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -203,8 +221,9 @@ def test_get_operation_when_disconnected( def test_get_operation_when_connected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the get() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -215,7 +234,7 @@ def test_get_operation_when_connected( assert not lst -def test_get_operation_returned_values(): +def test_get_operation_returned_values() -> None: """Test the get() method.""" # TODO: LCORE-721 # TODO: Implement proper unit test for testing PostgreSQL cache 'get' operation @@ -224,8 +243,9 @@ def test_get_operation_returned_values(): def test_insert_or_append_when_disconnected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the insert_or_append() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -239,8 +259,9 @@ def test_insert_or_append_when_disconnected( def test_insert_or_append_operation_when_connected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the insert_or_append() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -251,8 +272,9 @@ def test_insert_or_append_operation_when_connected( def test_insert_or_append_operation_operation_error( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the insert_or_append() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -266,7 +288,10 @@ def test_insert_or_append_operation_operation_error( cache.insert_or_append(USER_ID_1, CONVERSATION_ID_1, cache_entry_1, False) -def test_delete_when_disconnected(postgres_cache_config_fixture, mocker: MockerFixture): +def test_delete_when_disconnected( + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the delete() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -281,8 +306,9 @@ def test_delete_when_disconnected(postgres_cache_config_fixture, mocker: MockerF def test_delete_operation_when_connected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the delete() method.""" # prevent real connection to PG instance mock_connect = mocker.patch("psycopg2.connect") @@ -299,8 +325,9 @@ def test_delete_operation_when_connected( def test_delete_operation_operation_error( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the delete() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -315,8 +342,9 @@ def test_delete_operation_operation_error( def test_list_operation_when_disconnected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the list() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -331,8 +359,9 @@ def test_list_operation_when_disconnected( def test_list_operation_when_connected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test the list() method.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -344,7 +373,10 @@ def test_list_operation_when_connected( assert isinstance(lst, list) -def test_topic_summary_operations(postgres_cache_config_fixture, mocker: MockerFixture): +def test_topic_summary_operations( + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test topic summary set operations and retrieval via list.""" # prevent real connection to PG instance mock_connect = mocker.patch("psycopg2.connect") @@ -374,8 +406,9 @@ def test_topic_summary_operations(postgres_cache_config_fixture, mocker: MockerF def test_topic_summary_after_conversation_delete( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test that topic summary is deleted when conversation is deleted.""" # prevent real connection to PG instance mock_connect = mocker.patch("psycopg2.connect") @@ -397,8 +430,9 @@ def test_topic_summary_after_conversation_delete( def test_topic_summary_when_disconnected( - postgres_cache_config_fixture, mocker: MockerFixture -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test topic summary operations when cache is disconnected.""" # prevent real connection to PG instance mocker.patch("psycopg2.connect") @@ -412,8 +446,9 @@ def test_topic_summary_when_disconnected( def test_insert_and_get_with_referenced_documents( - postgres_cache_config_fixture, mocker -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test that a CacheEntry with referenced_documents is stored and retrieved correctly.""" # prevent real connection to PG instance mock_connect = mocker.patch("psycopg2.connect") @@ -473,8 +508,9 @@ def test_insert_and_get_with_referenced_documents( def test_insert_and_get_without_referenced_documents( - postgres_cache_config_fixture, mocker -): + postgres_cache_config_fixture: PostgreSQLDatabaseConfiguration, + mocker: MockerFixture, +) -> None: """Test that a CacheEntry with no referenced_documents is handled correctly.""" mock_connect = mocker.patch("psycopg2.connect") cache = PostgresCache(postgres_cache_config_fixture) diff --git a/tests/unit/cache/test_sqlite_cache.py b/tests/unit/cache/test_sqlite_cache.py index 1f77ee9f..907950a9 100644 --- a/tests/unit/cache/test_sqlite_cache.py +++ b/tests/unit/cache/test_sqlite_cache.py @@ -2,6 +2,8 @@ from pathlib import Path +from typing import Any + import sqlite3 import pytest @@ -40,10 +42,10 @@ class CursorMock: """Mock class for simulating DB cursor exceptions.""" - def __init__(self): + def __init__(self) -> None: """Construct the mock cursor class.""" - def execute(self, command): + def execute(self, command: Any) -> None: """Execute any SQL command.""" raise sqlite3.Error("can not SELECT") @@ -52,42 +54,42 @@ def execute(self, command): class ConnectionMock: """Mock class for connection.""" - def __init__(self): + def __init__(self) -> None: """Construct the connection mock class.""" - def cursor(self): + def cursor(self) -> Any: """Getter for mock cursor.""" return CursorMock() -def create_cache(path): +def create_cache(path: Path) -> SQLiteCache: """Create the cache instance.""" db_path = str(path / "test.sqlite") cc = SQLiteDatabaseConfiguration(db_path=db_path) return SQLiteCache(cc) -def test_cache_initialization(tmpdir): +def test_cache_initialization(tmpdir: Path) -> None: """Test the get operation when DB is not connected.""" cache = create_cache(tmpdir) assert cache is not None assert cache.connection is not None -def test_cache_initialization_wrong_connection(): +def test_cache_initialization_wrong_connection() -> None: """Test the get operation when DB can not be connected.""" with pytest.raises(Exception, match="unable to open database file"): _ = create_cache(Path("/foo/bar/baz")) -def test_connected_when_connected(tmpdir): +def test_connected_when_connected(tmpdir: Path) -> None: """Test the connected() method.""" # cache should be connected by default cache = create_cache(tmpdir) assert cache.connected() is True -def test_connected_when_disconnected(tmpdir): +def test_connected_when_disconnected(tmpdir: Path) -> None: """Test the connected() method.""" # simulate disconnected cache cache = create_cache(tmpdir) @@ -95,7 +97,7 @@ def test_connected_when_disconnected(tmpdir): assert cache.connected() is False -def test_connected_when_connection_error(tmpdir): +def test_connected_when_connection_error(tmpdir: Path) -> None: """Test the connected() method.""" # simulate connection error cache = create_cache(tmpdir) @@ -104,14 +106,14 @@ def test_connected_when_connection_error(tmpdir): assert cache.connected() is False -def test_initialize_cache_when_connected(tmpdir): +def test_initialize_cache_when_connected(tmpdir: Path) -> None: """Test the initialize_cache().""" cache = create_cache(tmpdir) # should not fail cache.initialize_cache() -def test_initialize_cache_when_disconnected(tmpdir): +def test_initialize_cache_when_disconnected(tmpdir: Path) -> None: """Test the initialize_cache().""" cache = create_cache(tmpdir) cache.connection = None @@ -120,7 +122,7 @@ def test_initialize_cache_when_disconnected(tmpdir): cache.initialize_cache() -def test_get_operation_when_disconnected(tmpdir): +def test_get_operation_when_disconnected(tmpdir: Path) -> None: """Test the get() method.""" cache = create_cache(tmpdir) cache.connection = None @@ -131,7 +133,7 @@ def test_get_operation_when_disconnected(tmpdir): cache.get(USER_ID_1, CONVERSATION_ID_1, False) -def test_get_operation_when_connected(tmpdir): +def test_get_operation_when_connected(tmpdir: Path) -> None: """Test the get() method.""" cache = create_cache(tmpdir) @@ -140,7 +142,7 @@ def test_get_operation_when_connected(tmpdir): assert not lst -def test_insert_or_append_when_disconnected(tmpdir): +def test_insert_or_append_when_disconnected(tmpdir: Path) -> None: """Test the insert_or_append() method.""" cache = create_cache(tmpdir) cache.connection = None @@ -151,7 +153,7 @@ def test_insert_or_append_when_disconnected(tmpdir): cache.insert_or_append(USER_ID_1, CONVERSATION_ID_1, cache_entry_1, False) -def test_insert_or_append_operation_when_connected(tmpdir): +def test_insert_or_append_operation_when_connected(tmpdir: Path) -> None: """Test the insert_or_append() method.""" cache = create_cache(tmpdir) @@ -159,7 +161,7 @@ def test_insert_or_append_operation_when_connected(tmpdir): cache.insert_or_append(USER_ID_1, CONVERSATION_ID_1, cache_entry_1, False) -def test_delete_operation_when_disconnected(tmpdir): +def test_delete_operation_when_disconnected(tmpdir: Path) -> None: """Test the delete() method.""" cache = create_cache(tmpdir) cache.connection = None @@ -170,7 +172,7 @@ def test_delete_operation_when_disconnected(tmpdir): cache.delete(USER_ID_1, CONVERSATION_ID_1, False) -def test_delete_operation_when_connected(tmpdir): +def test_delete_operation_when_connected(tmpdir: Path) -> None: """Test the delete() method.""" cache = create_cache(tmpdir) @@ -181,7 +183,7 @@ def test_delete_operation_when_connected(tmpdir): assert deleted is False -def test_list_operation_when_disconnected(tmpdir): +def test_list_operation_when_disconnected(tmpdir: Path) -> None: """Test the list() method.""" cache = create_cache(tmpdir) cache.connection = None @@ -192,7 +194,7 @@ def test_list_operation_when_disconnected(tmpdir): cache.list(USER_ID_1, False) -def test_list_operation_when_connected(tmpdir): +def test_list_operation_when_connected(tmpdir: Path) -> None: """Test the list() method.""" cache = create_cache(tmpdir) @@ -202,7 +204,7 @@ def test_list_operation_when_connected(tmpdir): assert isinstance(lst, list) -def test_ready_method(tmpdir): +def test_ready_method(tmpdir: Path) -> None: """Test the ready() method.""" cache = create_cache(tmpdir) @@ -211,7 +213,7 @@ def test_ready_method(tmpdir): assert ready is True -def test_get_operation_after_insert_or_append(tmpdir): +def test_get_operation_after_insert_or_append(tmpdir: Path) -> None: """Test the get() method called after insert_or_append() one.""" cache = create_cache(tmpdir) @@ -223,7 +225,7 @@ def test_get_operation_after_insert_or_append(tmpdir): assert lst[1] == cache_entry_2 -def test_get_operation_after_delete(tmpdir): +def test_get_operation_after_delete(tmpdir: Path) -> None: """Test the get() method called after delete() one.""" cache = create_cache(tmpdir) @@ -237,7 +239,7 @@ def test_get_operation_after_delete(tmpdir): assert not lst -def test_multiple_ids(tmpdir): +def test_multiple_ids(tmpdir: Path) -> None: """Test the get() method called after delete() one.""" cache = create_cache(tmpdir) @@ -269,7 +271,7 @@ def test_multiple_ids(tmpdir): assert lst[1] == cache_entry_2 -def test_list_with_conversations(tmpdir): +def test_list_with_conversations(tmpdir: Path) -> None: """Test the list() method with actual conversations.""" cache = create_cache(tmpdir) @@ -298,7 +300,7 @@ def test_list_with_conversations(tmpdir): assert CONVERSATION_ID_2 in conv_ids -def test_topic_summary_operations(tmpdir): +def test_topic_summary_operations(tmpdir: Path) -> None: """Test topic summary set operations and retrieval via list.""" cache = create_cache(tmpdir) @@ -324,7 +326,7 @@ def test_topic_summary_operations(tmpdir): assert conversations[0].topic_summary == updated_summary -def test_topic_summary_after_conversation_delete(tmpdir): +def test_topic_summary_after_conversation_delete(tmpdir: Path) -> None: """Test that topic summary is deleted when conversation is deleted.""" cache = create_cache(tmpdir) @@ -350,7 +352,7 @@ def test_topic_summary_after_conversation_delete(tmpdir): assert len(conversations) == 0 -def test_topic_summary_when_disconnected(tmpdir): +def test_topic_summary_when_disconnected(tmpdir: Path) -> None: """Test topic summary operations when cache is disconnected.""" cache = create_cache(tmpdir) cache.connection = None @@ -360,7 +362,7 @@ def test_topic_summary_when_disconnected(tmpdir): cache.set_topic_summary(USER_ID_1, CONVERSATION_ID_1, "Test", False) -def test_insert_and_get_with_referenced_documents(tmpdir): +def test_insert_and_get_with_referenced_documents(tmpdir: Path) -> None: """ Test that a CacheEntry with referenced_documents is correctly serialized, stored, and retrieved. @@ -390,7 +392,7 @@ def test_insert_and_get_with_referenced_documents(tmpdir): assert retrieved_entries[0].referenced_documents[0].doc_title == "Test Doc" -def test_insert_and_get_without_referenced_documents(tmpdir): +def test_insert_and_get_without_referenced_documents(tmpdir: Path) -> None: """ Test that a CacheEntry without referenced_documents is correctly stored and retrieved with its referenced_documents attribute as None.