openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.63.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/chat/completions/test_messages.py

119lines · modecode

1# 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:
19 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
20
21 @parametrize
22 def test_method_list(self, client: OpenAI) -> None:
23 message = client.chat.completions.messages.list(
24 completion_id="completion_id",
25 )
26 assert_matches_type(SyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
27
28 @parametrize
29 def test_method_list_with_all_params(self, client: OpenAI) -> None:
30 message = client.chat.completions.messages.list(
31 completion_id="completion_id",
32 after="after",
33 limit=0,
34 order="asc",
35 )
36 assert_matches_type(SyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
37
38 @parametrize
39 def test_raw_response_list(self, client: OpenAI) -> None:
40 response = client.chat.completions.messages.with_raw_response.list(
41 completion_id="completion_id",
42 )
43
44 assert response.is_closed is True
45 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
46 message = response.parse()
47 assert_matches_type(SyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
48
49 @parametrize
50 def test_streaming_response_list(self, client: OpenAI) -> None:
51 with client.chat.completions.messages.with_streaming_response.list(
52 completion_id="completion_id",
53 ) as response:
54 assert not response.is_closed
55 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
56
57 message = response.parse()
58 assert_matches_type(SyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
59
60 assert cast(Any, response.is_closed) is True
61
62 @parametrize
63 def test_path_params_list(self, client: OpenAI) -> None:
64 with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"):
65 client.chat.completions.messages.with_raw_response.list(
66 completion_id="",
67 )
68
69
70class TestAsyncMessages:
71 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
72
73 @parametrize
74 async def test_method_list(self, async_client: AsyncOpenAI) -> None:
75 message = await async_client.chat.completions.messages.list(
76 completion_id="completion_id",
77 )
78 assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
79
80 @parametrize
81 async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
82 message = await async_client.chat.completions.messages.list(
83 completion_id="completion_id",
84 after="after",
85 limit=0,
86 order="asc",
87 )
88 assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
89
90 @parametrize
91 async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
92 response = await async_client.chat.completions.messages.with_raw_response.list(
93 completion_id="completion_id",
94 )
95
96 assert response.is_closed is True
97 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
98 message = response.parse()
99 assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
100
101 @parametrize
102 async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
103 async with async_client.chat.completions.messages.with_streaming_response.list(
104 completion_id="completion_id",
105 ) as response:
106 assert not response.is_closed
107 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
108
109 message = await response.parse()
110 assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
111
112 assert cast(Any, response.is_closed) is True
113
114 @parametrize
115 async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
116 with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"):
117 await async_client.chat.completions.messages.with_raw_response.list(
118 completion_id="",
119 )
120