Skip to content

Commit 889383f

Browse files
committed
test: refactor sync and async tests to use Pydantic models and improve response validation
- Updated network tests to utilize Network and NetworkList models, enhancing response validation. - Refactored organization tests to use Organization model for better structure and clarity. - Modified package tests to implement Package and PackageList models, ensuring accurate response checks. - Revised project tests to leverage Project and ProjectList models for improved data handling. - Enhanced secret tests to utilize Secret and SecretList models, streamlining response validation. - Updated static route tests to incorporate StaticRoute and StaticRouteList models for better consistency. - Refactored user tests to use appropriate Pydantic models, improving clarity and response validation.
1 parent 17edb02 commit 889383f

26 files changed

+1885
-1386
lines changed
Lines changed: 55 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import httpx
22
import pytest
3-
import pytest_asyncio # noqa: F401
4-
from munch import Munch
3+
import pytest_asyncio
54
from asyncmock import AsyncMock
65

7-
from tests.data.mock_data import configtree_body # noqa: F401
8-
from tests.utils.fixtures import async_client as client # noqa: F401
6+
# ruff: noqa: F811, F401
7+
from tests.data.mock_data import configtree_body
8+
from tests.utils.fixtures import async_client
99

1010

1111
@pytest.mark.asyncio
12-
async def test_list_configtrees_success(client, mocker: AsyncMock): # noqa: F811
12+
async def test_list_configtrees_success(async_client, mocker: AsyncMock):
1313
# Mock the httpx.AsyncClient.get method
1414
mock_get = mocker.patch("httpx.AsyncClient.get")
1515

@@ -23,17 +23,16 @@ async def test_list_configtrees_success(client, mocker: AsyncMock): # noqa: F81
2323
)
2424

2525
# Call the list_configtrees method
26-
response = await client.list_configtrees()
26+
response = await async_client.list_configtrees()
2727

2828
# Validate the response
29-
assert isinstance(response, Munch)
3029
assert response["items"] == [
3130
{"name": "test-configtree", "guid": "mock_configtree_guid"}
3231
]
3332

3433

3534
@pytest.mark.asyncio
36-
async def test_list_configtrees_bad_gateway(client, mocker: AsyncMock): # noqa: F811
35+
async def test_list_configtrees_bad_gateway(async_client, mocker: AsyncMock):
3736
# Mock the httpx.AsyncClient.get method
3837
mock_get = mocker.patch("httpx.AsyncClient.get")
3938

@@ -45,13 +44,13 @@ async def test_list_configtrees_bad_gateway(client, mocker: AsyncMock): # noqa:
4544

4645
# Call the list_configtrees method
4746
with pytest.raises(Exception) as exc:
48-
await client.list_configtrees()
47+
await async_client.list_configtrees()
4948

5049
assert str(exc.value) == "bad gateway"
5150

5251

5352
@pytest.mark.asyncio
54-
async def test_create_configtree_success(client, mocker: AsyncMock): # noqa: F811
53+
async def test_create_configtree_success(async_client, mocker: AsyncMock):
5554
# Mock the httpx.AsyncClient.post method
5655
mock_post = mocker.patch("httpx.AsyncClient.post")
5756

@@ -64,15 +63,14 @@ async def test_create_configtree_success(client, mocker: AsyncMock): # noqa: F8
6463
)
6564

6665
# Call the create_configtree method
67-
response = await client.create_configtree(configtree_body)
66+
response = await async_client.create_configtree(configtree_body)
6867

6968
# Validate the response
70-
assert isinstance(response, Munch)
7169
assert response["metadata"]["guid"] == "test_configtree_guid"
7270

7371

7472
@pytest.mark.asyncio
75-
async def test_create_configtree_service_unavailable(client, mocker: AsyncMock): # noqa: F811
73+
async def test_create_configtree_service_unavailable(async_client, mocker: AsyncMock):
7674
# Mock the httpx.AsyncClient.post method
7775
mock_post = mocker.patch("httpx.AsyncClient.post")
7876

