openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_models.py
225lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | from typing import Any, cast |
| 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: |
| 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-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 | ) |
| 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-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 | |
| 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-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 | ) |
| 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-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 | |
| 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 | |
| 123 | class TestAsyncModels: |
| 124 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 125 | |
| 126 | @parametrize |
| 127 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 128 | model = await async_client.models.retrieve( |
| 129 | "gpt-3.5-turbo", |
| 130 | ) |
| 131 | assert_matches_type(Model, model, path=["response"]) |
| 132 | |
| 133 | @parametrize |
| 134 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 135 | response = await async_client.models.with_raw_response.retrieve( |
| 136 | "gpt-3.5-turbo", |
| 137 | ) |
| 138 | |
| 139 | assert response.is_closed is True |
| 140 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 141 | model = response.parse() |
| 142 | assert_matches_type(Model, model, path=["response"]) |
| 143 | |
| 144 | @parametrize |
| 145 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 146 | async with async_client.models.with_streaming_response.retrieve( |
| 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 | |
| 157 | @parametrize |
| 158 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 159 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"): |
| 160 | await async_client.models.with_raw_response.retrieve( |
| 161 | "", |
| 162 | ) |
| 163 | |
| 164 | @parametrize |
| 165 | async def test_method_list(self, async_client: AsyncOpenAI) -> None: |
| 166 | model = await async_client.models.list() |
| 167 | assert_matches_type(AsyncPage[Model], model, path=["response"]) |
| 168 | |
| 169 | @parametrize |
| 170 | async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None: |
| 171 | response = await async_client.models.with_raw_response.list() |
| 172 | |
| 173 | assert response.is_closed is True |
| 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 | |
| 178 | @parametrize |
| 179 | async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None: |
| 180 | async with async_client.models.with_streaming_response.list() as response: |
| 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 | |
| 189 | @parametrize |
| 190 | async def test_method_delete(self, async_client: AsyncOpenAI) -> None: |
| 191 | model = await async_client.models.delete( |
| 192 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 193 | ) |
| 194 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 195 | |
| 196 | @parametrize |
| 197 | async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 198 | response = await async_client.models.with_raw_response.delete( |
| 199 | "ft:gpt-3.5-turbo:acemeco:suffix:abc123", |
| 200 | ) |
| 201 | |
| 202 | assert response.is_closed is True |
| 203 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 204 | model = response.parse() |
| 205 | assert_matches_type(ModelDeleted, model, path=["response"]) |
| 206 | |
| 207 | @parametrize |
| 208 | async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 209 | async with async_client.models.with_streaming_response.delete( |
| 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 |
| 219 | |
| 220 | @parametrize |
| 221 | async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None: |
| 222 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `model` but received ''"): |
| 223 | await async_client.models.with_raw_response.delete( |
| 224 | "", |
| 225 | ) |
| 226 | |