1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import pytest
import functools
from devtools_testutils import get_credential, set_bodiless_matcher
from devtools_testutils.aio import recorded_by_proxy_async
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import (
DocumentAnalysisFeature,
AnalyzeDocumentRequest,
AnalyzeResult,
AnalyzeOutputOption,
)
from asynctestcase import AsyncDocumentIntelligenceTest
from conftest import skip_flaky_test
from preparers import DocumentIntelligencePreparer, GlobalClientPreparerAsync as _GlobalClientPreparer
DocumentIntelligenceClientPreparer = functools.partial(_GlobalClientPreparer, DocumentIntelligenceClient)
class TestDACAnalyzeLayoutAsync(AsyncDocumentIntelligenceTest):
@skip_flaky_test
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_layout_incorrect_feature_format(self, client):
with open(self.invoice_pdf, "rb") as fd:
document = fd.read()
async with client:
with pytest.raises(TypeError) as e:
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
features=DocumentAnalysisFeature.STYLE_FONT,
)
assert "features must be type [str]." in str(e.value)
@skip_flaky_test
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_layout_stream_transform_pdf(self, client):
with open(self.invoice_pdf, "rb") as fd:
document = fd.read()
def callback(raw_response, _, headers):
return raw_response
async with client:
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
features=[DocumentAnalysisFeature.STYLE_FONT],
cls=callback,
)
raw_response = await poller.result()
raw_analyze_result = AnalyzeResult._deserialize(raw_response.http_response.json()["analyzeResult"], [])
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
features=[DocumentAnalysisFeature.STYLE_FONT],
)
returned_model = await poller.result()
assert returned_model.model_id == raw_analyze_result.model_id
assert returned_model.api_version == raw_analyze_result.api_version
assert returned_model.content == raw_analyze_result.content
assert len(returned_model.pages) == len(raw_analyze_result.pages)
assert len(returned_model.tables) == len(raw_analyze_result.tables)
assert len(returned_model.paragraphs) == len(raw_analyze_result.paragraphs)
assert len(returned_model.styles) == len(raw_analyze_result.styles)
self.assertDocumentPagesTransformCorrect(returned_model.pages, raw_analyze_result.pages)
self.assertDocumentTransformCorrect(returned_model.documents, raw_analyze_result.documents)
self.assertDocumentTablesTransformCorrect(returned_model.tables, raw_analyze_result.tables)
self.assertDocumentKeyValuePairsTransformCorrect(
returned_model.key_value_pairs, raw_analyze_result.key_value_pairs
)
self.assertDocumentStylesTransformCorrect(returned_model.styles, raw_analyze_result.styles)
@skip_flaky_test
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_layout_stream_transform_jpg(self, client):
with open(self.form_jpg, "rb") as fd:
document = fd.read()
def callback(raw_response, _, headers):
return raw_response
async with client:
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
cls=callback,
)
raw_response = await poller.result()
raw_analyze_result = AnalyzeResult._deserialize(raw_response.http_response.json()["analyzeResult"], [])
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
)
returned_model = await poller.result()
assert returned_model.model_id == raw_analyze_result.model_id
assert returned_model.api_version == raw_analyze_result.api_version
assert returned_model.content == raw_analyze_result.content
assert len(returned_model.pages) == len(raw_analyze_result.pages)
assert len(returned_model.tables) == len(raw_analyze_result.tables)
assert len(returned_model.paragraphs) == len(raw_analyze_result.paragraphs)
assert len(returned_model.styles) == len(raw_analyze_result.styles)
self.assertDocumentPagesTransformCorrect(returned_model.pages, raw_analyze_result.pages)
self.assertDocumentTransformCorrect(returned_model.documents, raw_analyze_result.documents)
self.assertDocumentTablesTransformCorrect(returned_model.tables, raw_analyze_result.tables)
self.assertDocumentKeyValuePairsTransformCorrect(
returned_model.key_value_pairs, raw_analyze_result.key_value_pairs
)
self.assertDocumentStylesTransformCorrect(returned_model.styles, raw_analyze_result.styles)
@skip_flaky_test
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_layout_multipage_transform(self, client):
with open(self.multipage_invoice_pdf, "rb") as fd:
document = fd.read()
def callback(raw_response, _, headers):
return raw_response
async with client:
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
cls=callback,
)
raw_response = await poller.result()
raw_analyze_result = AnalyzeResult._deserialize(raw_response.http_response.json()["analyzeResult"], [])
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
)
returned_model = await poller.result()
assert returned_model.model_id == raw_analyze_result.model_id
assert returned_model.api_version == raw_analyze_result.api_version
assert returned_model.content == raw_analyze_result.content
assert len(returned_model.pages) == len(raw_analyze_result.pages)
assert len(returned_model.tables) == len(raw_analyze_result.tables)
assert len(returned_model.paragraphs) == len(raw_analyze_result.paragraphs)
assert len(returned_model.styles) == len(raw_analyze_result.styles)
self.assertDocumentPagesTransformCorrect(returned_model.pages, raw_analyze_result.pages)
self.assertDocumentTransformCorrect(returned_model.documents, raw_analyze_result.documents)
self.assertDocumentTablesTransformCorrect(returned_model.tables, raw_analyze_result.tables)
self.assertDocumentKeyValuePairsTransformCorrect(
returned_model.key_value_pairs, raw_analyze_result.key_value_pairs
)
self.assertDocumentStylesTransformCorrect(returned_model.styles, raw_analyze_result.styles)
@skip_flaky_test
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_layout_multipage_table_span_pdf(self, client):
with open(self.multipage_table_pdf, "rb") as fd:
document = fd.read()
async with client:
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
)
layout = await poller.result()
assert len(layout.tables) == 3
assert layout.tables[0].row_count == 30
assert layout.tables[0].column_count == 5
assert layout.tables[1].row_count == 6
assert layout.tables[1].column_count == 5
assert layout.tables[2].row_count == 24
assert layout.tables[2].column_count == 5
@skip_flaky_test
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_layout_multipage_table_span_pdf_continuation_token(self, client):
with open(self.multipage_table_pdf, "rb") as fd:
document = fd.read()
async with client:
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
)
continuation_token = poller.continuation_token()
layout = await (
await client.begin_analyze_document(None, None, continuation_token=continuation_token)
).result()
assert len(layout.tables) == 3
assert layout.tables[0].row_count == 30
assert layout.tables[0].column_count == 5
assert layout.tables[1].row_count == 6
assert layout.tables[1].column_count == 5
assert layout.tables[2].row_count == 24
assert layout.tables[2].column_count == 5
@skip_flaky_test
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_layout_url_barcodes(self, client):
set_bodiless_matcher()
async with client:
poller = await client.begin_analyze_document(
"prebuilt-layout",
AnalyzeDocumentRequest(url_source=self.barcode_url_tif),
features=[DocumentAnalysisFeature.BARCODES],
)
layout = await poller.result()
assert len(layout.pages) > 0
assert len(layout.pages[0].barcodes) == 2
assert layout.pages[0].barcodes[0].kind == "Code39"
assert layout.pages[0].barcodes[0].polygon
assert layout.pages[0].barcodes[0].confidence > 0.8
@skip_flaky_test
@DocumentIntelligencePreparer()
@recorded_by_proxy_async
async def test_polling_interval(self, documentintelligence_endpoint, **kwargs):
client = DocumentIntelligenceClient(documentintelligence_endpoint, get_credential(is_async=True))
assert client._config.polling_interval == 1
client = DocumentIntelligenceClient(
documentintelligence_endpoint, get_credential(is_async=True), polling_interval=7
)
assert client._config.polling_interval == 7
async with client:
poller = await client.begin_analyze_document(
"prebuilt-receipt", AnalyzeDocumentRequest(url_source=self.receipt_url_jpg), polling_interval=6
)
await poller.wait()
assert poller._polling_method._timeout == 6
poller2 = await client.begin_analyze_document(
"prebuilt-receipt",
AnalyzeDocumentRequest(url_source=self.receipt_url_jpg),
)
await poller2.wait()
assert poller2._polling_method._timeout == 7 # goes back to client default
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_get_analyze_result_pdf(self, client):
with open(self.layout_sample, "rb") as fd:
document = fd.read()
poller = await client.begin_analyze_document(
"prebuilt-read",
document,
output=[AnalyzeOutputOption.PDF],
)
result = await poller.result()
response = await client.get_analyze_result_pdf(
model_id=result.model_id, result_id=poller.details["operation_id"]
)
first_chunk_pdf_bytes = await response.__anext__()
assert first_chunk_pdf_bytes.startswith(b"%PDF-") # A PDF's header is expected to be: %PDF-
@pytest.mark.live_test_only("Needs to remove sanitizer on figure id in request url.")
@DocumentIntelligencePreparer()
@DocumentIntelligenceClientPreparer()
@recorded_by_proxy_async
async def test_get_analyze_result_figures(self, client):
with open(self.layout_sample, "rb") as fd:
document = fd.read()
poller = await client.begin_analyze_document(
"prebuilt-layout",
document,
output=[AnalyzeOutputOption.FIGURES],
)
result = await poller.result()
assert result.figures is not None
figure_id = result.figures[0].id
response = await client.get_analyze_result_figure(
model_id=result.model_id, result_id=poller.details["operation_id"], figure_id=figure_id
)
first_chunk_figure_bytes = await response.__anext__()
assert first_chunk_figure_bytes.startswith(b"\x89PNG") # A PNG's header is expected to start with: ‰PNG
|