@@ -84,13 +82,13 @@ async def test_create_configtree_service_unavailable(client, mocker: AsyncMock):
8482

8583
# Call the create_configtree method
8684
with pytest.raises(Exception) as exc:
87-
await client.create_configtree(configtree_body)
85+
await async_client.create_configtree(configtree_body)
8886

8987
assert str(exc.value) == "service unavailable"
9088

9189

9290
@pytest.mark.asyncio
93-
async def test_get_configtree_success(client, mocker: AsyncMock): # noqa: F811
91+
async def test_get_configtree_success(async_client, mocker: AsyncMock):
9492
# Mock the httpx.AsyncClient.get method
9593
mock_get = mocker.patch("httpx.AsyncClient.get")
9694

@@ -103,16 +101,15 @@ async def test_get_configtree_success(client, mocker: AsyncMock): # noqa: F811
103101
)
104102

105103
# Call the get_configtree method
106-
response = await client.get_configtree(name="mock_configtree_name")
104+
response = await async_client.get_configtree(name="mock_configtree_name")
107105

108106
# Validate the response
109-
assert isinstance(response, Munch)
110-
assert response.metadata.guid == "test_configtree_guid"
111-
assert response.metadata.name == "test_configtree"
107+
assert response["metadata"]["guid"] == "test_configtree_guid"
108+
assert response["metadata"]["name"] == "test_configtree"
112109

113110

114111
@pytest.mark.asyncio
115-
async def test_set_configtree_revision_success(client, mocker: AsyncMock): # noqa: F811
112+
async def test_set_configtree_revision_success(async_client, mocker: AsyncMock):
116113
# Mock the httpx.AsyncClient.put method
117114
mock_put = mocker.patch("httpx.AsyncClient.put")
118115

@@ -125,42 +122,34 @@ async def test_set_configtree_revision_success(client, mocker: AsyncMock): # no
125122
)
126123

127124
# Call the set_configtree_revision method
128-
response = await client.set_configtree_revision(
125+
response = await async_client.set_configtree_revision(
129126
name="mock_configtree_name", configtree=configtree_body
130127
)
131128

132129
# Validate the response
133-
assert isinstance(response, Munch)
134-
assert response.metadata.guid == "test_configtree_guid"
135-
assert response.metadata.name == "test_configtree"
130+
assert response["metadata"]["guid"] == "test_configtree_guid"
131+
assert response["metadata"]["name"] == "test_configtree"
136132

137133

138134
@pytest.mark.asyncio
139-
async def test_update_configtree_success(client, mocker: AsyncMock): # noqa: F811
135+
async def test_update_configtree_success(async_client, mocker: AsyncMock):
140136
# Mock the httpx.AsyncClient.put method
141137
mock_put = mocker.patch("httpx.AsyncClient.put")
142-
143-
# Set up the mock response
144138
mock_put.return_value = httpx.Response(
145139
status_code=200,
146140
json={
147141
"metadata": {"guid": "test_configtree_guid", "name": "test_configtree"},
148142
},
149143
)
150-
151-
# Call the update_configtree method
152-
response = await client.update_configtree(
144+
response = await async_client.update_configtree(
153145
name="mock_configtree_name", body=configtree_body
154146
)
155-
156-
# Validate the response
157-
assert isinstance(response, Munch)
158-
assert response.metadata.guid == "test_configtree_guid"
159-
assert response.metadata.name == "test_configtree"
147+
assert response["metadata"]["guid"] == "test_configtree_guid"
148+
assert response["metadata"]["name"] == "test_configtree"
160149

161150

162151
@pytest.mark.asyncio
163-
async def test_delete_configtree_success(client, mocker: AsyncMock): # noqa: F811
152+
async def test_delete_configtree_success(async_client, mocker: AsyncMock):
164153
# Mock the httpx.AsyncClient.delete method
165154
mock_delete = mocker.patch("httpx.AsyncClient.delete")
166155

@@ -171,14 +160,14 @@ async def test_delete_configtree_success(client, mocker: AsyncMock): # noqa: F8
171160
)
172161

173162
# Call the delete_configtree method
174-
response = await client.delete_configtree(name="mock_configtree_name")
163+
response = await async_client.delete_configtree(name="mock_configtree_name")
175164

176165
# Validate the response
177-
assert response["success"] is True
166+
assert response is None
178167

179168

180169
@pytest.mark.asyncio
181-
async def test_list_revisions_success(client, mocker: AsyncMock): # noqa: F811
170+
async def test_list_revisions_success(async_client, mocker: AsyncMock):
182171
# Mock the httpx.AsyncClient.get method
183172
mock_get = mocker.patch("httpx.AsyncClient.get")
184173

@@ -192,17 +181,16 @@ async def test_list_revisions_success(client, mocker: AsyncMock): # noqa: F811
192181
)
193182

