Skip to content

Commit

Permalink
close client session in async tests (#12656)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristapratico authored Jul 23, 2020
1 parent 0b08b5b commit 6e22b40
Show file tree
Hide file tree
Showing 12 changed files with 870 additions and 716 deletions.
151 changes: 85 additions & 66 deletions sdk/formrecognizer/azure-ai-formrecognizer/tests/test_content_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,85 +29,94 @@ async def test_content_bad_endpoint(self, resource_group, location, form_recogni
myfile = fd.read()
with self.assertRaises(ServiceRequestError):
client = FormRecognizerClient("http://notreal.azure.com", AzureKeyCredential(form_recognizer_account_key))
poller = await client.begin_recognize_content(myfile)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myfile)
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_content_authentication_successful_key(self, client):
with open(self.invoice_pdf, "rb") as fd:
myfile = fd.read()
poller = await client.begin_recognize_content(myfile)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myfile)
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
async def test_content_authentication_bad_key(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
client = FormRecognizerClient(form_recognizer_account, AzureKeyCredential("xxxx"))
with self.assertRaises(ClientAuthenticationError):
poller = await client.begin_recognize_content(b"xxx", content_type="application/pdf")
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(b"xxx", content_type="application/pdf")
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_passing_enum_content_type(self, client):
with open(self.invoice_pdf, "rb") as fd:
myfile = fd.read()
poller = await client.begin_recognize_content(
myfile,
content_type=FormContentType.APPLICATION_PDF
)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(
myfile,
content_type=FormContentType.APPLICATION_PDF
)
result = await poller.result()
self.assertIsNotNone(result)

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_damaged_file_passed_as_bytes(self, client):
damaged_pdf = b"\x25\x50\x44\x46\x55\x55\x55" # still has correct bytes to be recognized as PDF
with self.assertRaises(HttpResponseError):
poller = await client.begin_recognize_content(
damaged_pdf,
)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(
damaged_pdf,
)
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_damaged_file_bytes_fails_autodetect_content_type(self, client):
damaged_pdf = b"\x50\x44\x46\x55\x55\x55" # doesn't match any magic file numbers
with self.assertRaises(ValueError):
poller = await client.begin_recognize_content(
damaged_pdf,
)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(
damaged_pdf,
)
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_damaged_file_passed_as_bytes_io(self, client):
damaged_pdf = BytesIO(b"\x25\x50\x44\x46\x55\x55\x55") # still has correct bytes to be recognized as PDF
with self.assertRaises(HttpResponseError):
poller = await client.begin_recognize_content(
damaged_pdf,
)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(
damaged_pdf,
)
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_damaged_file_bytes_io_fails_autodetect(self, client):
damaged_pdf = BytesIO(b"\x50\x44\x46\x55\x55\x55") # doesn't match any magic file numbers
with self.assertRaises(ValueError):
poller = await client.begin_recognize_content(
damaged_pdf,
)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(
damaged_pdf,
)
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_blank_page(self, client):
with open(self.blank_pdf, "rb") as fd:
blank = fd.read()
poller = await client.begin_recognize_content(
blank,
)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(
blank,
)
result = await poller.result()
self.assertIsNotNone(result)

@GlobalFormRecognizerAccountPreparer()
Expand All @@ -116,18 +125,20 @@ async def test_passing_bad_content_type_param_passed(self, client):
with open(self.invoice_pdf, "rb") as fd:
myfile = fd.read()
with self.assertRaises(ValueError):
poller = await client.begin_recognize_content(
myfile,
content_type="application/jpeg"
)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(
myfile,
content_type="application/jpeg"
)
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_content_stream_passing_url(self, client):
with self.assertRaises(TypeError):
poller = await client.begin_recognize_content("https://badurl.jpg", content_type="application/json")
result = await poller.result()
async with client:
poller = await client.begin_recognize_content("https://badurl.jpg", content_type="application/json")
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
Expand All @@ -136,10 +147,11 @@ async def test_auto_detect_unsupported_stream_content(self, client):
myfile = fd.read()

with self.assertRaises(ValueError):
poller = await client.begin_recognize_content(
myfile
)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(
myfile
)
result = await poller.result()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
Expand All @@ -155,8 +167,9 @@ def callback(raw_response, _, headers):
responses.append(analyze_result)
responses.append(extracted_layout)

poller = await client.begin_recognize_content(myform, cls=callback)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myform, cls=callback)
result = await poller.result()
raw_response = responses[0]
layout = responses[1]
page_results = raw_response.analyze_result.page_results
Expand All @@ -171,8 +184,9 @@ async def test_content_stream_pdf(self, client):
with open(self.invoice_pdf, "rb") as fd:
myform = fd.read()

