Skip to content

Commit

Permalink
add ValueError for locale + v2 and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed Oct 7, 2020
1 parent f3949d6 commit 09c0fad
Show file tree
Hide file tree
Showing 10 changed files with 5,604 additions and 11 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.

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
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.0/prebuilt/receipt/analyze?includeTextDetails=false
response:
body:
string: '{"error": {"code": "401", "message": "Access denied due to invalid
subscription key or wrong API endpoint. Make sure to provide a valid key for
an active subscription and use a correct regional API endpoint for your resource."}}'
headers:
content-length:
- '224'
date:
- Wed, 07 Oct 2020 17:54:49 GMT
status:
code: 401
message: PermissionDenied
version: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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.0/prebuilt/receipt/analyze?includeTextDetails=false
response:
body:
string: '{"error": {"code": "401", "message": "Access denied due to invalid
subscription key or wrong API endpoint. Make sure to provide a valid key for
an active subscription and use a correct regional API endpoint for your resource."}}'
headers:
content-length: '224'
date: Wed, 07 Oct 2020 17:54:48 GMT
status:
code: 401
message: PermissionDenied
url: https://centraluseuap.api.cognitive.microsoft.com/formrecognizer/v2.0/prebuilt/receipt/analyze?includeTextDetails=false
version: 1
11 changes: 10 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 @@ -450,3 +450,12 @@ def test_receipt_locale_error(self, client):
with pytest.raises(HttpResponseError) as e:
client.begin_recognize_receipts(receipt, locale="not a locale")
assert "locale" in e.value.error.message

@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)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult
from azure.ai.formrecognizer._response_handlers import prepare_receipt
from azure.ai.formrecognizer.aio import FormRecognizerClient
from azure.ai.formrecognizer import FormContentType
from azure.ai.formrecognizer import FormContentType, FormRecognizerApiVersion
from testcase import GlobalFormRecognizerAccountPreparer
from asynctestcase import AsyncFormRecognizerTest
from testcase import GlobalClientPreparer as _GlobalClientPreparer
Expand Down Expand Up @@ -482,3 +482,13 @@ async def test_receipt_locale_error(self, client):
async with client:
await client.begin_recognize_receipts(receipt, locale="not a locale")
assert "locale" in e.value.error.message

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer(client_kwargs={"api_version": FormRecognizerApiVersion.V2_0})
async def test_receipt_locale_v2(self, client):
with open(self.receipt_jpg, "rb") as fd:
receipt = fd.read()
with pytest.raises(ValueError) as e:
async with client:
await 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)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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
from azure.ai.formrecognizer import FormRecognizerClient, FormRecognizerApiVersion
from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer
from testcase import GlobalClientPreparer as _GlobalClientPreparer

Expand Down Expand Up @@ -376,3 +376,10 @@ def test_receipt_locale_error(self, client):
with pytest.raises(HttpResponseError) as e:
client.begin_recognize_receipts_from_url(self.receipt_url_jpg, locale="not a locale")
assert "locale" in e.value.error.message

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer(client_kwargs={"api_version": FormRecognizerApiVersion.V2_0})
def test_receipt_locale_v2(self, client):
with pytest.raises(ValueError) as e:
client.begin_recognize_receipts_from_url(self.receipt_url_jpg, locale="en-US")
assert "'locale' is only available for API version V2_1_PREVIEW and up" in str(e.value)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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 FormRecognizerApiVersion
from azure.ai.formrecognizer.aio import FormRecognizerClient
from testcase import GlobalFormRecognizerAccountPreparer
from asynctestcase import AsyncFormRecognizerTest
Expand Down Expand Up @@ -414,3 +415,10 @@ async def test_receipt_locale_error(self, client):
await client.begin_recognize_receipts_from_url(self.receipt_url_jpg, locale="not a locale")
assert "locale" in e.value.error.message

@GlobalFormRecognizerAccountPreparer()
@GlobalClientPreparer(client_kwargs={"api_version": FormRecognizerApiVersion.V2_0})
async def test_receipt_locale_v2(self, client):
with pytest.raises(ValueError) as e:
async with client:
await client.begin_recognize_receipts_from_url(self.receipt_url_jpg, locale="en-US")
assert "'locale' is only available for API version V2_1_PREVIEW and up" in str(e.value)

0 comments on commit 09c0fad

Please sign in to comment.