Debian Codegraph @

tests/api_resources/test_models.py

raw
1 from __future__ import annotations
2
3 import os
4 from typing import Any, cast
5
6 import pytest
7
8 from anthropic import Anthropic, AsyncAnthropic
9 from tests.utils import assert_matches_type
10 from anthropic.types import ModelInfo
11 from anthropic.pagination import SyncPage, AsyncPage
12
13 base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
14
15
16 class TestModels:
17 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
18
19 @parametrize
20 def test_method_retrieve(self, client: Anthropic) -> None:
21 model = client.models.retrieve(
22 model_id="model_id",
23 )
24 assert_matches_type(ModelInfo, model, path=["response"])
25
26 @parametrize
27 def test_method_retrieve_with_all_params(self, client: Anthropic) -> None:
28 model = client.models.retrieve(
29 model_id="model_id",
30 betas=["message-batches-2024-09-24"],
31 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
32 )
33 assert_matches_type(ModelInfo, model, path=["response"])
34
35 @parametrize
36 def test_raw_response_retrieve(self, client: Anthropic) -> None:
37 response = client.models.with_raw_response.retrieve(
38 model_id="model_id",
39 )
40
41 assert response.is_closed is True
42 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
43 model = response.parse()
44 assert_matches_type(ModelInfo, model, path=["response"])
45
46 @parametrize
47 def test_streaming_response_retrieve(self, client: Anthropic) -> None:
48 with client.models.with_streaming_response.retrieve(
49 model_id="model_id",
50 ) as response:
51 assert not response.is_closed
52 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
53
54 model = response.parse()
55 assert_matches_type(ModelInfo, model, path=["response"])
56
57 assert cast(Any, response.is_closed) is True
58
59 @parametrize
60 def test_path_params_retrieve(self, client: Anthropic) -> None:
61 with pytest.raises(ValueError, match=r"Expected a non-empty value for `model_id` but received ''"):
62 client.models.with_raw_response.retrieve(
63 model_id="",
64 )
65
66 @parametrize
67 def test_method_list(self, client: Anthropic) -> None:
68 model = client.models.list()
69 assert_matches_type(SyncPage[ModelInfo], model, path=["response"])
70
71 @parametrize
72 def test_method_list_with_all_params(self, client: Anthropic) -> None:
73 model = client.models.list(
74 after_id="after_id",
75 before_id="before_id",
76 limit=1,
77 betas=["message-batches-2024-09-24"],
78 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
79 )
80 assert_matches_type(SyncPage[ModelInfo], model, path=["response"])
81
82 @parametrize
83 def test_raw_response_list(self, client: Anthropic) -> None:
84 response = client.models.with_raw_response.list()
85
86 assert response.is_closed is True
87 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
88 model = response.parse()
89 assert_matches_type(SyncPage[ModelInfo], model, path=["response"])
90
91 @parametrize
92 def test_streaming_response_list(self, client: Anthropic) -> None:
93 with client.models.with_streaming_response.list() as response:
94 assert not response.is_closed
95 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
96
97 model = response.parse()
98 assert_matches_type(SyncPage[ModelInfo], model, path=["response"])
99
100 assert cast(Any, response.is_closed) is True
101
102
103 class TestAsyncModels:
104 parametrize = pytest.mark.parametrize(
105 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
106 )
107
108 @parametrize
109 async def test_method_retrieve(self, async_client: AsyncAnthropic) -> None:
110 model = await async_client.models.retrieve(
111 model_id="model_id",
112 )
113 assert_matches_type(ModelInfo, model, path=["response"])
114
115 @parametrize
116 async def test_method_retrieve_with_all_params(self, async_client: AsyncAnthropic) -> None:
117 model = await async_client.models.retrieve(
118 model_id="model_id",
119 betas=["message-batches-2024-09-24"],
120 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
121 )
122 assert_matches_type(ModelInfo, model, path=["response"])
123
124 @parametrize
125 async def test_raw_response_retrieve(self, async_client: AsyncAnthropic) -> None:
126 response = await async_client.models.with_raw_response.retrieve(
127 model_id="model_id",
128 )
129
130 assert response.is_closed is True
131 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
132 model = await response.parse()
133 assert_matches_type(ModelInfo, model, path=["response"])
134
135 @parametrize
136 async def test_streaming_response_retrieve(self, async_client: AsyncAnthropic) -> None:
137 async with async_client.models.with_streaming_response.retrieve(
138 model_id="model_id",
139 ) as response:
140 assert not response.is_closed
141 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
142
143 model = await response.parse()
144 assert_matches_type(ModelInfo, model, path=["response"])
145
146 assert cast(Any, response.is_closed) is True
147
148 @parametrize
149 async def test_path_params_retrieve(self, async_client: AsyncAnthropic) -> None:
150 with pytest.raises(ValueError, match=r"Expected a non-empty value for `model_id` but received ''"):
151 await async_client.models.with_raw_response.retrieve(
152 model_id="",
153 )
154
155 @parametrize
156 async def test_method_list(self, async_client: AsyncAnthropic) -> None:
157 model = await async_client.models.list()
158 assert_matches_type(AsyncPage[ModelInfo], model, path=["response"])
159
160 @parametrize
161 async def test_method_list_with_all_params(self, async_client: AsyncAnthropic) -> None:
162 model = await async_client.models.list(
163 after_id="after_id",
164 before_id="before_id",
165 limit=1,
166 betas=["message-batches-2024-09-24"],
167 workspace_id="wrkspc_011CZkZaBF1tNoB5wlCeusgy",
168 )
169 assert_matches_type(AsyncPage[ModelInfo], model, path=["response"])
170
171 @parametrize
172 async def test_raw_response_list(self, async_client: AsyncAnthropic) -> 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 = await response.parse()
178 assert_matches_type(AsyncPage[ModelInfo], model, path=["response"])
179
180 @parametrize
181 async def test_streaming_response_list(self, async_client: AsyncAnthropic) -> 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[ModelInfo], model, path=["response"])
188
189 assert cast(Any, response.is_closed) is True