From defe404aa35dcbac2955184e3751c7ddfb3c07ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Fri, 18 Feb 2022 13:50:12 +0100 Subject: [PATCH 1/2] :bug: fix sending financial document via the CLI --- mindee/__main__.py | 4 ++-- tests/test_cli.py | 34 +++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/mindee/__main__.py b/mindee/__main__.py index b9b9fd84..be0d320f 100644 --- a/mindee/__main__.py +++ b/mindee/__main__.py @@ -24,7 +24,7 @@ "financial": { "help": "Financial Document (receipt or invoice)", "required_keys": ["invoice", "receipt"], - "doc_type": "financial", + "doc_type": "financial_doc", }, "custom": { "help": "Custom document type from API builder", @@ -40,7 +40,7 @@ def _ots_client(args: Namespace, info: dict): kwargs["%s_api_key" % key] = getattr(args, "%s_api_key" % key) else: kwargs["api_key"] = getattr(args, "%s_api_key" % args.product_name) - func = getattr(client, f"config_{args.product_name}") + func = getattr(client, f"config_{info['doc_type']}") func(**kwargs) return client diff --git a/tests/test_cli.py b/tests/test_cli.py index bb987eaa..b8ea2240 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -9,7 +9,6 @@ @pytest.fixture def custom_doc(monkeypatch): clear_envvars(monkeypatch) - return Namespace( product_name="custom", doc_type="license_plate", @@ -25,12 +24,9 @@ def custom_doc(monkeypatch): @pytest.fixture -def invoice_doc(monkeypatch): +def ots_doc(monkeypatch): clear_envvars(monkeypatch) - return Namespace( - product_name="invoice", - invoice_api_key="", raise_on_error=True, cut_pdf=True, input_type="path", @@ -45,6 +41,30 @@ def test_cli_custom_doc(custom_doc): call_endpoint(custom_doc) -def test_cli_invoice_doc(invoice_doc): +def test_cli_invoice(ots_doc): + ots_doc.product_name = "invoice" + ots_doc.invoice_api_key = "" + with pytest.raises(RuntimeError): + call_endpoint(ots_doc) + + +def test_cli_receipt(ots_doc): + ots_doc.product_name = "receipt" + ots_doc.receipt_api_key = "" + with pytest.raises(RuntimeError): + call_endpoint(ots_doc) + + +def test_cli_financial_doc(ots_doc): + ots_doc.product_name = "financial" + ots_doc.invoice_api_key = "" + ots_doc.receipt_api_key = "" + with pytest.raises(RuntimeError): + call_endpoint(ots_doc) + + +def test_cli_passport(ots_doc): + ots_doc.product_name = "passport" + ots_doc.passport_api_key = "" with pytest.raises(RuntimeError): - call_endpoint(invoice_doc) + call_endpoint(ots_doc) From 965cc00c56d276d5cb18e8c51ca370e47d86e753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Fri, 18 Feb 2022 18:00:42 +0100 Subject: [PATCH 2/2] having to rename to financial_doc to match the documentation --- mindee/__main__.py | 4 ++-- mindee/client.py | 12 ++++++++---- mindee/documents/financial_document.py | 6 +++--- tests/documents/test_document.py | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/mindee/__main__.py b/mindee/__main__.py index be0d320f..b693c127 100644 --- a/mindee/__main__.py +++ b/mindee/__main__.py @@ -1,11 +1,11 @@ import argparse from argparse import Namespace import json -from typing import Dict +from typing import Dict, Any from mindee import Client -DOCUMENTS: Dict[str, dict] = { +DOCUMENTS: Dict[str, Dict[str, Any]] = { "invoice": { "help": "Invoice", "required_keys": ["invoice"], diff --git a/mindee/client.py b/mindee/client.py index 0ffc5f53..732808c4 100644 --- a/mindee/client.py +++ b/mindee/client.py @@ -39,11 +39,15 @@ def parse( :param username: :param include_words: Bool, extract all words into http_response """ + found = [] + for k in self.doc_configs.keys(): + if k[1] == document_type: + found.append(k) + + if len(found) == 0: + raise RuntimeError(f"Unknown document type: {document_type}") + if not username: - found = [] - for k in self.doc_configs.keys(): - if k[1] == document_type: - found.append(k) if len(found) == 1: config_key = found[0] else: diff --git a/mindee/documents/financial_document.py b/mindee/documents/financial_document.py index 3f9066e0..aa410186 100644 --- a/mindee/documents/financial_document.py +++ b/mindee/documents/financial_document.py @@ -148,9 +148,9 @@ def get_document_config(): key_name="receipt", ), ], - "document_type": "financial_document", - "singular_name": "financial_document", - "plural_name": "financial_documents", + "document_type": "financial_doc", + "singular_name": "financial_doc", + "plural_name": "financial_docs", }, api_type=API_TYPE_OFF_THE_SHELF, ) diff --git a/tests/documents/test_document.py b/tests/documents/test_document.py index 070bf07d..d22d9f1c 100644 --- a/tests/documents/test_document.py +++ b/tests/documents/test_document.py @@ -79,7 +79,7 @@ def test_response_wrapper_financial_document_with_receipt( "financial", dummy_file_input, ) - assert parsed_financial_doc.financial_document.date.value == "2016-02-26" + assert parsed_financial_doc.financial_doc.date.value == "2016-02-26" def test_response_wrapper_financial_document_with_invoice( @@ -92,4 +92,4 @@ def test_response_wrapper_financial_document_with_invoice( "financial", dummy_file_input, ) - assert parsed_financial_doc.financial_document.date.value == "2018-09-25" + assert parsed_financial_doc.financial_doc.date.value == "2018-09-25"