openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ea4fa238abdcddbf14aa7d42b7689cdd00a2d290

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/test_module_client.py

182lines · modeblame

08b8179aDavid Schnurr2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os as _os
6
7import httpx
8import pytest
9from httpx import URL
10
11import openai
12from openai import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES
13
14
15def reset_state() -> None:
16openai._reset_client()
17openai.api_key = None or "My API Key"
18openai.organization = None
19openai.base_url = None
20openai.timeout = DEFAULT_TIMEOUT
21openai.max_retries = DEFAULT_MAX_RETRIES
22openai.default_headers = None
23openai.default_query = None
24openai.http_client = None
25openai.api_type = _os.environ.get("OPENAI_API_TYPE") # type: ignore
26openai.api_version = None
27openai.azure_endpoint = None
28openai.azure_ad_token = None
29openai.azure_ad_token_provider = None
30
31
32@pytest.fixture(autouse=True)
33def reset_state_fixture() -> None:
34reset_state()
35
36
37def test_base_url_option() -> None:
38assert openai.base_url is None
39assert openai.completions._client.base_url == URL("https://api.openai.com/v1/")
40
41openai.base_url = "http://foo.com"
42
43assert openai.base_url == URL("http://foo.com")
44assert openai.completions._client.base_url == URL("http://foo.com")
45
46
47def test_timeout_option() -> None:
48assert openai.timeout == openai.DEFAULT_TIMEOUT
49assert openai.completions._client.timeout == openai.DEFAULT_TIMEOUT
50
51openai.timeout = 3
52
53assert openai.timeout == 3
54assert openai.completions._client.timeout == 3
55
56
57def test_max_retries_option() -> None:
58assert openai.max_retries == openai.DEFAULT_MAX_RETRIES
59assert openai.completions._client.max_retries == openai.DEFAULT_MAX_RETRIES
60
61openai.max_retries = 1
62
63assert openai.max_retries == 1
64assert openai.completions._client.max_retries == 1
65
66
67def test_default_headers_option() -> None:
68assert openai.default_headers == None
69
70openai.default_headers = {"Foo": "Bar"}
71
72assert openai.default_headers["Foo"] == "Bar"
73assert openai.completions._client.default_headers["Foo"] == "Bar"
74
75
76def test_default_query_option() -> None:
77assert openai.default_query is None
78assert openai.completions._client._custom_query == {}
79
80openai.default_query = {"Foo": {"nested": 1}}
81
82assert openai.default_query["Foo"] == {"nested": 1}
83assert openai.completions._client._custom_query["Foo"] == {"nested": 1}
84
85
86def test_http_client_option() -> None:
87assert openai.http_client is None
88
89original_http_client = openai.completions._client._client
90assert original_http_client is not None
91
92new_client = httpx.Client()
93openai.http_client = new_client
94
95assert openai.completions._client._client is new_client
96
97
98import contextlib
99from typing import Iterator
100
101from openai.lib.azure import AzureOpenAI
102
103
104@contextlib.contextmanager
105def fresh_env() -> Iterator[None]:
106old = _os.environ.copy()
107
108try:
109_os.environ.clear()
110yield
111finally:
112_os.environ.update(old)
113
114
115def test_only_api_key_results_in_openai_api() -> None:
116with fresh_env():
117openai.api_type = None
118openai.api_key = "example API key"
119
120assert type(openai.completions._client).__name__ == "_ModuleClient"
121
122
123def test_azure_api_key_env_without_api_version() -> None:
124with fresh_env():
125openai.api_type = None
126_os.environ["AZURE_OPENAI_API_KEY"] = "example API key"
127
c5975bd0Stainless Bot2 years ago128with pytest.raises(
129ValueError,
130match=r"Must provide either the `api_version` argument or the `OPENAI_API_VERSION` environment variable",
131):
08b8179aDavid Schnurr2 years ago132openai.completions._client
133
134
135def test_azure_api_key_and_version_env() -> None:
136with fresh_env():
137openai.api_type = None
138_os.environ["AZURE_OPENAI_API_KEY"] = "example API key"
139_os.environ["OPENAI_API_VERSION"] = "example-version"
140
141with pytest.raises(
142ValueError,
c5975bd0Stainless Bot2 years ago143match=r"Must provide one of the `base_url` or `azure_endpoint` arguments, or the `AZURE_OPENAI_ENDPOINT` environment variable",
08b8179aDavid Schnurr2 years ago144):
145openai.completions._client
146
147
148def test_azure_api_key_version_and_endpoint_env() -> None:
149with fresh_env():
150openai.api_type = None
151_os.environ["AZURE_OPENAI_API_KEY"] = "example API key"
152_os.environ["OPENAI_API_VERSION"] = "example-version"
153_os.environ["AZURE_OPENAI_ENDPOINT"] = "https://www.example"
154
155openai.completions._client
156
157assert openai.api_type == "azure"
158
159
160def test_azure_azure_ad_token_version_and_endpoint_env() -> None:
161with fresh_env():
162openai.api_type = None
163_os.environ["AZURE_OPENAI_AD_TOKEN"] = "example AD token"
164_os.environ["OPENAI_API_VERSION"] = "example-version"
165_os.environ["AZURE_OPENAI_ENDPOINT"] = "https://www.example"
166
167client = openai.completions._client
168assert isinstance(client, AzureOpenAI)
169assert client._azure_ad_token == "example AD token"
170
171
172def test_azure_azure_ad_token_provider_version_and_endpoint_env() -> None:
173with fresh_env():
174openai.api_type = None
175_os.environ["OPENAI_API_VERSION"] = "example-version"
176_os.environ["AZURE_OPENAI_ENDPOINT"] = "https://www.example"
177openai.azure_ad_token_provider = lambda: "token"
178
179client = openai.completions._client
180assert isinstance(client, AzureOpenAI)
181assert client._azure_ad_token_provider is not None
182assert client._azure_ad_token_provider() == "token"