239 lines
8.8 KiB
Python
Executable File
239 lines
8.8 KiB
Python
Executable File
"""Real prescription pages append server pages without losing row actions."""
|
|
|
|
import os
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from PySide6.QtCore import Qt
|
|
from PySide6.QtTest import QTest
|
|
from PySide6.QtWidgets import QApplication, QPushButton
|
|
|
|
from doctor_workstation.core.models import Prescription
|
|
from doctor_workstation.ui.pages import prescription_library, prescriptions
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def app():
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
def inline(function, *, on_success, on_error, on_finished):
|
|
try:
|
|
result = function()
|
|
except Exception as error:
|
|
on_error(error)
|
|
else:
|
|
on_success(result)
|
|
finally:
|
|
on_finished()
|
|
|
|
|
|
class Repository:
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def get(self, **q):
|
|
self.calls.append(dict(q))
|
|
start = (q["page_no"] - 1) * q["page_size"]
|
|
return {
|
|
"lists": [
|
|
{
|
|
"id": i,
|
|
"prescription_name": f"Template {i}",
|
|
"sn": f"RX{i}",
|
|
"patient_name": f"Patient {i}",
|
|
"creator_id": 1,
|
|
"is_public": False,
|
|
"efficacy": "清热祛湿" if i > 15 else "益气养阴",
|
|
"herbs": [],
|
|
"create_time": "2026-09-01 10:00",
|
|
}
|
|
for i in range(start + 1, min(start + q["page_size"], 37) + 1)
|
|
],
|
|
"count": 37,
|
|
"extend": {"doctors": [{"id": 1, "name": "Doctor"}]},
|
|
}
|
|
|
|
list_prescriptions = get
|
|
list_prescription_templates = get
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"module,cls",
|
|
[
|
|
(prescriptions, prescriptions.PrescriptionsPage),
|
|
(prescription_library, prescription_library.PrescriptionLibraryPage),
|
|
],
|
|
)
|
|
def test_scroll_append_refresh_and_filter_reset(app, monkeypatch, module, cls):
|
|
monkeypatch.setattr(module, "run_async", inline)
|
|
repo = Repository()
|
|
page = cls(repo, {"*"}, SimpleNamespace(id=1, root=1, role_ids=[]))
|
|
page.resize(1250, 760)
|
|
page.show()
|
|
page.refresh()
|
|
# Let deferred filter/table geometry settle before scrolling its real viewport.
|
|
for _ in range(3):
|
|
app.processEvents()
|
|
QTest.qWait(35)
|
|
assert page.table.rowCount() == 15
|
|
page.table.selectRow(4)
|
|
before_id = page.table.current_data()["id"]
|
|
page.table.verticalScrollBar().setValue(page.table.verticalScrollBar().maximum())
|
|
for _ in range(50):
|
|
if page.table.rowCount() == 30:
|
|
break
|
|
QTest.qWait(20)
|
|
assert page.table.rowCount() == 30 and page.table.current_data()["id"] == before_id
|
|
assert (
|
|
len({page.table.item(r, 0).data(Qt.ItemDataRole.UserRole)["id"] for r in range(30)}) == 30
|
|
)
|
|
page.refresh()
|
|
assert page.table.rowCount() == 30
|
|
assert [q["page_no"] for q in repo.calls[-2:]] == [1, 2]
|
|
page.table.verticalScrollBar().setValue(page.table.verticalScrollBar().maximum())
|
|
QTest.qWait(90)
|
|
assert page.table.rowCount() == 37 and not page.pager.has_more
|
|
edit = page.name_filter if module is prescription_library else page.patient_filter
|
|
edit.setText("changed")
|
|
page._search()
|
|
assert page.table.rowCount() == 15 and repo.calls[-1]["page_no"] == 1
|
|
assert page.pager.height() == 24 and page.layout().contentsMargins().bottom() == 8
|
|
page.close()
|
|
|
|
|
|
def test_local_effect_filter_continues_until_matching_rows_are_visible(app, monkeypatch):
|
|
monkeypatch.setattr(prescription_library, "run_async", inline)
|
|
page = prescription_library.PrescriptionLibraryPage(
|
|
Repository(), {"*"}, SimpleNamespace(id=1, root=1, role_ids=[])
|
|
)
|
|
page.resize(1250, 760)
|
|
page.show()
|
|
page.effect_filter.setCurrentIndex(page.effect_filter.findData("清热祛湿"))
|
|
page._search()
|
|
QTest.qWait(180)
|
|
assert page.table.rowCount() >= 15
|
|
assert page.table.item(0, 0).data(Qt.ItemDataRole.UserRole)["id"] > 15
|
|
page.close()
|
|
|
|
|
|
def test_audit_status_sort_keeps_actions_on_the_visible_prescription(app, monkeypatch):
|
|
monkeypatch.setattr(prescriptions, "run_async", inline)
|
|
|
|
class AuditRepository(Repository):
|
|
def get(self, **query):
|
|
result = super().get(**query)
|
|
for row in result["lists"]:
|
|
identifier = row["id"]
|
|
row.update(
|
|
audit_status=identifier % 3,
|
|
audit_remark="rejected reason" if identifier % 3 == 2 else "",
|
|
business_prescription_audit_rejected=identifier % 2 == 0,
|
|
business_prescription_audit_remark="business reason"
|
|
if identifier % 2 == 0
|
|
else "",
|
|
)
|
|
return result
|
|
|
|
list_prescriptions = get
|
|
|
|
page = prescriptions.PrescriptionsPage(AuditRepository(), {"*"}, SimpleNamespace(id=1))
|
|
page.refresh()
|
|
opened = []
|
|
page._view_selected = lambda: opened.append(page.table.current_data()["id"])
|
|
page.table.sortItems(6, Qt.SortOrder.DescendingOrder)
|
|
page.pager.load_more()
|
|
page.refresh()
|
|
assert page.table.rowCount() == 30
|
|
for row in range(page.table.rowCount()):
|
|
expected = page.table.item(row, 0).data(Qt.ItemDataRole.UserRole)["id"]
|
|
buttons = page.table.cellWidget(row, 2).findChildren(QPushButton)
|
|
next(button for button in buttons if button.accessibleName() == "查看处方").click()
|
|
assert opened[-1] == expected
|
|
page.close()
|
|
|
|
|
|
def test_highlighted_prescription_follows_selection_and_silent_list_refresh(app, monkeypatch):
|
|
monkeypatch.setattr(prescriptions, "run_async", inline)
|
|
rows = [
|
|
{"id": 10, "patient_id": 750, "patient_name": "A", "diagnosis_id": 95},
|
|
{"id": 11, "patient_id": 0, "patient_name": "B", "diagnosis_id": 96, "sn": ""},
|
|
]
|
|
|
|
def list_prescriptions(*, patient_name="", **_filters):
|
|
records = [Prescription.from_dict(row) for row in rows if patient_name in row["patient_name"]]
|
|
return {"lists": records, "count": len(records)}
|
|
|
|
page = prescriptions.PrescriptionsPage(
|
|
SimpleNamespace(list_prescriptions=list_prescriptions), {"*"}
|
|
)
|
|
events = []
|
|
page.selected_prescription_changed.connect(events.append)
|
|
page.resize(1250, 760)
|
|
page.show()
|
|
try:
|
|
app.processEvents()
|
|
table = page.table
|
|
table.sortItems(5, Qt.SortOrder.AscendingOrder)
|
|
QTest.mouseClick(
|
|
table.viewport(), Qt.MouseButton.LeftButton,
|
|
pos=table.visualItemRect(table.item(1, 0)).center(),
|
|
)
|
|
assert page.selected_prescription() == events[-1] == rows[1]
|
|
QTest.mouseClick(
|
|
table.viewport(), Qt.MouseButton.LeftButton,
|
|
pos=table.visualItemRect(table.item(0, 5)).center(),
|
|
)
|
|
assert page.selected_prescription() == events[-1] == rows[0]
|
|
QTest.keyClick(table.copy_text, Qt.Key.Key_Down)
|
|
assert page.selected_prescription() == events[-1] == rows[1]
|
|
QTest.keyClick(table, Qt.Key.Key_Up)
|
|
assert page.selected_prescription() == events[-1] == rows[0]
|
|
|
|
viewed = []
|
|
monkeypatch.setattr(page, "_view_selected", lambda: viewed.append(page.selected_prescription()))
|
|
buttons = table.cellWidget(1, 2).findChildren(QPushButton)
|
|
next(button for button in buttons if button.accessibleName() == "查看处方").click()
|
|
assert viewed == [rows[1]]
|
|
assert events[-1] == rows[1]
|
|
|
|
events.clear()
|
|
rows.reverse() # A different server order must preserve the highlighted prescription.
|
|
page.refresh()
|
|
page.hide()
|
|
page.show()
|
|
assert page.selected_prescription()["id"] == 11
|
|
assert events == []
|
|
rows[0]["diagnosis_id"] = 106
|
|
page.refresh()
|
|
assert events == [rows[0]]
|
|
|
|
table.clearSelection()
|
|
assert page.selected_prescription() == events[-1] == {}
|
|
events.clear()
|
|
page.refresh()
|
|
assert page.selected_prescription() == {}
|
|
assert events == []
|
|
page.patient_filter.setText("A")
|
|
page._search()
|
|
assert events == [rows[1]]
|
|
page.patient_filter.clear()
|
|
page._search()
|
|
table.selectRow(1)
|
|
assert page.selected_prescription()["id"] == 11
|
|
rows.pop(0)
|
|
events.clear()
|
|
page.refresh()
|
|
assert table.rowCount() == 1
|
|
assert page.selected_prescription() == {}
|
|
assert events == [{}]
|
|
|
|
table.selectRow(0)
|
|
page.patient_filter.setText("missing")
|
|
page._search()
|
|
assert page.selected_prescription() == events[-1] == {}
|
|
finally:
|
|
page.close()
|