Skip to content

Commit c2a5e08

Browse files
committed
(WIP) test: add test for handling OasstError
1 parent fe21732 commit c2a5e08

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

oasst-shared/tests/test_oasst_api_client.py

+41
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
from unittest import mock
12
from uuid import uuid4
23

4+
import aiohttp
35
import pytest
46
from oasst_shared.api_client import OasstApiClient
7+
from oasst_shared.exceptions import OasstError, OasstErrorCode
58
from oasst_shared.schemas import protocol as protocol_schema
69

710

811
@pytest.fixture
912
def oasst_api_client_mocked():
13+
"""
14+
A an oasst_api_client pointed at the mocked backend.
15+
Relies on ./scripts/backend-development/start-mock-server.sh
16+
being run.
17+
"""
1018
client = OasstApiClient(backend_url="http://localhost:8080", api_key="123")
1119
yield client
1220
# TODO The fixture should close this connection, but there seems to be a bug
@@ -15,6 +23,20 @@ def oasst_api_client_mocked():
1523
# await client.close()
1624

1725

26+
@pytest.fixture
27+
def mock_http_session():
28+
yield mock.AsyncMock(spec=aiohttp.ClientSession)
29+
30+
31+
@pytest.fixture
32+
def oasst_api_client_fake_http(mock_http_session):
33+
"""
34+
An oasst_api_client that uses a mocked http session. No real requests are made.
35+
"""
36+
client = OasstApiClient(backend_url="http://localhost:8080", api_key="123", session=mock_http_session)
37+
yield client
38+
39+
1840
@pytest.mark.asyncio
1941
@pytest.mark.parametrize("task_type", protocol_schema.TaskRequestType)
2042
async def test_can_fetch_task(task_type: protocol_schema.TaskRequestType, oasst_api_client_mocked: OasstApiClient):
@@ -49,3 +71,22 @@ async def test_can_post_interaction(oasst_api_client_mocked: OasstApiClient):
4971
)
5072
is not None
5173
)
74+
75+
76+
@pytest.mark.asyncio
77+
async def test_can_handle_oasst_error_from_api(
78+
oasst_api_client_fake_http: OasstApiClient,
79+
mock_http_session: mock.AsyncMock,
80+
):
81+
# Return a 400 response with an OasstErrorResponse body
82+
response_body = protocol_schema.OasstErrorResponse(
83+
error_code=OasstErrorCode.GENERIC_ERROR,
84+
message="Some error",
85+
).json()
86+
status_code = 400
87+
88+
mock_http_session.post.return_value.__aenter__.return_value.json.return_value = response_body
89+
mock_http_session.post.return_value.__aenter__.return_value.status = status_code
90+
91+
with pytest.raises(OasstError):
92+
await oasst_api_client_fake_http.post("/some-path", data={})

0 commit comments

Comments
 (0)