Skip to content

Commit c1dacfe

Browse files
committed
More refactoring
1 parent 455f613 commit c1dacfe

File tree

8 files changed

+96
-42
lines changed

8 files changed

+96
-42
lines changed

tests/unit/authentication/test_k8s.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import os
66

77
import pytest
8+
from pytest_mock import MockerFixture
9+
810
from fastapi import HTTPException, Request
911
from kubernetes.client import AuthenticationV1Api, AuthorizationV1Api
1012
from kubernetes.client.rest import ApiException
@@ -70,7 +72,7 @@ def test_singleton_pattern():
7072
assert k1 is k2
7173

7274

73-
async def test_auth_dependency_valid_token(mocker):
75+
async def test_auth_dependency_valid_token(mocker: MockerFixture):
7476
"""Tests the auth dependency with a mocked valid-token."""
7577
dependency = K8SAuthDependency()
7678

@@ -103,7 +105,7 @@ async def test_auth_dependency_valid_token(mocker):
103105
assert token == "valid-token"
104106

105107

106-
async def test_auth_dependency_invalid_token(mocker):
108+
async def test_auth_dependency_invalid_token(mocker: MockerFixture):
107109
"""Test the auth dependency with a mocked invalid-token."""
108110
dependency = K8SAuthDependency()
109111

@@ -135,7 +137,7 @@ async def test_auth_dependency_invalid_token(mocker):
135137
assert exc_info.value.status_code == 403
136138

137139

138-
async def test_cluster_id_is_used_for_kube_admin(mocker):
140+
async def test_cluster_id_is_used_for_kube_admin(mocker: MockerFixture):
139141
"""Test the cluster id is used as user_id when user is kube:admin."""
140142
dependency = K8SAuthDependency()
141143
mock_authz_api = mocker.patch("authentication.k8s.K8sClientSingleton.get_authz_api")
@@ -175,7 +177,7 @@ async def test_cluster_id_is_used_for_kube_admin(mocker):
175177
assert token == "valid-token"
176178

177179

178-
def test_auth_dependency_config(mocker):
180+
def test_auth_dependency_config(mocker: MockerFixture):
179181
"""Test the auth dependency can load kubeconfig file."""
180182
mocker.patch.dict(os.environ, {"MY_ENV_VAR": "mocked"})
181183

@@ -189,7 +191,7 @@ def test_auth_dependency_config(mocker):
189191
), "authz_client is not an instance of AuthorizationV1Api"
190192

191193

192-
def test_get_cluster_id(mocker):
194+
def test_get_cluster_id(mocker: MockerFixture):
193195
"""Test get_cluster_id function."""
194196
mock_get_custom_objects_api = mocker.patch(
195197
"authentication.k8s.K8sClientSingleton.get_custom_objects_api"
@@ -228,7 +230,7 @@ def test_get_cluster_id(mocker):
228230
K8sClientSingleton._get_cluster_id()
229231

230232

231-
def test_get_cluster_id_in_cluster(mocker):
233+
def test_get_cluster_id_in_cluster(mocker: MockerFixture):
232234
"""Test get_cluster_id function when running inside of cluster."""
233235
mocker.patch("authentication.k8s.RUNNING_IN_CLUSTER", True)
234236
mocker.patch("authentication.k8s.K8sClientSingleton.__new__")
@@ -240,7 +242,7 @@ def test_get_cluster_id_in_cluster(mocker):
240242
assert K8sClientSingleton.get_cluster_id() == "some-cluster-id"
241243

242244

243-
def test_get_cluster_id_outside_of_cluster(mocker):
245+
def test_get_cluster_id_outside_of_cluster(mocker: MockerFixture):
244246
"""Test get_cluster_id function when running outside of cluster."""
245247
mocker.patch("authentication.k8s.RUNNING_IN_CLUSTER", False)
246248
mocker.patch("authentication.k8s.K8sClientSingleton.__new__")

tests/unit/cache/test_cache_factory.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Unit tests for CacheFactory class."""
22

33
import pytest
4+
from pytest_mock import MockerFixture
45

56
from constants import (
67
CACHE_TYPE_NOOP,
@@ -112,7 +113,9 @@ def test_conversation_cache_sqlite_improper_config(tmpdir):
112113
_ = CacheFactory.conversation_cache(cc)
113114

114115

115-
def test_conversation_cache_postgres(postgres_cache_config_fixture, mocker):
116+
def test_conversation_cache_postgres(
117+
postgres_cache_config_fixture, mocker: MockerFixture
118+
):
116119
"""Check if PostgreSQL is returned by factory with proper configuration."""
117120
mocker.patch("psycopg2.connect")
118121
cache = CacheFactory.conversation_cache(postgres_cache_config_fixture)

tests/unit/cache/test_postgres_cache.py

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Unit tests for PostgreSQL cache implementation."""
22

33
import pytest
4+
from pytest_mock import MockerFixture
45

56
import psycopg2
67

@@ -69,7 +70,7 @@ def postgres_cache_config():
6970
)
7071

