-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_ionq_client.py
More file actions
99 lines (75 loc) · 3.83 KB
/
Copy pathtest_ionq_client.py
File metadata and controls
99 lines (75 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import warnings
import httpx
import pytest
from ionq_core import IonQClient, __version__
from ionq_core._transport import ErrorRaisingTransport
class TestIonQClient:
def test_creates_client_with_explicit_key(self):
c = IonQClient(api_key="my-key")
assert c.token == "my-key"
assert c.prefix == "apiKey"
assert c.auth_header_name == "Authorization"
def test_creates_client_from_env_var(self, monkeypatch):
monkeypatch.setenv("IONQ_API_KEY", "env-key")
assert IonQClient().token == "env-key"
def test_explicit_key_takes_precedence_over_env(self, monkeypatch):
monkeypatch.setenv("IONQ_API_KEY", "env-key")
assert IonQClient(api_key="explicit-key").token == "explicit-key"
def test_raises_without_key(self, monkeypatch):
monkeypatch.delenv("IONQ_API_KEY", raising=False)
with pytest.raises(ValueError, match="api_key or IONQ_API_KEY"):
IonQClient()
def test_default_base_url(self):
assert IonQClient(api_key="key")._base_url == "https://api.ionq.co/v0.4"
def test_custom_base_url(self):
assert IonQClient(api_key="key", base_url="https://staging.example.com/v0.4")._base_url == (
"https://staging.example.com/v0.4"
)
def test_auth_header_set_correctly(self):
assert IonQClient(api_key="my-key").get_httpx_client().headers["Authorization"] == "apiKey my-key"
def test_context_manager(self):
with IonQClient(api_key="key") as c:
assert c.token == "key"
def test_user_agent_header(self):
ua = IonQClient(api_key="key").get_httpx_client().headers["User-Agent"]
assert ua.startswith("ionq-core/")
for token in ("python/", "httpx/", "os/"):
assert token in ua
def test_additional_user_agent(self):
ua = IonQClient(api_key="key", additional_user_agent="qiskit-ionq/1.0").get_httpx_client().headers["User-Agent"]
assert "qiskit-ionq/1.0" in ua
def test_default_timeout(self):
t = IonQClient(api_key="key").get_httpx_client().timeout
assert t.connect == 10.0
assert t.read == 60.0
def test_custom_timeout(self):
assert IonQClient(api_key="key", timeout=httpx.Timeout(120.0)).get_httpx_client().timeout.read == 120.0
def test_error_raising_transport_wired(self):
assert isinstance(IonQClient(api_key="key").get_httpx_client()._transport, ErrorRaisingTransport)
def test_async_client_wired(self):
ac = IonQClient(api_key="key").get_async_httpx_client()
assert isinstance(ac._transport, ErrorRaisingTransport)
assert "apiKey key" in ac.headers["Authorization"]
assert ac.headers["User-Agent"].startswith("ionq-core/")
def test_version_exposed(self):
assert isinstance(__version__, str)
assert __version__ != ""
def test_token_not_in_repr(self):
c = IonQClient(api_key="super-secret-key")
assert "super-secret-key" not in repr(c)
def test_http_base_url_warns(self):
with pytest.warns(UserWarning, match="does not use HTTPS"):
IonQClient(api_key="key", base_url="http://api.ionq.co/v0.4")
def test_https_base_url_no_warning(self):
with warnings.catch_warnings():
warnings.simplefilter("error")
IonQClient(api_key="key", base_url="https://api.ionq.co/v0.4")
def test_verify_ssl_false_warns(self):
with pytest.warns(UserWarning, match="verify_ssl=False"):
IonQClient(api_key="key", verify_ssl=False)
def test_async_client_inherits_follow_redirects(self):
ac = IonQClient(api_key="key", follow_redirects=True).get_async_httpx_client()
assert ac.follow_redirects is True
def test_async_client_default_no_follow_redirects(self):
ac = IonQClient(api_key="key").get_async_httpx_client()
assert ac.follow_redirects is False