更新
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
"""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
|
||||
|
||||
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):
|
||||
from PySide6.QtWidgets import QPushButton
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user