@@ -66,6 +66,44 @@ def test_convert_to_markdown_payload_invalid_page_break_comments() -> None:
6666 )
6767
6868
69+ @pytest .mark .asyncio
70+ async def test_async_convert_to_markdown_payload_rejects_non_pdf () -> None :
71+ file_id = str (PdfRestFileID .generate ())
72+ text_file = PdfRestFile .model_validate (
73+ {
74+ "id" : file_id ,
75+ "name" : "notes.txt" ,
76+ "url" : f"https://api.pdfrest.com/resource/{ file_id } " ,
77+ "type" : "text/plain" ,
78+ "size" : 64 ,
79+ "modified" : "2024-01-01T00:00:00Z" ,
80+ "scheduledDeletionTimeUtc" : None ,
81+ }
82+ )
83+ with pytest .raises (ValidationError , match = "Must be a PDF file" ):
84+ ConvertToMarkdownPayload .model_validate ({"files" : [text_file ]})
85+
86+
87+ @pytest .mark .asyncio
88+ async def test_async_convert_to_markdown_payload_invalid_page_range () -> None :
89+ file_repr = make_pdf_file (PdfRestFileID .generate (1 ))
90+ with pytest .raises (
91+ ValidationError , match = "The start page must be less than or equal to the end"
92+ ):
93+ ConvertToMarkdownPayload .model_validate (
94+ {"files" : [file_repr ], "pages" : ["5-2" ]}
95+ )
96+
97+
98+ @pytest .mark .asyncio
99+ async def test_async_convert_to_markdown_payload_invalid_page_break_comments () -> None :
100+ file_repr = make_pdf_file (PdfRestFileID .generate (1 ))
101+ with pytest .raises (ValidationError , match = "Input should be 'on' or 'off'" ):
102+ ConvertToMarkdownPayload .model_validate (
103+ {"files" : [file_repr ], "page_break_comments" : "maybe" }
104+ )
105+
106+
69107def test_convert_to_markdown_success (monkeypatch : pytest .MonkeyPatch ) -> None :
70108 monkeypatch .delenv ("PDFREST_API_KEY" , raising = False )
71109 input_file = make_pdf_file (PdfRestFileID .generate (1 ))
@@ -198,6 +236,74 @@ def handler(request: httpx.Request) -> httpx.Response:
198236 assert get_timeout == pytest .approx (0.4 )
199237
200238
239+ @pytest .mark .asyncio
240+ async def test_async_convert_to_markdown_request_customization (
241+ monkeypatch : pytest .MonkeyPatch ,
242+ ) -> None :
243+ monkeypatch .delenv ("PDFREST_API_KEY" , raising = False )
244+ input_file = make_pdf_file (PdfRestFileID .generate (2 ))
245+ payload_dump = ConvertToMarkdownPayload .model_validate (
246+ {
247+ "files" : [input_file ],
248+ "output_type" : "file" ,
249+ "page_break_comments" : "off" ,
250+ }
251+ ).model_dump (mode = "json" , by_alias = True , exclude_none = True , exclude_unset = True )
252+ output_id = str (PdfRestFileID .generate ())
253+ captured_timeout : dict [str , float | dict [str , float ] | None ] = {}
254+
255+ def handler (request : httpx .Request ) -> httpx .Response :
256+ if request .method == "POST" and request .url .path == "/markdown" :
257+ assert request .url .params ["trace" ] == "true"
258+ assert request .headers ["X-Debug" ] == "async"
259+ captured_timeout ["value" ] = request .extensions .get ("timeout" )
260+ payload = json .loads (request .content .decode ("utf-8" ))
261+ for key , value in payload_dump .items ():
262+ assert payload [key ] == value
263+ assert payload ["debug" ] is True
264+ return httpx .Response (
265+ 200 ,
266+ json = {
267+ "inputId" : [str (input_file .id )],
268+ "outputId" : [output_id ],
269+ },
270+ )
271+ if request .method == "GET" and request .url .path == f"/resource/{ output_id } " :
272+ assert request .url .params ["format" ] == "info"
273+ assert request .url .params ["trace" ] == "true"
274+ assert request .headers ["X-Debug" ] == "async"
275+ return httpx .Response (
276+ 200 ,
277+ json = _make_markdown_file (output_id , "debug-async.md" ).model_dump (
278+ mode = "json" , by_alias = True
279+ ),
280+ )
281+ msg = f"Unexpected request { request .method } { request .url } "
282+ raise AssertionError (msg )
283+
284+ transport = httpx .MockTransport (handler )
285+ async with AsyncPdfRestClient (api_key = ASYNC_API_KEY , transport = transport ) as client :
286+ response = await client .convert_to_markdown (
287+ input_file ,
288+ extra_query = {"trace" : "true" },
289+ extra_headers = {"X-Debug" : "async" },
290+ extra_body = {"debug" : True },
291+ timeout = 0.4 ,
292+ page_break_comments = "off" ,
293+ )
294+
295+ assert isinstance (response , PdfRestFileBasedResponse )
296+ assert len (response .output_files ) == 1
297+ timeout_value = captured_timeout ["value" ]
298+ assert timeout_value is not None
299+ if isinstance (timeout_value , dict ):
300+ assert all (
301+ component == pytest .approx (0.4 ) for component in timeout_value .values ()
302+ )
303+ else :
304+ assert timeout_value == pytest .approx (0.4 )
305+
306+
201307@pytest .mark .asyncio
202308async def test_async_convert_to_markdown_success (
203309 monkeypatch : pytest .MonkeyPatch ,
0 commit comments