-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathtest_client.py
More file actions
181 lines (145 loc) · 6.25 KB
/
test_client.py
File metadata and controls
181 lines (145 loc) · 6.25 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from __future__ import annotations
from unittest import mock # noqa: F401
import pytest # noqa: F401
from hcloud import Client
from hcloud.datacenters import BoundDatacenter, DatacentersClient, DatacenterServerTypes
from hcloud.locations import BoundLocation
class TestBoundDatacenter:
def test_bound_datacenter_init(self, datacenter_response):
bound_datacenter = BoundDatacenter(
client=mock.MagicMock(), data=datacenter_response["datacenter"]
)
assert bound_datacenter.id == 1
assert bound_datacenter.name == "fsn1-dc8"
assert bound_datacenter.description == "Falkenstein 1 DC 8"
assert bound_datacenter.complete is True
assert isinstance(bound_datacenter.location, BoundLocation)
assert bound_datacenter.location.id == 1
assert bound_datacenter.location.name == "fsn1"
assert bound_datacenter.location.complete is True
with pytest.deprecated_call():
assert isinstance(bound_datacenter.server_types, DatacenterServerTypes)
assert len(bound_datacenter.server_types.supported) == 3
assert bound_datacenter.server_types.supported[0].id == 1
assert bound_datacenter.server_types.supported[0].complete is False
assert bound_datacenter.server_types.supported[1].id == 2
assert bound_datacenter.server_types.supported[1].complete is False
assert bound_datacenter.server_types.supported[2].id == 3
assert bound_datacenter.server_types.supported[2].complete is False
assert len(bound_datacenter.server_types.available) == 3
assert bound_datacenter.server_types.available[0].id == 1
assert bound_datacenter.server_types.available[0].complete is False
assert bound_datacenter.server_types.available[1].id == 2
assert bound_datacenter.server_types.available[1].complete is False
assert bound_datacenter.server_types.available[2].id == 3
assert bound_datacenter.server_types.available[2].complete is False
assert len(bound_datacenter.server_types.available_for_migration) == 3
assert bound_datacenter.server_types.available_for_migration[0].id == 1
assert (
bound_datacenter.server_types.available_for_migration[0].complete
is False
)
assert bound_datacenter.server_types.available_for_migration[1].id == 2
assert (
bound_datacenter.server_types.available_for_migration[1].complete
is False
)
assert bound_datacenter.server_types.available_for_migration[2].id == 3
assert (
bound_datacenter.server_types.available_for_migration[2].complete
is False
)
class TestDatacentersClient:
@pytest.fixture()
def datacenters_client(self, client: Client):
return DatacentersClient(client)
def test_get_by_id(
self,
request_mock: mock.MagicMock,
datacenters_client: DatacentersClient,
datacenter_response,
):
request_mock.return_value = datacenter_response
datacenter = datacenters_client.get_by_id(1)
request_mock.assert_called_with(
method="GET",
url="/datacenters/1",
)
assert datacenter._client is datacenters_client
assert datacenter.id == 1
assert datacenter.name == "fsn1-dc8"
@pytest.mark.parametrize(
"params", [{"name": "fsn1", "page": 1, "per_page": 10}, {"name": ""}, {}]
)
def test_get_list(
self,
request_mock: mock.MagicMock,
datacenters_client: DatacentersClient,
two_datacenters_response,
params,
):
request_mock.return_value = two_datacenters_response
result = datacenters_client.get_list(**params)
request_mock.assert_called_with(
method="GET",
url="/datacenters",
params=params,
)
datacenters = result.datacenters
assert result.meta is not None
assert len(datacenters) == 2
datacenter1 = datacenters[0]
datacenter2 = datacenters[1]
assert datacenter1._client is datacenters_client
assert datacenter1.id == 1
assert datacenter1.name == "fsn1-dc8"
assert isinstance(datacenter1.location, BoundLocation)
assert datacenter2._client is datacenters_client
assert datacenter2.id == 2
assert datacenter2.name == "nbg1-dc3"
assert isinstance(datacenter2.location, BoundLocation)
@pytest.mark.parametrize("params", [{"name": "fsn1"}, {}])
def test_get_all(
self,
request_mock: mock.MagicMock,
datacenters_client: DatacentersClient,
two_datacenters_response,
params,
):
request_mock.return_value = two_datacenters_response
datacenters = datacenters_client.get_all(**params)
params.update({"page": 1, "per_page": 50})
request_mock.assert_called_with(
method="GET",
url="/datacenters",
params=params,
)
assert len(datacenters) == 2
datacenter1 = datacenters[0]
datacenter2 = datacenters[1]
assert datacenter1._client is datacenters_client
assert datacenter1.id == 1
assert datacenter1.name == "fsn1-dc8"
assert isinstance(datacenter1.location, BoundLocation)
assert datacenter2._client is datacenters_client
assert datacenter2.id == 2
assert datacenter2.name == "nbg1-dc3"
assert isinstance(datacenter2.location, BoundLocation)
def test_get_by_name(
self,
request_mock: mock.MagicMock,
datacenters_client: DatacentersClient,
one_datacenters_response,
):
request_mock.return_value = one_datacenters_response
datacenter = datacenters_client.get_by_name("fsn1-dc8")
params = {"name": "fsn1-dc8"}
request_mock.assert_called_with(
method="GET",
url="/datacenters",
params=params,
)
assert datacenter._client is datacenters_client
assert datacenter.id == 1
assert datacenter.name == "fsn1-dc8"
assert isinstance(datacenter.location, BoundLocation)