33from unittest .mock import Mock
44
55import pytest
6+ from fastapi import FastAPI
67from fastapi .testclient import TestClient
78
8- from cli .serve .app import app , make_chat_endpoint
9+ from cli .serve .app import make_chat_endpoint
910from cli .serve .models import ChatCompletionRequest , ChatMessage
1011
1112
@@ -61,11 +62,10 @@ def sample_request():
6162@pytest .mark .unit
6263def test_successful_completion (mock_module_success , sample_request ):
6364 """Test successful chat completion."""
65+ app = FastAPI ()
6466 endpoint = make_chat_endpoint (mock_module_success )
65- client = TestClient (app )
66-
67- # Add the endpoint to the app
6867 app .add_api_route ("/test/completions" , endpoint , methods = ["POST" ])
68+ client = TestClient (app )
6969
7070 response = client .post ("/test/completions" , json = sample_request .model_dump ())
7171
@@ -80,10 +80,10 @@ def test_successful_completion(mock_module_success, sample_request):
8080@pytest .mark .unit
8181def test_attribute_error_handling (mock_module_attribute_error , sample_request ):
8282 """Test handling of AttributeError (e.g., missing 'value' attribute)."""
83+ app = FastAPI ()
8384 endpoint = make_chat_endpoint (mock_module_attribute_error )
84- client = TestClient (app )
85-
8685 app .add_api_route ("/test/attribute-error" , endpoint , methods = ["POST" ])
86+ client = TestClient (app )
8787
8888 response = client .post ("/test/attribute-error" , json = sample_request .model_dump ())
8989
@@ -97,10 +97,10 @@ def test_attribute_error_handling(mock_module_attribute_error, sample_request):
9797@pytest .mark .unit
9898def test_value_error_handling (mock_module_value_error , sample_request ):
9999 """Test handling of ValueError (validation errors)."""
100+ app = FastAPI ()
100101 endpoint = make_chat_endpoint (mock_module_value_error )
101- client = TestClient (app )
102-
103102 app .add_api_route ("/test/value-error" , endpoint , methods = ["POST" ])
103+ client = TestClient (app )
104104
105105 response = client .post ("/test/value-error" , json = sample_request .model_dump ())
106106
@@ -115,10 +115,10 @@ def test_value_error_handling(mock_module_value_error, sample_request):
115115@pytest .mark .unit
116116def test_generic_error_handling (mock_module_generic_error , sample_request ):
117117 """Test handling of generic exceptions."""
118+ app = FastAPI ()
118119 endpoint = make_chat_endpoint (mock_module_generic_error )
119- client = TestClient (app )
120-
121120 app .add_api_route ("/test/generic-error" , endpoint , methods = ["POST" ])
121+ client = TestClient (app )
122122
123123 response = client .post ("/test/generic-error" , json = sample_request .model_dump ())
124124
0 commit comments