49 lines
2.0 KiB
Python
49 lines
2.0 KiB
Python
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from config import Config
|
||
|
|
from database import connection, init_database, now_iso
|
||
|
|
from ocr_dashboard_service import _overlay_acceptance_questions
|
||
|
|
|
||
|
|
|
||
|
|
class OcrDashboardOverlayTest(unittest.TestCase):
|
||
|
|
def setUp(self):
|
||
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
||
|
|
self.original_database_path = Config.DATABASE_PATH
|
||
|
|
Config.DATABASE_PATH = Path(self.temp_dir.name) / "holy.db"
|
||
|
|
init_database()
|
||
|
|
now = now_iso()
|
||
|
|
with connection() as conn:
|
||
|
|
task_id = conn.execute(
|
||
|
|
"""INSERT INTO acceptance_tasks
|
||
|
|
(public_task_id,name,acceptance_method,status,progress_percent,device_count,
|
||
|
|
created_by_id,created_by_name,created_at,updated_at,dashboard_batch_key)
|
||
|
|
VALUES ('root','民生银行','combined','completed',100,1,'u','u',?,?,?)""",
|
||
|
|
(now, now, "batch-1"),
|
||
|
|
).lastrowid
|
||
|
|
conn.executemany(
|
||
|
|
"INSERT INTO acceptance_keywords(task_id,keyword_order,keyword,created_at) VALUES (?,?,?,?)",
|
||
|
|
[(task_id, 0, "修改后的第一题", now), (task_id, 1, "第二题", now)],
|
||
|
|
)
|
||
|
|
|
||
|
|
def tearDown(self):
|
||
|
|
Config.DATABASE_PATH = self.original_database_path
|
||
|
|
self.temp_dir.cleanup()
|
||
|
|
|
||
|
|
def test_current_acceptance_questions_replace_historical_list(self):
|
||
|
|
data = {"questions": [
|
||
|
|
{"question_code": "Q1", "search_keyword": "旧的第一题", "result_count": 10},
|
||
|
|
{"question_code": "Q2", "search_keyword": "第二题", "result_count": 8},
|
||
|
|
]}
|
||
|
|
|
||
|
|
result = _overlay_acceptance_questions(data, "batch-1")
|
||
|
|
|
||
|
|
self.assertEqual([item["search_keyword"] for item in result["questions"]], ["修改后的第一题", "第二题"])
|
||
|
|
self.assertEqual(result["questions"][0]["result_count"], 0)
|
||
|
|
self.assertEqual(result["questions"][1]["result_count"], 8)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|