194183
# Call the list_revisions method
195-
response = await client.list_revisions(tree_name="mock_configtree_name")
184+
response = await async_client.list_revisions(tree_name="mock_configtree_name")
196185

197186
# Validate the response
198-
assert isinstance(response, Munch)
199187
assert response["items"] == [
200188
{"name": "test-configtree", "guid": "mock_configtree_guid"}
201189
]
202190

203191

204192
@pytest.mark.asyncio
205-
async def test_create_revision_success(client, mocker: AsyncMock): # noqa: F811
193+
async def test_create_revision_success(async_client, mocker: AsyncMock):
206194
# Mock the httpx.AsyncClient.post method
207195
mock_post = mocker.patch("httpx.AsyncClient.post")
208196

@@ -215,17 +203,16 @@ async def test_create_revision_success(client, mocker: AsyncMock): # noqa: F811
215203
)
216204

217205
# Call the create_revision method
218-
response = await client.create_revision(
206+
response = await async_client.create_revision(
219207
name="mock_configtree_name", body=configtree_body
220208
)
221209

222210
# Validate the response
223-
assert isinstance(response, Munch)
224211
assert response["metadata"]["guid"] == "test_revision_guid"
225212

226213

227214
@pytest.mark.asyncio
228-
async def test_put_keys_in_revision_success(client, mocker: AsyncMock): # noqa: F811
215+
async def test_put_keys_in_revision_success(async_client, mocker: AsyncMock):
229216
# Mock the httpx.AsyncClient.put method
230217
mock_put = mocker.patch("httpx.AsyncClient.put")
231218

@@ -238,20 +225,19 @@ async def test_put_keys_in_revision_success(client, mocker: AsyncMock): # noqa:
238225
)
239226

240227
# Call the put_keys_in_revision method
241-
response = await client.put_keys_in_revision(
228+
response = await async_client.put_keys_in_revision(
242229
name="mock_configtree_name",
243230
revision_id="mock_revision_id",
244231
config_values=["mock_value1", "mock_value2"],
245232
)
246233

247234
# Validate the response
248-
assert isinstance(response, Munch)
249-
assert response.metadata.guid == "test_revision_guid"
250-
assert response.metadata.name == "test_revision"
235+
assert response["metadata"]["guid"] == "test_revision_guid"
236+
assert response["metadata"]["name"] == "test_revision"
251237

252238

253239
@pytest.mark.asyncio
254-
async def test_commit_revision_success(client, mocker: AsyncMock): # noqa: F811
240+
async def test_commit_revision_success(async_client, mocker: AsyncMock):
255241
# Mock the httpx.AsyncClient.put method
256242
mock_patch = mocker.patch("httpx.AsyncClient.patch")
257243

@@ -264,19 +250,18 @@ async def test_commit_revision_success(client, mocker: AsyncMock): # noqa: F811
264250
)
265251

