Skip to content

Commit f7dc7f2

Browse files
add even more coverage idk
1 parent 8a44f70 commit f7dc7f2

File tree

4 files changed

+91
-17
lines changed

4 files changed

+91
-17
lines changed

mindee/parsing/v2/error_response.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
class ErrorResponse(RuntimeError):
1+
from mindee.parsing.common.string_dict import StringDict
2+
3+
4+
class ErrorResponse:
25
"""Error response info."""
36

47
detail: str
58
"""Detail relevant to the error."""
6-
79
status: int
810
"""Http error code."""
911

12+
def __init__(self, raw_response: StringDict):
13+
self.detail = raw_response["detail"]
14+
self.status = raw_response["status"]
15+
1016
def __str__(self):
1117
return f"HTTP Status: {self.status} - {self.detail}"

mindee/parsing/v2/job.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from datetime import datetime
2+
from typing import List, Optional
3+
14
from mindee.parsing.common.string_dict import StringDict
25
from mindee.parsing.v2.error_response import ErrorResponse
36

@@ -7,7 +10,7 @@ class Job:
710

811
id: str
912
"""Job ID."""
10-
error: ErrorResponse
13+
error: Optional[ErrorResponse]
1114
"""Error response if any."""
1215
model_id: str
1316
"""ID of the model."""
@@ -17,11 +20,25 @@ class Job:
1720
"""Optional alias for the file."""
1821
status: str
1922
"""Status of the job."""
23+
polling_url: str
24+
"""URL to poll for the job status."""
25+
result_url: Optional[str]
26+
"""URL to poll for the job result, redirects to the result if available."""
27+
webhooks: List[str]
28+
"""ID of webhooks associated with the job."""
2029

2130
def __init__(self, raw_response: StringDict) -> None:
2231
self.id = raw_response["id"]
2332
self.status = raw_response["status"]
24-
self.error = ErrorResponse(raw_response["error"])
33+
self.error = (
34+
ErrorResponse(raw_response["error"]) if raw_response["error"] else None
35+
)
36+
self.created_at = datetime.fromisoformat(
37+
raw_response["created_at"].replace("Z", "+00:00")
38+
)
2539
self.model_id = raw_response["model_id"]
40+
self.polling_url = raw_response["polling_url"]
2641
self.filename = raw_response["filename"]
42+
self.result_url = raw_response["result_url"]
2743
self.alias = raw_response["alias"]
44+
self.webhooks = raw_response["webhooks"]

mindee/parsing/v2/webhook.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from typing import Optional
23

34
from mindee.parsing.common.string_dict import StringDict
45
from mindee.parsing.v2.error_response import ErrorResponse
@@ -9,7 +10,7 @@ class Webhook:
910

1011
id: str
1112
"""ID of the webhook."""
12-
error: ErrorResponse
13+
error: Optional[ErrorResponse]
1314
"""Error response if any."""
1415
created_at: datetime
1516
"""Date and time the webhook was sent at."""
@@ -18,7 +19,9 @@ class Webhook:
1819

1920
def __init__(self, raw_response: StringDict) -> None:
2021
self.id = raw_response["id"]
21-
self.error = ErrorResponse(raw_response["error"])
22+
self.error = (
23+
ErrorResponse(raw_response["error"]) if raw_response["error"] else None
24+
)
2225
self.created_at = self.parse_date(raw_response["created_at"])
2326
self.status = raw_response["status"]
2427

tests/test_client_v2.py

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import json
2+
13
import pytest
24

35
from mindee import ClientV2, InferencePredictOptions, LocalResponse
46
from mindee.error.mindee_error import MindeeApiV2Error
57
from mindee.error.mindee_http_error_v2 import MindeeHTTPErrorV2
68
from mindee.input import LocalInputSource, PathInput
79
from mindee.mindee_http.base_settings import USER_AGENT
10+
from mindee.parsing.v2 import Job, PollingResponse
811
from tests.test_inputs import FILE_TYPES_DIR, V2_DATA_DIR
912
from tests.utils import dummy_envvars
1013

