Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions tests/unit/cache/test_cache_factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Unit tests for CacheFactory class."""

from pathlib import Path

import pytest
from pytest_mock import MockerFixture

Expand All @@ -25,21 +27,21 @@


@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)
)


@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,
Expand All @@ -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(
Expand All @@ -59,30 +61,34 @@ 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
# check if the object has the right type
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
# check if the object has the right type
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)
Expand All @@ -93,15 +99,17 @@ 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
# check if the object has the right type
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(
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)
38 changes: 19 additions & 19 deletions tests/unit/cache/test_noop_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -49,15 +49,15 @@ 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(
USER_PROVIDED_USER_ID, CONVERSATION_ID, cache_entry_1, skip_user_id_check
)


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,
Expand All @@ -71,15 +71,15 @@ 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 (
cache_fixture.get("ffffffff-ffff-ffff-ffff-ffffffffffff", CONVERSATION_ID) == []
)


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)

Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()

Expand All @@ -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")
Loading
Loading