Skip to content

Commit 6361e59

Browse files
authored
Merge pull request #734 from tisnik/lcore-740-type-hints-in-conversation-cache-unit-tests
LCORE-740: Type hints in conversation cache unit tests
2 parents dbb065c + 3169582 commit 6361e59

File tree

4 files changed

+163
-115
lines changed

4 files changed

+163
-115
lines changed

tests/unit/cache/test_cache_factory.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Unit tests for CacheFactory class."""
22

3+
from pathlib import Path
4+
35
import pytest
46
from pytest_mock import MockerFixture
57

@@ -25,21 +27,21 @@
2527

2628

2729
@pytest.fixture(scope="module", name="noop_cache_config_fixture")
28-
def noop_cache_config():
30+
def noop_cache_config() -> ConversationCacheConfiguration:
2931
"""Fixture containing initialized instance of ConversationCacheConfiguration."""
3032
return ConversationCacheConfiguration(type=CACHE_TYPE_NOOP)
3133

3234

3335
@pytest.fixture(scope="module", name="memory_cache_config_fixture")
34-
def memory_cache_config():
36+
def memory_cache_config() -> ConversationCacheConfiguration:
3537
"""Fixture containing initialized instance of InMemory cache."""
3638
return ConversationCacheConfiguration(
3739
type=CACHE_TYPE_MEMORY, memory=InMemoryCacheConfig(max_entries=10)
3840
)
3941

4042

4143
@pytest.fixture(scope="module", name="postgres_cache_config_fixture")
42-
def postgres_cache_config():
44+
def postgres_cache_config() -> ConversationCacheConfiguration:
4345
"""Fixture containing initialized instance of PostgreSQL cache."""
4446
return ConversationCacheConfiguration(
4547
type=CACHE_TYPE_POSTGRES,
@@ -50,7 +52,7 @@ def postgres_cache_config():
5052

5153

5254
@pytest.fixture(name="sqlite_cache_config_fixture")
53-
def sqlite_cache_config(tmpdir):
55+
def sqlite_cache_config(tmpdir: Path) -> ConversationCacheConfiguration:
5456
"""Fixture containing initialized instance of SQLite cache."""
5557
db_path = str(tmpdir / "test.sqlite")
5658
return ConversationCacheConfiguration(
@@ -59,30 +61,34 @@ def sqlite_cache_config(tmpdir):
5961

6062

6163
@pytest.fixture(scope="module", name="invalid_cache_type_config_fixture")
62-
def invalid_cache_type_config():
64+
def invalid_cache_type_config() -> ConversationCacheConfiguration:
6365
"""Fixture containing instance of ConversationCacheConfiguration with improper settings."""
6466
c = ConversationCacheConfiguration()
6567
c.type = "foo bar baz"
6668
return c
6769

6870

69-
def test_conversation_cache_noop(noop_cache_config_fixture):
71+
def test_conversation_cache_noop(
72+
noop_cache_config_fixture: ConversationCacheConfiguration,
73+
) -> None:
7074
"""Check if NoopCache is returned by factory with proper configuration."""
7175
cache = CacheFactory.conversation_cache(noop_cache_config_fixture)
7276
assert cache is not None
7377
# check if the object has the right type
7478
assert isinstance(cache, NoopCache)
7579

7680

77-
def test_conversation_cache_in_memory(memory_cache_config_fixture):
81+
def test_conversation_cache_in_memory(
82+
memory_cache_config_fixture: ConversationCacheConfiguration,
83+
) -> None:
7884
"""Check if InMemoryCache is returned by factory with proper configuration."""
7985
cache = CacheFactory.conversation_cache(memory_cache_config_fixture)
8086
assert cache is not None
8187
# check if the object has the right type
8288
assert isinstance(cache, InMemoryCache)
8389

8490

85-
def test_conversation_cache_in_memory_improper_config():
91+
def test_conversation_cache_in_memory_improper_config() -> None:
8692
"""Check if memory cache configuration is checked in cache factory."""
8793
cc = ConversationCacheConfiguration(
8894
type=CACHE_TYPE_MEMORY, memory=InMemoryCacheConfig(max_entries=10)
@@ -93,15 +99,17 @@ def test_conversation_cache_in_memory_improper_config():
9399
_ = CacheFactory.conversation_cache(cc)
94100

95101

96-
def test_conversation_cache_sqlite(sqlite_cache_config_fixture):
102+
def test_conversation_cache_sqlite(
103+
sqlite_cache_config_fixture: ConversationCacheConfiguration,
104+
) -> None:
97105
"""Check if SQLiteCache is returned by factory with proper configuration."""
98106
cache = CacheFactory.conversation_cache(sqlite_cache_config_fixture)
99107
assert cache is not None
100108
# check if the object has the right type
101109
assert isinstance(cache, SQLiteCache)
102110

103111

104-
def test_conversation_cache_sqlite_improper_config(tmpdir):
112+
def test_conversation_cache_sqlite_improper_config(tmpdir: Path) -> None:
105113
"""Check if memory cache configuration is checked in cache factory."""
106114
db_path = str(tmpdir / "test.sqlite")
107115
cc = ConversationCacheConfiguration(
@@ -114,8 +122,8 @@ def test_conversation_cache_sqlite_improper_config(tmpdir):
114122

115123

116124
def test_conversation_cache_postgres(
117-
postgres_cache_config_fixture, mocker: MockerFixture
118-
):
125+
postgres_cache_config_fixture: ConversationCacheConfiguration, mocker: MockerFixture
126+
) -> None:
119127
"""Check if PostgreSQL is returned by factory with proper configuration."""
120128
mocker.patch("psycopg2.connect")
121129
cache = CacheFactory.conversation_cache(postgres_cache_config_fixture)
@@ -124,7 +132,7 @@ def test_conversation_cache_postgres(
124132
assert isinstance(cache, PostgresCache)
125133

126134

127-
def test_conversation_cache_postgres_improper_config():
135+
def test_conversation_cache_postgres_improper_config() -> None:
128136
"""Check if PostgreSQL cache configuration is checked in cache factory."""
129137
cc = ConversationCacheConfiguration(
130138
type=CACHE_TYPE_POSTGRES,
@@ -138,7 +146,7 @@ def test_conversation_cache_postgres_improper_config():
138146
_ = CacheFactory.conversation_cache(cc)
139147

140148

141-
def test_conversation_cache_no_type():
149+
def test_conversation_cache_no_type() -> None:
142150
"""Check if wrong cache configuration is detected properly."""
143151
cc = ConversationCacheConfiguration(type=CACHE_TYPE_NOOP)
144152
# simulate improper configuration (can not be done directly as model checks this)
@@ -147,7 +155,9 @@ def test_conversation_cache_no_type():
147155
CacheFactory.conversation_cache(cc)
148156

149157

150-
def test_conversation_cache_wrong_cache(invalid_cache_type_config_fixture):
158+
def test_conversation_cache_wrong_cache(
159+
invalid_cache_type_config_fixture: ConversationCacheConfiguration,
160+
) -> None:
151161
"""Check if wrong cache configuration is detected properly."""
152162
with pytest.raises(ValueError, match="Invalid cache type"):
153163
CacheFactory.conversation_cache(invalid_cache_type_config_fixture)

tests/unit/cache/test_noop_cache.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import pytest
44

55
from models.cache_entry import CacheEntry
6-
from cache.noop_cache import NoopCache
76
from utils import suid
7+
from cache.noop_cache import NoopCache
88

99
USER_ID = suid.get_suid()
1010
CONVERSATION_ID = suid.get_suid()
@@ -28,19 +28,19 @@
2828

2929

3030
@pytest.fixture(name="cache_fixture")
31-
def cache():
31+
def cache() -> NoopCache:
3232
"""Fixture with constucted and initialized in memory cache object."""
3333
c = NoopCache()
3434
c.initialize_cache()
3535
return c
3636

3737

38-
def test_connect(cache_fixture):
38+
def test_connect(cache_fixture: NoopCache) -> None:
3939
"""Test the behavior of connect method."""
4040
cache_fixture.connect()
4141

4242

43-
def test_insert_or_append(cache_fixture):
43+
def test_insert_or_append(cache_fixture: NoopCache) -> None:
4444
"""Test the behavior of insert_or_append method."""
4545
cache_fixture.insert_or_append(
4646
USER_ID,
@@ -49,15 +49,15 @@ def test_insert_or_append(cache_fixture):
4949
)
5050

5151

52-
def test_insert_or_append_skip_user_id_check(cache_fixture):
52+
def test_insert_or_append_skip_user_id_check(cache_fixture: NoopCache) -> None:
5353
"""Test the behavior of insert_or_append method."""
5454
skip_user_id_check = True
5555
cache_fixture.insert_or_append(
5656
USER_PROVIDED_USER_ID, CONVERSATION_ID, cache_entry_1, skip_user_id_check
5757
)
5858

5959

60-
def test_insert_or_append_existing_key(cache_fixture):
60+
def test_insert_or_append_existing_key(cache_fixture: NoopCache) -> None:
6161
"""Test the behavior of insert_or_append method for existing item."""
6262
cache_fixture.insert_or_append(
6363
USER_ID,
@@ -71,15 +71,15 @@ def test_insert_or_append_existing_key(cache_fixture):
7171
)
7272

7373

74-
def test_get_nonexistent_user(cache_fixture):
74+
def test_get_nonexistent_user(cache_fixture: NoopCache) -> None:
7575
"""Test how non-existent items are handled by the cache."""
7676
# this UUID is different from DEFAULT_USER_UID
7777
assert (
7878
cache_fixture.get("ffffffff-ffff-ffff-ffff-ffffffffffff", CONVERSATION_ID) == []
7979
)
8080

8181

82-
def test_delete_existing_conversation(cache_fixture):
82+
def test_delete_existing_conversation(cache_fixture: NoopCache) -> None:
8383
"""Test deleting an existing conversation."""
8484
cache_fixture.insert_or_append(USER_ID, CONVERSATION_ID, cache_entry_1)
8585

@@ -88,19 +88,19 @@ def test_delete_existing_conversation(cache_fixture):
8888
assert result is True
8989

9090

91-
def test_delete_nonexistent_conversation(cache_fixture):
91+
def test_delete_nonexistent_conversation(cache_fixture: NoopCache) -> None:
9292
"""Test deleting a conversation that doesn't exist."""
9393
result = cache_fixture.delete(USER_ID, CONVERSATION_ID)
9494
assert result is True
9595

