11# pylint: disable=invalid-name
22
3- import json
43from unittest .mock import patch
54
65import pytest
76import requests
87
9- import meilisearch
108from meilisearch .errors import MeilisearchApiError , MeilisearchCommunicationError
11- from tests import BASE_URL , MASTER_KEY
129
1310
1411class MockStreamingResponse :
1512 """Mock response object for testing streaming functionality."""
16-
13+
1714 def __init__ (self , lines , ok = True , status_code = 200 , text = "" ):
1815 self .lines = lines
1916 self .ok = ok
2017 self .status_code = status_code
2118 self .text = text
2219 self ._closed = False
23-
20+
2421 def iter_lines (self ):
2522 """Simulate iter_lines() method of requests.Response."""
26- for line in self .lines :
27- yield line
28-
23+ yield from self .lines
24+
2925 def close (self ):
3026 """Simulate close() method of requests.Response."""
3127 self ._closed = True
32-
28+
3329 def raise_for_status (self ):
3430 """Simulate raise_for_status() method of requests.Response."""
3531 if not self .ok :
@@ -44,11 +40,11 @@ def test_create_chat_completion_basic_stream(client):
4440 b'data: [DONE]'
4541 ]
4642 mock_resp = MockStreamingResponse (dummy_lines )
47-
43+
4844 with patch .object (client .http , 'post_stream' , return_value = mock_resp ) as mock_post :
4945 messages = [{"role" : "user" , "content" : "Hi" }]
5046 chunks = list (client .create_chat_completion ("my-assistant" , messages = messages ))
51-
47+
5248 # Verify the HTTP call was made correctly
5349 mock_post .assert_called_once_with (
5450 "chats/my-assistant/chat/completions" ,
@@ -58,18 +54,18 @@ def test_create_chat_completion_basic_stream(client):
5854 "stream" : True
5955 }
6056 )
61-
57+
6258 # Verify the chunks are parsed correctly
6359 assert len (chunks ) == 2
6460 assert chunks [0 ]["choices" ][0 ]["delta" ]["content" ] == "Hello"
6561 assert chunks [1 ]["choices" ][0 ]["delta" ]["content" ] == " world"
66- assert mock_resp ._closed
62+ assert mock_resp ._closed # pylint: disable=protected-access
6763
6864
6965def test_create_chat_completion_stream_false_raises_error (client ):
7066 """Test that stream=False raises ValueError."""
7167 messages = [{"role" : "user" , "content" : "Test" }]
72-
68+
7369 with pytest .raises (ValueError , match = "Non-streaming chat completions are not supported" ):
7470 list (client .create_chat_completion ("my-assistant" , messages = messages , stream = False ))
7571
@@ -80,10 +76,10 @@ def test_create_chat_completion_json_decode_error(client):
8076 b'data: {"invalid": json}' , # Malformed JSON
8177 ]
8278 mock_resp = MockStreamingResponse (dummy_lines )
83-
79+
8480 with patch .object (client .http , 'post_stream' , return_value = mock_resp ):
8581 messages = [{"role" : "user" , "content" : "Test" }]
86-
82+
8783 with pytest .raises (MeilisearchCommunicationError , match = "Failed to parse chat chunk" ):
8884 list (client .create_chat_completion ("my-assistant" , messages = messages ))
8985
@@ -94,7 +90,7 @@ def test_create_chat_completion_http_error_propagated(client):
9490 error_response = MockStreamingResponse ([], ok = False , status_code = 400 , text = '{"message": "API Error"}' )
9591 mock_post .side_effect = MeilisearchApiError ("API Error" , error_response )
9692 messages = [{"role" : "user" , "content" : "Test" }]
97-
93+
9894 with pytest .raises (MeilisearchApiError , match = "API Error" ):
9995 list (client .create_chat_completion ("my-assistant" , messages = messages ))
10096
@@ -110,13 +106,13 @@ def test_get_chat_workspaces(client):
110106 "limit" : 20 ,
111107 "total" : 2
112108 }
113-
109+
114110 with patch .object (client .http , 'get' , return_value = mock_response ) as mock_get :
115111 result = client .get_chat_workspaces ()
116-
112+
117113 # Verify the HTTP call was made correctly
118114 mock_get .assert_called_once_with ("chats" )
119-
115+
120116 # Verify the response is returned as-is
121117 assert result == mock_response
122118
@@ -128,17 +124,17 @@ def test_update_chat_workspace_settings(client):
128124 "temperature" : 0.8 ,
129125 "max_tokens" : 1500
130126 }
131-
127+
132128 settings_update = {
133129 "temperature" : 0.8 ,
134130 "max_tokens" : 1500
135131 }
136-
132+
137133 with patch .object (client .http , 'patch' , return_value = mock_response ) as mock_patch :
138134 result = client .update_chat_workspace_settings ("my-workspace" , settings_update )
139-
135+
140136 # Verify the HTTP call was made correctly
141137 mock_patch .assert_called_once_with ("chats/my-workspace/settings" , body = settings_update )
142-
138+
143139 # Verify the response is returned as-is
144- assert result == mock_response
140+ assert result == mock_response
0 commit comments