main
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(
72 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
73 )
74
75 @parametrize
76 async def test_method_list(self, async_client: AsyncOpenAI) -> None:
77 message = await async_client.chat.completions.messages.list(
78 completion_id="completion_id",
79 )
80 assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
81
82 @parametrize
83 async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
84 message = await async_client.chat.completions.messages.list(
85 completion_id="completion_id",
86 after="after",
87 limit=0,
88 order="asc",
89 )
90 assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
91
92 @parametrize
93 async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
94 response = await async_client.chat.completions.messages.with_raw_response.list(
95 completion_id="completion_id",
96 )
97
98 assert response.is_closed is True
99 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
100 message = response.parse()
101 assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
102
103 @parametrize
104 async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
105 async with async_client.chat.completions.messages.with_streaming_response.list(
106 completion_id="completion_id",
107 ) as response:
108 assert not response.is_closed
109 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
110
111 message = await response.parse()
112 assert_matches_type(AsyncCursorPage[ChatCompletionStoreMessage], message, path=["response"])
113
114 assert cast(Any, response.is_closed) is True
115
116 @parametrize
117 async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
118 with pytest.raises(ValueError, match=r"Expected a non-empty value for `completion_id` but received ''"):
119 await async_client.chat.completions.messages.with_raw_response.list(
120 completion_id="",
121 )