openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
next

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/containers/files/test_content.py

154lines · modeblame

71058dd6stainless-app[bot]1 years ago1# 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
cca09707stainless-app[bot]1 years ago8import httpx
71058dd6stainless-app[bot]1 years ago9import pytest
cca09707stainless-app[bot]1 years ago10from respx import MockRouter
71058dd6stainless-app[bot]1 years ago11
cca09707stainless-app[bot]1 years ago12import openai._legacy_response as _legacy_response
71058dd6stainless-app[bot]1 years ago13from openai import OpenAI, AsyncOpenAI
cca09707stainless-app[bot]1 years ago14from tests.utils import assert_matches_type
15
16# pyright: reportDeprecated=false
71058dd6stainless-app[bot]1 years ago17
18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
19
20
21class TestContent:
22parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
23
24@parametrize
cca09707stainless-app[bot]1 years ago25@pytest.mark.respx(base_url=base_url)
26def test_method_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
27respx_mock.get("/containers/container_id/files/file_id/content").mock(
28return_value=httpx.Response(200, json={"foo": "bar"})
29)
71058dd6stainless-app[bot]1 years ago30content = client.containers.files.content.retrieve(
31file_id="file_id",
32container_id="container_id",
33)
cca09707stainless-app[bot]1 years ago34assert isinstance(content, _legacy_response.HttpxBinaryResponseContent)
35assert content.json() == {"foo": "bar"}
71058dd6stainless-app[bot]1 years ago36
37@parametrize
cca09707stainless-app[bot]1 years ago38@pytest.mark.respx(base_url=base_url)
39def test_raw_response_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
40respx_mock.get("/containers/container_id/files/file_id/content").mock(
41return_value=httpx.Response(200, json={"foo": "bar"})
42)
43
71058dd6stainless-app[bot]1 years ago44response = client.containers.files.content.with_raw_response.retrieve(
45file_id="file_id",
46container_id="container_id",
47)
48
49assert response.is_closed is True
50assert response.http_request.headers.get("X-Stainless-Lang") == "python"
51content = response.parse()
cca09707stainless-app[bot]1 years ago52assert_matches_type(_legacy_response.HttpxBinaryResponseContent, content, path=["response"])
71058dd6stainless-app[bot]1 years ago53
54@parametrize
cca09707stainless-app[bot]1 years ago55@pytest.mark.respx(base_url=base_url)
56def test_streaming_response_retrieve(self, client: OpenAI, respx_mock: MockRouter) -> None:
57respx_mock.get("/containers/container_id/files/file_id/content").mock(
58return_value=httpx.Response(200, json={"foo": "bar"})
59)
71058dd6stainless-app[bot]1 years ago60with client.containers.files.content.with_streaming_response.retrieve(
61file_id="file_id",
62container_id="container_id",
63) as response:
64assert not response.is_closed
65assert response.http_request.headers.get("X-Stainless-Lang") == "python"
66
67content = response.parse()
cca09707stainless-app[bot]1 years ago68assert_matches_type(bytes, content, path=["response"])
71058dd6stainless-app[bot]1 years ago69
70assert cast(Any, response.is_closed) is True
71
72@parametrize
cca09707stainless-app[bot]1 years ago73@pytest.mark.respx(base_url=base_url)
71058dd6stainless-app[bot]1 years ago74def test_path_params_retrieve(self, client: OpenAI) -> None:
75with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"):
76client.containers.files.content.with_raw_response.retrieve(
77file_id="file_id",
78container_id="",
79)
80
81with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
82client.containers.files.content.with_raw_response.retrieve(
83file_id="",
84container_id="container_id",
85)
86
87
88class TestAsyncContent:
c62e9907stainless-app[bot]1 years ago89parametrize = pytest.mark.parametrize(
90"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
91)
71058dd6stainless-app[bot]1 years ago92
93@parametrize
cca09707stainless-app[bot]1 years ago94@pytest.mark.respx(base_url=base_url)
95async def test_method_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
96respx_mock.get("/containers/container_id/files/file_id/content").mock(
97return_value=httpx.Response(200, json={"foo": "bar"})
98)
71058dd6stainless-app[bot]1 years ago99content = await async_client.containers.files.content.retrieve(
100file_id="file_id",
101container_id="container_id",
102)
cca09707stainless-app[bot]1 years ago103assert isinstance(content, _legacy_response.HttpxBinaryResponseContent)
104assert content.json() == {"foo": "bar"}
71058dd6stainless-app[bot]1 years ago105
106@parametrize
cca09707stainless-app[bot]1 years ago107@pytest.mark.respx(base_url=base_url)
108async def test_raw_response_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
109respx_mock.get("/containers/container_id/files/file_id/content").mock(
110return_value=httpx.Response(200, json={"foo": "bar"})
111)
112
71058dd6stainless-app[bot]1 years ago113response = await async_client.containers.files.content.with_raw_response.retrieve(
114file_id="file_id",
115container_id="container_id",
116)
117
118assert response.is_closed is True
119assert response.http_request.headers.get("X-Stainless-Lang") == "python"
120content = response.parse()
cca09707stainless-app[bot]1 years ago121assert_matches_type(_legacy_response.HttpxBinaryResponseContent, content, path=["response"])
71058dd6stainless-app[bot]1 years ago122
123@parametrize
cca09707stainless-app[bot]1 years ago124@pytest.mark.respx(base_url=base_url)
125async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
126respx_mock.get("/containers/container_id/files/file_id/content").mock(
127return_value=httpx.Response(200, json={"foo": "bar"})
128)
71058dd6stainless-app[bot]1 years ago129async with async_client.containers.files.content.with_streaming_response.retrieve(
130file_id="file_id",
131container_id="container_id",
132) as response:
133assert not response.is_closed
134assert response.http_request.headers.get("X-Stainless-Lang") == "python"
135
136content = await response.parse()
cca09707stainless-app[bot]1 years ago137assert_matches_type(bytes, content, path=["response"])
71058dd6stainless-app[bot]1 years ago138
139assert cast(Any, response.is_closed) is True
140
141@parametrize
cca09707stainless-app[bot]1 years ago142@pytest.mark.respx(base_url=base_url)
71058dd6stainless-app[bot]1 years ago143async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
144with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"):
145await async_client.containers.files.content.with_raw_response.retrieve(
146file_id="file_id",
147container_id="",
148)
149
150with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
151await async_client.containers.files.content.with_raw_response.retrieve(
152file_id="",
153container_id="container_id",
154)