Skip to content

Commit

Permalink
[form recognizer] add receipt locale tests (#14327)
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft authored Oct 8, 2020
1 parent 0d70c4b commit de390a6
Show file tree
Hide file tree
Showing 14 changed files with 11,738 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ def begin_recognize_receipts(self, receipt, **kwargs):
if content_type is None:
content_type = get_content_type(receipt)

if self.api_version == "2.1-preview.1" and locale:
kwargs.update({"locale": locale})
# FIXME: part of this code will be removed once autorest can handle diff mixin
# signatures across API versions
if locale:
if self.api_version == "2.1-preview.1":
kwargs.update({"locale": locale})
else:
raise ValueError("'locale' is only available for API version V2_1_PREVIEW and up")

return self._client.begin_analyze_receipt_async( # type: ignore
file_stream=receipt,
Expand Down Expand Up @@ -167,8 +172,14 @@ def begin_recognize_receipts_from_url(self, receipt_url, **kwargs):
locale = kwargs.pop("locale", None)
include_field_elements = kwargs.pop("include_field_elements", False)
cls = kwargs.pop("cls", self._receipt_callback)
if self.api_version == "2.1-preview.1" and locale:
kwargs.update({"locale": locale})

# FIXME: part of this code will be removed once autorest can handle diff mixin
# signatures across API versions
if locale:
if self.api_version == "2.1-preview.1":
kwargs.update({"locale": locale})
else:
raise ValueError("'locale' is only available for API version V2_1_PREVIEW and up")
return self._client.begin_analyze_receipt_async( # type: ignore
file_stream={"source": receipt_url},
include_text_details=include_field_elements,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ async def begin_recognize_receipts(
if content_type is None:
content_type = get_content_type(receipt)

if self.api_version == "2.1-preview.1" and locale:
kwargs.update({"locale": locale})
# FIXME: part of this code will be removed once autorest can handle diff mixin
# signatures across API versions
if locale:
if self.api_version == "2.1-preview.1":
kwargs.update({"locale": locale})
else:
raise ValueError("'locale' is only available for API version V2_1_PREVIEW and up")

return await self._client.begin_analyze_receipt_async( # type: ignore
file_stream=receipt,
Expand Down Expand Up @@ -171,8 +176,13 @@ async def begin_recognize_receipts_from_url(

include_field_elements = kwargs.pop("include_field_elements", False)

if self.api_version == "2.1-preview.1" and locale:
kwargs.update({"locale": locale})
# FIXME: part of this code will be removed once autorest can handle diff mixin
# signatures across API versions
if locale:
if self.api_version == "2.1-preview.1":
kwargs.update({"locale": locale})
else:
raise ValueError("'locale' is only available for API version V2_1_PREVIEW and up")

return await self._client.begin_analyze_receipt_async( # type: ignore
file_stream={"source": receipt_url},
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
interactions:
- request:
body: 'b''{"source": "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-allinone.jpg"}'''
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '172'
Content-Type:
- application/json
User-Agent:
- azsdk-python-ai-formrecognizer/3.1.0b1 Python/3.8.5 (macOS-10.13.6-x86_64-i386-64bit)
method: POST
uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyze?includeTextDetails=false&locale=not%20a%20locale
response:
body:
string: '{"error": {"code": "UnsupportedLocale", "innerError": {"requestId":
"1bddf385-9578-4b65-8a4c-8b1d0056f4a2"}, "message": "Locale unsupported. Supported
locales include en-AU, en-CA, en-GB, en-IN and en-US."}}'
headers:
apim-request-id:
- 1bddf385-9578-4b65-8a4c-8b1d0056f4a2
content-length:
- '200'
content-type:
- application/json; charset=utf-8
date:
- Wed, 07 Oct 2020 17:00:08 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '472'
status:
code: 400
message: Bad Request
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
interactions:
- request:
body: 'b''{"source": "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-allinone.jpg"}'''
headers:
Accept:
- application/json
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '172'
Content-Type:
- application/json
User-Agent:
- azsdk-python-ai-formrecognizer/3.1.0b1 Python/3.8.5 (macOS-10.13.6-x86_64-i386-64bit)
method: POST
uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyze?includeTextDetails=false&locale=en-IN
response:
body:
string: ''
headers:
apim-request-id:
- c488f61a-c341-4156-a22e-088945bfb87d
content-length:
- '0'
date:
- Wed, 07 Oct 2020 18:21:20 GMT
operation-location:
- https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyzeResults/c488f61a-c341-4156-a22e-088945bfb87d
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '458'
status:
code: 202
message: Accepted
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- azsdk-python-ai-formrecognizer/3.1.0b1 Python/3.8.5 (macOS-10.13.6-x86_64-i386-64bit)
method: GET
uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyzeResults/c488f61a-c341-4156-a22e-088945bfb87d
response:
body:
string: '{"status": "succeeded", "createdDateTime": "2020-10-07T18:21:21Z",
"lastUpdatedDateTime": "2020-10-07T18:21:24Z", "analyzeResult": {"version":
"2.1.0", "readResults": [{"page": 1, "angle": 0.1273, "width": 1688, "height":
3000, "unit": "pixel"}], "documentResults": [{"docType": "prebuilt:receipt",
"pageRange": [1, 1], "fields": {"ReceiptType": {"type": "string", "valueString":
"Itemized", "confidence": 0.99}, "MerchantName": {"type": "string", "valueString":
"Contoso Contoso", "text": "Contoso Contoso", "boundingBox": [380.9, 282.9,
1116, 473.2, 1025, 824.4, 290, 634], "page": 1, "confidence": 0.769}, "MerchantAddress":
{"type": "string", "valueString": "123 Main Street Redmond, WA 98052", "text":
"123 Main Street Redmond, WA 98052", "boundingBox": [298.3, 676.5, 844.6,
781.5, 810.7, 958, 264.4, 853], "page": 1, "confidence": 0.99}, "MerchantPhoneNumber":
{"type": "phoneNumber", "valuePhoneNumber": "+919876543210", "text": "987-654-3210",
"boundingBox": [271, 1000, 651, 1049, 643, 1125, 264, 1075], "page": 1, "confidence":
0.995}, "TransactionDate": {"type": "date", "valueDate": "2019-10-06", "text":
"6/10/2019", "boundingBox": [259, 1224, 510, 1246, 505, 1331, 255, 1312],
"page": 1, "confidence": 0.995}, "TransactionTime": {"type": "time", "valueTime":
"13:59:00", "text": "13:59", "boundingBox": [527, 1248, 681, 1262, 675, 1341,
522, 1332], "page": 1, "confidence": 0.995}, "Items": {"type": "array", "valueArray":
[{"type": "object", "valueObject": {"Quantity": {"type": "number", "valueNumber":
1, "text": "1", "boundingBox": [244, 1581, 288, 1584, 285, 1678, 241, 1675],
"page": 1, "confidence": 0.936}, "Name": {"type": "string", "valueString":
"Cappuccino", "text": "Cappuccino", "boundingBox": [306, 1585, 658, 1598,
657, 1688, 304, 1679], "page": 1, "confidence": 0.976}, "TotalPrice": {"type":
"number", "valueNumber": 2.2, "text": "$2.20", "boundingBox": [1106, 1579,
1260, 1574, 1263, 1656, 1108, 1662], "page": 1, "confidence": 0.988}}}, {"type":
"object", "valueObject": {"Quantity": {"type": "number", "valueNumber": 1,
"text": "1", "boundingBox": [227, 1839, 278, 1839, 277, 1924, 226, 1924],
"page": 1, "confidence": 0.882}, "Name": {"type": "string", "valueString":
"BACON & EGGS Sunny-side-up", "text": "BACON & EGGS Sunny-side-up", "boundingBox":
[294, 1839, 757, 1839, 757, 2064, 294, 2064], "page": 1, "confidence": 0.506},
"TotalPrice": {"type": "number", "valueNumber": 9.5, "text": "$9.5", "boundingBox":
[1134, 1948, 1252, 1948, 1252, 2041, 1134, 2041], "page": 1, "confidence":
0.987}}}]}, "Tax": {"type": "number", "valueNumber": 1.17, "text": "1.17",
"boundingBox": [1186, 2356, 1307, 2361, 1303, 2452, 1182, 2447], "page": 1,
"confidence": 0.994}, "Total": {"type": "number", "valueNumber": 14.5, "text":
"$14.50", "boundingBox": [1033, 2623, 1376, 2641, 1370, 2758, 1027, 2740],
"page": 1, "confidence": 0.988}, "Subtotal": {"type": "number", "valueNumber":
11.7, "text": "11.70", "boundingBox": [1139, 2228, 1309, 2228, 1308, 2313,
1138, 2313], "page": 1, "confidence": 0.269}}}]}}'
headers:
apim-request-id:
- 67697a5f-f252-4214-a878-d7c0631a8894
content-length:
- '2711'
content-type:
- application/json; charset=utf-8
date:
- Wed, 07 Oct 2020 18:21:26 GMT
strict-transport-security:
- max-age=31536000; includeSubDomains; preload
x-content-type-options:
- nosniff
x-envoy-upstream-service-time:
- '19'
status:
code: 200
message: OK
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
interactions:
- request:
body: 'b''{"source": "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-allinone.jpg"}'''
headers:
Accept:
- application/json
Content-Length:
- '172'
Content-Type:
- application/json
User-Agent:
- azsdk-python-ai-formrecognizer/3.1.0b1 Python/3.8.5 (macOS-10.13.6-x86_64-i386-64bit)
method: POST
uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyze?includeTextDetails=false&locale=not%20a%20locale
response:
body:
string: '{"error": {"code": "UnsupportedLocale", "innerError": {"requestId":
"cffca367-a746-4559-b678-e292a9932ea3"}, "message": "Locale unsupported. Supported
locales include en-AU, en-CA, en-GB, en-IN and en-US."}}'
headers:
apim-request-id: cffca367-a746-4559-b678-e292a9932ea3
content-length: '200'
content-type: application/json; charset=utf-8
date: Wed, 07 Oct 2020 17:00:08 GMT
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
x-envoy-upstream-service-time: '331'
status:
code: 400
message: Bad Request
url: https://centraluseuap.api.cognitive.microsoft.com//formrecognizer/v2.1-preview.1/prebuilt/receipt/analyze?includeTextDetails=false&locale=not%20a%20locale
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
interactions:
- request:
body: 'b''{"source": "https://raw.githubusercontent.com/Azure/azure-sdk-for-python/master/sdk/formrecognizer/azure-ai-formrecognizer/tests/sample_forms/receipt/contoso-allinone.jpg"}'''
headers:
Accept:
- application/json
Content-Length:
- '172'
Content-Type:
- application/json
User-Agent:
- azsdk-python-ai-formrecognizer/3.1.0b1 Python/3.8.5 (macOS-10.13.6-x86_64-i386-64bit)
method: POST
uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyze?includeTextDetails=false&locale=en-IN
response:
body:
string: ''
headers:
apim-request-id: 306c9122-82a7-4aba-aa06-3b72db71c6e3
content-length: '0'
date: Wed, 07 Oct 2020 18:21:26 GMT
operation-location: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyzeResults/306c9122-82a7-4aba-aa06-3b72db71c6e3
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
x-envoy-upstream-service-time: '296'
status:
code: 202
message: Accepted
url: https://centraluseuap.api.cognitive.microsoft.com//formrecognizer/v2.1-preview.1/prebuilt/receipt/analyze?includeTextDetails=false&locale=en-IN
- request:
body: null
headers:
User-Agent:
- azsdk-python-ai-formrecognizer/3.1.0b1 Python/3.8.5 (macOS-10.13.6-x86_64-i386-64bit)
method: GET
uri: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyzeResults/306c9122-82a7-4aba-aa06-3b72db71c6e3
response:
body:
string: '{"status": "succeeded", "createdDateTime": "2020-10-07T18:21:27Z",
"lastUpdatedDateTime": "2020-10-07T18:21:34Z", "analyzeResult": {"version":
"2.1.0", "readResults": [{"page": 1, "angle": 0.1273, "width": 1688, "height":
3000, "unit": "pixel"}], "documentResults": [{"docType": "prebuilt:receipt",
"pageRange": [1, 1], "fields": {"ReceiptType": {"type": "string", "valueString":
"Itemized", "confidence": 0.99}, "MerchantName": {"type": "string", "valueString":
"Contoso Contoso", "text": "Contoso Contoso", "boundingBox": [380.9, 282.9,
1116, 473.2, 1025, 824.4, 290, 634], "page": 1, "confidence": 0.769}, "MerchantAddress":
{"type": "string", "valueString": "123 Main Street Redmond, WA 98052", "text":
"123 Main Street Redmond, WA 98052", "boundingBox": [298.3, 676.5, 844.6,
781.5, 810.7, 958, 264.4, 853], "page": 1, "confidence": 0.99}, "MerchantPhoneNumber":
{"type": "phoneNumber", "valuePhoneNumber": "+919876543210", "text": "987-654-3210",
"boundingBox": [271, 1000, 651, 1049, 643, 1125, 264, 1075], "page": 1, "confidence":
0.995}, "TransactionDate": {"type": "date", "valueDate": "2019-10-06", "text":
"6/10/2019", "boundingBox": [259, 1224, 510, 1246, 505, 1331, 255, 1312],
"page": 1, "confidence": 0.995}, "TransactionTime": {"type": "time", "valueTime":
"13:59:00", "text": "13:59", "boundingBox": [527, 1248, 681, 1262, 675, 1341,
522, 1332], "page": 1, "confidence": 0.995}, "Items": {"type": "array", "valueArray":
[{"type": "object", "valueObject": {"Quantity": {"type": "number", "valueNumber":
1, "text": "1", "boundingBox": [244, 1581, 288, 1584, 285, 1678, 241, 1675],
"page": 1, "confidence": 0.936}, "Name": {"type": "string", "valueString":
"Cappuccino", "text": "Cappuccino", "boundingBox": [306, 1585, 658, 1598,
657, 1688, 304, 1679], "page": 1, "confidence": 0.976}, "TotalPrice": {"type":
"number", "valueNumber": 2.2, "text": "$2.20", "boundingBox": [1106, 1579,
1260, 1574, 1263, 1656, 1108, 1662], "page": 1, "confidence": 0.988}}}, {"type":
"object", "valueObject": {"Quantity": {"type": "number", "valueNumber": 1,
"text": "1", "boundingBox": [227, 1839, 278, 1839, 277, 1924, 226, 1924],
"page": 1, "confidence": 0.882}, "Name": {"type": "string", "valueString":
"BACON & EGGS Sunny-side-up", "text": "BACON & EGGS Sunny-side-up", "boundingBox":
[294, 1839, 757, 1839, 757, 2064, 294, 2064], "page": 1, "confidence": 0.506},
"TotalPrice": {"type": "number", "valueNumber": 9.5, "text": "$9.5", "boundingBox":
[1134, 1948, 1252, 1948, 1252, 2041, 1134, 2041], "page": 1, "confidence":
0.987}}}]}, "Tax": {"type": "number", "valueNumber": 1.17, "text": "1.17",
"boundingBox": [1186, 2356, 1307, 2361, 1303, 2452, 1182, 2447], "page": 1,
"confidence": 0.994}, "Total": {"type": "number", "valueNumber": 14.5, "text":
"$14.50", "boundingBox": [1033, 2623, 1376, 2641, 1370, 2758, 1027, 2740],
"page": 1, "confidence": 0.988}, "Subtotal": {"type": "number", "valueNumber":
11.7, "text": "11.70", "boundingBox": [1139, 2228, 1309, 2228, 1308, 2313,
1138, 2313], "page": 1, "confidence": 0.269}}}]}}'
headers:
apim-request-id: 3adda0b7-a2af-41d8-ad97-7e81ed615f62
content-length: '2711'
content-type: application/json; charset=utf-8
date: Wed, 07 Oct 2020 18:21:31 GMT
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
x-envoy-upstream-service-time: '15'
status:
code: 200
message: OK
url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.1-preview.1/prebuilt/receipt/analyzeResults/306c9122-82a7-4aba-aa06-3b72db71c6e3
version: 1
29 changes: 28 additions & 1 deletion sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult
from azure.ai.formrecognizer._response_handlers import prepare_receipt
from azure.ai.formrecognizer import FormRecognizerClient, FormContentType
from azure.ai.formrecognizer import FormRecognizerClient, FormContentType, FormRecognizerApiVersion
from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer
from testcase import GlobalClientPreparer as _GlobalClientPreparer

Expand Down Expand Up @@ -421,3 +421,30 @@ def test_receipt_continuation_token(self, client):
result = poller.result()
self.assertIsNotNone(result)
initial_poller.wait() # necessary so azure-devtools doesn't throw assertion error

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
def test_receipt_locale_specified(self, client):
with open(self.receipt_jpg, "rb") as fd:
receipt = fd.read()
poller = client.begin_recognize_receipts(receipt, locale="en-IN")
assert 'en-IN' == poller._polling_method._initial_response.http_response.request.query['locale']
poller.wait()

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer()
def test_receipt_locale_error(self, client):
with open(self.receipt_jpg, "rb") as fd:
receipt = fd.read()
with pytest.raises(HttpResponseError) as e:
client.begin_recognize_receipts(receipt, locale="not a locale")
assert "UnsupportedLocale" == e.value.error.code

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer(client_kwargs={"api_version": FormRecognizerApiVersion.V2_0})
def test_receipt_locale_v2(self, client):
with open(self.receipt_jpg, "rb") as fd:
receipt = fd.read()
with pytest.raises(ValueError) as e:
client.begin_recognize_receipts(receipt, locale="en-US")
assert "'locale' is only available for API version V2_1_PREVIEW and up" in str(e.value)
Loading

0 comments on commit de390a6

Please sign in to comment.