Skip to content

Commit d4b94b5

Browse files
Add unit tests for the Blueprint API Client
Added a comprehensive test suite for `enapter.http.api.blueprints.Client` covering get, upload (data/file/directory), download, and validation methods. Mocks `httpx.AsyncClient` responses to verify request structures and data parsing without making external network requests. Also formats with `black`. Co-authored-by: rnovatorov <20299819+rnovatorov@users.noreply.github.com>
1 parent 44363ef commit d4b94b5

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

tests/unit/test_http/test_api/test_blueprints/test_client.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Unit tests for the Blueprints HTTP API client."""
22

3+
import datetime
4+
import io
35
import pathlib
46
import zipfile
5-
import io
6-
import datetime
77
from unittest.mock import AsyncMock, MagicMock
88

99
import httpx
@@ -30,17 +30,16 @@ async def test_get_blueprint(client, mock_httpx_client):
3030
mock_response = MagicMock(spec=httpx.Response)
3131
mock_response.status_code = 200
3232
mock_response.json.return_value = {
33-
"blueprint": {
34-
"id": "bp_123",
35-
"created_at": "2024-04-01T12:00:00+00:00"
36-
}
33+
"blueprint": {"id": "bp_123", "created_at": "2024-04-01T12:00:00+00:00"}
3734
}
3835
mock_httpx_client.get = AsyncMock(return_value=mock_response)
3936

4037
blueprint = await client.get("bp_123")
4138

4239
assert blueprint.id == "bp_123"
43-
assert blueprint.created_at == datetime.datetime.fromisoformat("2024-04-01T12:00:00+00:00")
40+
assert blueprint.created_at == datetime.datetime.fromisoformat(
41+
"2024-04-01T12:00:00+00:00"
42+
)
4443
mock_httpx_client.get.assert_called_once_with("v3/blueprints/bp_123")
4544

4645

@@ -50,18 +49,17 @@ async def test_upload_data(client, mock_httpx_client):
5049
mock_response = MagicMock(spec=httpx.Response)
5150
mock_response.status_code = 200
5251
mock_response.json.return_value = {
53-
"blueprint": {
54-
"id": "bp_new",
55-
"created_at": "2024-04-01T12:00:00+00:00"
56-
}
52+
"blueprint": {"id": "bp_new", "created_at": "2024-04-01T12:00:00+00:00"}
5753
}
5854
mock_httpx_client.post = AsyncMock(return_value=mock_response)
5955

6056
data = b"blueprint content"
6157
blueprint = await client.upload(data)
6258

6359
assert blueprint.id == "bp_new"
64-
assert blueprint.created_at == datetime.datetime.fromisoformat("2024-04-01T12:00:00+00:00")
60+
assert blueprint.created_at == datetime.datetime.fromisoformat(
61+
"2024-04-01T12:00:00+00:00"
62+
)
6563
mock_httpx_client.post.assert_called_once_with("v3/blueprints/upload", content=data)
6664

6765

@@ -71,10 +69,7 @@ async def test_upload_file(client, mock_httpx_client, tmp_path):
7169
mock_response = MagicMock(spec=httpx.Response)
7270
mock_response.status_code = 200
7371
mock_response.json.return_value = {
74-
"blueprint": {
75-
"id": "bp_file",
76-
"created_at": "2024-04-01T12:00:00+00:00"
77-
}
72+
"blueprint": {"id": "bp_file", "created_at": "2024-04-01T12:00:00+00:00"}
7873
}
7974
mock_httpx_client.post = AsyncMock(return_value=mock_response)
8075

@@ -84,7 +79,9 @@ async def test_upload_file(client, mock_httpx_client, tmp_path):
8479
blueprint = await client.upload_file(file_path)
8580

8681
assert blueprint.id == "bp_file"
87-
mock_httpx_client.post.assert_called_once_with("v3/blueprints/upload", content=b"manifest content")
82+
mock_httpx_client.post.assert_called_once_with(
83+
"v3/blueprints/upload", content=b"manifest content"
84+
)
8885

8986

9087
@pytest.mark.asyncio
@@ -93,10 +90,7 @@ async def test_upload_directory(client, mock_httpx_client, tmp_path):
9390
mock_response = MagicMock(spec=httpx.Response)
9491
mock_response.status_code = 200
9592
mock_response.json.return_value = {
96-
"blueprint": {
97-
"id": "bp_dir",
98-
"created_at": "2024-04-01T12:00:00+00:00"
99-
}
93+
"blueprint": {"id": "bp_dir", "created_at": "2024-04-01T12:00:00+00:00"}
10094
}
10195
mock_httpx_client.post = AsyncMock(return_value=mock_response)
10296

@@ -148,7 +142,9 @@ async def test_validate_success(client, mock_httpx_client):
148142

149143
await client.validate(b"blueprint content")
150144

151-
mock_httpx_client.post.assert_called_once_with("v3/blueprints/validate", content=b"blueprint content")
145+
mock_httpx_client.post.assert_called_once_with(
146+
"v3/blueprints/validate", content=b"blueprint content"
147+
)
152148

153149

154150
@pytest.mark.asyncio
@@ -168,7 +164,9 @@ async def test_validate_error(client, mock_httpx_client):
168164
assert len(errors) == 2
169165
assert errors[0].message == "Invalid manifest"
170166
assert errors[1].message == "Missing field"
171-
mock_httpx_client.post.assert_called_once_with("v3/blueprints/validate", content=b"blueprint content")
167+
mock_httpx_client.post.assert_called_once_with(
168+
"v3/blueprints/validate", content=b"blueprint content"
169+
)
172170

173171

174172
@pytest.mark.asyncio
@@ -184,7 +182,9 @@ async def test_validate_file(client, mock_httpx_client, tmp_path):
184182

185183
await client.validate_file(file_path)
186184

187-
mock_httpx_client.post.assert_called_once_with("v3/blueprints/validate", content=b"manifest content")
185+
mock_httpx_client.post.assert_called_once_with(
186+
"v3/blueprints/validate", content=b"manifest content"
187+
)
188188

189189

190190
@pytest.mark.asyncio

0 commit comments

Comments
 (0)