Skip to content

Commit 9096813

Browse files
authored
test: use request_mock for all clients tests (#537)
Similar to #536 but focused on the resources clients. Pass request_mock directly to the test functions to configure the mocked HTTP requests. We do not want to update/modify protected properties (the mock object) within the client class. Allows us to move the location of the client request function (that is mocked) in the future, without breaking the tests. This also starts adding typing information to the tests, which should open the door for linting our test code as well. A lot of the diff is from search and replaces and breaking the test arguments onto multiple lines. This is the second patch of a larger set. Based on top of #536
1 parent d38772b commit 9096813

File tree

18 files changed

+1682
-827
lines changed

18 files changed

+1682
-827
lines changed

tests/unit/actions/test_client.py

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ def test_wait_until_finished_max_retries(
7777

7878
class TestResourceActionsClient:
7979
@pytest.fixture()
80-
def actions_client(self):
81-
return ResourceActionsClient(client=mock.MagicMock(), resource="/resource")
80+
def actions_client(self, client: Client):
81+
return ResourceActionsClient(client, resource="/resource")
8282

83-
def test_get_by_id(self, actions_client, generic_action):
84-
actions_client._client.request.return_value = generic_action
83+
def test_get_by_id(
84+
self,
85+
request_mock: mock.MagicMock,
86+
actions_client: ActionsClient,
87+
generic_action,
88+
):
89+
request_mock.return_value = generic_action
8590
action = actions_client.get_by_id(1)
86-
actions_client._client.request.assert_called_with(
87-
url="/resource/actions/1", method="GET"
88-
)
91+
request_mock.assert_called_with(url="/resource/actions/1", method="GET")
8992
assert action._client == actions_client._client.actions
9093
assert action.id == 1
9194
assert action.command == "stop_server"
@@ -94,10 +97,16 @@ def test_get_by_id(self, actions_client, generic_action):
9497
"params",
9598
[{}, {"status": ["active"], "sort": ["status"], "page": 2, "per_page": 10}],
9699
)
97-
def test_get_list(self, actions_client, generic_action_list, params):
98-
actions_client._client.request.return_value = generic_action_list
100+
def test_get_list(
101+
self,
102+
request_mock: mock.MagicMock,
103+
actions_client: ActionsClient,
104+
generic_action_list,
105+
params,
106+
):
107+
request_mock.return_value = generic_action_list
99108
result = actions_client.get_list(**params)
100-
actions_client._client.request.assert_called_with(
109+
request_mock.assert_called_with(
101110
url="/resource/actions", method="GET", params=params
102111
)
103112

@@ -118,13 +127,19 @@ def test_get_list(self, actions_client, generic_action_list, params):
118127
assert action2.command == "stop_server"
119128

120129
@pytest.mark.parametrize("params", [{}, {"status": ["active"], "sort": ["status"]}])
121-
def test_get_all(self, actions_client, generic_action_list, params):
122-
actions_client._client.request.return_value = generic_action_list
130+
def test_get_all(
131+
self,
132+
request_mock: mock.MagicMock,
133+
actions_client: ActionsClient,
134+
generic_action_list,
135+
params,
136+
):
137+
request_mock.return_value = generic_action_list
123138
actions = actions_client.get_all(**params)
124139

125140
params.update({"page": 1, "per_page": 50})
126141

127-
actions_client._client.request.assert_called_with(
142+
request_mock.assert_called_with(
128143
url="/resource/actions", method="GET", params=params
129144
)
130145

@@ -144,15 +159,18 @@ def test_get_all(self, actions_client, generic_action_list, params):
144159

145160
class TestActionsClient:
146161
@pytest.fixture()
147-
def actions_client(self):
148-
return ActionsClient(client=mock.MagicMock())
162+
def actions_client(self, client: Client):
163+
return ActionsClient(client)
149164

150-
def test_get_by_id(self, actions_client, generic_action):
151-
actions_client._client.request.return_value = generic_action
165+
def test_get_by_id(
166+
self,
167+
request_mock: mock.MagicMock,
168+
actions_client: ActionsClient,
169+
generic_action,
170+
):
171+
request_mock.return_value = generic_action
152172
action = actions_client.get_by_id(1)
153-
actions_client._client.request.assert_called_with(
154-
url="/actions/1", method="GET"
155-
)
173+
request_mock.assert_called_with(url="/actions/1", method="GET")
156174
assert action._client == actions_client._client.actions
157175
assert action.id == 1
158176
assert action.command == "stop_server"
@@ -161,13 +179,17 @@ def test_get_by_id(self, actions_client, generic_action):
161179
"params",
162180
[{}, {"status": ["active"], "sort": ["status"], "page": 2, "per_page": 10}],
163181
)
164-
def test_get_list(self, actions_client, generic_action_list, params):
165-
actions_client._client.request.return_value = generic_action_list
182+
def test_get_list(
183+
self,
184+
request_mock: mock.MagicMock,
185+
actions_client: ActionsClient,
186+
generic_action_list,
187+
params,
188+
):
189+
request_mock.return_value = generic_action_list
166190
with pytest.deprecated_call():
167191
result = actions_client.get_list(**params)
168-
actions_client._client.request.assert_called_with(
169-
url="/actions", method="GET", params=params
170-
)
192+
request_mock.assert_called_with(url="/actions", method="GET", params=params)
171193

172194
assert result.meta is not None
173195

@@ -186,16 +208,20 @@ def test_get_list(self, actions_client, generic_action_list, params):
186208
assert action2.command == "stop_server"
187209

188210
@pytest.mark.parametrize("params", [{}, {"status": ["active"], "sort": ["status"]}])
189-
def test_get_all(self, actions_client, generic_action_list, params):
190-
actions_client._client.request.return_value = generic_action_list
211+
def test_get_all(
212+
self,
213+
request_mock: mock.MagicMock,
214+
actions_client: ActionsClient,
215+
generic_action_list,
216+
params,
217+
):
218+
request_mock.return_value = generic_action_list
191219
with pytest.deprecated_call():
192220
actions = actions_client.get_all(**params)
193221

194222
params.update({"page": 1, "per_page": 50})
195223

196-
actions_client._client.request.assert_called_with(
197-
url="/actions", method="GET", params=params
198-
)
224+
request_mock.assert_called_with(url="/actions", method="GET", params=params)
199225

200226
assert len(actions) == 2
201227

0 commit comments

Comments
 (0)