Skip to content

Commit 8de4107

Browse files
authored
test: add global action fixtures (#544)
Move the actions fixtures to all tests, to be able to reuse them in the future.
1 parent a73e7d4 commit 8de4107

File tree

12 files changed

+339
-361
lines changed

12 files changed

+339
-361
lines changed

tests/unit/actions/conftest.py

Lines changed: 0 additions & 79 deletions
This file was deleted.

tests/unit/actions/test_client.py

Lines changed: 66 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,76 +6,86 @@
66

77
from hcloud import Client
88
from hcloud.actions import (
9-
Action,
109
ActionFailedException,
1110
ActionsClient,
1211
ActionTimeoutException,
1312
BoundAction,
1413
ResourceActionsClient,
1514
)
1615

16+
from ..conftest import assert_bound_action1, assert_bound_action2
17+
1718

1819
class TestBoundAction:
1920
@pytest.fixture()
20-
def bound_running_action(self, client: Client):
21-
return BoundAction(
22-
client=client.actions,
23-
data=dict(id=14, status=Action.STATUS_RUNNING),
24-
)
21+
def bound_running_action(self, client: Client, action1_running):
22+
return BoundAction(client=client.actions, data=action1_running)
2523

2624
def test_wait_until_finished(
2725
self,
2826
request_mock: mock.MagicMock,
2927
bound_running_action,
30-
running_action,
31-
successfully_action,
28+
action1_running,
29+
action1_success,
3230
):
3331
request_mock.side_effect = [
34-
running_action,
35-
successfully_action,
32+
{"action": action1_running},
33+
{"action": action1_success},
3634
]
3735

3836
bound_running_action.wait_until_finished()
3937

4038
request_mock.assert_called_with(
4139
method="GET",
42-
url="/actions/2",
40+
url="/actions/1",
4341
)
4442

4543
assert bound_running_action.status == "success"
44+
assert bound_running_action.id == 1
45+
4646
assert request_mock.call_count == 2
4747

4848
def test_wait_until_finished_with_error(
4949
self,
5050
request_mock: mock.MagicMock,
5151
bound_running_action,
52-
running_action,
53-
failed_action,
52+
action1_running,
53+
action1_error,
5454
):
55-
request_mock.side_effect = [running_action, failed_action]
56-
with pytest.raises(ActionFailedException) as exception_info:
55+
request_mock.side_effect = [
56+
{"action": action1_running},
57+
{"action": action1_error},
58+
]
59+
60+
with pytest.raises(ActionFailedException) as exc:
5761
bound_running_action.wait_until_finished()
5862

5963
assert bound_running_action.status == "error"
60-
assert exception_info.value.action.id == 2
64+
assert bound_running_action.id == 1
65+
assert exc.value.action.id == 1
66+
67+
assert request_mock.call_count == 2
6168

6269
def test_wait_until_finished_max_retries(
6370
self,
6471
request_mock: mock.MagicMock,
6572
bound_running_action,
66-
running_action,
67-
successfully_action,
73+
action1_running,
74+
action1_success,
6875
):
6976
request_mock.side_effect = [
70-
running_action,
71-
running_action,
72-
successfully_action,
77+
{"action": action1_running},
78+
{"action": action1_running},
79+
{"action": action1_success},
7380
]
74-
with pytest.raises(ActionTimeoutException) as exception_info:
81+
82+
with pytest.raises(ActionTimeoutException) as exc:
7583
bound_running_action.wait_until_finished(max_retries=1)
7684

7785
assert bound_running_action.status == "running"
78-
assert exception_info.value.action.id == 2
86+
assert bound_running_action.id == 1
87+
assert exc.value.action.id == 1
88+
7989
assert request_mock.call_count == 1
8090

8191

@@ -88,32 +98,34 @@ def test_get_by_id(
8898
self,
8999
request_mock: mock.MagicMock,
90100
actions_client: ActionsClient,
91-
generic_action,
101+
action_response,
92102
):
93-
request_mock.return_value = generic_action
103+
request_mock.return_value = action_response
94104

95105
action = actions_client.get_by_id(1)
96106

97107
request_mock.assert_called_with(
98108
method="GET",
99109
url="/resource/actions/1",
100110
)
101-
assert action._client == actions_client._parent.actions
102-
assert action.id == 1
103-
assert action.command == "stop_server"
111+
112+
assert_bound_action1(action, actions_client._parent.actions)
104113

105114
@pytest.mark.parametrize(
106115
"params",
107-
[{}, {"status": ["active"], "sort": ["status"], "page": 2, "per_page": 10}],
116+
[
117+
{},
118+
{"status": ["active"], "sort": ["status"], "page": 2, "per_page": 10},
119+
],
108120
)
109121
def test_get_list(
110122
self,
111123
request_mock: mock.MagicMock,
112124
actions_client: ActionsClient,
113-
generic_action_list,
125+
action_list_response,
114126
params,
115127
):
116-
request_mock.return_value = generic_action_list
128+
request_mock.return_value = action_list_response
117129

118130
result = actions_client.get_list(**params)
119131

@@ -127,50 +139,30 @@ def test_get_list(
127139

128140
actions = result.actions
129141
assert len(actions) == 2
130-
131-
action1 = actions[0]
132-
action2 = actions[1]
133-
134-
assert action1._client == actions_client._parent.actions
135-
assert action1.id == 1
136-
assert action1.command == "start_server"
137-
138-
assert action2._client == actions_client._parent.actions
139-
assert action2.id == 2
140-
assert action2.command == "stop_server"
142+
assert_bound_action1(actions[0], actions_client._parent.actions)
143+
assert_bound_action2(actions[1], actions_client._parent.actions)
141144

142145
@pytest.mark.parametrize("params", [{}, {"status": ["active"], "sort": ["status"]}])
143146
def test_get_all(
144147
self,
145148
request_mock: mock.MagicMock,
146149
actions_client: ActionsClient,
147-
generic_action_list,
150+
action_list_response,
148151
params,
149152
):
150-
request_mock.return_value = generic_action_list
153+
request_mock.return_value = action_list_response
151154

152155
actions = actions_client.get_all(**params)
153156

154-
params.update({"page": 1, "per_page": 50})
155-
156157
request_mock.assert_called_with(
157158
method="GET",
158159
url="/resource/actions",
159-
params=params,
160+
params={**params, "page": 1, "per_page": 50},
160161
)
161162

162163
assert len(actions) == 2
163-
164-
action1 = actions[0]
165-
action2 = actions[1]
166-
167-
assert action1._client == actions_client._parent.actions
168-
assert action1.id == 1
169-
assert action1.command == "start_server"
170-
171-
assert action2._client == actions_client._parent.actions
172-
assert action2.id == 2
173-
assert action2.command == "stop_server"
164+
assert_bound_action1(actions[0], actions_client._parent.actions)
165+
assert_bound_action2(actions[1], actions_client._parent.actions)
174166

175167

176168
class TestActionsClient:
@@ -182,32 +174,33 @@ def test_get_by_id(
182174
self,
183175
request_mock: mock.MagicMock,
184176
actions_client: ActionsClient,
185-
generic_action,
177+
action_response,
186178
):
187-
request_mock.return_value = generic_action
179+
request_mock.return_value = action_response
188180

189181
action = actions_client.get_by_id(1)
190182

191183
request_mock.assert_called_with(
192184
method="GET",
193185
url="/actions/1",
194186
)
195-
assert action._client == actions_client._parent.actions
196-
assert action.id == 1
197-
assert action.command == "stop_server"
187+
assert_bound_action1(action, actions_client._parent.actions)
198188

199189
@pytest.mark.parametrize(
200190
"params",
201-
[{}, {"status": ["active"], "sort": ["status"], "page": 2, "per_page": 10}],
191+
[
192+
{},
193+
{"status": ["active"], "sort": ["status"], "page": 2, "per_page": 10},
194+
],
202195
)
203196
def test_get_list(
204197
self,
205198
request_mock: mock.MagicMock,
206199
actions_client: ActionsClient,
207-
generic_action_list,
200+
action_list_response,
208201
params,
209202
):
210-
request_mock.return_value = generic_action_list
203+
request_mock.return_value = action_list_response
211204

212205
with pytest.deprecated_call():
213206
result = actions_client.get_list(**params)
@@ -222,48 +215,28 @@ def test_get_list(
222215

223216
actions = result.actions
224217
assert len(actions) == 2
225-
226-
action1 = actions[0]
227-
action2 = actions[1]
228-
229-
assert action1._client == actions_client._parent.actions
230-
assert action1.id == 1
231-
assert action1.command == "start_server"
232-
233-
assert action2._client == actions_client._parent.actions
234-
assert action2.id == 2
235-
assert action2.command == "stop_server"
218+
assert_bound_action1(actions[0], actions_client._parent.actions)
219+
assert_bound_action2(actions[1], actions_client._parent.actions)
236220

237221
@pytest.mark.parametrize("params", [{}, {"status": ["active"], "sort": ["status"]}])
238222
def test_get_all(
239223
self,
240224
request_mock: mock.MagicMock,
241225
actions_client: ActionsClient,
242-
generic_action_list,
226+
action_list_response,
243227
params,
244228
):
245-
request_mock.return_value = generic_action_list
229+
request_mock.return_value = action_list_response
246230

247231
with pytest.deprecated_call():
248232
actions = actions_client.get_all(**params)
249233

250-
params.update({"page": 1, "per_page": 50})
251-
252234
request_mock.assert_called_with(
253235
method="GET",
254236
url="/actions",
255-
params=params,
237+
params={**params, "page": 1, "per_page": 50},
256238
)
257239

258240
assert len(actions) == 2
259-
260-
action1 = actions[0]
261-
action2 = actions[1]
262-
263-
assert action1._client == actions_client._parent.actions
264-
assert action1.id == 1
265-
assert action1.command == "start_server"
266-
267-
assert action2._client == actions_client._parent.actions
268-
assert action2.id == 2
269-
assert action2.command == "stop_server"
241+
assert_bound_action1(actions[0], actions_client._parent.actions)
242+
assert_bound_action2(actions[1], actions_client._parent.actions)

0 commit comments

Comments
 (0)