diff --git a/test_samples/test_01/Invoice_larger_than_5MB.jpg b/test_samples/test_01/Invoice_larger_than_5MB.jpg new file mode 100644 index 0000000..2171524 Binary files /dev/null and b/test_samples/test_01/Invoice_larger_than_5MB.jpg differ diff --git a/test_samples/test_03/image_larger_than_5K.jpg b/test_samples/test_03/image_larger_than_5K.jpg new file mode 100644 index 0000000..2171524 Binary files /dev/null and b/test_samples/test_03/image_larger_than_5K.jpg differ diff --git a/test_samples/test_04/blank_image.png b/test_samples/test_04/blank_image.png new file mode 100644 index 0000000..8fd56d3 Binary files /dev/null and b/test_samples/test_04/blank_image.png differ diff --git a/test_samples/test_07/imei_valid_1.jpg b/test_samples/test_07/imei_valid_1.jpg new file mode 100644 index 0000000..6292147 Binary files /dev/null and b/test_samples/test_07/imei_valid_1.jpg differ diff --git a/test_samples/test_07/imei_valid_2.jpg b/test_samples/test_07/imei_valid_2.jpg new file mode 100644 index 0000000..7ca6c71 Binary files /dev/null and b/test_samples/test_07/imei_valid_2.jpg differ diff --git a/test_samples/test_07/imei_valid_3.jpg b/test_samples/test_07/imei_valid_3.jpg new file mode 100644 index 0000000..81e81a9 Binary files /dev/null and b/test_samples/test_07/imei_valid_3.jpg differ diff --git a/test_samples/test_07/imei_valid_4.jpg b/test_samples/test_07/imei_valid_4.jpg new file mode 100644 index 0000000..f50000c Binary files /dev/null and b/test_samples/test_07/imei_valid_4.jpg differ diff --git a/test_samples/test_07/imei_valid_5.jpg b/test_samples/test_07/imei_valid_5.jpg new file mode 100644 index 0000000..b83b2b6 Binary files /dev/null and b/test_samples/test_07/imei_valid_5.jpg differ diff --git a/test_samples/test_07/imei_valid_6.jpg b/test_samples/test_07/imei_valid_6.jpg new file mode 100644 index 0000000..262df8d Binary files /dev/null and b/test_samples/test_07/imei_valid_6.jpg differ diff --git a/test_samples/test_07/invoice_jpg.jpg b/test_samples/test_07/invoice_jpg.jpg new file mode 100644 index 0000000..dbdc4f2 Binary files /dev/null and b/test_samples/test_07/invoice_jpg.jpg differ diff --git a/tests/test_set_01_invoice_05_imeis.py b/tests/test_set_00_invoice_05_imeis.py similarity index 100% rename from tests/test_set_01_invoice_05_imeis.py rename to tests/test_set_00_invoice_05_imeis.py diff --git a/tests/test_set_01_big_image_files.py b/tests/test_set_01_big_image_files.py new file mode 100644 index 0000000..509f93d --- /dev/null +++ b/tests/test_set_01_big_image_files.py @@ -0,0 +1,31 @@ +import sys +sys.path.append(".") +from tests.utils import login, make_sbt_request, get_field +import os +import logging + +HOST = os.environ.get("IDP_HOST", "XXXXXXXXXXXXXXXXXXXXX") +USERNAME = os.environ.get("IDP_USERNAME", "XXXXXXXXXXXXXXXXXXXXX") +PASSWORD = os.environ.get("IDP_PASSWORD", "XXXXXXXXXXXXXXXXXXXXX") +token = login(HOST, USERNAME, PASSWORD) + + +def test_invoice_larger_than_5MB(): + invoice_files = [ + "test_samples/test_01/Invoice_larger_than_5MB.jpg" + ] + imei_files = [ + "test_samples/sbt/imei1.jpg" + ] + data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False) + response_data = data["data"] + assert "413 Request Entity Too Large" in str(response_data) or "is not valid, maximum size for one side is 5000" in str(response_data) + +def test_imei_larger_than_5MB(): + invoice_files = [] + imei_files = [ + "test_samples/test_01/Invoice_larger_than_5MB.jpg" + ] + data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False) + response_data = data["data"] + assert "413 Request Entity Too Large" in str(response_data) or "is not valid, maximum size for one side is 5000" in str(response_data) diff --git a/tests/test_set_03_big_image_sizes.py b/tests/test_set_03_big_image_sizes.py new file mode 100644 index 0000000..bdbebff --- /dev/null +++ b/tests/test_set_03_big_image_sizes.py @@ -0,0 +1,22 @@ +import sys +sys.path.append(".") +from tests.utils import login, make_sbt_request, get_field +import os +import logging + +HOST = os.environ.get("IDP_HOST", "XXXXXXXXXXXXXXXXXXXXX") +USERNAME = os.environ.get("IDP_USERNAME", "XXXXXXXXXXXXXXXXXXXXX") +PASSWORD = os.environ.get("IDP_PASSWORD", "XXXXXXXXXXXXXXXXXXXXX") +token = login(HOST, USERNAME, PASSWORD) + + +def test_invoice_larger_than_5000x5000(): + invoice_files = [ + "test_samples/test_03/image_larger_than_5K.jpg" + ] + imei_files = [ + "test_samples/sbt/imei1.jpg" + ] + data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False) + response_data = data["data"] + assert "is not valid, maximum size for one side is 5000" in str(response_data) diff --git a/tests/test_set_05_blank_image.py b/tests/test_set_05_blank_image.py new file mode 100644 index 0000000..ce46497 --- /dev/null +++ b/tests/test_set_05_blank_image.py @@ -0,0 +1,28 @@ +import sys +sys.path.append(".") +from tests.utils import login, make_sbt_request, get_field +import os +import logging + +HOST = os.environ.get("IDP_HOST", "XXXXXXXXXXXXXXXXXXXXX") +USERNAME = os.environ.get("IDP_USERNAME", "XXXXXXXXXXXXXXXXXXXXX") +PASSWORD = os.environ.get("IDP_PASSWORD", "XXXXXXXXXXXXXXXXXXXXX") +token = login(HOST, USERNAME, PASSWORD) + + +def test_blank_image(): + invoice_files = [ + "test_samples/test_04/blank_image.png" + ] + imei_files = [ + "test_samples/test_04/blank_image.png" + ] + data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False) + response_data = data["data"]["data"] + + document = response_data["document"][0] + assert not get_field(document, "retailername") + assert not get_field(document, "sold_to_party") + assert not get_field(document, "purchase_date") + assert not get_field(document, "imeis") + diff --git a/tests/test_set_06_invoice_only.py b/tests/test_set_06_invoice_only.py new file mode 100644 index 0000000..fb88b24 --- /dev/null +++ b/tests/test_set_06_invoice_only.py @@ -0,0 +1,21 @@ +import sys +sys.path.append(".") +from tests.utils import login, make_sbt_request, get_field +import os +import logging + +HOST = os.environ.get("IDP_HOST", "XXXXXXXXXXXXXXXXXXXXX") +USERNAME = os.environ.get("IDP_USERNAME", "XXXXXXXXXXXXXXXXXXXXX") +PASSWORD = os.environ.get("IDP_PASSWORD", "XXXXXXXXXXXXXXXXXXXXX") +token = login(HOST, USERNAME, PASSWORD) + + +def test_invoice_only(): + invoice_files = [ + "test_samples/sbt/20220303025923NHNE_20220222_Starhub_Order_Confirmation_by_Email.pdf" + ] + imei_files = [] + data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False) + response_data = data["data"] + + assert "imei_file param is required" in str(response_data) diff --git a/tests/test_set_07_01_invoice_06_imei.py b/tests/test_set_07_01_invoice_06_imei.py new file mode 100644 index 0000000..27b906c --- /dev/null +++ b/tests/test_set_07_01_invoice_06_imei.py @@ -0,0 +1,87 @@ +import sys +sys.path.append(".") +from tests.utils import login, make_sbt_request, get_field +import os +import logging +import json +import deepdiff + +HOST = os.environ.get("IDP_HOST", "XXXXXXXXXXXXXXXXXXXXX") +USERNAME = os.environ.get("IDP_USERNAME", "XXXXXXXXXXXXXXXXXXXXX") +PASSWORD = os.environ.get("IDP_PASSWORD", "XXXXXXXXXXXXXXXXXXXXX") +token = login(HOST, USERNAME, PASSWORD) + + +def test_1_invoice_6_imei(): + invoice_files = [ + "test_samples/test_07/invoice_jpg.jpg", + ] + imei_files = [ + "test_samples/test_07/imei_valid_1.jpg", + "test_samples/test_07/imei_valid_2.jpg", + "test_samples/test_07/imei_valid_3.jpg", + "test_samples/test_07/imei_valid_4.jpg", + "test_samples/test_07/imei_valid_5.jpg", + "test_samples/test_07/imei_valid_6.jpg", + ] + data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False) + + assert "Limit reached. Number of imei_file limit at 5" in str(data["data"]) + + +def test_1_invoice_5_imei(): + invoice_files = [ + "test_samples/test_07/invoice_jpg.jpg", + ] + imei_files = [ + "test_samples/test_07/imei_valid_1.jpg", + "test_samples/test_07/imei_valid_2.jpg", + "test_samples/test_07/imei_valid_3.jpg", + "test_samples/test_07/imei_valid_4.jpg", + "test_samples/test_07/imei_valid_5.jpg", + ] + data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False) + + data = data["data"]["data"] + assert data["total_pages"] == 6 + assert data["ocr_num_pages"] == 6 + assert data["status"] == 200 + + document = data["document"][0] + assert document["end_page"] == 6 + assert document["start_page"] == 1 + + gt = { + "content": [ + { + "label": "retailername", + "value": "Best Denki" + }, + { + "label": "sold_to_party", + "value": None + }, + { + "label": "purchase_date", + "value": [ + "2022-02-18" + ] + }, + { + "label": "imei_number", + "value": [ + "357822611219904", + "RFAW2022FED", + "5AWH14MT400396N", + "0HU33NIW200044K", + "0GJG4DBW200318X" + ] + } + ], + "doc_type": "sbt_document", + "end_page": 6, + "start_page": 1 + } + diff = deepdiff.DeepDiff(document, gt) + + assert diff == {}, f"Different result! GT: {gt}; RESULT: {document}" \ No newline at end of file diff --git a/tests/utils.py b/tests/utils.py index 9a9d357..2994ef9 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -24,7 +24,7 @@ def login(host, username, password): return token -def make_sbt_request(host, token, invoice_files=None, imei_files=None): +def make_sbt_request(host, token, invoice_files=None, imei_files=None, ensure_success=True): files = [] if invoice_files: for file in invoice_files: @@ -62,23 +62,28 @@ def make_sbt_request(host, token, invoice_files=None, imei_files=None): upload_time = end_of_upload_time - start_time process_time = time.time() - start_time - upload_time + parsed_data = None + try: + parsed_data = response.json() + except: + pass + if ensure_success: + assert upload_time > 0 + assert process_time > 0 + assert num_files > 0 + assert response.status_code == 200 - assert upload_time > 0 - assert process_time > 0 - assert num_files > 0 - assert response.status_code == 200 - - parsed_data = response.json() - assert "id" in parsed_data - assert "pages" in parsed_data - assert "created_at" in parsed_data - assert "data" in parsed_data - assert "files" in parsed_data + assert parsed_data is not None + assert "id" in parsed_data + assert "pages" in parsed_data + assert "created_at" in parsed_data + assert "data" in parsed_data + assert "files" in parsed_data return { "upload_time": upload_time, "process_time": process_time, - "data": parsed_data, + "data": parsed_data if parsed_data is not None else response.content, "num_files": num_files, }