Skip to content

Commit 35bcc4b

Browse files
fix(tests): fix: tests which call HTTP endpoints directly with the example parameters
1 parent 68044ee commit 35bcc4b

File tree

1 file changed

+40
-89
lines changed

1 file changed

+40
-89
lines changed

tests/test_client.py

Lines changed: 40 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323

2424
from openai import OpenAI, AsyncOpenAI, APIResponseValidationError
2525
from openai._types import Omit
26-
from openai._utils import maybe_transform
2726
from openai._models import BaseModel, FinalRequestOptions
28-
from openai._constants import RAW_RESPONSE_HEADER
2927
from openai._streaming import Stream, AsyncStream
3028
from openai._exceptions import OpenAIError, APIStatusError, APITimeoutError, APIResponseValidationError
3129
from openai._base_client import (
@@ -36,7 +34,6 @@
3634
DefaultAsyncHttpxClient,
3735
make_request_options,
3836
)
39-
from openai.types.chat.completion_create_params import CompletionCreateParamsNonStreaming
4037

4138
from .utils import update_env
4239

@@ -725,60 +722,37 @@ def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str
725722

726723
@mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
727724
@pytest.mark.respx(base_url=base_url)
728-
def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> None:
725+
def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter, client: OpenAI) -> None:
729726
respx_mock.post("/chat/completions").mock(side_effect=httpx.TimeoutException("Test timeout error"))
730727

731728
with pytest.raises(APITimeoutError):
732-
self.client.post(
733-
"/chat/completions",
734-
body=cast(
735-
object,
736-
maybe_transform(
737-
dict(
738-
messages=[
739-
{
740-
"role": "user",
741-
"content": "Say this is a test",
742-
}
743-
],
744-
model="gpt-4o",
745-
),
746-
CompletionCreateParamsNonStreaming,
747-
),
748-
),
749-
cast_to=httpx.Response,
750-
options={"headers": {RAW_RESPONSE_HEADER: "stream"}},
751-
)
729+
client.chat.completions.with_streaming_response.create(
730+
messages=[
731+
{
732+
"content": "string",
733+
"role": "developer",
734+
}
735+
],
736+
model="gpt-4o",
737+
).__enter__()
752738

753739
assert _get_open_connections(self.client) == 0
754740

755741
@mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
756742
@pytest.mark.respx(base_url=base_url)
757-
def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> None:
743+
def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter, client: OpenAI) -> None:
758744
respx_mock.post("/chat/completions").mock(return_value=httpx.Response(500))
759745

760746
with pytest.raises(APIStatusError):
761-
self.client.post(
762-
"/chat/completions",
763-
body=cast(
764-
object,
765-
maybe_transform(
766-
dict(
767-
messages=[
768-
{
769-
"role": "user",
770-
"content": "Say this is a test",
771-
}
772-
],
773-
model="gpt-4o",
774-
),
775-
CompletionCreateParamsNonStreaming,
776-
),
777-
),
778-
cast_to=httpx.Response,
779-
options={"headers": {RAW_RESPONSE_HEADER: "stream"}},
780-
)
781-
747+
client.chat.completions.with_streaming_response.create(
748+
messages=[
749+
{
750+
"content": "string",
751+
"role": "developer",
752+
}
753+
],
754+
model="gpt-4o",
755+
).__enter__()
782756
assert _get_open_connections(self.client) == 0
783757

784758
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
@@ -1647,60 +1621,37 @@ async def test_parse_retry_after_header(self, remaining_retries: int, retry_afte
16471621

16481622
@mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
16491623
@pytest.mark.respx(base_url=base_url)
1650-
async def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> None:
1624+
async def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter, async_client: AsyncOpenAI) -> None:
16511625
respx_mock.post("/chat/completions").mock(side_effect=httpx.TimeoutException("Test timeout error"))
16521626

16531627
with pytest.raises(APITimeoutError):
1654-
await self.client.post(
1655-
"/chat/completions",
1656-
body=cast(
1657-
object,
1658-
maybe_transform(
1659-
dict(
1660-
messages=[
1661-
{
1662-
"role": "user",
1663-
"content": "Say this is a test",
1664-
}
1665-
],
1666-
model="gpt-4o",
1667-
),
1668-
CompletionCreateParamsNonStreaming,
1669-
),
1670-
),
1671-
cast_to=httpx.Response,
1672-
options={"headers": {RAW_RESPONSE_HEADER: "stream"}},
1673-
)
1628+
await async_client.chat.completions.with_streaming_response.create(
1629+
messages=[
1630+
{
1631+
"content": "string",
1632+
"role": "developer",
1633+
}
1634+
],
1635+
model="gpt-4o",
1636+
).__aenter__()
16741637

16751638
assert _get_open_connections(self.client) == 0
16761639

16771640
@mock.patch("openai._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
16781641
@pytest.mark.respx(base_url=base_url)
1679-
async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> None:
1642+
async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter, async_client: AsyncOpenAI) -> None:
16801643
respx_mock.post("/chat/completions").mock(return_value=httpx.Response(500))
16811644

16821645
with pytest.raises(APIStatusError):
1683-
await self.client.post(
1684-
"/chat/completions",
1685-
body=cast(
1686-
object,
1687-
maybe_transform(
1688-
dict(
1689-
messages=[
1690-
{
1691-
"role": "user",
1692-
"content": "Say this is a test",
1693-
}
1694-
],
1695-
model="gpt-4o",
1696-
),
1697-
CompletionCreateParamsNonStreaming,
1698-
),
1699-
),
1700-
cast_to=httpx.Response,
1701-
options={"headers": {RAW_RESPONSE_HEADER: "stream"}},
1702-
)
1703-
1646+
await async_client.chat.completions.with_streaming_response.create(
1647+
messages=[
1648+
{
1649+
"content": "string",
1650+
"role": "developer",
1651+
}
1652+
],
1653+
model="gpt-4o",
1654+
).__aenter__()
17041655
assert _get_open_connections(self.client) == 0
17051656

17061657
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])

0 commit comments

Comments
 (0)