Files
zyt/app/tests/test_consultation_global_search.py
2026-11-06 13:35:33 +08:00

110 lines
3.8 KiB
Python

"""Global patient search must not inherit a narrow queue or stale pagination."""
from __future__ import annotations
import os
from typing import Any
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
import pytest
from PySide6.QtCore import QDate, Signal
from PySide6.QtWidgets import QApplication, QWidget
from doctor_workstation.services.repository import RemoteDoctorRepository
from doctor_workstation.ui.pages import consultations as module
class Client:
def __init__(self) -> None:
self.calls = []
def get(self, endpoint: str, params: Any = None) -> Any:
params = dict(params or {})
self.calls.append((endpoint, params))
page = params.get("page_no", 1)
return {"lists": [{"id": page, "patient_name": "测试患者"}], "count": 2}
class DetailStub(QWidget):
saved = Signal()
def __init__(self, _repository: Any, parent: QWidget | None = None) -> None:
super().__init__(parent)
@pytest.fixture(scope="module")
def application() -> QApplication:
return QApplication.instance() or QApplication([])
@pytest.fixture
def page(application: QApplication, monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(module, "DiagnosisDialog", DetailStub)
def immediate(function: Any, **callbacks: Any) -> None:
callbacks["on_success"](function())
if callbacks.get("on_finished"):
callbacks["on_finished"]()
monkeypatch.setattr(module, "run_async", immediate)
client = Client()
view = module.ConsultationsPage(RemoteDoctorRepository(client))
monkeypatch.setattr(view, "_refresh_counts", lambda: None)
yield view, client
view.poll_timer.stop()
view.pager.invalidate()
view.close()
view.deleteLater()
application.processEvents()
@pytest.mark.parametrize("query", [" 林患者 ", "13800138000", "138", "", " "])
def test_global_search_submits_only_new_keyword_and_normal_scoped_list(page, query):
view, client = page
view.patient_name_edit.setText("旧患者")
view.doctor_edit.setText("旧医生")
view.keyword_edit.setText("旧手机号")
view.pending_assign_keyword.setText("旧待分配关键词")
view._pending_assign = "1"
view._pending_booking = "1"
view._completed_appointment = "1"
for combo in (view.department_combo, view.assistant_combo, view.channel_combo):
combo.addItem("旧筛选", 123)
combo.setCurrentIndex(combo.count() - 1)
view.latest_appointment_start_date.setDate(QDate(2026, 1, 3))
view.latest_appointment_end_date.setDate(QDate(2026, 1, 1))
view.latest_assign_start_date.setDate(QDate(2026, 1, 3))
view.latest_assign_end_date.setDate(QDate(2026, 1, 1))
view.unserved_sort_combo.setCurrentIndex(1)
client.calls.clear()
view.search_patient(query)
assert len(client.calls) == 1
endpoint, params = client.calls[0]
assert endpoint == "tcm.diagnosis/lists"
assert params.get("keyword", "") == query.strip()
assert params["page_no"] == 1
assert all(not value for key, value in params.items() if key not in {
"keyword", "page_no", "page_size",
})
assert view.keyword_edit.text() == query.strip()
assert view.patient_name_edit.text() == view.pending_assign_keyword.text() == ""
assert view._appointment_date == view._appointment_status_filter == ""
assert view._pending_assign == ""
assert view.table_host.model._sort_direction == ""
assert view.table_host.model.rowCount() == 1
def test_repeating_global_search_starts_at_first_page(page):
view, client = page
view.search_patient("林患者")
view.pager.load_more()
assert view.pager.page == 2
client.calls.clear()
view.search_patient("林患者")
assert [params["page_no"] for _, params in client.calls] == [1]
assert view.pager.page == 1
assert view.table_host.model.rowCount() == 1