This repository was archived by the owner on Feb 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_client.py
93 lines (64 loc) · 2.72 KB
/
test_client.py
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
from __future__ import annotations
from unittest import mock
import pytest
from unkey import Client
from unkey import services
async def test_all_services_exist() -> None:
client = Client("api_key_123")
assert isinstance(client.keys, services.KeyService)
assert isinstance(client.apis, services.ApiService)
await client.close()
@mock.patch("unkey.client.serializer.Serializer")
@mock.patch("unkey.client.services.HttpService")
async def test_basic_init(http: mock.MagicMock, serializer: mock.MagicMock) -> None:
_ = Client("abc123")
http.assert_called_once_with("abc123", None, None)
serializer.assert_called_once()
@mock.patch("unkey.client.serializer.Serializer")
@mock.patch("unkey.client.services.HttpService")
async def test_full_init(http: mock.MagicMock, serializer: mock.MagicMock) -> None:
_ = Client("abc", api_version=69, api_base_url="fake")
http.assert_called_once_with("abc", 69, "fake")
serializer.assert_called_once()
def test_empty_api_key_fails() -> None:
with pytest.raises(ValueError) as e:
Client("")
assert e.exconly() == "ValueError: Api key must not be empty."
def test_none_api_key() -> None:
client = Client(None)
assert "Authorization" not in client._http._headers # pyright: ignore
@mock.patch("unkey.client.services.HttpService.set_api_key")
async def test_set_api_key(set_api_key: mock.MagicMock) -> None:
client = Client("fake")
client.set_api_key("hello")
set_api_key.assert_called_once_with("hello")
@mock.patch("unkey.client.services.HttpService.set_base_url")
async def test_set_api_base_url(set_base_url: mock.MagicMock) -> None:
client = Client("abc")
client.set_api_base_url("https://localhost:6969")
set_base_url.assert_called_once_with("https://localhost:6969")
@mock.patch("unkey.client.services.HttpService.start")
async def test_start(start: mock.MagicMock) -> None:
client = Client("abc123")
await client.start()
start.assert_called_once()
@mock.patch("unkey.client.services.HttpService.close")
async def test_close(close: mock.MagicMock) -> None:
client = Client("abc123")
await client.close()
close.assert_called_once()
@mock.patch("unkey.client.Client._Client__init_service")
async def test_init_services(init_service: mock.MagicMock) -> None:
_ = Client("abc")
init_service.assert_has_calls(
(
mock.call(services.ApiService),
mock.call(services.KeyService),
)
)
async def test_init_service_fails() -> None:
client = Client("abc")
with pytest.raises(TypeError) as e:
client._Client__init_service(int) # type: ignore
assert e.exconly() == "TypeError: 'int' can not be initialized as a service."
await client.close()