7172

72-
def test_cache_initialization(postgres_cache_config_fixture, mocker):
73+
def test_cache_initialization(postgres_cache_config_fixture, mocker: MockerFixture):
7374
"""Test the get operation when DB is connected."""
7475
# prevent real connection to PG instance
7576
mocker.patch("psycopg2.connect")
@@ -80,7 +81,9 @@ def test_cache_initialization(postgres_cache_config_fixture, mocker):
8081
assert cache.connection is not None
8182

8283

83-
def test_cache_initialization_on_error(postgres_cache_config_fixture, mocker):
84+
def test_cache_initialization_on_error(
85+
postgres_cache_config_fixture, mocker: MockerFixture
86+
):
8487
"""Test the get operation when DB is not connected."""
8588
# prevent real connection to PG instance
8689
mocker.patch("psycopg2.connect", side_effect=Exception("foo"))
@@ -90,7 +93,9 @@ def test_cache_initialization_on_error(postgres_cache_config_fixture, mocker):
9093
_ = PostgresCache(postgres_cache_config_fixture)
9194

9295

93-
def test_cache_initialization_connect_finalizer(postgres_cache_config_fixture, mocker):
96+
def test_cache_initialization_connect_finalizer(
97+
postgres_cache_config_fixture, mocker: MockerFixture
98+
):
9499
"""Test the get operation when DB is not connected."""
95100
# prevent real connection to PG instance
96101
mocker.patch("psycopg2.connect")
@@ -106,7 +111,7 @@ def test_cache_initialization_connect_finalizer(postgres_cache_config_fixture, m
106111
_ = PostgresCache(postgres_cache_config_fixture)
107112

108113

109-
def test_connected_when_connected(postgres_cache_config_fixture, mocker):
114+
def test_connected_when_connected(postgres_cache_config_fixture, mocker: MockerFixture):
110115
"""Test the connected() method."""
111116
# prevent real connection to PG instance
112117
mocker.patch("psycopg2.connect")
@@ -116,7 +121,9 @@ def test_connected_when_connected(postgres_cache_config_fixture, mocker):
116121
assert cache.connected() is True
117122

118123

119-
def test_connected_when_disconnected(postgres_cache_config_fixture, mocker):
124+
def test_connected_when_disconnected(
125+
postgres_cache_config_fixture, mocker: MockerFixture
126+
):
120127
"""Test the connected() method."""
121128
# prevent real connection to PG instance
122129
mocker.patch("psycopg2.connect")
@@ -128,7 +135,9 @@ def test_connected_when_disconnected(postgres_cache_config_fixture, mocker):
128135
assert cache.connected() is False
129136

130137

131-
def test_connected_when_connection_error(postgres_cache_config_fixture, mocker):
138+
def test_connected_when_connection_error(
139+
postgres_cache_config_fixture, mocker: MockerFixture
140+
):
132141
"""Test the connected() method."""
133142
# prevent real connection to PG instance
134143
mocker.patch("psycopg2.connect")
@@ -139,7 +148,9 @@ def test_connected_when_connection_error(postgres_cache_config_fixture, mocker):
139148
assert cache.connected() is False
140149

141150

142-
def test_initialize_cache_when_connected(postgres_cache_config_fixture, mocker):
151+
def test_initialize_cache_when_connected(
152+
postgres_cache_config_fixture, mocker: MockerFixture
153+
):
143154
"""Test the initialize_cache()."""
144155
# prevent real connection to PG instance
145156
mocker.patch("psycopg2.connect")
@@ -148,7 +159,9 @@ def test_initialize_cache_when_connected(postgres_cache_config_fixture, mocker):
148159
cache.initialize_cache()
149160

150161

151-
def test_initialize_cache_when_disconnected(postgres_cache_config_fixture, mocker):
162+
def test_initialize_cache_when_disconnected(
163+
postgres_cache_config_fixture, mocker: MockerFixture
164+
):
152165
"""Test the initialize_cache()."""
153166
# prevent real connection to PG instance
154167
mocker.patch("psycopg2.connect")
@@ -159,7 +172,7 @@ def test_initialize_cache_when_disconnected(postgres_cache_config_fixture, mocke
159172
cache.initialize_cache()
160173

161174

162-
def test_ready_method(postgres_cache_config_fixture, mocker):
175+
def test_ready_method(postgres_cache_config_fixture, mocker: MockerFixture):
163176
"""Test the ready() method."""
164177
# prevent real connection to PG instance
165178
mocker.patch("psycopg2.connect")
@@ -170,7 +183,9 @@ def test_ready_method(postgres_cache_config_fixture, mocker):
170183
assert ready is True
171184

172185

173-
def test_get_operation_when_disconnected(postgres_cache_config_fixture, mocker):
186+
def test_get_operation_when_disconnected(
187+
postgres_cache_config_fixture, mocker: MockerFixture
188+
):
174189
"""Test the get() method."""
175190
# prevent real connection to PG instance
176191
mocker.patch("psycopg2.connect")
@@ -184,7 +199,9 @@ def test_get_operation_when_disconnected(postgres_cache_config_fixture, mocker):
184199
cache.get(USER_ID_1, CONVERSATION_ID_1, False)
185200

186201

187-
def test_get_operation_when_connected(postgres_cache_config_fixture, mocker):
202+
def test_get_operation_when_connected(
203+
postgres_cache_config_fixture, mocker: MockerFixture
204+
):
188205
"""Test the get() method."""
189206
# prevent real connection to PG instance
190207
mocker.patch("psycopg2.connect")
@@ -203,7 +220,9 @@ def test_get_operation_returned_values():
203220
# Need to mock the cursor.execute() method
204221

205222

206-
def test_insert_or_append_when_disconnected(postgres_cache_config_fixture, mocker):
223+
def test_insert_or_append_when_disconnected(
224+
postgres_cache_config_fixture, mocker: MockerFixture
225+
):
207226
"""Test the insert_or_append() method."""
208227
# prevent real connection to PG instance
209228
mocker.patch("psycopg2.connect")
@@ -217,7 +236,7 @@ def test_insert_or_append_when_disconnected(postgres_cache_config_fixture, mocke
217236

218237

219238
def test_insert_or_append_operation_when_connected(
220-
postgres_cache_config_fixture, mocker
239+
postgres_cache_config_fixture, mocker: MockerFixture
221240
):
222241
"""Test the insert_or_append() method."""
223242
# prevent real connection to PG instance
@@ -229,7 +248,7 @@ def test_insert_or_append_operation_when_connected(
229248

230249

231250
def test_insert_or_append_operation_operation_error(
232-
postgres_cache_config_fixture, mocker
251+
postgres_cache_config_fixture, mocker: MockerFixture
233252
):
234253
"""Test the insert_or_append() method."""
235254
# prevent real connection to PG instance
@@ -244,7 +263,7 @@ def test_insert_or_append_operation_operation_error(
244263
cache.insert_or_append(USER_ID_1, CONVERSATION_ID_1, cache_entry_1, False)
245264

246265

247-
def test_delete_when_disconnected(postgres_cache_config_fixture, mocker):
266+
def test_delete_when_disconnected(postgres_cache_config_fixture, mocker: MockerFixture):
248267
"""Test the delete() method."""
249268
# prevent real connection to PG instance
250269
mocker.patch("psycopg2.connect")
@@ -258,7 +277,9 @@ def test_delete_when_disconnected(postgres_cache_config_fixture, mocker):
258277
cache.delete(USER_ID_1, CONVERSATION_ID_1, False)
259278

260279

261-
def test_delete_operation_when_connected(postgres_cache_config_fixture, mocker):
280+
def test_delete_operation_when_connected(
281+
postgres_cache_config_fixture, mocker: MockerFixture
282+
):
262283
"""Test the delete() method."""
263284
# prevent real connection to PG instance
264285
mock_connect = mocker.patch("psycopg2.connect")
@@ -274,7 +295,9 @@ def test_delete_operation_when_connected(postgres_cache_config_fixture, mocker):
274295
assert cache.delete(USER_ID_1, CONVERSATION_ID_1, False) is False
275296

276297

277-
def test_delete_operation_operation_error(postgres_cache_config_fixture, mocker):
298+
def test_delete_operation_operation_error(
299+
postgres_cache_config_fixture, mocker: MockerFixture
300+
):
278301
"""Test the delete() method."""
279302
# prevent real connection to PG instance
280303
mocker.patch("psycopg2.connect")
@@ -288,7 +311,9 @@ def test_delete_operation_operation_error(postgres_cache_config_fixture, mocker)
288311
cache.delete(USER_ID_1, CONVERSATION_ID_1, False)
289312

290313

291-
def test_list_operation_when_disconnected(postgres_cache_config_fixture, mocker):
314+
def test_list_operation_when_disconnected(
315+
postgres_cache_config_fixture, mocker: MockerFixture
316+
):
292317
"""Test the list() method."""
293318
# prevent real connection to PG instance
294319
mocker.patch("psycopg2.connect")
@@ -302,7 +327,9 @@ def test_list_operation_when_disconnected(postgres_cache_config_fixture, mocker)
302327
cache.list(USER_ID_1, False)
303328

304329

305-
def test_list_operation_when_connected(postgres_cache_config_fixture, mocker):
330+
def test_list_operation_when_connected(
331+
postgres_cache_config_fixture, mocker: MockerFixture
332+
):
306333
"""Test the list() method."""
307334
# prevent real connection to PG instance
308335
mocker.patch("psycopg2.connect")
@@ -314,7 +341,7 @@ def test_list_operation_when_connected(postgres_cache_config_fixture, mocker):
314341
assert isinstance(lst, list)
315342

316343

317-
def test_topic_summary_operations(postgres_cache_config_fixture, mocker):
344+
def test_topic_summary_operations(postgres_cache_config_fixture, mocker: MockerFixture):
318345
"""Test topic summary set operations and retrieval via list."""
319346
# prevent real connection to PG instance
320347
mock_connect = mocker.patch("psycopg2.connect")
@@ -343,7 +370,9 @@ def test_topic_summary_operations(postgres_cache_config_fixture, mocker):
343370
assert isinstance(conversations[0], ConversationData)
344371

345372

346-
def test_topic_summary_after_conversation_delete(postgres_cache_config_fixture, mocker):
373+
def test_topic_summary_after_conversation_delete(
374+
postgres_cache_config_fixture, mocker: MockerFixture
375+
):
347376
"""Test that topic summary is deleted when conversation is deleted."""
348377
# prevent real connection to PG instance
349378
mock_connect = mocker.patch("psycopg2.connect")
@@ -364,7 +393,9 @@ def test_topic_summary_after_conversation_delete(postgres_cache_config_fixture,
364393
assert deleted is True
365394

366395

367-
def test_topic_summary_when_disconnected(postgres_cache_config_fixture, mocker):
396+
def test_topic_summary_when_disconnected(
397+
postgres_cache_config_fixture, mocker: MockerFixture
398+
):
368399
"""Test topic summary operations when cache is disconnected."""
369400
# prevent real connection to PG instance
370401
mocker.patch("psycopg2.connect")

tests/unit/metrics/test_utis.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Unit tests for functions defined in metrics/utils.py"""
22

3+
from pytest_mock import MockerFixture
34
from metrics.utils import setup_model_metrics, update_llm_token_count_from_turn
45

56

6-
async def test_setup_model_metrics(mocker) -> None:
7+
async def test_setup_model_metrics(mocker: MockerFixture) -> None:
78
"""Test the setup_model_metrics function."""
89

910
# Mock the LlamaStackAsLibraryClient
@@ -76,7 +77,7 @@ async def test_setup_model_metrics(mocker) -> None:
7677
)
7778

7879

79-
def test_update_llm_token_count_from_turn(mocker) -> None:
80+
def test_update_llm_token_count_from_turn(mocker: MockerFixture) -> None:
8081
"""Test the update_llm_token_count_from_turn function."""
8182
mocker.patch("metrics.utils.Tokenizer.get_instance")
8283
mock_formatter_class = mocker.patch("metrics.utils.ChatFormat")

tests/unit/utils/auth_helpers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Helper functions for mocking authorization in tests."""
22

3-
from typing import Any
43
from unittest.mock import AsyncMock, Mock
54
from pytest_mock import MockerFixture
65

tests/unit/utils/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""Test module for utils/common.py."""
22

33
from unittest.mock import Mock, AsyncMock
4-
from pytest_mock import MockerFixture
54
from logging import Logger
5+
from pytest_mock import MockerFixture
66

77
import pytest
88

0 commit comments

Comments
 (0)