9696

97-
def test_delete_improper_conversation_id(cache_fixture):
97+
def test_delete_improper_conversation_id(cache_fixture: NoopCache) -> None:
9898
"""Test delete with invalid conversation ID."""
9999
with pytest.raises(ValueError, match="Invalid conversation ID"):
100100
cache_fixture.delete(USER_ID, "invalid-id")
101101

102102

103-
def test_delete_skip_user_id_check(cache_fixture):
103+
def test_delete_skip_user_id_check(cache_fixture: NoopCache) -> None:
104104
"""Test deleting an existing conversation."""
105105
skip_user_id_check = True
106106
cache_fixture.insert_or_append(
@@ -114,7 +114,7 @@ def test_delete_skip_user_id_check(cache_fixture):
114114
assert result is True
115115

116116

117-
def test_list_conversations(cache_fixture):
117+
def test_list_conversations(cache_fixture: NoopCache) -> None:
118118
"""Test listing conversations for a user."""
119119
# Create multiple conversations
120120
conversation_id_1 = suid.get_suid()
@@ -128,7 +128,7 @@ def test_list_conversations(cache_fixture):
128128
assert len(conversations) == 0
129129

130130

131-
def test_list_conversations_skip_user_id_check(cache_fixture):
131+
def test_list_conversations_skip_user_id_check(cache_fixture: NoopCache) -> None:
132132
"""Test listing conversations for a user."""
133133
# Create multiple conversations
134134
conversation_id_1 = suid.get_suid()
@@ -147,13 +147,13 @@ def test_list_conversations_skip_user_id_check(cache_fixture):
147147
assert len(conversations) == 0
148148

149149

150-
def test_list_no_conversations(cache_fixture):
150+
def test_list_no_conversations(cache_fixture: NoopCache) -> None:
151151
"""Test listing conversations for a user with no conversations."""
152152
conversations = cache_fixture.list(USER_ID)
153153
assert len(conversations) == 0
154154

155155

156-
def test_ready(cache_fixture):
156+
def test_ready(cache_fixture: NoopCache) -> None:
157157
"""Test if in memory cache always report ready."""
158158
assert cache_fixture.ready()
159159

@@ -172,27 +172,27 @@ def test_ready(cache_fixture):
172172

173173

174174
@pytest.mark.parametrize("uuid", improper_user_uuids)
175-
def test_list_improper_user_id(cache_fixture, uuid):
175+
def test_list_improper_user_id(cache_fixture: NoopCache, uuid: str | None) -> None:
176176
"""Test list with invalid user ID."""
177177
with pytest.raises(ValueError, match=f"Invalid user ID {uuid}"):
178178
cache_fixture.list(uuid)
179179

180180

181181
@pytest.mark.parametrize("uuid", improper_user_uuids)
182-
def test_delete_improper_user_id(cache_fixture, uuid):
182+
def test_delete_improper_user_id(cache_fixture: NoopCache, uuid: str | None) -> None:
183183
"""Test delete with invalid user ID."""
184184
with pytest.raises(ValueError, match=f"Invalid user ID {uuid}"):
185185
cache_fixture.delete(uuid, CONVERSATION_ID)
186186

187187

188188
@pytest.mark.parametrize("uuid", improper_user_uuids)
189-
def test_get_improper_user_id(cache_fixture, uuid):
189+
def test_get_improper_user_id(cache_fixture: NoopCache, uuid: str | None) -> None:
190190
"""Test how improper user ID is handled."""
191191
with pytest.raises(ValueError, match=f"Invalid user ID {uuid}"):
192192
cache_fixture.get(uuid, CONVERSATION_ID)
193193

194194

195-
def test_get_improper_conversation_id(cache_fixture):
195+
def test_get_improper_conversation_id(cache_fixture: NoopCache) -> None:
196196
"""Test how improper conversation ID is handled."""
197197
with pytest.raises(ValueError, match="Invalid conversation ID"):
198198
cache_fixture.get(USER_ID, "this-is-not-valid-uuid")

0 commit comments

Comments
 (0)