openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.25.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/chatkit/test_sessions.py

230lines · modeblame

9ada2c74stainless-app[bot]8 months 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.types.beta.chatkit import (
13ChatSession,
14)
15
16base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
17
18
19class TestSessions:
20parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
21
22@parametrize
23def test_method_create(self, client: OpenAI) -> None:
24session = client.beta.chatkit.sessions.create(
25user="x",
26workflow={"id": "id"},
27)
28assert_matches_type(ChatSession, session, path=["response"])
29
30@parametrize
31def test_method_create_with_all_params(self, client: OpenAI) -> None:
32session = client.beta.chatkit.sessions.create(
33user="x",
34workflow={
35"id": "id",
36"state_variables": {"foo": "string"},
37"tracing": {"enabled": True},
38"version": "version",
39},
40chatkit_configuration={
41"automatic_thread_titling": {"enabled": True},
42"file_upload": {
43"enabled": True,
44"max_file_size": 1,
45"max_files": 1,
46},
47"history": {
48"enabled": True,
49"recent_threads": 1,
50},
51},
52expires_after={
53"anchor": "created_at",
54"seconds": 1,
55},
56rate_limits={"max_requests_per_1_minute": 1},
57)
58assert_matches_type(ChatSession, session, path=["response"])
59
60@parametrize
61def test_raw_response_create(self, client: OpenAI) -> None:
62response = client.beta.chatkit.sessions.with_raw_response.create(
63user="x",
64workflow={"id": "id"},
65)
66
67assert response.is_closed is True
68assert response.http_request.headers.get("X-Stainless-Lang") == "python"
69session = response.parse()
70assert_matches_type(ChatSession, session, path=["response"])
71
72@parametrize
73def test_streaming_response_create(self, client: OpenAI) -> None:
74with client.beta.chatkit.sessions.with_streaming_response.create(
75user="x",
76workflow={"id": "id"},
77) as response:
78assert not response.is_closed
79assert response.http_request.headers.get("X-Stainless-Lang") == "python"
80
81session = response.parse()
82assert_matches_type(ChatSession, session, path=["response"])
83
84assert cast(Any, response.is_closed) is True
85
86@parametrize
87def test_method_cancel(self, client: OpenAI) -> None:
88session = client.beta.chatkit.sessions.cancel(
89"cksess_123",
90)
91assert_matches_type(ChatSession, session, path=["response"])
92
93@parametrize
94def test_raw_response_cancel(self, client: OpenAI) -> None:
95response = client.beta.chatkit.sessions.with_raw_response.cancel(
96"cksess_123",
97)
98
99assert response.is_closed is True
100assert response.http_request.headers.get("X-Stainless-Lang") == "python"
101session = response.parse()
102assert_matches_type(ChatSession, session, path=["response"])
103
104@parametrize
105def test_streaming_response_cancel(self, client: OpenAI) -> None:
106with client.beta.chatkit.sessions.with_streaming_response.cancel(
107"cksess_123",
108) as response:
109assert not response.is_closed
110assert response.http_request.headers.get("X-Stainless-Lang") == "python"
111
112session = response.parse()
113assert_matches_type(ChatSession, session, path=["response"])
114
115assert cast(Any, response.is_closed) is True
116
117@parametrize
118def test_path_params_cancel(self, client: OpenAI) -> None:
119with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"):
120client.beta.chatkit.sessions.with_raw_response.cancel(
121"",
122)
123
124
125class TestAsyncSessions:
126parametrize = pytest.mark.parametrize(
127"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
128)
129
130@parametrize
131async def test_method_create(self, async_client: AsyncOpenAI) -> None:
132session = await async_client.beta.chatkit.sessions.create(
133user="x",
134workflow={"id": "id"},
135)
136assert_matches_type(ChatSession, session, path=["response"])
137
138@parametrize
139async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
140session = await async_client.beta.chatkit.sessions.create(
141user="x",
142workflow={
143"id": "id",
144"state_variables": {"foo": "string"},
145"tracing": {"enabled": True},
146"version": "version",
147},
148chatkit_configuration={
149"automatic_thread_titling": {"enabled": True},
150"file_upload": {
151"enabled": True,
152"max_file_size": 1,
153"max_files": 1,
154},
155"history": {
156"enabled": True,
157"recent_threads": 1,
158},
159},
160expires_after={
161"anchor": "created_at",
162"seconds": 1,
163},
164rate_limits={"max_requests_per_1_minute": 1},
165)
166assert_matches_type(ChatSession, session, path=["response"])
167
168@parametrize
169async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
170response = await async_client.beta.chatkit.sessions.with_raw_response.create(
171user="x",
172workflow={"id": "id"},
173)
174
175assert response.is_closed is True
176assert response.http_request.headers.get("X-Stainless-Lang") == "python"
177session = response.parse()
178assert_matches_type(ChatSession, session, path=["response"])
179
180@parametrize
181async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
182async with async_client.beta.chatkit.sessions.with_streaming_response.create(
183user="x",
184workflow={"id": "id"},
185) as response:
186assert not response.is_closed
187assert response.http_request.headers.get("X-Stainless-Lang") == "python"
188
189session = await response.parse()
190assert_matches_type(ChatSession, session, path=["response"])
191
192assert cast(Any, response.is_closed) is True
193
194@parametrize
195async def test_method_cancel(self, async_client: AsyncOpenAI) -> None:
196session = await async_client.beta.chatkit.sessions.cancel(
197"cksess_123",
198)
199assert_matches_type(ChatSession, session, path=["response"])
200
201@parametrize
202async def test_raw_response_cancel(self, async_client: AsyncOpenAI) -> None:
203response = await async_client.beta.chatkit.sessions.with_raw_response.cancel(
204"cksess_123",
205)
206
207assert response.is_closed is True
208assert response.http_request.headers.get("X-Stainless-Lang") == "python"
209session = response.parse()
210assert_matches_type(ChatSession, session, path=["response"])
211
212@parametrize
213async def test_streaming_response_cancel(self, async_client: AsyncOpenAI) -> None:
214async with async_client.beta.chatkit.sessions.with_streaming_response.cancel(
215"cksess_123",
216) as response:
217assert not response.is_closed
218assert response.http_request.headers.get("X-Stainless-Lang") == "python"
219
220session = await response.parse()
221assert_matches_type(ChatSession, session, path=["response"])
222
223assert cast(Any, response.is_closed) is True
224
225@parametrize
226async def test_path_params_cancel(self, async_client: AsyncOpenAI) -> None:
227with pytest.raises(ValueError, match=r"Expected a non-empty value for `session_id` but received ''"):
228await async_client.beta.chatkit.sessions.with_raw_response.cancel(
229"",
230)