openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
custom-code-from-generated-20260521

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/chat/completions/test_messages.py

121lines · modeblame

6e8aaf5dstainless-app[bot]1 years ago1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.pagination import SyncCursorPage, AsyncCursorPage
13from openai.types.chat import ChatCompletionStoreMessage
14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16
17
18class TestMessages:
19parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
20
21@parametrize
22def test_method_list(self, client: OpenAI) -> None:
23message = client.chat.completions.messages.list(
24completion_id="completion_id",
25)
26assert_matches_type(SyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
27
28@parametrize
29def test_method_list_with_all_params(self, client: OpenAI) -> None:
30message = client.chat.completions.messages.list(
31completion_id="completion_id",
32after="after",
33limit=0,
34order="asc",
35)
36assert_matches_type(SyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
37
38@parametrize
39def test_raw_response_list(self, client: OpenAI) -> None:
40response = client.chat.completions.messages.with_raw_response.list(
41completion_id="completion_id",
42)
43
44assert response.is_closed is True
45assert response.http_request.headers.get("X-Stainless-Lang") == "python"
46message = response.parse()
47assert_matches_type(SyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
48
49@parametrize
50def test_streaming_response_list(self, client: OpenAI) -> None:
51with client.chat.completions.messages.with_streaming_response.list(
52completion_id="completion_id",
53) as response:
54assert not response.is_closed
55assert response.http_request.headers.get("X-Stainless-Lang") == "python"
56
57message = response.parse()
58assert_matches_type(SyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
59
60assert cast(Any, response.is_closed) is True
61
62@parametrize
63def test_path_params_list(self, client: OpenAI) -> None:
64with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"):
65client.chat.completions.messages.with_raw_response.list(
66completion_id="",
67)
68
69
70class TestAsyncMessages:
d7700afdstainless-app[bot]1 years ago71parametrize = pytest.mark.parametrize(
72"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
73)
6e8aaf5dstainless-app[bot]1 years ago74
75@parametrize
76async def test_method_list(self, async_client: AsyncOpenAI) -> None:
77message = await async_client.chat.completions.messages.list(
78completion_id="completion_id",
79)
80assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
81
82@parametrize
83async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
84message = await async_client.chat.completions.messages.list(
85completion_id="completion_id",
86after="after",
87limit=0,
88order="asc",
89)
90assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
91
92@parametrize
93async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
94response = await async_client.chat.completions.messages.with_raw_response.list(
95completion_id="completion_id",
96)
97
98assert response.is_closed is True
99assert response.http_request.headers.get("X-Stainless-Lang") == "python"
100message = response.parse()
101assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
102
103@parametrize
104async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
105async with async_client.chat.completions.messages.with_streaming_response.list(
106completion_id="completion_id",
107) as response:
108assert not response.is_closed
109assert response.http_request.headers.get("X-Stainless-Lang") == "python"
110
111message = await response.parse()
112assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
113
114assert cast(Any, response.is_closed) is True
115
116@parametrize
117async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
118with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"):
119await async_client.chat.completions.messages.with_raw_response.list(
120completion_id="",
121)