173 lines
5.7 KiB
Python
173 lines
5.7 KiB
Python
"""Global patient searches enter the patient list with one scoped request."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Any
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
import pytest
|
|
from PySide6.QtCore import QTimer
|
|
from PySide6.QtWidgets import QApplication
|
|
|
|
from doctor_workstation.core import PermissionSet
|
|
from doctor_workstation.ui.pages import patients as patients_module
|
|
from doctor_workstation.ui.pages.patients import PatientsPage
|
|
|
|
|
|
class Repository:
|
|
def __init__(self) -> None:
|
|
self.calls: list[tuple[str, dict[str, Any]]] = []
|
|
|
|
def _result(self, kind: str, query: dict[str, Any]) -> dict[str, Any]:
|
|
self.calls.append((kind, dict(query)))
|
|
start = (query["page_no"] - 1) * query["page_size"]
|
|
return {
|
|
"lists": [
|
|
{
|
|
"id": index + 100,
|
|
"diagnosis_id": index + 100,
|
|
"patient_name": f"患者 {index}",
|
|
"appointment_status": 1,
|
|
"queue_status": "waiting",
|
|
}
|
|
for index in range(start, min(start + query["page_size"], 37))
|
|
],
|
|
"count": 37,
|
|
"extend": {"scope": {"label": "本人患者"}, "summary": {}},
|
|
}
|
|
|
|
def list_patients(self, **query: Any) -> dict[str, Any]:
|
|
return self._result("patients", query)
|
|
|
|
def patient_orders(self, **query: Any) -> dict[str, Any]:
|
|
return self._result("orders", query)
|
|
|
|
def patient_progress(self, **query: Any) -> dict[str, Any]:
|
|
return self._result("progress", query)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def application() -> QApplication:
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
@pytest.fixture
|
|
def page(application: QApplication, monkeypatch: pytest.MonkeyPatch):
|
|
def immediate(function: Any, **callbacks: Any) -> object:
|
|
result = function()
|
|
callbacks["on_success"](result)
|
|
if callbacks.get("on_finished"):
|
|
callbacks["on_finished"]()
|
|
return object()
|
|
|
|
monkeypatch.setattr(patients_module, "run_async", immediate)
|
|
repository = Repository()
|
|
widget = PatientsPage(repository, PermissionSet(["firstvisit.myPatient/lists"]))
|
|
widget.resize(1280, 900)
|
|
widget.show()
|
|
application.processEvents()
|
|
yield widget, repository
|
|
for timer in widget.findChildren(QTimer):
|
|
timer.stop()
|
|
widget.close()
|
|
widget.deleteLater()
|
|
application.processEvents()
|
|
|
|
|
|
def assert_patient_query(repository: Repository, keyword: str) -> None:
|
|
assert repository.calls == [
|
|
(
|
|
"patients",
|
|
{
|
|
"page_no": 1,
|
|
"page_size": 15,
|
|
"keyword": keyword,
|
|
"status": "",
|
|
"start_date": "",
|
|
"end_date": "",
|
|
},
|
|
)
|
|
]
|
|
|
|
|
|
@pytest.mark.parametrize("tab_index", [0, 1, 2], ids=["patients", "orders", "progress"])
|
|
@pytest.mark.parametrize("expanded", [False, True], ids=["collapsed", "expanded"])
|
|
def test_search_enters_patient_list_and_clears_old_filters(
|
|
page, monkeypatch: pytest.MonkeyPatch, tab_index: int, expanded: bool
|
|
) -> None:
|
|
widget, repository = page
|
|
workspace = widget.patient_workspace
|
|
workspace.keyword_edit.setText("旧患者")
|
|
workspace.status_combo.setCurrentIndex(2)
|
|
workspace.set_date_mode("today", refresh=False)
|
|
widget.filter_disclosures[0].set_expanded(expanded)
|
|
widget.order_workspace.keyword_edit.setText("旧订单")
|
|
widget.tabs.setCurrentIndex(tab_index)
|
|
repository.calls.clear()
|
|
refreshes: list[bool] = []
|
|
original_refresh = widget.refresh
|
|
|
|
def refresh() -> None:
|
|
refreshes.append(True)
|
|
original_refresh()
|
|
|
|
monkeypatch.setattr(widget, "refresh", refresh)
|
|
widget.search_patient(" 13800138000 ")
|
|
|
|
assert widget.tabs.currentWidget() is workspace
|
|
assert_patient_query(repository, "13800138000")
|
|
assert refreshes == [True]
|
|
assert workspace.keyword_edit.text() == "13800138000"
|
|
assert workspace._date_mode == "all"
|
|
assert workspace.status_buttons[""].isChecked()
|
|
assert workspace._page == workspace.pager.page == 1
|
|
assert widget.filter_disclosures[0].expanded is expanded
|
|
assert widget.order_workspace.keyword_edit.text() == "旧订单"
|
|
assert not widget.progress_workspace.timer.isActive()
|
|
|
|
|
|
@pytest.mark.parametrize("query", ["", " "], ids=["empty", "whitespace"])
|
|
def test_empty_search_clears_keyword_even_when_patient_page_is_hidden(page, query: str) -> None:
|
|
widget, repository = page
|
|
widget.patient_workspace.keyword_edit.setText("旧患者")
|
|
widget.patient_workspace.status_combo.setCurrentIndex(3)
|
|
widget.tabs.setCurrentIndex(1)
|
|
widget.hide()
|
|
repository.calls.clear()
|
|
|
|
widget.search_patient(query)
|
|
|
|
assert widget.tabs.currentIndex() == 0
|
|
assert widget.patient_workspace.keyword_edit.text() == ""
|
|
assert_patient_query(repository, "")
|
|
|
|
|
|
def test_repeated_search_discards_previously_loaded_pages(page) -> None:
|
|
widget, repository = page
|
|
widget.search_patient("患者")
|
|
workspace = widget.patient_workspace
|
|
workspace.pager.load_more()
|
|
assert workspace._page == workspace.pager.page == 2
|
|
repository.calls.clear()
|
|
|
|
widget.search_patient("患者")
|
|
|
|
assert_patient_query(repository, "患者")
|
|
assert workspace._page == workspace.pager.page == 1
|
|
assert workspace.table.rowCount() == 15
|
|
|
|
|
|
def test_reset_button_still_accepts_qt_clicked_signal(page) -> None:
|
|
widget, repository = page
|
|
workspace = widget.patient_workspace
|
|
workspace.keyword_edit.setText("旧患者")
|
|
workspace.status_combo.setCurrentIndex(2)
|
|
repository.calls.clear()
|
|
|
|
workspace.reset_button.click()
|
|
|
|
assert workspace.keyword_edit.text() == ""
|
|
assert_patient_query(repository, "")
|