poller = await client.begin_recognize_content(myform)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myform)
result = await poller.result()
self.assertEqual(len(result), 1)
layout = result[0]
self.assertEqual(layout.page_number, 1)
Expand All @@ -195,8 +209,9 @@ def callback(raw_response, _, headers):
responses.append(analyze_result)
responses.append(extracted_layout)

poller = await client.begin_recognize_content(myform, cls=callback)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myform, cls=callback)
result = await poller.result()
raw_response = responses[0]
layout = responses[1]
page_results = raw_response.analyze_result.page_results
Expand All @@ -211,8 +226,9 @@ async def test_content_stream_jpg(self, client):
with open(self.form_jpg, "rb") as fd:
myform = fd.read()

poller = await client.begin_recognize_content(myform)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myform)
result = await poller.result()
self.assertEqual(len(result), 1)
layout = result[0]
self.assertEqual(layout.page_number, 1)
Expand All @@ -229,8 +245,9 @@ async def test_content_stream_jpg(self, client):
async def test_content_multipage(self, client):
with open(self.multipage_invoice_pdf, "rb") as fd:
invoice = fd.read()
poller = await client.begin_recognize_content(invoice)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(invoice)
result = await poller.result()

self.assertEqual(len(result), 3)
self.assertFormPagesHasValues(result)
Expand All @@ -249,8 +266,9 @@ def callback(raw_response, _, headers):
responses.append(analyze_result)
responses.append(extracted_layout)

poller = await client.begin_recognize_content(myform, cls=callback)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myform, cls=callback)
result = await poller.result()
raw_response = responses[0]
layout = responses[1]
page_results = raw_response.analyze_result.page_results
Expand All @@ -265,22 +283,23 @@ def callback(raw_response, _, headers):
async def test_content_continuation_token(self, client):
with open(self.form_jpg, "rb") as fd:
myfile = fd.read()
initial_poller = await client.begin_recognize_content(myfile)
cont_token = initial_poller.continuation_token()

poller = await client.begin_recognize_content(myfile, continuation_token=cont_token)
result = await poller.result()
self.assertIsNotNone(result)
await initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error
async with client:
initial_poller = await client.begin_recognize_content(myfile)
cont_token = initial_poller.continuation_token()
poller = await client.begin_recognize_content(myfile, continuation_token=cont_token)
result = await poller.result()
self.assertIsNotNone(result)
await initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error


@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
async def test_content_multipage_table_span_pdf(self, client):
with open(self.multipage_table_pdf, "rb") as fd:
myfile = fd.read()
poller = await client.begin_recognize_content(myfile)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myfile)
result = await poller.result()
self.assertEqual(len(result), 2)
layout = result[0]
self.assertEqual(layout.page_number, 1)
Expand Down Expand Up @@ -312,9 +331,9 @@ def callback(raw_response, _, headers):
extracted_layout = prepare_content_result(analyze_result)
responses.append(analyze_result)
responses.append(extracted_layout)

poller = await client.begin_recognize_content(myform, cls=callback)
result = await poller.result()
async with client:
poller = await client.begin_recognize_content(myform, cls=callback)
result = await poller.result()
raw_response = responses[0]
layout = responses[1]
page_results = raw_response.analyze_result.page_results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class TestContentFromUrl(FormRecognizerTest):
@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
def test_content_encoded_url(self, client):
try:
with pytest.raises(HttpResponseError) as e:
poller = client.begin_recognize_content_from_url("https://fakeuri.com/blank%20space")
except HttpResponseError as e:
self.assertIn("https://fakeuri.com/blank%20space", e.response.request.body)
client.close()
self.assertIn("https://fakeuri.com/blank%20space", e.value.response.request.body)

@GlobalFormRecognizerAccountPreparer()
def test_content_url_bad_endpoint(self, resource_group, location, form_recognizer_account, form_recognizer_account_key):
Expand Down
Loading

0 comments on commit 6e22b40

Please sign in to comment.