openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/test_models.py
225lines · modeblame
5cfb125aStainless Bot2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
08b8179aDavid Schnurr2 years ago | 2 | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
86379b44Stainless Bot2 years ago | 6 | from typing import Any, cast |
08b8179aDavid Schnurr2 years ago | 7 | |
| 8 | import pytest | |
| 9 | | |
| 10 | from openai import OpenAI, AsyncOpenAI | |
| 11 | from tests.utils import assert_matches_type | |
| 12 | from openai.types import Model, ModelDeleted | |
| 13 | from openai.pagination import SyncPage, AsyncPage | |
| 14 | | |
| 15 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 16 | | |
| 17 | | |
| 18 | class TestModels: | |
98d779fbStainless Bot2 years ago | 19 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 20 | |
| 21 | @parametrize | |
| 22 | def test_method_retrieve(self, client: OpenAI) -> None: | |
| 23 | model = client.models.retrieve( | |
| 24 | "gpt-3.5-turbo", | |
| 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-3.5-turbo", | |
| 32 | ) | |
86379b44Stainless Bot2 years ago | 33 | |
| 34 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 35 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 36 | model = response.parse() | |
| 37 | assert_matches_type(Model, model, path=["response"]) | |
| 38 | | |
86379b44Stainless Bot2 years ago | 39 | @parametrize |
| 40 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: | |
| 41 | with client.models.with_streaming_response.retrieve( | |
| 42 | "gpt-3.5-turbo", | |
| 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 | | |
023a4e66Stainless Bot2 years ago | 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 | | |
08b8179aDavid Schnurr2 years ago | 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() | |
86379b44Stainless Bot2 years ago | 67 | |
| 68 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 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 | | |
86379b44Stainless Bot2 years ago | 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 | | |
08b8179aDavid Schnurr2 years ago | 84 | @parametrize |
| 85 | def test_method_delete(self, client: OpenAI) -> None: | |
| 86 | model = client.models.delete( | |
| 87 | "ft:gpt-3.5-turbo: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-3.5-turbo:acemeco:suffix:abc123", | |
| 95 | ) | |
86379b44Stainless Bot2 years ago | 96 | |
| 97 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 98 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 99 | model = response.parse() | |
| 100 | assert_matches_type(ModelDeleted, model, path=["response"]) | |
| 101 | | |
86379b44Stainless Bot2 years ago | 102 | @parametrize |
| 103 | def test_streaming_response_delete(self, client: OpenAI) -> None: | |
| 104 | with client.models.with_streaming_response.delete( | |
| 105 | "ft:gpt-3.5-turbo: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 | | |
023a4e66Stainless Bot2 years ago | 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 | | |
08b8179aDavid Schnurr2 years ago | 122 | |
| 123 | class TestAsyncModels: | |
98d779fbStainless Bot2 years ago | 124 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 125 | |
| 126 | @parametrize | |
98d779fbStainless Bot2 years ago | 127 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 128 | model = await async_client.models.retrieve( | |
08b8179aDavid Schnurr2 years ago | 129 | "gpt-3.5-turbo", |
| 130 | ) | |
| 131 | assert_matches_type(Model, model, path=["response"]) | |
| 132 | | |
| 133 | @parametrize | |
98d779fbStainless Bot2 years ago | 134 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 135 | response = await async_client.models.with_raw_response.retrieve( | |
08b8179aDavid Schnurr2 years ago | 136 | "gpt-3.5-turbo", |
| 137 | ) | |
86379b44Stainless Bot2 years ago | 138 | |
| 139 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 140 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 141 | model = response.parse() | |
| 142 | assert_matches_type(Model, model, path=["response"]) | |
| 143 | | |
86379b44Stainless Bot2 years ago | 144 | @parametrize |
98d779fbStainless Bot2 years ago | 145 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 146 | async with async_client.models.with_streaming_response.retrieve( | |
86379b44Stainless Bot2 years ago | 147 | "gpt-3.5-turbo", |
| 148 | ) as response: | |
| 149 | assert not response.is_closed | |
| 150 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 151 | | |
| 152 | model = await response.parse() | |
| 153 | assert_matches_type(Model, model, path=["response"]) | |
| 154 | | |
| 155 | assert cast(Any, response.is_closed) is True | |
| 156 | | |
023a4e66Stainless Bot2 years ago | 157 | @parametrize |
98d779fbStainless Bot2 years ago | 158 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 159 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"): |
98d779fbStainless Bot2 years ago | 160 | await async_client.models.with_raw_response.retrieve( |
023a4e66Stainless Bot2 years ago | 161 | "", |
| 162 | ) | |
| 163 | | |
08b8179aDavid Schnurr2 years ago | 164 | @parametrize |
98d779fbStainless Bot2 years ago | 165 | async def test_method_list(self, async_client: AsyncOpenAI) -> None: |
| 166 | model = await async_client.models.list() | |
08b8179aDavid Schnurr2 years ago | 167 | assert_matches_type(AsyncPage[Model], model, path=["response"]) |
| 168 | | |
| 169 | @parametrize | |
98d779fbStainless Bot2 years ago | 170 | async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None: |
| 171 | response = await async_client.models.with_raw_response.list() | |
86379b44Stainless Bot2 years ago | 172 | |
| 173 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 174 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 175 | model = response.parse() | |
| 176 | assert_matches_type(AsyncPage[Model], model, path=["response"]) | |
| 177 | | |
86379b44Stainless Bot2 years ago | 178 | @parametrize |
98d779fbStainless Bot2 years ago | 179 | async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None: |
| 180 | async with async_client.models.with_streaming_response.list() as response: | |
86379b44Stainless Bot2 years ago | 181 | assert not response.is_closed |
| 182 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 183 | | |
| 184 | model = await response.parse() | |
| 185 | assert_matches_type(AsyncPage[Model], model, path=["response"]) | |
| 186 | | |
| 187 | assert cast(Any, response.is_closed) is True | |
| 188 | | |
08b8179aDavid Schnurr2 years ago | 189 | @parametrize |
98d779fbStainless Bot2 years ago | 190 | async def test_method_delete(self, async_client: AsyncOpenAI) -> None: |
| 191 | model = await async_client.models.delete( | |
08b8179aDavid Schnurr2 years ago | 192 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 193 | ) | |
| 194 | assert_matches_type(ModelDeleted, model, path=["response"]) | |
| 195 | | |
| 196 | @parametrize | |
98d779fbStainless Bot2 years ago | 197 | async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 198 | response = await async_client.models.with_raw_response.delete( | |
08b8179aDavid Schnurr2 years ago | 199 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 200 | ) | |
86379b44Stainless Bot2 years ago | 201 | |
| 202 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 203 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 204 | model = response.parse() | |
| 205 | assert_matches_type(ModelDeleted, model, path=["response"]) | |
86379b44Stainless Bot2 years ago | 206 | |
| 207 | @parametrize | |
98d779fbStainless Bot2 years ago | 208 | async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 209 | async with async_client.models.with_streaming_response.delete( | |
86379b44Stainless Bot2 years ago | 210 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 211 | ) as response: | |
| 212 | assert not response.is_closed | |
| 213 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" | |
| 214 | | |
| 215 | model = await response.parse() | |
| 216 | assert_matches_type(ModelDeleted, model, path=["response"]) | |
| 217 | | |
| 218 | assert cast(Any, response.is_closed) is True | |
023a4e66Stainless Bot2 years ago | 219 | |
| 220 | @parametrize | |
98d779fbStainless Bot2 years ago | 221 | async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None: |
023a4e66Stainless Bot2 years ago | 222 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"): |
98d779fbStainless Bot2 years ago | 223 | await async_client.models.with_raw_response.delete( |
023a4e66Stainless Bot2 years ago | 224 | "", |
| 225 | ) |