openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.17.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_models.py

225lines · modeblame

5cfb125aStainless Bot2 years ago1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
08b8179aDavid Schnurr2 years ago2
3from __future__ import annotations
4
5import os
86379b44Stainless Bot2 years ago6from typing import Any, cast
08b8179aDavid Schnurr2 years ago7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.types import Model, ModelDeleted
13from openai.pagination import SyncPage, AsyncPage
14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16
17
18class TestModels:
98d779fbStainless Bot2 years ago19parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago20
21@parametrize
22def test_method_retrieve(self, client: OpenAI) -> None:
23model = client.models.retrieve(
24"gpt-3.5-turbo",
25)
26assert_matches_type(Model, model, path=["response"])
27
28@parametrize
29def test_raw_response_retrieve(self, client: OpenAI) -> None:
30response = client.models.with_raw_response.retrieve(
31"gpt-3.5-turbo",
32)
86379b44Stainless Bot2 years ago33
34assert response.is_closed is True
08b8179aDavid Schnurr2 years ago35assert response.http_request.headers.get("X-Stainless-Lang") == "python"
36model = response.parse()
37assert_matches_type(Model, model, path=["response"])
38
86379b44Stainless Bot2 years ago39@parametrize
40def test_streaming_response_retrieve(self, client: OpenAI) -> None:
41with client.models.with_streaming_response.retrieve(
42"gpt-3.5-turbo",
43) as response:
44assert not response.is_closed
45assert response.http_request.headers.get("X-Stainless-Lang") == "python"
46
47model = response.parse()
48assert_matches_type(Model, model, path=["response"])
49
50assert cast(Any, response.is_closed) is True
51
023a4e66Stainless Bot2 years ago52@parametrize
53def test_path_params_retrieve(self, client: OpenAI) -> None:
54with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
55client.models.with_raw_response.retrieve(
56"",
57)
58
08b8179aDavid Schnurr2 years ago59@parametrize
60def test_method_list(self, client: OpenAI) -> None:
61model = client.models.list()
62assert_matches_type(SyncPage[Model], model, path=["response"])
63
64@parametrize
65def test_raw_response_list(self, client: OpenAI) -> None:
66response = client.models.with_raw_response.list()
86379b44Stainless Bot2 years ago67
68assert response.is_closed is True
08b8179aDavid Schnurr2 years ago69assert response.http_request.headers.get("X-Stainless-Lang") == "python"
70model = response.parse()
71assert_matches_type(SyncPage[Model], model, path=["response"])
72
86379b44Stainless Bot2 years ago73@parametrize
74def test_streaming_response_list(self, client: OpenAI) -> None:
75with client.models.with_streaming_response.list() as response:
76assert not response.is_closed
77assert response.http_request.headers.get("X-Stainless-Lang") == "python"
78
79model = response.parse()
80assert_matches_type(SyncPage[Model], model, path=["response"])
81
82assert cast(Any, response.is_closed) is True
83
08b8179aDavid Schnurr2 years ago84@parametrize
85def test_method_delete(self, client: OpenAI) -> None:
86model = client.models.delete(
87"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
88)
89assert_matches_type(ModelDeleted, model, path=["response"])
90
91@parametrize
92def test_raw_response_delete(self, client: OpenAI) -> None:
93response = client.models.with_raw_response.delete(
94"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
95)
86379b44Stainless Bot2 years ago96
97assert response.is_closed is True
08b8179aDavid Schnurr2 years ago98assert response.http_request.headers.get("X-Stainless-Lang") == "python"
99model = response.parse()
100assert_matches_type(ModelDeleted, model, path=["response"])
101
86379b44Stainless Bot2 years ago102@parametrize
103def test_streaming_response_delete(self, client: OpenAI) -> None:
104with client.models.with_streaming_response.delete(
105"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
106) as response:
107assert not response.is_closed
108assert response.http_request.headers.get("X-Stainless-Lang") == "python"
109
110model = response.parse()
111assert_matches_type(ModelDeleted, model, path=["response"])
112
113assert cast(Any, response.is_closed) is True
114
023a4e66Stainless Bot2 years ago115@parametrize
116def test_path_params_delete(self, client: OpenAI) -> None:
117with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
118client.models.with_raw_response.delete(
119"",
120)
121
08b8179aDavid Schnurr2 years ago122
123class TestAsyncModels:
98d779fbStainless Bot2 years ago124parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago125
126@parametrize
98d779fbStainless Bot2 years ago127async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
128model = await async_client.models.retrieve(
08b8179aDavid Schnurr2 years ago129"gpt-3.5-turbo",
130)
131assert_matches_type(Model, model, path=["response"])
132
133@parametrize
98d779fbStainless Bot2 years ago134async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
135response = await async_client.models.with_raw_response.retrieve(
08b8179aDavid Schnurr2 years ago136"gpt-3.5-turbo",
137)
86379b44Stainless Bot2 years ago138
139assert response.is_closed is True
08b8179aDavid Schnurr2 years ago140assert response.http_request.headers.get("X-Stainless-Lang") == "python"
141model = response.parse()
142assert_matches_type(Model, model, path=["response"])
143
86379b44Stainless Bot2 years ago144@parametrize
98d779fbStainless Bot2 years ago145async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
146async with async_client.models.with_streaming_response.retrieve(
86379b44Stainless Bot2 years ago147"gpt-3.5-turbo",
148) as response:
149assert not response.is_closed
150assert response.http_request.headers.get("X-Stainless-Lang") == "python"
151
152model = await response.parse()
153assert_matches_type(Model, model, path=["response"])
154
155assert cast(Any, response.is_closed) is True
156
023a4e66Stainless Bot2 years ago157@parametrize
98d779fbStainless Bot2 years ago158async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago159with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
98d779fbStainless Bot2 years ago160await async_client.models.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago161"",
162)
163
08b8179aDavid Schnurr2 years ago164@parametrize
98d779fbStainless Bot2 years ago165async def test_method_list(self, async_client: AsyncOpenAI) -> None:
166model = await async_client.models.list()
08b8179aDavid Schnurr2 years ago167assert_matches_type(AsyncPage[Model], model, path=["response"])
168
169@parametrize
98d779fbStainless Bot2 years ago170async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
171response = await async_client.models.with_raw_response.list()
86379b44Stainless Bot2 years ago172
173assert response.is_closed is True
08b8179aDavid Schnurr2 years ago174assert response.http_request.headers.get("X-Stainless-Lang") == "python"
175model = response.parse()
176assert_matches_type(AsyncPage[Model], model, path=["response"])
177
86379b44Stainless Bot2 years ago178@parametrize
98d779fbStainless Bot2 years ago179async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
180async with async_client.models.with_streaming_response.list() as response:
86379b44Stainless Bot2 years ago181assert not response.is_closed
182assert response.http_request.headers.get("X-Stainless-Lang") == "python"
183
184model = await response.parse()
185assert_matches_type(AsyncPage[Model], model, path=["response"])
186
187assert cast(Any, response.is_closed) is True
188
08b8179aDavid Schnurr2 years ago189@parametrize
98d779fbStainless Bot2 years ago190async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
191model = await async_client.models.delete(
08b8179aDavid Schnurr2 years ago192"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
193)
194assert_matches_type(ModelDeleted, model, path=["response"])
195
196@parametrize
98d779fbStainless Bot2 years ago197async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
198response = await async_client.models.with_raw_response.delete(
08b8179aDavid Schnurr2 years ago199"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
200)
86379b44Stainless Bot2 years ago201
202assert response.is_closed is True
08b8179aDavid Schnurr2 years ago203assert response.http_request.headers.get("X-Stainless-Lang") == "python"
204model = response.parse()
205assert_matches_type(ModelDeleted, model, path=["response"])
86379b44Stainless Bot2 years ago206
207@parametrize
98d779fbStainless Bot2 years ago208async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
209async with async_client.models.with_streaming_response.delete(
86379b44Stainless Bot2 years ago210"ft:gpt-3.5-turbo:acemeco:suffix:abc123",
211) as response:
212assert not response.is_closed
213assert response.http_request.headers.get("X-Stainless-Lang") == "python"
214
215model = await response.parse()
216assert_matches_type(ModelDeleted, model, path=["response"])
217
218assert cast(Any, response.is_closed) is True
023a4e66Stainless Bot2 years ago219
220@parametrize
98d779fbStainless Bot2 years ago221async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago222with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
98d779fbStainless Bot2 years ago223await async_client.models.with_raw_response.delete(
023a4e66Stainless Bot2 years ago224"",
225)