Add more tests

This commit is contained in:
Viet Anh Nguyen 2023-12-26 15:58:44 +07:00
parent 97380fa753
commit 9ae369d7c6
17 changed files with 207 additions and 13 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 173 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 615 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 559 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

View File

@ -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)

View File

@ -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)

View File

@ -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")

View File

@ -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)

View File

@ -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}"

View File

@ -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,
}