diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index a740919023f1..6fe1439c6791 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -22,6 +22,9 @@ `CustomFormModel` and `CustomFormModelInfo` models. - `models` property of `CustomFormModel` is renamed to `submodels` - `CustomFormSubModel` is renamed to `CustomFormSubmodel` +- Removed `USReceipt`. To see how to deal with the return value of `begin_recognize_receipts`, see the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. +- Removed `USReceiptItem`. To see how to access the individual items on a receipt, see the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. +- Removed `ReceiptType` and the `receipt_type` property from `RecognizedReceipt`. See the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. **New features** diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/README.md b/sdk/formrecognizer/azure-ai-formrecognizer/README.md index 6f562db53da0..a304148bd79a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/README.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/README.md @@ -210,7 +210,7 @@ for cell in table.cells: ``` ### Recognize Receipts -Recognize data from USA sales receipts using a prebuilt model. +Recognize data from USA sales receipts using a prebuilt model. [Here][service_recognize_receipt] are the fields the service returns for a recognized receipt. ```python from azure.ai.formrecognizer import FormRecognizerClient @@ -227,21 +227,16 @@ with open("", "rb") as fd: poller = form_recognizer_client.begin_recognize_receipts(receipt) result = poller.result() -r = result[0] -print("Receipt contained the following values with confidences: ") -print("Receipt Type: {} has confidence: {}".format(r.receipt_type.type, r.receipt_type.confidence)) -print("Merchant Name: {} has confidence: {}".format(r.merchant_name.value, r.merchant_name.confidence)) -print("Transaction Date: {} has confidence: {}".format(r.transaction_date.value, r.transaction_date.confidence)) -print("Receipt items:") -for item in r.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) -print("Subtotal: {} has confidence: {}".format(r.subtotal.value, r.subtotal.confidence)) -print("Tax: {} has confidence: {}".format(r.tax.value, r.tax.confidence)) -print("Tip: {} has confidence: {}".format(r.tip.value, r.tip.confidence)) -print("Total: {} has confidence: {}".format(r.total.value, r.total.confidence)) +for receipt in result: + for name, field in receipt.fields.items(): + if name == "Items": + print("Receipt Items:") + for idx, items in enumerate(field.value): + print("...Item #{}".format(idx)) + for item_name, item in items.value.items(): + print("......{}: {} has confidence {}".format(item_name, item.value, item.confidence)) + else: + print("{}: {} has confidence {}".format(name, field.value, field.confidence)) ``` ### Train a model @@ -439,6 +434,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con [cognitive_authentication_aad]: https://docs.microsoft.com/azure/cognitive-services/authentication#authenticate-with-azure-active-directory [azure_identity_credentials]: ../../identity/azure-identity#credentials [default_azure_credential]: ../../identity/azure-identity#defaultazurecredential +[service_recognize_receipt]: https://westus2.dev.cognitive.microsoft.com/docs/services/form-recognizer-api-v2-preview/operations/GetAnalyzeReceiptResult [cla]: https://cla.microsoft.com [code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py index f65b9e8e92af..dd86f0f42e41 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py @@ -14,9 +14,6 @@ TrainingStatus, CustomFormModelStatus, FormContentType, - USReceipt, - ReceiptType, - USReceiptItem, FormTable, FormTableCell, TrainingDocumentInfo, @@ -45,9 +42,6 @@ 'CustomFormModelStatus', 'FormContentType', 'FormContent', - 'USReceipt', - 'ReceiptType', - 'USReceiptItem', 'FormTable', 'FormTableCell', 'TrainingDocumentInfo', diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py index d3d2173442aa..ac2bb7cd304f 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py @@ -17,7 +17,7 @@ from azure.core.polling.base_polling import LROBasePolling from ._generated._form_recognizer_client import FormRecognizerClient as FormRecognizer from ._response_handlers import ( - prepare_us_receipt, + prepare_receipt, prepare_content_result, prepare_form_result ) @@ -74,7 +74,7 @@ def __init__(self, endpoint, credential, **kwargs): def _receipt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response) - return prepare_us_receipt(analyze_result) + return prepare_receipt(analyze_result) @distributed_trace def begin_recognize_receipts(self, receipt, **kwargs): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py index cf61dd1038f4..b15c7d89866b 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py @@ -195,82 +195,13 @@ class RecognizedReceipt(RecognizedForm): :ivar list[~azure.ai.formrecognizer.FormPage] pages: A list of pages recognized from the input document. Contains lines, words, tables and page metadata. - :ivar ~azure.ai.formrecognizer.ReceiptType receipt_type: - The reciept type and confidence. - :ivar str receipt_locale: Defaults to "en-US". """ - def __init__(self, **kwargs): - super(RecognizedReceipt, self).__init__(**kwargs) - self.receipt_type = kwargs.get("receipt_type", None) - self.receipt_locale = kwargs.get("receipt_locale", "en-US") def __repr__(self): - return "RecognizedReceipt(form_type={}, fields={}, page_range={}, pages={}, " \ - "receipt_type={}, receipt_locale={})".format( - self.form_type, repr(self.fields), repr(self.page_range), repr(self.pages), - repr(self.receipt_type), self.receipt_locale + return "RecognizedReceipt(form_type={}, fields={}, page_range={}, pages={})".format( + self.form_type, repr(self.fields), repr(self.page_range), repr(self.pages) )[:1024] -class USReceipt(RecognizedReceipt): # pylint: disable=too-many-instance-attributes - """Extracted fields found on the US sales receipt. Provides - attributes for accessing common fields present in US sales receipts. - - :ivar ~azure.ai.formrecognizer.FormField merchant_address: - The address of the merchant. - :ivar ~azure.ai.formrecognizer.FormField merchant_name: - The name of the merchant. - :ivar ~azure.ai.formrecognizer.FormField merchant_phone_number: - The phone number associated with the merchant. - :ivar list[~azure.ai.formrecognizer.USReceiptItem] receipt_items: - The purchased items found on the receipt. - :ivar ~azure.ai.formrecognizer.FormField subtotal: - The subtotal found on the receipt - :ivar ~azure.ai.formrecognizer.FormField tax: - The tax value found on the receipt. - :ivar ~azure.ai.formrecognizer.FormField tip: - The tip value found on the receipt. - :ivar ~azure.ai.formrecognizer.FormField total: - The total amount found on the receipt. - :ivar ~azure.ai.formrecognizer.FormField transaction_date: - The transaction date of the sale. - :ivar ~azure.ai.formrecognizer.FormField transaction_time: - The transaction time of the sale. - :ivar fields: - A dictionary of the fields found on the receipt. - :vartype fields: dict[str, ~azure.ai.formrecognizer.FormField] - :ivar ~azure.ai.formrecognizer.FormPageRange page_range: - The first and last page number of the input receipt. - :ivar list[~azure.ai.formrecognizer.FormPage] pages: - Contains page metadata such as page width, length, text angle, unit. - If `include_text_content=True` is passed, contains a list - of extracted text lines for each page in the input document. - :ivar str form_type: The type of form. - """ - - def __init__(self, **kwargs): - super(USReceipt, self).__init__(**kwargs) - self.merchant_address = kwargs.get("merchant_address", None) - self.merchant_name = kwargs.get("merchant_name", None) - self.merchant_phone_number = kwargs.get("merchant_phone_number", None) - self.receipt_items = kwargs.get("receipt_items", None) - self.subtotal = kwargs.get("subtotal", None) - self.tax = kwargs.get("tax", None) - self.tip = kwargs.get("tip", None) - self.total = kwargs.get("total", None) - self.transaction_date = kwargs.get("transaction_date", None) - self.transaction_time = kwargs.get("transaction_time", None) - - def __repr__(self): - return "USReceipt(merchant_address={}, merchant_name={}, merchant_phone_number={}, " \ - "receipt_type={}, receipt_items={}, subtotal={}, tax={}, tip={}, total={}, "\ - "transaction_date={}, transaction_time={}, fields={}, page_range={}, pages={}, " \ - "form_type={}, receipt_locale={})".format( - repr(self.merchant_address), repr(self.merchant_name), repr(self.merchant_phone_number), - repr(self.receipt_type), repr(self.receipt_items), repr(self.subtotal), repr(self.tax), - repr(self.tip), repr(self.total), repr(self.transaction_date), repr(self.transaction_time), - repr(self.fields), repr(self.page_range), repr(self.pages), self.form_type, self.receipt_locale - )[:1024] - class FormField(object): """Represents a field recognized in an input form. @@ -513,69 +444,6 @@ def __repr__(self): )[:1024] -class ReceiptType(object): - """The type of the analyzed US receipt and the confidence - value of that type. - - :ivar str type: The type of the receipt. For example, "Itemized", - "CreditCard", "Gas", "Parking", "Gas", "Other". - :ivar float confidence: - Measures the degree of certainty of the recognition result. Value is between [0.0, 1.0]. - """ - - def __init__(self, **kwargs): - self.type = kwargs.get("type", None) - self.confidence = kwargs.get("confidence", None) - - @classmethod - def _from_generated(cls, item): - return cls( - type=item.value_string, - confidence=adjust_confidence(item.confidence)) if item else None - - def __repr__(self): - return "ReceiptType(type={}, confidence={})".format(self.type, self.confidence)[:1024] - - -class USReceiptItem(object): - """A receipt item on a US sales receipt. - Contains the item name, quantity, price, and total price. - - :ivar ~azure.ai.formrecognizer.FormField name: - The name of the item. - :ivar ~azure.ai.formrecognizer.FormField quantity: - The quantity associated with this item. - :ivar ~azure.ai.formrecognizer.FormField price: - The price of a single unit of this item. - :ivar ~azure.ai.formrecognizer.FormField total_price: - The total price of this item, taking the quantity into account. - """ - - def __init__(self, **kwargs): - self.name = kwargs.get("name", None) - self.quantity = kwargs.get("quantity", None) - self.price = kwargs.get("price", None) - self.total_price = kwargs.get("total_price", None) - - @classmethod - def _from_generated(cls, items, read_result): - try: - receipt_item = items.value_array - return [cls( - name=FormField._from_generated("Name", item.value_object.get("Name"), read_result), - quantity=FormField._from_generated("Quantity", item.value_object.get("Quantity"), read_result), - price=FormField._from_generated("Price", item.value_object.get("Price"), read_result), - total_price=FormField._from_generated("TotalPrice", item.value_object.get("TotalPrice"), read_result), - ) for item in receipt_item] - except AttributeError: - return [] - - def __repr__(self): - return "USReceiptItem(name={}, quantity={}, price={}, total_price={})".format( - repr(self.name), repr(self.quantity), repr(self.price), repr(self.total_price) - )[:1024] - - class FormTable(object): """Information about the extracted table contained on a page. diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py index 08d8fa9d9461..90fe97582902 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py @@ -7,20 +7,18 @@ # pylint: disable=protected-access from ._models import ( - USReceipt, - ReceiptType, FormField, - USReceiptItem, FormPage, FormLine, FormTable, FormTableCell, FormPageRange, - RecognizedForm + RecognizedForm, + RecognizedReceipt ) -def prepare_us_receipt(response): +def prepare_receipt(response): receipts = [] read_result = response.analyze_result.read_results document_result = response.analyze_result.document_results @@ -28,43 +26,14 @@ def prepare_us_receipt(response): for page in document_result: if page.fields is None: - receipt = USReceipt( + receipt = RecognizedReceipt( page_range=FormPageRange(first_page_number=page.page_range[0], last_page_number=page.page_range[1]), pages=form_page[page.page_range[0]-1:page.page_range[1]], form_type=page.doc_type, ) receipts.append(receipt) continue - receipt = USReceipt( - merchant_address=FormField._from_generated( - "MerchantAddress", page.fields.get("MerchantAddress"), read_result - ), - merchant_name=FormField._from_generated( - "MerchantName", page.fields.get("MerchantName"), read_result - ), - merchant_phone_number=FormField._from_generated( - "MerchantPhoneNumber", - page.fields.get("MerchantPhoneNumber"), - read_result, - ), - receipt_type=ReceiptType._from_generated(page.fields.get("ReceiptType")), - receipt_items=USReceiptItem._from_generated( - page.fields.get("Items"), read_result - ), - subtotal=FormField._from_generated( - "Subtotal", page.fields.get("Subtotal"), read_result - ), - tax=FormField._from_generated("Tax", page.fields.get("Tax"), read_result), - tip=FormField._from_generated("Tip", page.fields.get("Tip"), read_result), - total=FormField._from_generated( - "Total", page.fields.get("Total"), read_result - ), - transaction_date=FormField._from_generated( - "TransactionDate", page.fields.get("TransactionDate"), read_result - ), - transaction_time=FormField._from_generated( - "TransactionTime", page.fields.get("TransactionTime"), read_result - ), + receipt = RecognizedReceipt( page_range=FormPageRange( first_page_number=page.page_range[0], last_page_number=page.page_range[1] ), @@ -73,7 +42,7 @@ def prepare_us_receipt(response): fields={ key: FormField._from_generated(key, value, read_result) for key, value in page.fields.items() - }, + } ) receipts.append(receipt) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py index 8e0a4dd81d56..ae35b3c275c6 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py @@ -17,7 +17,7 @@ from azure.core.polling.async_base_polling import AsyncLROBasePolling from .._generated.aio._form_recognizer_client_async import FormRecognizerClient as FormRecognizer from .._response_handlers import ( - prepare_us_receipt, + prepare_receipt, prepare_content_result, prepare_form_result ) @@ -84,7 +84,7 @@ def __init__( def _receipt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response) - return prepare_us_receipt(analyze_result) + return prepare_receipt(analyze_result) @distributed_trace_async async def recognize_receipts( diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py index 2e41b244a5e3..3bca0eb2549d 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py @@ -45,19 +45,42 @@ async def recognize_receipts(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) - print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) - print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) - print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) + receipt_type = receipt.fields.get("ReceiptType") + if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) + merchant_name = receipt.fields.get("MerchantName") + if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) + transaction_date = receipt.fields.get("TransactionDate") + if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) - print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) - print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) - print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) - print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) + for idx, item in enumerate(receipt.fields.get("Items").value): + print("...Item #{}".format(idx)) + item_name = item.value.get("Name") + if item_name: + print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + subtotal = receipt.fields.get("Subtotal") + if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) + tax = receipt.fields.get("Tax") + if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) + tip = receipt.fields.get("Tip") + if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) + total = receipt.fields.get("Total") + if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------") # [END recognize_receipts_async] diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py index 8831e076fc69..3559261094fb 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py @@ -42,19 +42,42 @@ async def recognize_receipts_from_url(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) - print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) - print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) - print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) + receipt_type = receipt.fields.get("ReceiptType") + if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) + merchant_name = receipt.fields.get("MerchantName") + if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) + transaction_date = receipt.fields.get("TransactionDate") + if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) - print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) - print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) - print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) - print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) + for idx, item in enumerate(receipt.fields.get("Items").value): + print("...Item #{}".format(idx)) + item_name = item.value.get("Name") + if item_name: + print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + subtotal = receipt.fields.get("Subtotal") + if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) + tax = receipt.fields.get("Tax") + if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) + tip = receipt.fields.get("Tip") + if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) + total = receipt.fields.get("Total") + if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------") # [END recognize_receipts_from_url_async] diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py index b321ef76553e..a53c04497daa 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py @@ -42,19 +42,42 @@ def recognize_receipts(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) - print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) - print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) - print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) + receipt_type = receipt.fields.get("ReceiptType") + if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) + merchant_name = receipt.fields.get("MerchantName") + if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) + transaction_date = receipt.fields.get("TransactionDate") + if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) - print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) - print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) - print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) - print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) + for idx, item in enumerate(receipt.fields.get("Items").value): + print("...Item #{}".format(idx)) + item_name = item.value.get("Name") + if item_name: + print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + subtotal = receipt.fields.get("Subtotal") + if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) + tax = receipt.fields.get("Tax") + if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) + tip = receipt.fields.get("Tip") + if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) + total = receipt.fields.get("Total") + if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------") # [END recognize_receipts] diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py index 7ccd5c58586a..f3b333c4d7f1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py @@ -42,19 +42,42 @@ def recognize_receipts_from_url(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) - print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) - print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) - print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) + receipt_type = receipt.fields.get("ReceiptType") + if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) + merchant_name = receipt.fields.get("MerchantName") + if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) + transaction_date = receipt.fields.get("TransactionDate") + if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) - print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) - print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) - print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) - print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) + for idx, item in enumerate(receipt.fields.get("Items").value): + print("...Item #{}".format(idx)) + item_name = item.value.get("Name") + if item_name: + print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + subtotal = receipt.fields.get("Subtotal") + if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) + tax = receipt.fields.get("Tax") + if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) + tip = receipt.fields.get("Tip") + if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) + total = receipt.fields.get("Total") + if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------") # [END recognize_receipts_from_url] diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py index e7a605036e63..c1296b292cbb 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py @@ -9,7 +9,7 @@ from azure.core.exceptions import ClientAuthenticationError, ServiceRequestError, HttpResponseError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult -from azure.ai.formrecognizer._response_handlers import prepare_us_receipt +from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer import FormRecognizerClient, FormContentType from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer @@ -136,7 +136,7 @@ def test_receipt_stream_transform_png(self, resource_group, location, form_recog def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -157,17 +157,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -184,11 +173,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -201,7 +191,7 @@ def test_receipt_stream_transform_jpg(self, resource_group, location, form_recog def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -223,17 +213,6 @@ def callback(raw_response, _, headers): document_results = raw_response.analyze_result.document_results page_results = raw_response.analyze_result.page_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -250,11 +229,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -271,21 +251,22 @@ def test_receipt_jpg(self, resource_group, location, form_recognizer_account, fo result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.merchant_phone_number.value, '+19876543210') - self.assertEqual(receipt.subtotal.value, 11.7) - self.assertEqual(receipt.tax.value, 1.17) - self.assertEqual(receipt.tip.value, 1.63) - self.assertEqual(receipt.total.value, 14.5) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') + self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) + self.assertEqual(receipt.fields.get("Tax").value, 1.17) + self.assertEqual(receipt.fields.get("Tip").value, 1.63) + self.assertEqual(receipt.fields.get("Total").value, 14.5) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') - self.assertReceiptItemsHasValues(receipt.receipt_items, receipt.page_range.first_page_number, False) + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') + self.assertReceiptItemsHasValues(receipt.fields['Items'].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() def test_receipt_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -297,18 +278,19 @@ def test_receipt_png(self, resource_group, location, form_recognizer_account, fo result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.subtotal.value, 1098.99) - self.assertEqual(receipt.tax.value, 104.4) - self.assertEqual(receipt.total.value, 1203.39) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("Subtotal").value, 1098.99) + self.assertEqual(receipt.fields.get("Tax").value, 104.4) + self.assertEqual(receipt.fields.get("Total").value, 1203.39) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() def test_receipt_jpg_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -323,7 +305,7 @@ def test_receipt_jpg_include_text_content(self, resource_group, location, form_r self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale"]: + if field not in ["receipt_items", "page_range", "pages", "fields", "form_type"]: form_field = getattr(receipt, field) self.assertTextContentHasValues(form_field.value_data.text_content, receipt.page_range.first_page_number) @@ -340,27 +322,29 @@ def test_receipt_multipage(self, resource_group, location, form_recognizer_accou self.assertEqual(len(result), 3) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 300.0) - self.assertEqual(receipt.total.value, 100.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Bilbo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 300.0) + self.assertEqual(receipt.fields.get("Total").value, 100.0) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') receipt = result[2] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 3000.0) - self.assertEqual(receipt.total.value, 1000.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 3000.0) + self.assertEqual(receipt.fields.get("Total").value, 1000.0) self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() def test_receipt_multipage_transform(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -370,7 +354,7 @@ def test_receipt_multipage_transform(self, resource_group, location, form_recogn def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -395,15 +379,6 @@ def callback(raw_response, _, headers): for receipt, actual in zip(returned_model, actual): if actual.fields is None: # second page is blank continue - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.fields.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.fields.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.fields.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.fields.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.fields.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.fields.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.fields.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.fields.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.fields.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) @@ -421,11 +396,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual.fields["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual.fields["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py index f496ee796fb5..3cfd22fc875d 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py @@ -9,7 +9,7 @@ from azure.core.exceptions import ServiceRequestError, ClientAuthenticationError, HttpResponseError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult -from azure.ai.formrecognizer._response_handlers import prepare_us_receipt +from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer.aio import FormRecognizerClient from azure.ai.formrecognizer import FormContentType from testcase import GlobalFormRecognizerAccountPreparer @@ -135,7 +135,7 @@ async def test_receipt_stream_transform_png(self, resource_group, location, form def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -155,17 +155,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -182,11 +171,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -199,7 +189,7 @@ async def test_receipt_stream_transform_jpg(self, resource_group, location, form def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -220,17 +210,6 @@ def callback(raw_response, _, headers): document_results = raw_response.analyze_result.document_results page_results = raw_response.analyze_result.page_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -247,11 +226,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -267,21 +247,22 @@ async def test_receipt_jpg(self, resource_group, location, form_recognizer_accou self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.merchant_phone_number.value, '+19876543210') - self.assertEqual(receipt.subtotal.value, 11.7) - self.assertEqual(receipt.tax.value, 1.17) - self.assertEqual(receipt.tip.value, 1.63) - self.assertEqual(receipt.total.value, 14.5) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') + self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) + self.assertEqual(receipt.fields.get("Tax").value, 1.17) + self.assertEqual(receipt.fields.get("Tip").value, 1.63) + self.assertEqual(receipt.fields.get("Total").value, 14.5) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') - self.assertReceiptItemsHasValues(receipt.receipt_items, receipt.page_range.first_page_number, False) + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') + self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() async def test_receipt_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -293,18 +274,19 @@ async def test_receipt_png(self, resource_group, location, form_recognizer_accou result = await client.recognize_receipts(receipt) self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.subtotal.value, 1098.99) - self.assertEqual(receipt.tax.value, 104.4) - self.assertEqual(receipt.total.value, 1203.39) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("Subtotal").value, 1098.99) + self.assertEqual(receipt.fields.get("Tax").value, 104.4) + self.assertEqual(receipt.fields.get("Total").value, 1203.39) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() async def test_receipt_jpg_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -318,7 +300,7 @@ async def test_receipt_jpg_include_text_content(self, resource_group, location, self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale"]: + if field not in ["receipt_items", "page_range", "pages", "fields", "form_type"]: form_field = getattr(receipt, field) self.assertTextContentHasValues(form_field.value_data.text_content, receipt.page_range.first_page_number) @@ -334,27 +316,29 @@ async def test_receipt_multipage(self, resource_group, location, form_recognizer self.assertEqual(len(result), 3) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 300.0) - self.assertEqual(receipt.total.value, 100.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Bilbo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 300.0) + self.assertEqual(receipt.fields.get("Total").value, 100.0) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') receipt = result[2] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 3000.0) - self.assertEqual(receipt.total.value, 1000.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 3000.0) + self.assertEqual(receipt.fields.get("Total").value, 1000.0) self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() async def test_receipt_multipage_transform(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -364,7 +348,7 @@ async def test_receipt_multipage_transform(self, resource_group, location, form_ def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -388,15 +372,6 @@ def callback(raw_response, _, headers): for receipt, actual in zip(returned_model, actual): if actual.fields is None: # second page is blank continue - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.fields.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.fields.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.fields.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.fields.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.fields.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.fields.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.fields.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.fields.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.fields.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) @@ -414,11 +389,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual.fields["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual.fields["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py index 9771d8255746..2ab8315d9077 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py @@ -9,7 +9,7 @@ from azure.core.exceptions import HttpResponseError, ServiceRequestError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult -from azure.ai.formrecognizer._response_handlers import prepare_us_receipt +from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer import FormRecognizerClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer @@ -65,7 +65,7 @@ def test_receipt_url_transform_jpg(self, resource_group, location, form_recogniz def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -83,17 +83,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -110,11 +99,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -127,7 +117,7 @@ def test_receipt_url_transform_png(self, resource_group, location, form_recogniz def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -145,17 +135,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -172,11 +151,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -196,7 +176,7 @@ def test_receipt_url_include_text_content(self, resource_group, location, form_r self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale"]: + if field not in ["receipt_items", "page_range", "pages", "fields", "form_type"]: field = getattr(receipt, field) self.assertTextContentHasValues(field.value_data.text_content, receipt.page_range.first_page_number) @@ -212,21 +192,22 @@ def test_receipt_url_jpg(self, resource_group, location, form_recognizer_account result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.merchant_phone_number.value, '+19876543210') - self.assertEqual(receipt.subtotal.value, 11.7) - self.assertEqual(receipt.tax.value, 1.17) - self.assertEqual(receipt.tip.value, 1.63) - self.assertEqual(receipt.total.value, 14.5) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') + self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) + self.assertEqual(receipt.fields.get("Tax").value, 1.17) + self.assertEqual(receipt.fields.get("Tip").value, 1.63) + self.assertEqual(receipt.fields.get("Total").value, 14.5) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') - self.assertReceiptItemsHasValues(receipt.receipt_items, receipt.page_range.first_page_number, False) + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') + self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() def test_receipt_url_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -237,18 +218,19 @@ def test_receipt_url_png(self, resource_group, location, form_recognizer_account result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.subtotal.value, 1098.99) - self.assertEqual(receipt.tax.value, 104.4) - self.assertEqual(receipt.total.value, 1203.39) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("Subtotal").value, 1098.99) + self.assertEqual(receipt.fields.get("Tax").value, 104.4) + self.assertEqual(receipt.fields.get("Total").value, 1203.39) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() def test_receipt_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -259,27 +241,29 @@ def test_receipt_multipage_url(self, resource_group, location, form_recognizer_a self.assertEqual(len(result), 3) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 300.0) - self.assertEqual(receipt.total.value, 100.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Bilbo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 300.0) + self.assertEqual(receipt.fields.get("Total").value, 100.0) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') receipt = result[2] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 3000.0) - self.assertEqual(receipt.total.value, 1000.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 3000.0) + self.assertEqual(receipt.fields.get("Total").value, 1000.0) self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() def test_receipt_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -289,7 +273,7 @@ def test_receipt_multipage_transform_url(self, resource_group, location, form_re def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -311,15 +295,6 @@ def callback(raw_response, _, headers): for receipt, actual in zip(returned_model, actual): if actual.fields is None: # second page is blank continue - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.fields.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.fields.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.fields.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.fields.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.fields.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.fields.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.fields.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.fields.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.fields.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) @@ -337,11 +312,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual.fields["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual.fields["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py index 3c95d8cf8d9d..d3981db07f21 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py @@ -9,7 +9,7 @@ from azure.core.exceptions import HttpResponseError, ServiceRequestError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult -from azure.ai.formrecognizer._response_handlers import prepare_us_receipt +from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer.aio import FormRecognizerClient from testcase import GlobalFormRecognizerAccountPreparer from asynctestcase import AsyncFormRecognizerTest @@ -75,7 +75,7 @@ async def test_receipt_url_transform_jpg(self, resource_group, location, form_re def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -92,17 +92,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -119,11 +108,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -136,7 +126,7 @@ async def test_receipt_url_transform_png(self, resource_group, location, form_re def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -153,17 +143,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -180,11 +159,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -203,7 +183,7 @@ async def test_receipt_url_include_text_content(self, resource_group, location, self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale"]: + if field not in ["page_range", "pages", "fields", "form_type"]: field = getattr(receipt, field) self.assertTextContentHasValues(field.value_data.text_content, receipt.page_range.first_page_number) @@ -220,21 +200,22 @@ async def test_receipt_url_jpg(self, resource_group, location, form_recognizer_a self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.merchant_phone_number.value, '+19876543210') - self.assertEqual(receipt.subtotal.value, 11.7) - self.assertEqual(receipt.tax.value, 1.17) - self.assertEqual(receipt.tip.value, 1.63) - self.assertEqual(receipt.total.value, 14.5) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') + self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) + self.assertEqual(receipt.fields.get("Tax").value, 1.17) + self.assertEqual(receipt.fields.get("Tip").value, 1.63) + self.assertEqual(receipt.fields.get("Total").value, 14.5) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') - self.assertReceiptItemsHasValues(receipt.receipt_items, receipt.page_range.first_page_number, False) + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') + self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() async def test_receipt_url_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -244,18 +225,19 @@ async def test_receipt_url_png(self, resource_group, location, form_recognizer_a self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.subtotal.value, 1098.99) - self.assertEqual(receipt.tax.value, 104.4) - self.assertEqual(receipt.total.value, 1203.39) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("Subtotal").value, 1098.99) + self.assertEqual(receipt.fields.get("Tax").value, 104.4) + self.assertEqual(receipt.fields.get("Total").value, 1203.39) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() async def test_receipt_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -265,27 +247,29 @@ async def test_receipt_multipage_url(self, resource_group, location, form_recogn self.assertEqual(len(result), 3) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 300.0) - self.assertEqual(receipt.total.value, 100.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Bilbo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 300.0) + self.assertEqual(receipt.fields.get("Total").value, 100.0) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') receipt = result[2] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 3000.0) - self.assertEqual(receipt.total.value, 1000.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 3000.0) + self.assertEqual(receipt.fields.get("Total").value, 1000.0) self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() async def test_receipt_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -295,7 +279,7 @@ async def test_receipt_multipage_transform_url(self, resource_group, location, f def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -316,15 +300,6 @@ def callback(raw_response, _, headers): for receipt, actual in zip(returned_model, actual): if actual.fields is None: # second page is blank continue - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.fields.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.fields.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.fields.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.fields.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.fields.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.fields.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.fields.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.fields.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.fields.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) @@ -342,11 +317,12 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual.fields["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual.fields["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py index ee4ab885f386..9510f300bc59 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py @@ -95,20 +95,6 @@ def form_page(form_table, form_line): assert repr(model) == model_repr return model, model_repr -@pytest.fixture -def us_receipt_type(): - model = _models.ReceiptType(type="Itemized", confidence=1.0) - model_repr = "ReceiptType(type=Itemized, confidence=1.0)" - assert repr(model) == model_repr - return model, model_repr - -@pytest.fixture -def us_receipt_item(form_field_two): - model = _models.USReceiptItem(name=form_field_two[0], quantity=form_field_two[0], price=form_field_two[0], total_price=form_field_two[0]) - model_repr = "USReceiptItem(name={}, quantity={}, price={}, total_price={})".format(form_field_two[1], form_field_two[1], form_field_two[1], form_field_two[1])[:1024] - assert repr(model) == model_repr - return model, model_repr - @pytest.fixture def custom_form_model_field(): model = _models.CustomFormModelField(label="label", name="name", accuracy=0.99) @@ -141,65 +127,21 @@ def training_document_info(form_recognizer_error): class TestRepr(): # Not inheriting form FormRecognizerTest because that doesn't allow me to define pytest fixtures in the same file # Not worth moving pytest fixture definitions to conftest since all I would use is assertEqual and I can just use assert - def test_recognized_form(self, form_field_one, page_range, form_page, us_receipt_type, us_receipt_item): + def test_recognized_form(self, form_field_one, page_range, form_page): model = _models.RecognizedForm(form_type="receipt", fields={"one": form_field_one[0]}, page_range=page_range[0], pages=[form_page[0]]) model_repr = "RecognizedForm(form_type=receipt, fields={{'one': {}}}, page_range={}, pages=[{}])".format( form_field_one[1], page_range[1], form_page[1] )[:1024] assert repr(model) == model_repr - def test_recognized_receipt(self, form_field_one, page_range, form_page, us_receipt_type): + def test_recognized_receipt(self, form_field_one, page_range, form_page): model = _models.RecognizedReceipt( - form_type="receipt", fields={"one": form_field_one[0]}, page_range=page_range[0], pages=[form_page[0]], - receipt_type=us_receipt_type[0], receipt_locale="en-US") + form_type="receipt", fields={"one": form_field_one[0]}, page_range=page_range[0], pages=[form_page[0]]) model_repr = "RecognizedReceipt(form_type=receipt, fields={{'one': {}}}, page_range={}, pages=[{}])".format( - form_field_one[1], page_range[1], form_page[1], us_receipt_type[0], "en-US" + form_field_one[1], page_range[1], form_page[1] )[:1024] assert repr(model) == model_repr - def test_us_receipt(self, form_field_one, form_field_two, us_receipt_type, us_receipt_item, page_range, form_page): - model = _models.USReceipt( - merchant_address=form_field_one[0], - merchant_name=form_field_two[0], - merchant_phone_number=form_field_one[0], - receipt_type=us_receipt_type[0], - receipt_items=[us_receipt_item[0], us_receipt_item[0]], - subtotal=form_field_two[0], - tax=form_field_one[0], - tip=form_field_two[0], - total=form_field_one[0], - transaction_date=form_field_two[0], - transaction_time=form_field_one[0], - fields={ - "one": form_field_one[0] - }, - page_range=page_range[0], - pages=[form_page[0]], - form_type="test", - receipt_locale="en-US" - ) - model_repr="USReceipt(merchant_address={}, merchant_name={}, merchant_phone_number={}, receipt_type={}, receipt_items=[{}, {}], subtotal={}, " \ - "tax={}, tip={}, total={}, transaction_date={}, transaction_time={}, fields={{'one': {}}}, page_range={}, pages=[{}], " \ - "form_type=test, receipt_locale=en-US)".format( - form_field_one[1], - form_field_two[1], - form_field_one[1], - us_receipt_type[1], - us_receipt_item[1], - us_receipt_item[1], - form_field_two[1], - form_field_one[1], - form_field_two[1], - form_field_one[1], - form_field_two[1], - form_field_one[1], - form_field_one[1], - page_range[1], - form_page[1] - )[:1024] - - - assert repr(model) == model_repr def test_custom_form_model(self, custom_form_sub_model, form_recognizer_error, training_document_info): model = _models.CustomFormModel( diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py index bdc95253117a..caeb23bb5a67 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py @@ -262,10 +262,10 @@ def assertReceiptItemsTransformCorrect(self, items, actual_items, read_results=N actual = actual_items.value_array for r, a in zip(items, actual): - self.assertFormFieldTransformCorrect(r.name, a.value_object.get("Name"), read_results) - self.assertFormFieldTransformCorrect(r.quantity, a.value_object.get("Quantity"), read_results) - self.assertFormFieldTransformCorrect(r.total_price, a.value_object.get("TotalPrice"), read_results) - self.assertFormFieldTransformCorrect(r.price, a.value_object.get("Price"), read_results) + self.assertFormFieldTransformCorrect(r.value.get("Name"), a.value_object.get("Name"), read_results) + self.assertFormFieldTransformCorrect(r.value.get("Quantity"), a.value_object.get("Quantity"), read_results) + self.assertFormFieldTransformCorrect(r.value.get("TotalPrice"), a.value_object.get("TotalPrice"), read_results) + self.assertFormFieldTransformCorrect(r.value.get("Price"), a.value_object.get("Price"), read_results) def assertTablesTransformCorrect(self, layout, actual_layout, read_results=None, **kwargs): for table, actual_table in zip(layout, actual_layout): @@ -287,24 +287,24 @@ def assertTablesTransformCorrect(self, layout, actual_layout, read_results=None, def assertReceiptItemsHasValues(self, items, page_number, include_text_content): for item in items: - self.assertBoundingBoxHasPoints(item.name.value_data.bounding_box) - self.assertIsNotNone(item.name.confidence) - self.assertIsNotNone(item.name.value_data.text) - self.assertBoundingBoxHasPoints(item.quantity.value_data.bounding_box) - self.assertIsNotNone(item.quantity.confidence) - self.assertIsNotNone(item.quantity.value_data.text) - self.assertBoundingBoxHasPoints(item.total_price.value_data.bounding_box) - self.assertIsNotNone(item.total_price.confidence) - self.assertIsNotNone(item.total_price.value_data.text) + self.assertBoundingBoxHasPoints(item.value.get("Name").value_data.bounding_box) + self.assertIsNotNone(item.value.get("Name").confidence) + self.assertIsNotNone(item.value.get("Name").value_data.text) + self.assertBoundingBoxHasPoints(item.value.get("Quantity").value_data.bounding_box) + self.assertIsNotNone(item.value.get("Quantity").confidence) + self.assertIsNotNone(item.value.get("Quantity").value_data.text) + self.assertBoundingBoxHasPoints(item.value.get("TotalPrice").value_data.bounding_box) + self.assertIsNotNone(item.value.get("TotalPrice").confidence) + self.assertIsNotNone(item.value.get("TotalPrice").value_data.text) if include_text_content: - self.assertTextContentHasValues(item.name.value_data.text_content, page_number) - self.assertTextContentHasValues(item.name.value_data.text_content, page_number) - self.assertTextContentHasValues(item.name.value_data.text_content, page_number) + self.assertTextContentHasValues(item.value.get("Name").value_data.text_content, page_number) + self.assertTextContentHasValues(item.value.get("Name").value_data.text_content, page_number) + self.assertTextContentHasValues(item.value.get("Name").value_data.text_content, page_number) else: - self.assertIsNone(item.name.value_data.text_content) - self.assertIsNone(item.name.value_data.text_content) - self.assertIsNone(item.name.value_data.text_content) + self.assertIsNone(item.value.get("Name").value_data.text_content) + self.assertIsNone(item.value.get("Name").value_data.text_content) + self.assertIsNone(item.value.get("Name").value_data.text_content) def assertBoundingBoxHasPoints(self, box): if box is None: