1010TEST FILE: test_sample_delete_result_async.py
1111
1212DESCRIPTION:
13- These tests validate the sample_delete_result .py sample code (async version).
13+ These tests validate the sample_delete_result_async .py sample code (async version).
1414 This sample demonstrates deleting analysis results for immediate cleanup.
1515
1616USAGE:
2121import pytest
2222from devtools_testutils .aio import recorded_by_proxy_async
2323from testpreparer_async import ContentUnderstandingPreparer , ContentUnderstandingClientTestBaseAsync
24+ from azure .ai .contentunderstanding .models import AnalyzeInput , AnalyzeResult
2425
2526
2627class TestSampleDeleteResultAsync (ContentUnderstandingClientTestBaseAsync ):
@@ -33,16 +34,17 @@ async def test_sample_delete_result_async(self, azure_content_understanding_endp
3334
3435 This test validates:
3536 1. Document analysis to create a result
36- 2. Extracting result ID
37- 3. Deleting the result
37+ 2. Extracting operation ID
38+ 3. Deleting the result using operation ID
3839
39- 13_DeleteResult .DeleteResultAsync()
40+ Equivalent to: Sample13_DeleteResult .DeleteResultAsync()
4041 """
4142 client = self .create_async_client (endpoint = azure_content_understanding_endpoint )
4243
4344 # First, analyze a document to create a result
44- tests_dir = os .path .dirname (os .path .dirname (__file__ ))
45- file_path = os .path .join (tests_dir , "test_data" , "sample_invoice.pdf" )
45+ current_dir = os .path .dirname (os .path .abspath (__file__ ))
46+ test_data_dir = os .path .join (os .path .dirname (current_dir ), "test_data" )
47+ file_path = os .path .join (test_data_dir , "sample_invoice.pdf" )
4648
4749 assert os .path .exists (file_path ), f"Sample file not found at { file_path } "
4850 print (f"[PASS] Sample file exists: { file_path } " )
@@ -53,59 +55,45 @@ async def test_sample_delete_result_async(self, azure_content_understanding_endp
5355 assert len (file_bytes ) > 0 , "File should not be empty"
5456 print (f"[PASS] File loaded: { len (file_bytes )} bytes" )
5557
56- # Analyze to get a result ID
57- poller = await client .begin_analyze_binary (
58- analyzer_id = "prebuilt-documentSearch " , binary_input = file_bytes , content_type = "application/pdf"
58+ # Analyze to get an operation ID
59+ analyze_operation = await client .begin_analyze (
60+ analyzer_id = "prebuilt-invoice " , inputs = [ AnalyzeInput ( data = file_bytes )]
5961 )
6062
61- result = await poller .result ()
63+ result : AnalyzeResult = await analyze_operation .result ()
6264
6365 # Assertions for analysis
64- assert poller is not None , "Analysis operation should not be null"
65- assert poller .done (), "Operation should be completed"
66+ assert analyze_operation is not None , "Analysis operation should not be null"
67+ assert analyze_operation .done (), "Operation should be completed"
6668 assert result is not None , "Analysis result should not be null"
6769 print ("[PASS] Analysis completed successfully" )
6870
69- # Extract operation ID from the poller
70- # The operation ID is needed to delete the result
71- operation_id = None
71+ # Get operation ID - this is needed to delete the result
72+ operation_id = analyze_operation .operation_id
73+ assert operation_id is not None , "Operation ID should not be null"
74+ assert isinstance (operation_id , str ), "Operation ID should be a string"
75+ assert operation_id .strip (), "Operation ID should not be empty"
76+ print (f"[PASS] Operation ID extracted: { operation_id [:50 ]} ..." )
77+
78+ # Verify we have analysis content
79+ assert hasattr (result , "contents" ), "Result should contain contents"
80+ contents = getattr (result , "contents" , None )
81+ assert contents is not None , "Result contents should not be null"
82+ assert len (contents ) > 0 , "Result should have at least one content"
83+ print (f"[PASS] Analysis result contains { len (contents )} content item(s)" )
84+
85+ # Delete the result
7286 try :
73- # Extract operation ID from polling URL
74- if hasattr (poller , "_polling_method" ):
75- polling_method = getattr (poller , "_polling_method" , None )
76- if polling_method and hasattr (polling_method , "_operation" ):
77- operation = getattr (polling_method , "_operation" , None ) # type: ignore
78- if operation and hasattr (operation , "get_polling_url" ):
79- polling_url = operation .get_polling_url () # type: ignore
80- # Extract operation ID from URL (last segment before query string)
81- operation_id = polling_url .split ("/" )[- 1 ]
82- if "?" in operation_id :
83- operation_id = operation_id .split ("?" )[0 ]
87+ await client .delete_result (operation_id = operation_id )
88+ print (f"[PASS] Result deleted successfully (operation ID: { operation_id [:50 ]} ...)" )
89+ print ("[INFO] Deletion success verified by no exception thrown" )
8490 except Exception as e :
85- print (f"[WARN] Could not extract operation ID: { str (e )[:100 ]} " )
86-
87- # Assertion: Verify we have an operation ID
88- if operation_id :
89- assert operation_id is not None , "Operation ID should not be null"
90- assert isinstance (operation_id , str ), "Operation ID should be a string"
91- assert operation_id .strip (), "Operation ID should not be empty"
92- print (f"[PASS] Operation ID extracted: { operation_id [:50 ]} ..." )
93-
94- # Delete the result
95- try :
96- await client .delete_result (operation_id = operation_id )
97- print (f"[PASS] Result deleted successfully (operation ID: { operation_id [:50 ]} ...)" )
98- print ("[INFO] Deletion success verified by no exception thrown" )
99- except Exception as e :
100- error_msg = str (e )
101- # Some implementations might not support result deletion or result might auto-expire
102- if "not found" in error_msg .lower () or "404" in error_msg :
103- print (f"[INFO] Result already deleted or not found: { error_msg [:100 ]} " )
104- else :
105- print (f"[WARN] Delete result failed: { error_msg [:100 ]} " )
106- else :
107- print ("[INFO] Operation ID not available in response" )
108- print ("[INFO] Delete result operation skipped - operation ID extraction not supported" )
91+ error_msg = str (e )
92+ # Some implementations might not support result deletion or result might auto-expire
93+ if "not found" in error_msg .lower () or "404" in error_msg :
94+ print (f"[INFO] Result already deleted or not found: { error_msg [:100 ]} " )
95+ else :
96+ raise
10997
11098 await client .close ()
11199 print ("\n [SUCCESS] All test_sample_delete_result_async assertions passed" )
0 commit comments