Skip to content

Commit

Permalink
fix: unified test exceptions tests into one
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik committed Nov 26, 2024
1 parent d5f6b59 commit d66488d
Showing 1 changed file with 14 additions and 69 deletions.
83 changes: 14 additions & 69 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


@pytest.mark.parametrize("stream", [False, True])
@pytest.mark.parametrize("repeats", [1, 2, 3])
@pytest.mark.parametrize("repeats", [0, 1, 2, 3])
async def test_interceptor_errors(stream: bool, repeats: int):
httpx_client = create_httpx_client(
[
Expand Down Expand Up @@ -76,80 +76,25 @@ async def test_interceptor_errors(stream: bool, repeats: int):
created=created_checker,
)

expected = create_sse_stream_checker(
chunk_checker(),
{
# FIXME: error chunks shouldn't have id/created/object fields
# https://github.com/epam/ai-dial-sdk/blob/development/aidial_sdk/chat_completion/chunks.py#L31-L35
# Alternatively, the errors in the stream should be
# translated to DIALExceptions in the Interceptors SDK
if repeats == 0:
extra_fields = {}
else:
# FIXME: The errors in the stream should be
# translated to DIALExceptions in the Interceptors SDK instead of
# being used as if they are normal chat completion chunks in
# the method `handle_streaming_errors`.
extra_fields = {
"id": id_checker,
"created": created_checker,
"object": "chat.completion.chunk",
"error": {"message": "Too many requests", "code": "429"},
},
"[DONE]",
)

match_objects(actual, expected)


@pytest.mark.parametrize("stream", [False, True])
async def test_direct_errors(stream: bool):
httpx_client = create_httpx_client(
[
(
"upstream",
(
"chat/completions",
create_broken_application(to_many_requests_error),
),
),
("no-op", ChatCompletionNoOpInterceptor),
],
["upstream"],
)

response = await httpx_client.post(
"chat/completions",
json={
"stream": stream,
"messages": [{"role": "user", "content": "hello"}],
},
)

if not stream:
assert response.status_code == 429
actual_headers = {
k.decode(): v.decode() for k, v in response.headers.raw
}
assert match_objects(
actual_headers,
{
"content-length": has_type(str),
"content-type": "application/json",
"retry-after": "42",
},
)
assert response.json() == {
"error": {"message": "Too many requests", "code": "429"}
}
else:
assert response.status_code == 200

actual = [line async for line in response.aiter_lines()]

id_checker = memorize(has_type(str))
created_checker = memorize(has_type(int))
chunk_checker = create_chunk_checker(
stream=stream,
id=id_checker,
created=created_checker,
)
}

expected = create_sse_stream_checker(
chunk_checker(),
{"error": {"message": "Too many requests", "code": "429"}},
{
**extra_fields,
"error": {"message": "Too many requests", "code": "429"},
},
"[DONE]",
)

Expand Down

0 comments on commit d66488d

Please sign in to comment.