openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
70719e92f877f159df5930467ae49256d2b48ebf

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/containers/files/test_content.py

152lines · modecode

1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import httpx
9import pytest
10from respx import MockRouter
11
12import openai._legacy_response as _legacy_response
13from openai import OpenAI, AsyncOpenAI
14from tests.utils import assert_matches_type
15
16# pyright: reportDeprecated=false
17
18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
19
20
21class TestContent:
22 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
23
24 @parametrize
25 @pytest.mark.respx(base_url=base_url)
26 def test_method_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
27 respx_mock.get("/containers/container_id/files/file_id/content").mock(
28 return_value=httpx.Response(200, json={"foo": "bar"})
29 )
30 content = client.containers.files.content.retrieve(
31 file_id="file_id",
32 container_id="container_id",
33 )
34 assert isinstance(content, _legacy_response.HttpxBinaryResponseContent)
35 assert content.json() == {"foo": "bar"}
36
37 @parametrize
38 @pytest.mark.respx(base_url=base_url)
39 def test_raw_response_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
40 respx_mock.get("/containers/container_id/files/file_id/content").mock(
41 return_value=httpx.Response(200, json={"foo": "bar"})
42 )
43
44 response = client.containers.files.content.with_raw_response.retrieve(
45 file_id="file_id",
46 container_id="container_id",
47 )
48
49 assert response.is_closed is True
50 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
51 content = response.parse()
52 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, content, path=["response"])
53
54 @parametrize
55 @pytest.mark.respx(base_url=base_url)
56 def test_streaming_response_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
57 respx_mock.get("/containers/container_id/files/file_id/content").mock(
58 return_value=httpx.Response(200, json={"foo": "bar"})
59 )
60 with client.containers.files.content.with_streaming_response.retrieve(
61 file_id="file_id",
62 container_id="container_id",
63 ) as response:
64 assert not response.is_closed
65 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
66
67 content = response.parse()
68 assert_matches_type(bytes, content, path=["response"])
69
70 assert cast(Any, response.is_closed) is True
71
72 @parametrize
73 @pytest.mark.respx(base_url=base_url)
74 def test_path_params_retrieve(self, client: OpenAI) -> None:
75 with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"):
76 client.containers.files.content.with_raw_response.retrieve(
77 file_id="file_id",
78 container_id="",
79 )
80
81 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
82 client.containers.files.content.with_raw_response.retrieve(
83 file_id="",
84 container_id="container_id",
85 )
86
87
88class TestAsyncContent:
89 parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
90
91 @parametrize
92 @pytest.mark.respx(base_url=base_url)
93 async def test_method_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
94 respx_mock.get("/containers/container_id/files/file_id/content").mock(
95 return_value=httpx.Response(200, json={"foo": "bar"})
96 )
97 content = await async_client.containers.files.content.retrieve(
98 file_id="file_id",
99 container_id="container_id",
100 )
101 assert isinstance(content, _legacy_response.HttpxBinaryResponseContent)
102 assert content.json() == {"foo": "bar"}
103
104 @parametrize
105 @pytest.mark.respx(base_url=base_url)
106 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
107 respx_mock.get("/containers/container_id/files/file_id/content").mock(
108 return_value=httpx.Response(200, json={"foo": "bar"})
109 )
110
111 response = await async_client.containers.files.content.with_raw_response.retrieve(
112 file_id="file_id",
113 container_id="container_id",
114 )
115
116 assert response.is_closed is True
117 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
118 content = response.parse()
119 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, content, path=["response"])
120
121 @parametrize
122 @pytest.mark.respx(base_url=base_url)
123 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
124 respx_mock.get("/containers/container_id/files/file_id/content").mock(
125 return_value=httpx.Response(200, json={"foo": "bar"})
126 )
127 async with async_client.containers.files.content.with_streaming_response.retrieve(
128 file_id="file_id",
129 container_id="container_id",
130 ) as response:
131 assert not response.is_closed
132 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
133
134 content = await response.parse()
135 assert_matches_type(bytes, content, path=["response"])
136
137 assert cast(Any, response.is_closed) is True
138
139 @parametrize
140 @pytest.mark.respx(base_url=base_url)
141 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
142 with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"):
143 await async_client.containers.files.content.with_raw_response.retrieve(
144 file_id="file_id",
145 container_id="",
146 )
147
148 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
149 await async_client.containers.files.content.with_raw_response.retrieve(
150 file_id="",
151 container_id="container_id",
152 )