16
README.md
@ -1,2 +1,18 @@
|
||||
# SBT Project
|
||||
|
||||
## Local development
|
||||
|
||||
- Prepare `.env` file.
|
||||
- Run for local development:
|
||||
|
||||
```bash
|
||||
docker compose --profile local up -d
|
||||
```
|
||||
|
||||
## Run tests
|
||||
|
||||
```bash
|
||||
pip3 install pytest requests
|
||||
IDP_HOST=https://sbt.idp.sdsrv.ai IDP_USERNAME=<username> IDP_PASSWORD=<password> python3 -m pytest --log-cli-level=DEBUG --capture=sys tests
|
||||
```
|
||||
|
||||
|
BIN
test_samples/test_01/Invoice_larger_than_5MB.jpg
Normal file
After Width: | Height: | Size: 7.1 MiB |
BIN
test_samples/test_03/image_larger_than_5K.jpg
Normal file
After Width: | Height: | Size: 7.1 MiB |
BIN
test_samples/test_04/blank_image.png
Normal file
After Width: | Height: | Size: 5.1 KiB |
BIN
test_samples/test_07/imei_valid_1.jpg
Normal file
After Width: | Height: | Size: 173 KiB |
BIN
test_samples/test_07/imei_valid_2.jpg
Normal file
After Width: | Height: | Size: 615 KiB |
BIN
test_samples/test_07/imei_valid_3.jpg
Normal file
After Width: | Height: | Size: 3.2 MiB |
BIN
test_samples/test_07/imei_valid_4.jpg
Normal file
After Width: | Height: | Size: 559 KiB |
BIN
test_samples/test_07/imei_valid_5.jpg
Normal file
After Width: | Height: | Size: 176 KiB |
BIN
test_samples/test_07/imei_valid_6.jpg
Normal file
After Width: | Height: | Size: 310 KiB |
BIN
test_samples/test_07/invoice_jpg.jpg
Normal file
After Width: | Height: | Size: 1.1 MiB |
BIN
test_samples/test_15/imei_jpg.jpg
Normal file
After Width: | Height: | Size: 173 KiB |
25
test_samples/test_15/result.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"label": "retailername",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "sold_to_party",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "purchase_date",
|
||||
"value": []
|
||||
},
|
||||
{
|
||||
"label": "imei_number",
|
||||
"value": [
|
||||
"357822611219904"
|
||||
]
|
||||
}
|
||||
],
|
||||
"doc_type": "sbt_document",
|
||||
"end_page": 1,
|
||||
"start_page": 1
|
||||
}
|
BIN
test_samples/test_17/imei_jpeg.jpeg
Normal file
After Width: | Height: | Size: 615 KiB |
25
test_samples/test_17/result.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"label": "retailername",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "sold_to_party",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "purchase_date",
|
||||
"value": []
|
||||
},
|
||||
{
|
||||
"label": "imei_number",
|
||||
"value": [
|
||||
"RFAW2022FED"
|
||||
]
|
||||
}
|
||||
],
|
||||
"doc_type": "sbt_document",
|
||||
"end_page": 1,
|
||||
"start_page": 1
|
||||
}
|
27
test_samples/test_21.json
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"label": "retailername",
|
||||
"value": "Best Denki"
|
||||
},
|
||||
{
|
||||
"label": "sold_to_party",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "purchase_date",
|
||||
"value": [
|
||||
"2022-02-18"
|
||||
]
|
||||
},
|
||||
{
|
||||
"label": "imei_number",
|
||||
"value": [
|
||||
"357822611219904"
|
||||
]
|
||||
}
|
||||
],
|
||||
"doc_type": "sbt_document",
|
||||
"end_page": 2,
|
||||
"start_page": 1
|
||||
}
|
28
test_samples/test_22.json
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"label": "retailername",
|
||||
"value": "Best Denki"
|
||||
},
|
||||
{
|
||||
"label": "sold_to_party",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "purchase_date",
|
||||
"value": [
|
||||
"2022-02-18"
|
||||
]
|
||||
},
|
||||
{
|
||||
"label": "imei_number",
|
||||
"value": [
|
||||
"357822611219904",
|
||||
"RFAW2022FED"
|
||||
]
|
||||
}
|
||||
],
|
||||
"doc_type": "sbt_document",
|
||||
"end_page": 3,
|
||||
"start_page": 1
|
||||
}
|
29
test_samples/test_23.json
Normal file
@ -0,0 +1,29 @@
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"label": "retailername",
|
||||
"value": "Best Denki"
|
||||
},
|
||||
{
|
||||
"label": "sold_to_party",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "purchase_date",
|
||||
"value": [
|
||||
"2022-02-18"
|
||||
]
|
||||
},
|
||||
{
|
||||
"label": "imei_number",
|
||||
"value": [
|
||||
"357822611219904",
|
||||
"RFAW2022FED",
|
||||
"5AWH14MT400396N"
|
||||
]
|
||||
}
|
||||
],
|
||||
"doc_type": "sbt_document",
|
||||
"end_page": 4,
|
||||
"start_page": 1
|
||||
}
|
30
test_samples/test_24.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"label": "retailername",
|
||||
"value": "Best Denki"
|
||||
},
|
||||
{
|
||||
"label": "sold_to_party",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"label": "purchase_date",
|
||||
"value": [
|
||||
"2022-02-18"
|
||||
]
|
||||
},
|
||||
{
|
||||
"label": "imei_number",
|
||||
"value": [
|
||||
"357822611219904",
|
||||
"RFAW2022FED",
|
||||
"5AWH14MT400396N",
|
||||
"0HU33NIW200044K"
|
||||
]
|
||||
}
|
||||
],
|
||||
"doc_type": "sbt_document",
|
||||
"end_page": 5,
|
||||
"start_page": 1
|
||||
}
|
31
test_samples/test_25.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"content": [
|
||||
{
|
||||
"label": "retailername",
|
||||
"value": "Best Denki"
|
||||
},
|
||||
{
|
||||
"label": "sold_to_party",
|
||||
"value": null
|
||||
},
|
||||
{
|
||||
"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
|
||||
}
|
12
tests/test_login.py
Normal file
@ -0,0 +1,12 @@
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
from tests.utils import login
|
||||
import os
|
||||
|
||||
HOST = os.environ.get("IDP_HOST", "XXXXXXXXXXXXXXXXXXXXX")
|
||||
USERNAME = os.environ.get("IDP_USERNAME", "XXXXXXXXXXXXXXXXXXXXX")
|
||||
PASSWORD = os.environ.get("IDP_PASSWORD", "XXXXXXXXXXXXXXXXXXXXX")
|
||||
|
||||
def test_login():
|
||||
token = login(HOST, USERNAME, PASSWORD)
|
||||
assert token is not None
|
148
tests/test_set_00_invoice_05_imeis.py
Normal file
@ -0,0 +1,148 @@
|
||||
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 check_invoice_data(document):
|
||||
assert document["doc_type"] == "sbt_document"
|
||||
assert get_field(document, "retailername") == "Arrow Communication"
|
||||
assert get_field(document, "sold_to_party") is None
|
||||
assert "2021-02-09" in get_field(document, "purchase_date")
|
||||
|
||||
def check_invoice_data_2(document):
|
||||
assert document["doc_type"] == "sbt_document"
|
||||
assert get_field(document, "retailername") == "Starhub Shop"
|
||||
assert get_field(document, "sold_to_party") is None
|
||||
assert "2022-02-22" in get_field(document, "purchase_date")
|
||||
|
||||
def test_1_invoice_1_imei():
|
||||
invoice_files = [
|
||||
"test_samples/sbt/invoice.jpg"
|
||||
]
|
||||
imei_files = [
|
||||
"test_samples/sbt/imei1.jpg"
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files)
|
||||
|
||||
data = data["data"]["data"]
|
||||
assert data["total_pages"] == 2
|
||||
assert data["ocr_num_pages"] == 2
|
||||
assert data["status"] == 200
|
||||
|
||||
document = data["document"][0]
|
||||
assert document["end_page"] == 2
|
||||
assert document["start_page"] == 1
|
||||
check_invoice_data(document)
|
||||
|
||||
assert "358975990917032" == get_field(document, "imei_number")[0]
|
||||
|
||||
|
||||
def test_1_invoice_3_imei():
|
||||
invoice_files = [
|
||||
"test_samples/sbt/invoice.jpg"
|
||||
]
|
||||
imei_files = [
|
||||
"test_samples/sbt/imei1.jpg",
|
||||
"test_samples/sbt/imei2.jpg",
|
||||
"test_samples/sbt/imei3.jpg"
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files)
|
||||
|
||||
data = data["data"]["data"]
|
||||
assert data["total_pages"] == 4
|
||||
assert data["ocr_num_pages"] == 4
|
||||
assert data["status"] == 200
|
||||
|
||||
document = data["document"][0]
|
||||
assert document["end_page"] == 4
|
||||
assert document["start_page"] == 1
|
||||
check_invoice_data(document)
|
||||
|
||||
assert 3 == len(get_field(document, "imei_number"))
|
||||
assert "358975990917032" == get_field(document, "imei_number")[0]
|
||||
assert "350731691693549" == get_field(document, "imei_number")[1]
|
||||
assert "R52W70BHDWX" == get_field(document, "imei_number")[2]
|
||||
|
||||
|
||||
def test_1_invoice_5_imei():
|
||||
invoice_files = [
|
||||
"test_samples/sbt/invoice.jpg"
|
||||
]
|
||||
imei_files = [
|
||||
"test_samples/sbt/imei1.jpg",
|
||||
"test_samples/sbt/imei2.jpg",
|
||||
"test_samples/sbt/imei3.jpg",
|
||||
"test_samples/sbt/imei4.jpeg",
|
||||
"test_samples/sbt/imei5.jpg"
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files)
|
||||
|
||||
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
|
||||
check_invoice_data(document)
|
||||
|
||||
assert 5 == len(get_field(document, "imei_number"))
|
||||
assert "358975990917032" == get_field(document, "imei_number")[0]
|
||||
assert "350731691693549" == get_field(document, "imei_number")[1]
|
||||
assert "R52W70BHDWX" == get_field(document, "imei_number")[2]
|
||||
assert "350073345090297" == get_field(document, "imei_number")[3]
|
||||
assert "0PBL3NHW500023N" == get_field(document, "imei_number")[4]
|
||||
|
||||
|
||||
def test_0_invoice_3_imei():
|
||||
invoice_files = []
|
||||
imei_files = [
|
||||
"test_samples/sbt/imei1.jpg",
|
||||
"test_samples/sbt/imei2.jpg",
|
||||
"test_samples/sbt/imei3.jpg"
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files)
|
||||
|
||||
data = data["data"]["data"]
|
||||
assert data["total_pages"] == 3
|
||||
assert data["ocr_num_pages"] == 3
|
||||
assert data["status"] == 200
|
||||
|
||||
document = data["document"][0]
|
||||
assert document["end_page"] == 3
|
||||
assert document["start_page"] == 1
|
||||
|
||||
assert 3 == len(get_field(document, "imei_number"))
|
||||
assert "358975990917032" == get_field(document, "imei_number")[0]
|
||||
assert "350731691693549" == get_field(document, "imei_number")[1]
|
||||
assert "R52W70BHDWX" == get_field(document, "imei_number")[2]
|
||||
|
||||
|
||||
def test_1_invoice_pdf_1_imei():
|
||||
invoice_files = [
|
||||
"test_samples/sbt/20220303025923NHNE_20220222_Starhub_Order_Confirmation_by_Email.pdf"
|
||||
]
|
||||
imei_files = [
|
||||
"test_samples/sbt/imei1.jpg"
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files)
|
||||
|
||||
data = data["data"]["data"]
|
||||
assert data["total_pages"] == 2
|
||||
assert data["ocr_num_pages"] == 2
|
||||
assert data["status"] == 200
|
||||
|
||||
document = data["document"][0]
|
||||
assert document["end_page"] == 2
|
||||
assert document["start_page"] == 1
|
||||
check_invoice_data_2(document)
|
||||
|
||||
assert "358975990917032" == get_field(document, "imei_number")[0]
|
31
tests/test_set_01_big_image_files.py
Normal 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 "Limit reached" 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 "Limit reached" in str(response_data)
|
22
tests/test_set_03_big_image_sizes.py
Normal 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) or "Limit reached" in str(response_data)
|
28
tests/test_set_05_blank_image.py
Normal 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")
|
||||
|
21
tests/test_set_06_invoice_only.py
Normal 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)
|
87
tests/test_set_07_01_invoice_06_imei.py
Normal 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}"
|
24
tests/test_set_15_1_jpg_imei.py
Normal file
@ -0,0 +1,24 @@
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
from tests.utils import login, make_sbt_request
|
||||
import os
|
||||
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_1_imei():
|
||||
invoice_files = []
|
||||
imei_files = [
|
||||
"test_samples/test_15/imei_jpg.jpg",
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False)
|
||||
document = data["data"]["data"]["document"][0]
|
||||
gt = json.load(open("test_samples/test_15/result.json", "r"))
|
||||
diff = deepdiff.DeepDiff(document, gt)
|
||||
|
||||
assert diff == {}, f"Different result! GT: {gt}; RESULT: {document}"
|
24
tests/test_set_17_1_jpeg_imei.py
Normal file
@ -0,0 +1,24 @@
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
from tests.utils import login, make_sbt_request
|
||||
import os
|
||||
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_1_imei():
|
||||
invoice_files = []
|
||||
imei_files = [
|
||||
"test_samples/test_17/imei_jpeg.jpeg",
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False)
|
||||
document = data["data"]["data"]["document"][0]
|
||||
gt = json.load(open("test_samples/test_17/result.json", "r"))
|
||||
diff = deepdiff.DeepDiff(document, gt)
|
||||
|
||||
assert diff == {}, f"Different result! GT: {gt}; RESULT: {document}"
|
97
tests/test_set_21_to_25.py
Normal file
@ -0,0 +1,97 @@
|
||||
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_1_imei():
|
||||
invoice_files = [
|
||||
"test_samples/test_07/invoice_jpg.jpg",
|
||||
]
|
||||
imei_files = [
|
||||
"test_samples/test_07/imei_valid_1.jpg",
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False)
|
||||
document = data["data"]["data"]["document"][0]
|
||||
gt = json.load(open("test_samples/test_21.json", "r"))
|
||||
diff = deepdiff.DeepDiff(document, gt)
|
||||
|
||||
assert diff == {}, f"Different result! GT: {gt}; RESULT: {document}"
|
||||
|
||||
|
||||
def test_1_invoice_2_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",
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False)
|
||||
document = data["data"]["data"]["document"][0]
|
||||
gt = json.load(open("test_samples/test_22.json", "r"))
|
||||
diff = deepdiff.DeepDiff(document, gt)
|
||||
|
||||
assert diff == {}, f"Different result! GT: {gt}; RESULT: {document}"
|
||||
|
||||
|
||||
def test_1_invoice_3_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",
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False)
|
||||
document = data["data"]["data"]["document"][0]
|
||||
gt = json.load(open("test_samples/test_23.json", "r"))
|
||||
diff = deepdiff.DeepDiff(document, gt)
|
||||
|
||||
assert diff == {}, f"Different result! GT: {gt}; RESULT: {document}"
|
||||
|
||||
|
||||
def test_1_invoice_4_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",
|
||||
]
|
||||
data = make_sbt_request(host=HOST, token=token, invoice_files=invoice_files, imei_files=imei_files, ensure_success=False)
|
||||
document = data["data"]["data"]["document"][0]
|
||||
gt = json.load(open("test_samples/test_24.json", "r"))
|
||||
diff = deepdiff.DeepDiff(document, gt)
|
||||
|
||||
assert diff == {}, f"Different result! GT: {gt}; RESULT: {document}"
|
||||
|
||||
|
||||
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)
|
||||
document = data["data"]["data"]["document"][0]
|
||||
gt = json.load(open("test_samples/test_25.json", "r"))
|
||||
diff = deepdiff.DeepDiff(document, gt)
|
||||
|
||||
assert diff == {}, f"Different result! GT: {gt}; RESULT: {document}"
|
95
tests/utils.py
Normal file
@ -0,0 +1,95 @@
|
||||
import requests
|
||||
import traceback
|
||||
import logging
|
||||
import time
|
||||
from requests_toolbelt import MultipartEncoderMonitor
|
||||
|
||||
|
||||
def login(host, username, password):
|
||||
print(host, username, password)
|
||||
response = requests.post(f'{host}/api/ctel/login/', json={
|
||||
'username': username,
|
||||
'password': password
|
||||
})
|
||||
try:
|
||||
token = response.json().get('token', '')
|
||||
except:
|
||||
logging.error("Failed to login")
|
||||
traceback.print_exc()
|
||||
raise ValueError("Could not login. Please check host, username or password.")
|
||||
if not token:
|
||||
logging.error(response.content)
|
||||
raise ValueError("Could not get a valid token. Please check host, username or password.")
|
||||
|
||||
return token
|
||||
|
||||
|
||||
def make_sbt_request(host, token, invoice_files=None, imei_files=None, ensure_success=True):
|
||||
files = []
|
||||
if invoice_files:
|
||||
for file in invoice_files:
|
||||
files.append(('invoice_file', (file, open(file, "rb"), 'application/octet-stream')))
|
||||
if imei_files:
|
||||
for file in imei_files:
|
||||
files.append(('imei_files', (file, open(file, "rb"), 'application/octet-stream')))
|
||||
num_files = len(files)
|
||||
files.append(('processType', '12'))
|
||||
files.append(('is_test_request', 'true'))
|
||||
start_time = time.time()
|
||||
end_of_upload_time = 0
|
||||
|
||||
def upload_callback(monitor):
|
||||
nonlocal end_of_upload_time
|
||||
if monitor.bytes_read == monitor.len:
|
||||
end_of_upload_time = time.time()
|
||||
m = MultipartEncoderMonitor.from_fields(
|
||||
fields=files,
|
||||
callback=upload_callback
|
||||
)
|
||||
try:
|
||||
response = requests.post(f'{host}/api/ctel/images/process_sync/', headers={
|
||||
'Authorization': token,
|
||||
'Content-Type': m.content_type
|
||||
}, data=m, timeout=300)
|
||||
except requests.exceptions.Timeout as e:
|
||||
print(e)
|
||||
traceback.print_exc()
|
||||
raise TimeoutError("Timeout when sending request to server.")
|
||||
except Exception as e:
|
||||
print(e)
|
||||
traceback.print_exc()
|
||||
raise e
|
||||
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 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 if parsed_data is not None else response.content,
|
||||
"num_files": num_files,
|
||||
}
|
||||
|
||||
|
||||
def get_field(document, label):
|
||||
for field in document["content"]:
|
||||
if field["label"] == label:
|
||||
return field["value"]
|
||||
return None
|