Skip to content

Commit 41c4fbc

Browse files
committed
test: generate actions tests for bound models
1 parent 8f41e94 commit 41c4fbc

File tree

1 file changed

+107
-26
lines changed

1 file changed

+107
-26
lines changed

tests/unit/actions/test_client.py

Lines changed: 107 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,33 @@
1313
BoundAction,
1414
ResourceActionsClient,
1515
)
16-
from hcloud.certificates import CertificatesClient
17-
from hcloud.core import ResourceClientBase
18-
from hcloud.firewalls import FirewallsClient
19-
from hcloud.floating_ips import FloatingIPsClient
20-
from hcloud.images import ImagesClient
21-
from hcloud.load_balancers import LoadBalancersClient
22-
from hcloud.networks import NetworksClient
23-
from hcloud.primary_ips import PrimaryIPsClient
24-
from hcloud.servers import ServersClient
25-
from hcloud.volumes import VolumesClient
16+
from hcloud.certificates import BoundCertificate, CertificatesClient
17+
from hcloud.core import BoundModelBase, ResourceClientBase
18+
from hcloud.firewalls import BoundFirewall, FirewallsClient
19+
from hcloud.floating_ips import BoundFloatingIP, FloatingIPsClient
20+
from hcloud.images import BoundImage, ImagesClient
21+
from hcloud.load_balancers import BoundLoadBalancer, LoadBalancersClient
22+
from hcloud.networks import BoundNetwork, NetworksClient
23+
from hcloud.primary_ips import BoundPrimaryIP, PrimaryIPsClient
24+
from hcloud.servers import BoundServer, ServersClient
25+
from hcloud.volumes import BoundVolume, VolumesClient
2626

2727
from ..conftest import assert_bound_action1, assert_bound_action2
2828

29-
resource_clients_with_actions = {
30-
"certificates": CertificatesClient,
31-
"firewalls": FirewallsClient,
32-
"floating_ips": FloatingIPsClient,
33-
"images": ImagesClient,
34-
"load_balancers": LoadBalancersClient,
35-
"networks": NetworksClient,
36-
"primary_ips": PrimaryIPsClient,
37-
"servers": ServersClient,
38-
"volumes": VolumesClient,
29+
resources_with_actions: dict[str, tuple[ResourceClientBase, BoundModelBase]] = {
30+
"certificates": (CertificatesClient, BoundCertificate),
31+
"firewalls": (FirewallsClient, BoundFirewall),
32+
"floating_ips": (FloatingIPsClient, BoundFloatingIP),
33+
"images": (ImagesClient, BoundImage),
34+
"load_balancers": (LoadBalancersClient, BoundLoadBalancer),
35+
"networks": (NetworksClient, BoundNetwork),
36+
"primary_ips": (PrimaryIPsClient, BoundPrimaryIP),
37+
"servers": (ServersClient, BoundServer),
38+
"volumes": (VolumesClient, BoundVolume),
3939
}
4040

4141

42-
def test_resource_clients_with_actions(client: Client):
42+
def test_resources_with_actions(client: Client):
4343
"""
4444
Ensure that the list of resource clients above is up to date.
4545
"""
@@ -48,10 +48,12 @@ def test_resource_clients_with_actions(client: Client):
4848
predicate=lambda p: isinstance(p, ResourceClientBase) and hasattr(p, "actions"),
4949
)
5050
for name, member in members:
51-
assert name in resource_clients_with_actions
52-
assert member.__class__ is resource_clients_with_actions[name]
51+
assert name in resources_with_actions
5352

54-
assert len(members) == len(resource_clients_with_actions)
53+
resource_client_class, _ = resources_with_actions[name]
54+
assert member.__class__ is resource_client_class
55+
56+
assert len(members) == len(resources_with_actions)
5557

5658

5759
class TestBoundAction:
@@ -133,7 +135,7 @@ class TestResourceActionsClient:
133135
/<resource>/actions/<id>
134136
"""
135137

136-
@pytest.fixture(params=resource_clients_with_actions.keys())
138+
@pytest.fixture(params=resources_with_actions.keys())
137139
def resource(self, request) -> str:
138140
return request.param
139141

@@ -229,7 +231,7 @@ class TestResourceObjectActionsClient:
229231
/<resource>/<id>/actions
230232
"""
231233

232-
@pytest.fixture(params=resource_clients_with_actions.keys())
234+
@pytest.fixture(params=resources_with_actions.keys())
233235
def resource(self, request):
234236
if request.param == "primary_ips":
235237
pytest.skip("not implemented yet")
@@ -301,6 +303,85 @@ def test_get_actions(
301303
assert_bound_action2(actions[1], resource_client._parent.actions)
302304

303305

306+
class TestBoundModelActions:
307+
"""
308+
/<resource>/<id>/actions
309+
"""
310+
311+
@pytest.fixture(params=resources_with_actions.keys())
312+
def resource(self, request):
313+
if request.param == "primary_ips":
314+
pytest.skip("not implemented yet")
315+
return request.param
316+
317+
@pytest.fixture()
318+
def bound_model(self, client: Client, resource: str) -> ResourceClientBase:
319+
_, bound_model_class = resources_with_actions[resource]
320+
resource_client = getattr(client, resource)
321+
return bound_model_class(resource_client, data={"id": 1})
322+
323+
@pytest.mark.parametrize(
324+
"params",
325+
[
326+
{},
327+
{"status": ["running"], "sort": ["status"], "page": 2, "per_page": 10},
328+
],
329+
)
330+
def test_get_actions_list(
331+
self,
332+
request_mock: mock.MagicMock,
333+
bound_model: BoundModelBase,
334+
resource: str,
335+
action_list_response,
336+
params,
337+
):
338+
request_mock.return_value = action_list_response
339+
340+
result = bound_model.get_actions_list(**params)
341+
342+
request_mock.assert_called_with(
343+
method="GET",
344+
url=f"/{resource}/1/actions",
345+
params=params,
346+
)
347+
348+
assert result.meta is not None
349+
350+
actions = result.actions
351+
assert len(actions) == 2
352+
assert_bound_action1(actions[0], bound_model._client._parent.actions)
353+
assert_bound_action2(actions[1], bound_model._client._parent.actions)
354+
355+
@pytest.mark.parametrize(
356+
"params",
357+
[
358+
{},
359+
{"status": ["running"], "sort": ["status"]},
360+
],
361+
)
362+
def test_get_actions(
363+
self,
364+
request_mock: mock.MagicMock,
365+
bound_model: BoundModelBase,
366+
resource: str,
367+
action_list_response,
368+
params,
369+
):
370+
request_mock.return_value = action_list_response
371+
372+
actions = bound_model.get_actions(**params)
373+
374+
request_mock.assert_called_with(
375+
method="GET",
376+
url=f"/{resource}/1/actions",
377+
params={**params, "page": 1, "per_page": 50},
378+
)
379+
380+
assert len(actions) == 2
381+
assert_bound_action1(actions[0], bound_model._client._parent.actions)
382+
assert_bound_action2(actions[1], bound_model._client._parent.actions)
383+
384+
304385
class TestActionsClient:
305386
@pytest.fixture()
306387
def actions_client(self, client: Client) -> ActionsClient:

0 commit comments

Comments
 (0)