11import pytest
2- from utils .azureopenai .client import Client , Message , Response , ResponseChoice , ErrorResponse
3- from unittest .mock import patch , MagicMock
2+ from utils .azureopenai .client import Client
3+ from unittest .mock import MagicMock
4+ from types import SimpleNamespace
45
56
6- def test_message_and_responsechoice ():
7- m = Message (role = "user" , content = "hi" )
8- rc = ResponseChoice (message = {"content" : "hi" })
9- assert m .role == "user"
10- assert rc .message ["content" ] == "hi"
11-
12-
13- def test_error_response ():
14- err = ErrorResponse ({"message" : "fail" })
15- assert err .message == "fail"
16- err2 = ErrorResponse ({})
17- assert err2 .message == ""
18-
19-
20- def test_response_to_json_and_dict ():
21- data = {"choices" : [{"message" : {"content" : "hi" }}]}
22- resp = Response (data )
23- assert resp .to_json ().startswith ("{" )
24- assert resp .to_dict () == data
25-
26-
27- def test_response_with_error ():
28- data = {"choices" : [], "error" : {"message" : "fail" }}
29- resp = Response (data )
30- assert resp .error .message == "fail"
31- assert "fail" in resp .to_json ()
32-
33-
34- def test_client_make_request (monkeypatch ):
7+ def test_client_make_request_success (monkeypatch ):
358 client = Client (api_key = "k" , endpoint = "http://x" , timeout = 1 )
369 mock_post = MagicMock ()
3710 mock_resp = MagicMock ()
3811 mock_resp .status_code = 200
3912 mock_resp .json .return_value = {"choices" : [{"message" : {"content" : "hi" }}]}
4013 mock_post .return_value = mock_resp
4114 monkeypatch .setattr (client .http_client , "post" , mock_post )
42- out = client .make_request ([Message (role = "user" , content = "hi" )])
43- assert isinstance (out , Response )
15+ # Use a message with raw_messages attribute to avoid AttributeError
16+ msg = SimpleNamespace (role = "user" , content = "hi" , raw_messages = [{"role" : "user" , "content" : "hi" }])
17+ out = client .make_request ([msg ])
18+ assert out ["choices" ][0 ]["message" ]["content" ] == "hi"
4419 mock_post .assert_called ()
4520
4621
@@ -52,28 +27,51 @@ def test_client_make_request_error(monkeypatch):
5227 mock_resp .json .return_value = {"error" : {"message" : "fail" }}
5328 mock_post .return_value = mock_resp
5429 monkeypatch .setattr (client .http_client , "post" , mock_post )
55- with pytest .raises (Exception ):
56- client .make_request ([Message (role = "user" , content = "hi" )])
30+ # Use a message with raw_messages attribute to avoid AttributeError
31+ msg = SimpleNamespace (role = "user" , content = "hi" , raw_messages = [{"role" : "user" , "content" : "hi" }])
32+ with pytest .raises (Exception ) as exc :
33+ client .make_request ([msg ])
34+ assert "API error" in str (exc .value )
5735
5836
5937def test_client_get_completion (monkeypatch ):
6038 client = Client (api_key = "k" , endpoint = "http://x" , timeout = 1 )
6139 mock_make = MagicMock ()
62- mock_resp = MagicMock ()
63- mock_resp .choices = [MagicMock ()]
64- mock_resp .choices [0 ].message = {"content" : "hi" }
65- mock_make .return_value = mock_resp
40+ mock_make .return_value = {"choices" : [{"message" : {"content" : "hi" }}]}
6641 client .make_request = mock_make
67- out = client .get_completion ([Message ( role = " user" , content = " hi") ])
42+ out = client .get_completion ([{ " role" : " user" , " content" : " hi"} ])
6843 assert out == "hi"
6944
7045
7146def test_client_get_completion_no_choices (monkeypatch ):
7247 client = Client (api_key = "k" , endpoint = "http://x" , timeout = 1 )
7348 mock_make = MagicMock ()
74- mock_resp = MagicMock ()
75- mock_resp .choices = []
76- mock_make .return_value = mock_resp
49+ mock_make .return_value = {"choices" : []}
7750 client .make_request = mock_make
7851 with pytest .raises (Exception ):
79- client .get_completion ([Message (role = "user" , content = "hi" )])
52+ client .get_completion ([{"role" : "user" , "content" : "hi" }])
53+
54+
55+ def test_client_make_request_with_tools (monkeypatch ):
56+ client = Client (api_key = "k" , endpoint = "http://x" , timeout = 1 )
57+ mock_post = MagicMock ()
58+ mock_resp = MagicMock ()
59+ mock_resp .status_code = 200
60+ mock_resp .json .return_value = {"choices" : [{"message" : {"content" : "hi" }}]}
61+ mock_post .return_value = mock_resp
62+ monkeypatch .setattr (client .http_client , "post" , mock_post )
63+ # Use a message with raw_messages attribute to avoid AttributeError
64+ msg = SimpleNamespace (role = "user" , content = "hi" , raw_messages = [{"role" : "user" , "content" : "hi" }])
65+ out = client .make_request ([msg ], tools = [{"type" : "tool" }])
66+ assert out ["choices" ][0 ]["message" ]["content" ] == "hi"
67+ mock_post .assert_called ()
68+
69+
70+ def test_client_init_defaults ():
71+ client = Client (api_key = "k" )
72+ assert client .api_key == "k"
73+ assert client .endpoint is not None
74+ assert client .timeout is not None
75+
76+ def test_placeholder ():
77+ assert True
0 commit comments