266252
# Call the commit_revision method
267-
response = await client.commit_revision(
253+
response = await async_client.commit_revision(
268254
tree_name="mock_configtree_name",
269255
revision_id="mock_revision_id",
270256
)
271257

272258
# Validate the response
273-
assert isinstance(response, Munch)
274-
assert response.metadata.guid == "test_revision_guid"
275-
assert response.metadata.name == "test_revision"
259+
assert response["metadata"]["guid"] == "test_revision_guid"
260+
assert response["metadata"]["name"] == "test_revision"
276261

277262

278263
@pytest.mark.asyncio
279-
async def test_get_key_in_revision(client, mocker: AsyncMock): # noqa: F811
264+
async def test_get_key_in_revision(async_client, mocker: AsyncMock):
280265
# Mock the httpx.AsyncClient.get method
281266
mock_get = mocker.patch("httpx.AsyncClient.get")
282267

@@ -289,18 +274,17 @@ async def test_get_key_in_revision(client, mocker: AsyncMock): # noqa: F811
289274
)
290275

291276
# Call the get_key_in_revision method
292-
response = await client.get_key_in_revision(
277+
response = await async_client.get_key_in_revision(
293278
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
294279
)
295280

296281
# Validate the response
297-
assert isinstance(response, Munch)
298-
assert response.metadata.guid == "test_revision_guid"
299-
assert response.metadata.name == "test_revision"
282+
assert response["metadata"]["guid"] == "test_revision_guid"
283+
assert response["metadata"]["name"] == "test_revision"
300284

301285

302286
@pytest.mark.asyncio
303-
async def test_put_key_in_revision_success(client, mocker: AsyncMock): # noqa: F811
287+
async def test_put_key_in_revision_success(async_client, mocker: AsyncMock):
304288
# Mock the httpx.AsyncClient.put method
305289
mock_put = mocker.patch("httpx.AsyncClient.put")
306290

@@ -313,34 +297,33 @@ async def test_put_key_in_revision_success(client, mocker: AsyncMock): # noqa:
313297
)
314298

315299
# Call the put_key_in_revision method
316-
response = await client.put_key_in_revision(
300+
response = await async_client.put_key_in_revision(
317301
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
318302
)
319303

320304
# Validate the response
321-
assert isinstance(response, Munch)
322-
assert response.metadata.guid == "test_revision_guid"
323-
assert response.metadata.name == "test_revision"
305+
assert response["metadata"]["guid"] == "test_revision_guid"
306+
assert response["metadata"]["name"] == "test_revision"
324307

325308

326309
@pytest.mark.asyncio
327-
async def test_delete_key_in_revision_success(client, mocker: AsyncMock): # noqa: F811
310+
async def test_delete_key_in_revision_success(async_client, mocker: AsyncMock):
328311
mock_delete = mocker.patch("httpx.AsyncClient.delete")
329312

330313
mock_delete.return_value = httpx.Response(
331314
status_code=204,
332315
json={"success": True},
333316
)
334317

335-
response = await client.delete_key_in_revision(
318+
response = await async_client.delete_key_in_revision(
336319
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
337320
)
338321

339-
assert response["success"] is True
322+
assert response is None
340323

341324

342325
@pytest.mark.asyncio
343-
async def test_rename_key_in_revision_success(client, mocker: AsyncMock): # noqa: F811
326+
async def test_rename_key_in_revision_success(async_client, mocker: AsyncMock):
344327
mock_patch = mocker.patch("httpx.AsyncClient.patch")
345328

346329
mock_patch.return_value = httpx.Response(
@@ -350,13 +333,13 @@ async def test_rename_key_in_revision_success(client, mocker: AsyncMock): # noq
350333
},
351334
)
352335

353-
response = await client.rename_key_in_revision(
336+
response = await async_client.rename_key_in_revision(
354337
tree_name="mock_configtree_name",
355338
revision_id="mock_revision_id",
356339
key="mock_key",
357340
config_key_rename={"metadata": {"name": "test_key"}},
358341
)
359342

360-
assert isinstance(response, Munch)
361-
assert response.metadata.guid == "test_revision_guid"
362-
assert response.metadata.name == "test_revision"
343+
assert isinstance(response, dict)
344+
assert response["metadata"]["guid"] == "test_revision_guid"
345+
assert response["metadata"]["name"] == "test_revision"

0 commit comments

Comments
 (0)