Skip to content

Commit 0e01cf1

Browse files
committed
test: refactor sync tests to use model classes instead of Munch
- Updated test cases in `test_network.py` to assert against `Network` and `NetworkList` models. - Refactored `test_organization.py` to utilize `Organization` model for assertions. - Changed assertions in `test_package.py` to validate against `Package` and `PackageList` models. - Modified `test_project.py` to use `Project` and `ProjectList` models for response validation. - Updated `test_secret.py` to assert against `Secret` and `SecretList` models. - Refactored `test_staticroute.py` to validate responses using `StaticRoute` and `StaticRouteList` models. - Changed `test_user.py` to assert against user model attributes instead of Munch. - Improved mock data usage across tests for consistency and clarity.
1 parent f7efef9 commit 0e01cf1

26 files changed

+1887
-1393
lines changed

tests/async_tests/test_configtree_async.py

Lines changed: 52 additions & 68 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,15 +250,14 @@ 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
@@ -284,7 +269,7 @@ async def test_get_key_in_revision_str(client, mocker: AsyncMock): # noqa: F811
284269
mock_get.return_value = httpx.Response(status_code=200, text="test_value")
285270

286271
# Call the get_key_in_revision method
287-
response = await client.get_key_in_revision(
272+
response = await async_client.get_key_in_revision(
288273
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
289274
)
290275

@@ -330,7 +315,7 @@ async def test_get_key_in_revision_bool(client, mocker: AsyncMock): # noqa: F81
330315

331316

332317
@pytest.mark.asyncio
333-
async def test_put_key_in_revision_success(client, mocker: AsyncMock): # noqa: F811
318+
async def test_put_key_in_revision_success(async_client, mocker: AsyncMock):
334319
# Mock the httpx.AsyncClient.put method
335320
mock_put = mocker.patch("httpx.AsyncClient.put")
336321

@@ -343,34 +328,33 @@ async def test_put_key_in_revision_success(client, mocker: AsyncMock): # noqa:
343328
)
344329

345330
# Call the put_key_in_revision method
346-
response = await client.put_key_in_revision(
331+
response = await async_client.put_key_in_revision(
347332
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
348333
)
349334

350335
# Validate the response
351-
assert isinstance(response, Munch)
352-
assert response.metadata.guid == "test_revision_guid"
353-
assert response.metadata.name == "test_revision"
336+
assert response["metadata"]["guid"] == "test_revision_guid"
337+
assert response["metadata"]["name"] == "test_revision"
354338

355339

356340
@pytest.mark.asyncio
357-
async def test_delete_key_in_revision_success(client, mocker: AsyncMock): # noqa: F811
341+
async def test_delete_key_in_revision_success(async_client, mocker: AsyncMock):
358342
mock_delete = mocker.patch("httpx.AsyncClient.delete")
359343

360344
mock_delete.return_value = httpx.Response(
361345
status_code=204,
362346
json={"success": True},
363347
)
364348

365-
response = await client.delete_key_in_revision(
349+
response = await async_client.delete_key_in_revision(
366350
tree_name="mock_configtree_name", revision_id="mock_revision_id", key="mock_key"
367351
)
368352

369-
assert response["success"] is True
353+
assert response is None
370354

371355

372356
@pytest.mark.asyncio
373-
async def test_rename_key_in_revision_success(client, mocker: AsyncMock): # noqa: F811
357+
async def test_rename_key_in_revision_success(async_client, mocker: AsyncMock):
374358
mock_patch = mocker.patch("httpx.AsyncClient.patch")
375359

376360
mock_patch.return_value = httpx.Response(
@@ -380,13 +364,13 @@ async def test_rename_key_in_revision_success(client, mocker: AsyncMock): # noq
380364
},
381365
)
382366

383-
response = await client.rename_key_in_revision(
367+
response = await async_client.rename_key_in_revision(
384368
tree_name="mock_configtree_name",
385369
revision_id="mock_revision_id",
386370
key="mock_key",
387371
config_key_rename={"metadata": {"name": "test_key"}},
388372
)
389373

390-
assert isinstance(response, Munch)
391-
assert response.metadata.guid == "test_revision_guid"
392-
assert response.metadata.name == "test_revision"
374+
assert isinstance(response, dict)
375+
assert response["metadata"]["guid"] == "test_revision_guid"
376+
assert response["metadata"]["name"] == "test_revision"

0 commit comments

Comments
 (0)