openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/beta/threads/test_messages.py
234lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from openai import OpenAI, AsyncOpenAI |
| 10 | from tests.utils import assert_matches_type |
| 11 | from openai._client import OpenAI, AsyncOpenAI |
| 12 | from openai.pagination import SyncCursorPage, AsyncCursorPage |
| 13 | from openai.types.beta.threads import ThreadMessage |
| 14 | |
| 15 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 16 | api_key = "My API Key" |
| 17 | |
| 18 | |
| 19 | class TestMessages: |
| 20 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 21 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 22 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 23 | |
| 24 | @parametrize |
| 25 | def test_method_create(self, client: OpenAI) -> None: |
| 26 | message = client.beta.threads.messages.create( |
| 27 | "string", |
| 28 | content="x", |
| 29 | role="user", |
| 30 | ) |
| 31 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 32 | |
| 33 | @parametrize |
| 34 | def test_method_create_with_all_params(self, client: OpenAI) -> None: |
| 35 | message = client.beta.threads.messages.create( |
| 36 | "string", |
| 37 | content="x", |
| 38 | role="user", |
| 39 | file_ids=["string"], |
| 40 | metadata={}, |
| 41 | ) |
| 42 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 43 | |
| 44 | @parametrize |
| 45 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 46 | response = client.beta.threads.messages.with_raw_response.create( |
| 47 | "string", |
| 48 | content="x", |
| 49 | role="user", |
| 50 | ) |
| 51 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 52 | message = response.parse() |
| 53 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 54 | |
| 55 | @parametrize |
| 56 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 57 | message = client.beta.threads.messages.retrieve( |
| 58 | "string", |
| 59 | thread_id="string", |
| 60 | ) |
| 61 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 62 | |
| 63 | @parametrize |
| 64 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 65 | response = client.beta.threads.messages.with_raw_response.retrieve( |
| 66 | "string", |
| 67 | thread_id="string", |
| 68 | ) |
| 69 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 70 | message = response.parse() |
| 71 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 72 | |
| 73 | @parametrize |
| 74 | def test_method_update(self, client: OpenAI) -> None: |
| 75 | message = client.beta.threads.messages.update( |
| 76 | "string", |
| 77 | thread_id="string", |
| 78 | ) |
| 79 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 80 | |
| 81 | @parametrize |
| 82 | def test_method_update_with_all_params(self, client: OpenAI) -> None: |
| 83 | message = client.beta.threads.messages.update( |
| 84 | "string", |
| 85 | thread_id="string", |
| 86 | metadata={}, |
| 87 | ) |
| 88 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 89 | |
| 90 | @parametrize |
| 91 | def test_raw_response_update(self, client: OpenAI) -> None: |
| 92 | response = client.beta.threads.messages.with_raw_response.update( |
| 93 | "string", |
| 94 | thread_id="string", |
| 95 | ) |
| 96 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 97 | message = response.parse() |
| 98 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 99 | |
| 100 | @parametrize |
| 101 | def test_method_list(self, client: OpenAI) -> None: |
| 102 | message = client.beta.threads.messages.list( |
| 103 | "string", |
| 104 | ) |
| 105 | assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"]) |
| 106 | |
| 107 | @parametrize |
| 108 | def test_method_list_with_all_params(self, client: OpenAI) -> None: |
| 109 | message = client.beta.threads.messages.list( |
| 110 | "string", |
| 111 | after="string", |
| 112 | before="string", |
| 113 | limit=0, |
| 114 | order="asc", |
| 115 | ) |
| 116 | assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"]) |
| 117 | |
| 118 | @parametrize |
| 119 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 120 | response = client.beta.threads.messages.with_raw_response.list( |
| 121 | "string", |
| 122 | ) |
| 123 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 124 | message = response.parse() |
| 125 | assert_matches_type(SyncCursorPage[ThreadMessage], message, path=["response"]) |
| 126 | |
| 127 | |
| 128 | class TestAsyncMessages: |
| 129 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 130 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 131 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 132 | |
| 133 | @parametrize |
| 134 | async def test_method_create(self, client: AsyncOpenAI) -> None: |
| 135 | message = await client.beta.threads.messages.create( |
| 136 | "string", |
| 137 | content="x", |
| 138 | role="user", |
| 139 | ) |
| 140 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 141 | |
| 142 | @parametrize |
| 143 | async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None: |
| 144 | message = await client.beta.threads.messages.create( |
| 145 | "string", |
| 146 | content="x", |
| 147 | role="user", |
| 148 | file_ids=["string"], |
| 149 | metadata={}, |
| 150 | ) |
| 151 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 152 | |
| 153 | @parametrize |
| 154 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: |
| 155 | response = await client.beta.threads.messages.with_raw_response.create( |
| 156 | "string", |
| 157 | content="x", |
| 158 | role="user", |
| 159 | ) |
| 160 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 161 | message = response.parse() |
| 162 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 163 | |
| 164 | @parametrize |
| 165 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 166 | message = await client.beta.threads.messages.retrieve( |
| 167 | "string", |
| 168 | thread_id="string", |
| 169 | ) |
| 170 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 171 | |
| 172 | @parametrize |
| 173 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 174 | response = await client.beta.threads.messages.with_raw_response.retrieve( |
| 175 | "string", |
| 176 | thread_id="string", |
| 177 | ) |
| 178 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 179 | message = response.parse() |
| 180 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 181 | |
| 182 | @parametrize |
| 183 | async def test_method_update(self, client: AsyncOpenAI) -> None: |
| 184 | message = await client.beta.threads.messages.update( |
| 185 | "string", |
| 186 | thread_id="string", |
| 187 | ) |
| 188 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 189 | |
| 190 | @parametrize |
| 191 | async def test_method_update_with_all_params(self, client: AsyncOpenAI) -> None: |
| 192 | message = await client.beta.threads.messages.update( |
| 193 | "string", |
| 194 | thread_id="string", |
| 195 | metadata={}, |
| 196 | ) |
| 197 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 198 | |
| 199 | @parametrize |
| 200 | async def test_raw_response_update(self, client: AsyncOpenAI) -> None: |
| 201 | response = await client.beta.threads.messages.with_raw_response.update( |
| 202 | "string", |
| 203 | thread_id="string", |
| 204 | ) |
| 205 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 206 | message = response.parse() |
| 207 | assert_matches_type(ThreadMessage, message, path=["response"]) |
| 208 | |
| 209 | @parametrize |
| 210 | async def test_method_list(self, client: AsyncOpenAI) -> None: |
| 211 | message = await client.beta.threads.messages.list( |
| 212 | "string", |
| 213 | ) |
| 214 | assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"]) |
| 215 | |
| 216 | @parametrize |
| 217 | async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None: |
| 218 | message = await client.beta.threads.messages.list( |
| 219 | "string", |
| 220 | after="string", |
| 221 | before="string", |
| 222 | limit=0, |
| 223 | order="asc", |
| 224 | ) |
| 225 | assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"]) |
| 226 | |
| 227 | @parametrize |
| 228 | async def test_raw_response_list(self, client: AsyncOpenAI) -> None: |
| 229 | response = await client.beta.threads.messages.with_raw_response.list( |
| 230 | "string", |
| 231 | ) |
| 232 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 233 | message = response.parse() |
| 234 | assert_matches_type(AsyncCursorPage[ThreadMessage], message, path=["response"]) |
| 235 | |