Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mindee/__main__.py
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -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",
Expand All @@ -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

Expand Down
12 changes: 8 additions & 4 deletions mindee/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions mindee/documents/financial_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/documents/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"
34 changes: 27 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@pytest.fixture
def custom_doc(monkeypatch):
clear_envvars(monkeypatch)

return Namespace(
product_name="custom",
doc_type="license_plate",
Expand All @@ -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",
Expand All @@ -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)