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.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:
 19    parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
 20
 21    @parametrize
 22    def test_method_retrieve(self, client: OpenAI) -> None:
 23        model = client.models.retrieve(
 24            "gpt-4o-mini",
 25        )
 26        assert_matches_type(Model, model, path=["response"])
 27
 28    @parametrize
 29    def test_raw_response_retrieve(self, client: OpenAI) -> None:
 30        response = client.models.with_raw_response.retrieve(
 31            "gpt-4o-mini",
 32        )
 33
 34        assert response.is_closed is True
 35        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 36        model = response.parse()
 37        assert_matches_type(Model, model, path=["response"])
 38
 39    @parametrize
 40    def test_streaming_response_retrieve(self, client: OpenAI) -> None:
 41        with client.models.with_streaming_response.retrieve(
 42            "gpt-4o-mini",
 43        ) as response:
 44            assert not response.is_closed
 45            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 46
 47            model = response.parse()
 48            assert_matches_type(Model, model, path=["response"])
 49
 50        assert cast(Any, response.is_closed) is True
 51
 52    @parametrize
 53    def test_path_params_retrieve(self, client: OpenAI) -> None:
 54        with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
 55            client.models.with_raw_response.retrieve(
 56                "",
 57            )
 58
 59    @parametrize
 60    def test_method_list(self, client: OpenAI) -> None:
 61        model = client.models.list()
 62        assert_matches_type(SyncPage[Model], model, path=["response"])
 63
 64    @parametrize
 65    def test_raw_response_list(self, client: OpenAI) -> None:
 66        response = client.models.with_raw_response.list()
 67
 68        assert response.is_closed is True
 69        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 70        model = response.parse()
 71        assert_matches_type(SyncPage[Model], model, path=["response"])
 72
 73    @parametrize
 74    def test_streaming_response_list(self, client: OpenAI) -> None:
 75        with client.models.with_streaming_response.list() as response:
 76            assert not response.is_closed
 77            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 78
 79            model = response.parse()
 80            assert_matches_type(SyncPage[Model], model, path=["response"])
 81
 82        assert cast(Any, response.is_closed) is True
 83
 84    @parametrize
 85    def test_method_delete(self, client: OpenAI) -> None:
 86        model = client.models.delete(
 87            "ft:gpt-4o-mini:acemeco:suffix:abc123",
 88        )
 89        assert_matches_type(ModelDeleted, model, path=["response"])
 90
 91    @parametrize
 92    def test_raw_response_delete(self, client: OpenAI) -> None:
 93        response = client.models.with_raw_response.delete(
 94            "ft:gpt-4o-mini:acemeco:suffix:abc123",
 95        )
 96
 97        assert response.is_closed is True
 98        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
 99        model = response.parse()
100        assert_matches_type(ModelDeleted, model, path=["response"])
101
102    @parametrize
103    def test_streaming_response_delete(self, client: OpenAI) -> None:
104        with client.models.with_streaming_response.delete(
105            "ft:gpt-4o-mini:acemeco:suffix:abc123",
106        ) as response:
107            assert not response.is_closed
108            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
109
110            model = response.parse()
111            assert_matches_type(ModelDeleted, model, path=["response"])
112
113        assert cast(Any, response.is_closed) is True
114
115    @parametrize
116    def test_path_params_delete(self, client: OpenAI) -> None:
117        with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
118            client.models.with_raw_response.delete(
119                "",
120            )
121
122
123class TestAsyncModels:
124    parametrize = pytest.mark.parametrize(
125        "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
126    )
127
128    @parametrize
129    async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
130        model = await async_client.models.retrieve(
131            "gpt-4o-mini",
132        )
133        assert_matches_type(Model, model, path=["response"])
134
135    @parametrize
136    async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
137        response = await async_client.models.with_raw_response.retrieve(
138            "gpt-4o-mini",
139        )
140
141        assert response.is_closed is True
142        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
143        model = response.parse()
144        assert_matches_type(Model, model, path=["response"])
145
146    @parametrize
147    async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
148        async with async_client.models.with_streaming_response.retrieve(
149            "gpt-4o-mini",
150        ) as response:
151            assert not response.is_closed
152            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
153
154            model = await response.parse()
155            assert_matches_type(Model, model, path=["response"])
156
157        assert cast(Any, response.is_closed) is True
158
159    @parametrize
160    async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
161        with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
162            await async_client.models.with_raw_response.retrieve(
163                "",
164            )
165
166    @parametrize
167    async def test_method_list(self, async_client: AsyncOpenAI) -> None:
168        model = await async_client.models.list()
169        assert_matches_type(AsyncPage[Model], model, path=["response"])
170
171    @parametrize
172    async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
173        response = await async_client.models.with_raw_response.list()
174
175        assert response.is_closed is True
176        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
177        model = response.parse()
178        assert_matches_type(AsyncPage[Model], model, path=["response"])
179
180    @parametrize
181    async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
182        async with async_client.models.with_streaming_response.list() as response:
183            assert not response.is_closed
184            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
185
186            model = await response.parse()
187            assert_matches_type(AsyncPage[Model], model, path=["response"])
188
189        assert cast(Any, response.is_closed) is True
190
191    @parametrize
192    async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
193        model = await async_client.models.delete(
194            "ft:gpt-4o-mini:acemeco:suffix:abc123",
195        )
196        assert_matches_type(ModelDeleted, model, path=["response"])
197
198    @parametrize
199    async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
200        response = await async_client.models.with_raw_response.delete(
201            "ft:gpt-4o-mini:acemeco:suffix:abc123",
202        )
203
204        assert response.is_closed is True
205        assert response.http_request.headers.get("X-Stainless-Lang") == "python"
206        model = response.parse()
207        assert_matches_type(ModelDeleted, model, path=["response"])
208
209    @parametrize
210    async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
211        async with async_client.models.with_streaming_response.delete(
212            "ft:gpt-4o-mini:acemeco:suffix:abc123",
213        ) as response:
214            assert not response.is_closed
215            assert response.http_request.headers.get("X-Stainless-Lang") == "python"
216
217            model = await response.parse()
218            assert_matches_type(ModelDeleted, model, path=["response"])
219
220        assert cast(Any, response.is_closed) is True
221
222    @parametrize
223    async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
224        with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"):
225            await async_client.models.with_raw_response.delete(
226                "",
227            )