Skip to content

Commit 2eefadd

Browse files
authored
Merge pull request #759 from tisnik/lcore-888-fix-pyright-issues-in-cache-unit-tests
LCORE-888: Fix issues in cache unit tests found by Pyright linter
2 parents fa2121a + 817df51 commit 2eefadd

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

tests/unit/cache/test_cache_factory.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from pytest_mock import MockerFixture
7+
from pydantic import SecretStr
78

89
from constants import (
910
CACHE_TYPE_NOOP,
@@ -46,7 +47,7 @@ def postgres_cache_config() -> ConversationCacheConfiguration:
4647
return ConversationCacheConfiguration(
4748
type=CACHE_TYPE_POSTGRES,
4849
postgres=PostgreSQLDatabaseConfiguration(
49-
db="database", user="user", password="password"
50+
db="database", user="user", password=SecretStr("password")
5051
),
5152
)
5253

@@ -64,7 +65,8 @@ def sqlite_cache_config(tmpdir: Path) -> ConversationCacheConfiguration:
6465
def invalid_cache_type_config() -> ConversationCacheConfiguration:
6566
"""Fixture containing instance of ConversationCacheConfiguration with improper settings."""
6667
c = ConversationCacheConfiguration()
67-
c.type = "foo bar baz"
68+
# the conversation cache type name is incorrect in purpose
69+
c.type = "foo bar baz" # pyright: ignore
6870
return c
6971

7072

@@ -136,7 +138,9 @@ def test_conversation_cache_postgres_improper_config() -> None:
136138
"""Check if PostgreSQL cache configuration is checked in cache factory."""
137139
cc = ConversationCacheConfiguration(
138140
type=CACHE_TYPE_POSTGRES,
139-
postgres=PostgreSQLDatabaseConfiguration(db="db", user="u", password="p"),
141+
postgres=PostgreSQLDatabaseConfiguration(
142+
db="db", user="u", password=SecretStr("p")
143+
),
140144
)
141145
# simulate improper configuration (can not be done directly as model checks this)
142146
cc.postgres = None

tests/unit/cache/test_postgres_cache.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pytest
88
from pytest_mock import MockerFixture
9+
from pydantic import SecretStr, AnyUrl
910

1011
import psycopg2
1112

@@ -71,7 +72,11 @@ def postgres_cache_config() -> PostgreSQLDatabaseConfiguration:
7172
# can be any configuration, becuase tests won't really try to
7273
# connect to database
7374
return PostgreSQLDatabaseConfiguration(
74-
host="localhost", port=1234, db="database", user="user", password="password"
75+
host="localhost",
76+
port=1234,
77+
db="database",
78+
user="user",
79+
password=SecretStr("password"),
7580
)
7681

7782

@@ -158,7 +163,8 @@ def test_connected_when_connection_error(
158163
mocker.patch("psycopg2.connect")
159164
# simulate connection error
160165
cache = PostgresCache(postgres_cache_config_fixture)
161-
cache.connection = ConnectionMock()
166+
# connection does not have to have proper type
167+
cache.connection = ConnectionMock() # pyright: ignore
162168
assert cache.connection is not None
163169
assert cache.connected() is False
164170

@@ -282,7 +288,8 @@ def test_insert_or_append_operation_operation_error(
282288

283289
# no operation for @connection decorator
284290
cache.connect = lambda: None
285-
cache.connection = ConnectionMock()
291+
# connection does not have to have proper type
292+
cache.connection = ConnectionMock() # pyright: ignore
286293

287294
with pytest.raises(CacheError, match="insert_or_append"):
288295
cache.insert_or_append(USER_ID_1, CONVERSATION_ID_1, cache_entry_1, False)
@@ -335,7 +342,8 @@ def test_delete_operation_operation_error(
335342

336343
# no operation for @connection decorator
337344
cache.connect = lambda: None
338-
cache.connection = ConnectionMock()
345+
# connection does not have to have proper type
346+
cache.connection = ConnectionMock() # pyright: ignore
339347

340348
with pytest.raises(CacheError, match="delete"):
341349
cache.delete(USER_ID_1, CONVERSATION_ID_1, False)
@@ -458,7 +466,9 @@ def test_insert_and_get_with_referenced_documents(
458466
mock_cursor = mock_connection.cursor.return_value.__enter__.return_value
459467

460468
# Create a CacheEntry with referenced documents
461-
docs = [ReferencedDocument(doc_title="Test Doc", doc_url="http://example.com/")]
469+
docs = [
470+
ReferencedDocument(doc_title="Test Doc", doc_url=AnyUrl("http://example.com/"))
471+
]
462472
entry_with_docs = CacheEntry(
463473
query="user message",
464474
response="AI message",

tests/unit/cache/test_sqlite_cache.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import sqlite3
88

9+
from pydantic import AnyUrl
910
import pytest
1011

1112
from models.config import SQLiteDatabaseConfiguration
@@ -101,7 +102,8 @@ def test_connected_when_connection_error(tmpdir: Path) -> None:
101102
"""Test the connected() method."""
102103
# simulate connection error
103104
cache = create_cache(tmpdir)
104-
cache.connection = ConnectionMock()
105+
# connection can have any type
106+
cache.connection = ConnectionMock() # pyright: ignore
105107
assert cache.connection is not None
106108
assert cache.connected() is False
107109

@@ -370,7 +372,9 @@ def test_insert_and_get_with_referenced_documents(tmpdir: Path) -> None:
370372
cache = create_cache(tmpdir)
371373

372374
# Create a CacheEntry with referenced documents
373-
docs = [ReferencedDocument(doc_title="Test Doc", doc_url="http://example.com")]
375+
docs = [
376+
ReferencedDocument(doc_title="Test Doc", doc_url=AnyUrl("http://example.com"))
377+
]
374378
entry_with_docs = CacheEntry(
375379
query="user message",
376380
response="AI message",

0 commit comments

Comments
 (0)