@@ -17,28 +20,58 @@ def env_client(monkeypatch) -> ClientV2:
1720

1821
@pytest.fixture
1922
def custom_base_url_client(monkeypatch) -> ClientV2:
20-
class _FakeResp:
23+
class _FakePostResp:
2124
status_code = 400 # any non-2xx will do
2225
ok = False
2326

2427
def json(self):
2528
# Shape must match what handle_error_v2 expects
2629
return {"status": -1, "detail": "forced failure from test"}
2730

31+
class _FakeGetResp:
32+
status_code = 200
33+
ok = True
34+
35+
def json(self):
36+
return {
37+
"job": {
38+
"id": "12345678-1234-1234-1234-123456789ABC",
39+
"model_id": "87654321-4321-4321-4321-CBA987654321",
40+
"filename": "default_sample.jpg",
41+
"alias": "dummy-alias.jpg",
42+
"created_at": "2025-07-03T14:27:58.974451",
43+
"status": "Processing",
44+
"polling_url": "https://api-v2.mindee.net/v2/jobs/12345678-1234-1234-1234-123456789ABC",
45+
"result_url": None,
46+
"webhooks": [],
47+
"error": None,
48+
}
49+
}
50+
51+
@property
52+
def content(self) -> bytes:
53+
"""
54+
Raw (bytes) payload, mimicking `requests.Response.content`.
55+
"""
56+
return json.dumps(self.json()).encode("utf-8")
57+
2858
monkeypatch.setenv("MINDEE_V2_BASE_URL", "https://dummy-url")
2959

30-
def _fake_response(*args, **kwargs):
31-
return _FakeResp()
60+
def _fake_post_error(*args, **kwargs):
61+
return _FakePostResp()
62+
63+
def _fake_get_error(*args, **kwargs):
64+
return _FakeGetResp()
3265

3366
monkeypatch.setattr(
3467
"mindee.mindee_http.mindee_api_v2.requests.post",
35-
_fake_response,
68+
_fake_post_error,
3669
raising=True,
3770
)
3871

3972
monkeypatch.setattr(
4073
"mindee.mindee_http.mindee_api_v2.requests.get",
41-
_fake_response,
74+
_fake_get_error,
4275
raising=True,
4376
)
4477

@@ -78,12 +111,6 @@ def test_enqueue_and_parse_path_with_env_token(custom_base_url_client):
78111
)
79112

80113

81-
@pytest.mark.v2
82-
def test_parse_queued6_and_parse_path_with_env_token(custom_base_url_client):
83-
with pytest.raises(MindeeHTTPErrorV2):
84-
custom_base_url_client.parse_queued("dummy-queue")
85-
86-
87114
@pytest.mark.v2
88115
def test_loads_from_prediction(env_client):
89116
input_inference = LocalResponse(
@@ -104,3 +131,24 @@ def test_error_handling(custom_base_url_client):
104131
)
105132
assert e.status_code == -1
106133
assert e.detail == "forced failure from test"
134+
135+
136+
def test_enqueue(custom_base_url_client):
137+
response = custom_base_url_client.parse_queued(
138+
"12345678-1234-1234-1234-123456789ABC"
139+
)
140+
assert isinstance(response, PollingResponse)
141+
assert isinstance(response.job, Job)
142+
assert response.job.id == "12345678-1234-1234-1234-123456789ABC"
143+
assert response.job.model_id == "87654321-4321-4321-4321-CBA987654321"
144+
assert response.job.filename == "default_sample.jpg"
145+
assert response.job.alias == "dummy-alias.jpg"
146+
assert str(response.job.created_at) == "2025-07-03 14:27:58.974451"
147+
assert response.job.status == "Processing"
148+
assert (
149+
response.job.polling_url
150+
== "https://api-v2.mindee.net/v2/jobs/12345678-1234-1234-1234-123456789ABC"
151+
)
152+
assert not response.job.result_url
153+
assert len(response.job.webhooks) == 0
154+
assert not response.job.error

0 commit comments

Comments
 (0)