11import json
22
33import pytest
4+ import requests
45
56from mindee .client import Client
67from mindee .input .sources import PathInput
8+ from mindee .mindee_http .response_validation import is_valid_async_response
9+ from mindee .parsing .common .api_request import RequestStatus
710from mindee .parsing .common .async_predict_response import AsyncPredictResponse
811from mindee .product .invoice_splitter import InvoiceSplitterV1
912
1013ASYNC_DIR = "./tests/data/async"
1114
12- FILE_PATH_POST_SUCCESS = f"{ ASYNC_DIR } /post_success.json"
13- FILE_PATH_POST_FAIL = f"{ ASYNC_DIR } /post_fail_forbidden.json"
14- FILE_PATH_GET_PROCESSING = f"{ ASYNC_DIR } /get_processing.json"
15- FILE_PATH_GET_COMPLETED = f"{ ASYNC_DIR } /get_completed.json"
15+ FILE_PATH_POST_SUCCESS = f"{ ASYNC_DIR } /post_success.json"
16+ FILE_PATH_POST_FAIL = f"{ ASYNC_DIR } /post_fail_forbidden.json"
17+ FILE_PATH_GET_PROCESSING = f"{ ASYNC_DIR } /get_processing.json"
18+ FILE_PATH_GET_COMPLETED = f"{ ASYNC_DIR } /get_completed.json"
19+ FILE_PATH_GET_FAILED_JOB = f"{ ASYNC_DIR } /get_failed_job_error.json"
20+
21+
22+ class FakeResponse (requests .Response ):
23+ def __init__ (self , json_data , _status_code = 200 ):
24+ super ().__init__ ()
25+ self ._json_data = json_data
26+ self .status_code = _status_code
27+ self ._ok = True
28+
29+ def set_ok_status (self , ok_status ):
30+ self ._ok = ok_status
31+
32+ @property
33+ def ok (self ):
34+ return self ._ok
35+
36+ @property
37+ def content (self ) -> str :
38+ return json .dumps (self ._json_data )
1639
1740
1841@pytest .fixture
@@ -29,6 +52,9 @@ def dummy_client() -> Client:
2952def test_async_response_post_success ():
3053 response = json .load (open (FILE_PATH_POST_SUCCESS ))
3154 parsed_response = AsyncPredictResponse (InvoiceSplitterV1 , response )
55+ fake_response = FakeResponse (response )
56+ fake_response .set_ok_status (True )
57+ assert is_valid_async_response (fake_response ) is True
3258 assert parsed_response .job is not None
3359 assert (
3460 parsed_response .job .issued_at .isoformat () == "2023-02-16T12:33:49.602947+00:00"
@@ -41,18 +67,17 @@ def test_async_response_post_success():
4167
4268def test_async_response_post_fail ():
4369 response = json .load (open (FILE_PATH_POST_FAIL ))
44- parsed_response = AsyncPredictResponse (InvoiceSplitterV1 , response )
45- assert parsed_response .job is not None
46- assert parsed_response .job .issued_at .isoformat () == "2023-01-01T00:00:00+00:00"
47- assert parsed_response .job .available_at is None
48- assert parsed_response .job .status is None
49- assert parsed_response .job .id is None
50- assert parsed_response .api_request .error ["code" ] == "Forbidden"
70+ fake_response = FakeResponse (response )
71+ fake_response .set_ok_status (False )
72+ assert is_valid_async_response (fake_response ) is False
5173
5274
5375def test_async_get_processing ():
5476 response = json .load (open (FILE_PATH_GET_PROCESSING ))
5577 parsed_response = AsyncPredictResponse (InvoiceSplitterV1 , response )
78+ fake_response = FakeResponse (response )
79+ fake_response .set_ok_status (True )
80+ assert is_valid_async_response (fake_response ) is True
5681 assert parsed_response .job is not None
5782 assert parsed_response .job .issued_at .isoformat () == "2023-03-16T12:33:49.602947"
5883 assert parsed_response .job .available_at is None
@@ -64,8 +89,25 @@ def test_async_get_processing():
6489def test_async_response_get_completed ():
6590 response = json .load (open (FILE_PATH_GET_COMPLETED ))
6691 parsed_response = AsyncPredictResponse (InvoiceSplitterV1 , response )
92+ fake_response = FakeResponse (response )
93+ fake_response .set_ok_status (True )
94+ assert is_valid_async_response (fake_response ) is True
6795 assert parsed_response .job is not None
6896 assert parsed_response .job .issued_at .isoformat () == "2023-03-21T13:52:56.326107"
6997 assert parsed_response .job .available_at .isoformat () == "2023-03-21T13:53:00.990339"
7098 assert parsed_response .job .status == "completed"
7199 assert parsed_response .api_request .error == {}
100+
101+
102+ def test_async_get_failed_job ():
103+ response = json .load (open (FILE_PATH_GET_FAILED_JOB ))
104+ parsed_response = AsyncPredictResponse (InvoiceSplitterV1 , response )
105+ fake_response = FakeResponse (response )
106+ fake_response .set_ok_status (False )
107+ assert is_valid_async_response (fake_response ) is False
108+ assert parsed_response .api_request .status == RequestStatus .SUCCESS
109+ assert parsed_response .api_request .status_code == 200
110+ assert parsed_response .job .issued_at .isoformat () == "2024-02-20T10:31:06.878599"
111+ assert parsed_response .job .available_at .isoformat () == "2024-02-20T10:31:06.878599"
112+ assert parsed_response .job .status == "failed"
113+ assert parsed_response .job .error ["code" ] == "ServerError"
0 commit comments