|
1 | 1 | import pytest |
2 | 2 | from utils.azureopenai.client import Client |
3 | | -from unittest.mock import MagicMock |
| 3 | +from unittest.mock import MagicMock, AsyncMock |
4 | 4 | from types import SimpleNamespace |
5 | 5 |
|
6 | 6 |
|
7 | | -def test_client_make_request_success(monkeypatch): |
| 7 | +@pytest.mark.asyncio |
| 8 | +async def test_client_make_request_success(monkeypatch): |
8 | 9 | client = Client(api_key="k", endpoint="http://x", timeout=1) |
9 | | - mock_post = MagicMock() |
| 10 | + mock_post = AsyncMock() |
10 | 11 | mock_resp = MagicMock() |
11 | 12 | mock_resp.status_code = 200 |
12 | 13 | mock_resp.json.return_value = {"choices": [{"message": {"content": "hi"}}]} |
13 | 14 | mock_post.return_value = mock_resp |
14 | 15 | monkeypatch.setattr(client.http_client, "post", mock_post) |
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]) |
| 16 | + |
| 17 | + # Test with a regular dict instead of SimpleNamespace |
| 18 | + out = await client.make_request([{"role": "user", "content": "hi"}]) |
18 | 19 | assert out["choices"][0]["message"]["content"] == "hi" |
19 | 20 | mock_post.assert_called() |
20 | 21 |
|
21 | 22 |
|
22 | | -def test_client_make_request_error(monkeypatch): |
| 23 | +@pytest.mark.asyncio |
| 24 | +async def test_client_make_request_error(monkeypatch): |
23 | 25 | client = Client(api_key="k", endpoint="http://x", timeout=1) |
24 | | - mock_post = MagicMock() |
| 26 | + mock_post = AsyncMock() |
25 | 27 | mock_resp = MagicMock() |
26 | 28 | mock_resp.status_code = 400 |
27 | 29 | mock_resp.json.return_value = {"error": {"message": "fail"}} |
28 | 30 | mock_post.return_value = mock_resp |
29 | 31 | monkeypatch.setattr(client.http_client, "post", mock_post) |
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 | + |
| 33 | + # Test with a regular dict instead of SimpleNamespace |
32 | 34 | with pytest.raises(Exception) as exc: |
33 | | - client.make_request([msg]) |
| 35 | + await client.make_request([{"role": "user", "content": "hi"}]) |
34 | 36 | assert "API error" in str(exc.value) |
35 | 37 |
|
36 | 38 |
|
37 | | -def test_client_get_completion(monkeypatch): |
| 39 | +@pytest.mark.asyncio |
| 40 | +async def test_client_get_completion(monkeypatch): |
38 | 41 | client = Client(api_key="k", endpoint="http://x", timeout=1) |
39 | | - mock_make = MagicMock() |
40 | | - mock_make.return_value = {"choices": [{"message": {"content": "hi"}}]} |
41 | | - client.make_request = mock_make |
42 | | - out = client.get_completion([{"role": "user", "content": "hi"}]) |
| 42 | + # Add the get_completion method implementation for testing |
| 43 | + async def mock_make_request(*args, **kwargs): |
| 44 | + return {"choices": [{"message": {"content": "hi"}}]} |
| 45 | + client.make_request = mock_make_request |
| 46 | + out = await client.get_completion([{"role": "user", "content": "hi"}]) |
43 | 47 | assert out == "hi" |
44 | 48 |
|
45 | 49 |
|
46 | | -def test_client_get_completion_no_choices(monkeypatch): |
| 50 | +@pytest.mark.asyncio |
| 51 | +async def test_client_get_completion_no_choices(monkeypatch): |
47 | 52 | client = Client(api_key="k", endpoint="http://x", timeout=1) |
48 | | - mock_make = MagicMock() |
49 | | - mock_make.return_value = {"choices": []} |
50 | | - client.make_request = mock_make |
| 53 | + # Add the get_completion method implementation for testing |
| 54 | + async def mock_make_request(*args, **kwargs): |
| 55 | + return {"choices": []} |
| 56 | + client.make_request = mock_make_request |
51 | 57 | with pytest.raises(Exception): |
52 | | - client.get_completion([{"role": "user", "content": "hi"}]) |
| 58 | + await client.get_completion([{"role": "user", "content": "hi"}]) |
53 | 59 |
|
54 | 60 |
|
55 | | -def test_client_make_request_with_tools(monkeypatch): |
| 61 | +@pytest.mark.asyncio |
| 62 | +async def test_client_make_request_with_tools(monkeypatch): |
56 | 63 | client = Client(api_key="k", endpoint="http://x", timeout=1) |
57 | | - mock_post = MagicMock() |
| 64 | + mock_post = AsyncMock() |
58 | 65 | mock_resp = MagicMock() |
59 | 66 | mock_resp.status_code = 200 |
60 | 67 | mock_resp.json.return_value = {"choices": [{"message": {"content": "hi"}}]} |
61 | 68 | mock_post.return_value = mock_resp |
62 | 69 | 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"}]) |
| 70 | + |
| 71 | + out = await client.make_request([{"role": "user", "content": "hi"}], tools=[{"type": "tool"}]) |
66 | 72 | assert out["choices"][0]["message"]["content"] == "hi" |
67 | 73 | mock_post.assert_called() |
68 | 74 |
|
|
0 commit comments