Files
zyt/app/tests/test_consultation_assistant_names.py
2026-11-06 12:01:31 +08:00

140 lines
5.5 KiB
Python

"""Medical-assistant names must survive independent list/options loading."""
from __future__ import annotations
import os
from copy import deepcopy
from typing import Any
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
import pytest
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QApplication, QWidget
from doctor_workstation.core.errors import ApiBusinessError
from doctor_workstation.services.repository import RemoteDoctorRepository
from doctor_workstation.ui.pages import consultations as module
class Client:
def __init__(self) -> None:
self.assistants: Any = [{"id": 66, "name": "周医助"}]
self.rows = [
{"id": 1, "assistant_id": "66", "patient_name": "测试患者"},
{"id": 2, "assistant_id": 66, "assistant_name": "诊单医助"},
{"id": 3, "assistant_id": 99},
{"id": 4, "assistant_id": 0},
{"id": 5, "assistant_id": None},
]
def get(self, endpoint: str, params: Any = None) -> Any:
if endpoint == "tcm.diagnosis/lists":
return {"lists": deepcopy(self.rows), "count": len(self.rows)}
if endpoint == "tcm.diagnosis/getAssistants":
if isinstance(self.assistants, Exception):
raise self.assistants
return deepcopy(self.assistants)
return []
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)
jobs = []
def defer(function: Any, **callbacks: Any) -> None:
jobs.append(lambda: callbacks["on_success"](function()))
monkeypatch.setattr(module, "run_async", defer)
client = Client()
result = module.ConsultationsPage(RemoteDoctorRepository(client))
yield result, client, jobs
result.poll_timer.stop()
result.close()
result.deleteLater()
application.processEvents()
@pytest.mark.parametrize("options_first", [False, True])
def test_names_render_with_either_response_order_without_changing_source(page, options_first):
view, client, jobs = page
result = view.repository.list_consultations()
before = deepcopy(result)
view._load_filter_options()
if options_first:
jobs.pop()()
view._apply_result(result, view._generation)
model = view.table_host.model
model.setData(model.index(0, 0), Qt.CheckState.Checked, Qt.ItemDataRole.CheckStateRole)
view.table.selectRow(0)
if not options_first:
assert model.index(0, 7).data() == "—"
changes = []
model.dataChanged.connect(lambda first, last, roles: changes.append((first, last, roles)))
jobs.pop()()
assert any(first.column() == last.column() == 7 for first, last, _ in changes)
assert [model.index(row, 7).data() for row in range(5)] == [
"周医助", "诊单医助", "—", "—", "—",
]
assert model.index(0, 7).data(Qt.ItemDataRole.ToolTipRole) == "周医助"
assert [row.id for row in model.checked_records()] == [1]
assert view.table.current_data().id == 1
assert result == before
assert "assistant_name" not in client.rows[0]
# A later page uses the same lookup; reassignment uses the new ID, not a cached row name.
client.rows = [{"id": 1, "assistant_id": 99}, {"id": 6, "assistant_id": 66}]
view._apply_result(view.repository.list_consultations(), view._generation)
assert [model.index(row, 7).data() for row in range(2)] == ["—", "周医助"]
@pytest.mark.parametrize("options", [[], ApiBusinessError("权限不足,无法访问或操作")])
def test_server_name_still_displays_when_picker_empty_or_denied(page, options):
view, client, jobs = page
client.assistants = options
view._apply_result(view.repository.list_consultations(), view._generation)
view._load_filter_options()
jobs.pop()()
assert view.table_host.model.index(1, 7).data() == "诊单医助"
assert view.table_host.model.index(0, 7).data() == "—"
def test_updated_and_stale_options_do_not_leave_old_assistant_names(page):
view, _client, jobs = page
view._apply_result(view.repository.list_consultations(), view._generation)
view._load_filter_options()
jobs.pop()()
generation = view._options_generation
view._apply_filter_options({"assistants": [{"id": 66, "name": "新医助"}]}, generation)
assert view.table_host.model.index(0, 7).data() == "新医助"
view._apply_filter_options({"assistants": []}, generation - 1)
assert view.table_host.model.index(0, 7).data() == "新医助"
view._apply_filter_options({"assistants": []}, generation)
assert view.table_host.model.index(0, 7).data() == "—"
assert view.table_host.model.index(1, 7).data() == "诊单医助"
def test_long_assistant_name_resizes_both_table_sections(page):
view, _client, _jobs = page
host = view.table_host
view._apply_result(view.repository.list_consultations(), view._generation)
original_height = host.main.rowHeight(0)
name = "完整医助姓名用于显示换行" * 5
host.set_assistant_names({66: name, 0: "不能用于未分配患者", -1: "无效医助"})
assert host.model.index(0, 7).data() == name
assert host.main.rowHeight(0) > original_height
assert host.main.rowHeight(0) == host.fixed.rowHeight(0)
assert host.model.index(3, 7).data() == "—"