48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
|
|
import unittest
|
||
|
|
from unittest.mock import Mock, patch
|
||
|
|
|
||
|
|
from ocr_dashboard_service import export_evidences, get_summary, list_batches, list_results
|
||
|
|
from task_service import TaskError
|
||
|
|
|
||
|
|
|
||
|
|
class OcrDashboardServiceTest(unittest.TestCase):
|
||
|
|
@patch("ocr_dashboard_service.SESSION.get")
|
||
|
|
def test_list_batches_unwraps_framework_response(self, get):
|
||
|
|
response = Mock()
|
||
|
|
response.json.return_value = {"success": True, "code": 200, "data": [{"batch_key": "b1"}]}
|
||
|
|
get.return_value = response
|
||
|
|
self.assertEqual(list_batches()[0]["batch_key"], "b1")
|
||
|
|
|
||
|
|
@patch("ocr_dashboard_service.SESSION.get")
|
||
|
|
def test_summary_forwards_scope(self, get):
|
||
|
|
response = Mock()
|
||
|
|
response.json.return_value = {"success": True, "code": 200, "data": {"questions": []}}
|
||
|
|
get.return_value = response
|
||
|
|
get_summary("b1", "c1")
|
||
|
|
self.assertEqual(get.call_args.kwargs["params"], {"batch_key": "b1", "collection_key": "c1"})
|
||
|
|
|
||
|
|
def test_results_rejects_oversized_page(self):
|
||
|
|
with self.assertRaises(TaskError):
|
||
|
|
list_results({"page_size": "201"})
|
||
|
|
|
||
|
|
@patch("ocr_dashboard_service.SESSION.get")
|
||
|
|
@patch("ocr_dashboard_service.list_evidences")
|
||
|
|
def test_export_evidences_builds_zip(self, list_evidences, get):
|
||
|
|
list_evidences.return_value = {"items": [{
|
||
|
|
"question_code": "Q1", "search_keyword": "山姆会员卡", "device_code": "DEV-1",
|
||
|
|
"city": "厦门", "sort_order": 1, "source_file_name": "shot.jpg",
|
||
|
|
"cos_url": "https://bucket.cos.ap-guangzhou.myqcloud.com/shot.jpg",
|
||
|
|
}]}
|
||
|
|
response = Mock(content=b"image-bytes")
|
||
|
|
get.return_value = response
|
||
|
|
|
||
|
|
payload, filename, count = export_evidences({"batch_key": "b1", "collection_key": "c1"})
|
||
|
|
|
||
|
|
self.assertEqual(filename, "holy-crab-evidences-all.zip")
|
||
|
|
self.assertEqual(count, 1)
|
||
|
|
self.assertTrue(payload.read(2) == b"PK")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|