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