feat: 添加网格监控并修复程序卡死与打包启动
This commit is contained in:
229
tests/test_ui.py
229
tests/test_ui.py
@@ -1,7 +1,19 @@
|
||||
from decimal import Decimal
|
||||
from datetime import date, datetime
|
||||
import threading
|
||||
import time
|
||||
|
||||
from grid_trading.domain.models import Instrument
|
||||
from grid_trading.domain.models import (
|
||||
GridDirection,
|
||||
GridAlertSource,
|
||||
GridAlertState,
|
||||
GridMonitorItem,
|
||||
GridMonitorSnapshot,
|
||||
Instrument,
|
||||
Trade,
|
||||
TradeGroup,
|
||||
TradeSide,
|
||||
)
|
||||
|
||||
|
||||
def test_formatters_render_money_percent_and_empty_values():
|
||||
@@ -28,7 +40,14 @@ def test_main_window_can_be_constructed_offscreen(tmp_path, monkeypatch):
|
||||
|
||||
assert window.windowTitle() == "Grid Trading Manager"
|
||||
assert window.holdings_table.columnCount() > 0
|
||||
assert window.holdings_table.columnCount() == 11
|
||||
assert "底仓" not in window.HOLDING_COLUMNS
|
||||
assert "网格仓" not in window.HOLDING_COLUMNS
|
||||
assert window.trades_table.columnCount() == 9
|
||||
assert window.TRADE_COLUMNS[:2] == ["代码", "名称"]
|
||||
assert "分组" not in window.TRADE_COLUMNS
|
||||
assert any(button.text() == "刷新行情" for button in window.findChildren(QPushButton))
|
||||
assert any(button.text() == "删除标的" for button in window.findChildren(QPushButton))
|
||||
summary_titles = [label.text() for label in window.findChildren(QLabel)]
|
||||
assert "账户权益" in summary_titles
|
||||
assert "总资产" not in summary_titles
|
||||
@@ -50,9 +69,10 @@ def test_main_window_contains_grid_level_table(tmp_path, monkeypatch):
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
window = MainWindow(service)
|
||||
|
||||
assert window.grid_levels_table.columnCount() == 7
|
||||
assert window.grid_levels_table.columnCount() == 9
|
||||
assert window.GRID_LEVEL_COLUMNS[:2] == ["代码", "名称"]
|
||||
tab_titles = _tab_titles(window.details_tabs)
|
||||
assert tab_titles == ["网格档位", "待卖网格", "配对明细", "最近成交"]
|
||||
assert tab_titles == ["网格档位", "未完成网格", "配对明细", "最近成交"]
|
||||
assert isinstance(window.details_tabs, QTabWidget)
|
||||
|
||||
window.close()
|
||||
@@ -72,9 +92,11 @@ def test_main_window_contains_open_grid_lots_table(tmp_path, monkeypatch):
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
window = MainWindow(service)
|
||||
|
||||
assert window.open_grid_lots_table.columnCount() == 8
|
||||
assert "待卖网格" in _tab_titles(window.details_tabs)
|
||||
assert window.grid_matches_table.columnCount() == 9
|
||||
assert window.open_grid_lots_table.columnCount() == 11
|
||||
assert window.OPEN_GRID_LOT_COLUMNS[:2] == ["代码", "名称"]
|
||||
assert "未完成网格" in _tab_titles(window.details_tabs)
|
||||
assert window.grid_matches_table.columnCount() == 13
|
||||
assert window.GRID_MATCH_COLUMNS[:2] == ["代码", "名称"]
|
||||
assert "配对明细" in _tab_titles(window.details_tabs)
|
||||
|
||||
window.close()
|
||||
@@ -82,6 +104,98 @@ def test_main_window_contains_open_grid_lots_table(tmp_path, monkeypatch):
|
||||
app.processEvents()
|
||||
|
||||
|
||||
def test_detail_tables_show_and_follow_selected_instrument(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from grid_trading.services.trading_service import TradingService
|
||||
from grid_trading.ui.main_window import MainWindow
|
||||
|
||||
app = QApplication.instance() or QApplication([])
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
service.ensure_defaults()
|
||||
account = service.get_active_account()
|
||||
first = service.add_instrument(Instrument(id=None, code="000001", name="平安银行"))
|
||||
second = service.add_instrument(Instrument(id=None, code="600000", name="浦发银行"))
|
||||
for instrument in (first, second):
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=instrument.id,
|
||||
trade_date=date(2026, 7, 27),
|
||||
side=TradeSide.BUY,
|
||||
price=Decimal("10"),
|
||||
quantity=200,
|
||||
trade_group=TradeGroup.GRID,
|
||||
grid_direction=GridDirection.FORWARD,
|
||||
)
|
||||
)
|
||||
service.save_trade(
|
||||
Trade(
|
||||
id=None,
|
||||
account_id=account.id,
|
||||
instrument_id=first.id,
|
||||
trade_date=date(2026, 7, 28),
|
||||
side=TradeSide.SELL,
|
||||
price=Decimal("10.3"),
|
||||
quantity=100,
|
||||
trade_group=TradeGroup.GRID,
|
||||
grid_direction=GridDirection.FORWARD,
|
||||
)
|
||||
)
|
||||
window = MainWindow(service)
|
||||
|
||||
window.holdings_table.selectRow(0)
|
||||
app.processEvents()
|
||||
assert window.trades_table.item(0, 0).text() == "000001"
|
||||
assert window.trades_table.item(0, 1).text() == "平安银行"
|
||||
assert window.open_grid_lots_table.item(0, 0).text() == "000001"
|
||||
assert window.open_grid_lots_table.item(0, 1).text() == "平安银行"
|
||||
assert window.grid_matches_table.item(0, 0).text() == "000001"
|
||||
assert window.grid_matches_table.item(0, 1).text() == "平安银行"
|
||||
|
||||
window.holdings_table.selectRow(1)
|
||||
app.processEvents()
|
||||
assert window.trades_table.item(0, 0).text() == "600000"
|
||||
assert window.trades_table.item(0, 1).text() == "浦发银行"
|
||||
assert window.open_grid_lots_table.item(0, 0).text() == "600000"
|
||||
assert window.open_grid_lots_table.item(0, 1).text() == "浦发银行"
|
||||
|
||||
window.close()
|
||||
service.close()
|
||||
app.processEvents()
|
||||
|
||||
|
||||
def test_main_window_deletes_selected_instrument(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from PySide6.QtWidgets import QApplication, QMessageBox
|
||||
|
||||
from grid_trading.services.trading_service import TradingService
|
||||
from grid_trading.ui.main_window import MainWindow
|
||||
|
||||
app = QApplication.instance() or QApplication([])
|
||||
service = TradingService(tmp_path / "grid.db")
|
||||
instrument = service.add_instrument(Instrument(id=None, code="600000", name="浦发银行"))
|
||||
window = MainWindow(service)
|
||||
window.holdings_table.selectRow(0)
|
||||
monkeypatch.setattr(
|
||||
QMessageBox,
|
||||
"question",
|
||||
lambda *args, **kwargs: QMessageBox.StandardButton.Yes,
|
||||
)
|
||||
|
||||
window._delete_selected_instrument()
|
||||
|
||||
assert service.list_instruments() == []
|
||||
assert service.repository.get_instrument(instrument.id) is None
|
||||
window.close()
|
||||
service.close()
|
||||
app.processEvents()
|
||||
|
||||
|
||||
def test_main_window_applies_light_theme(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
@@ -144,8 +258,8 @@ def test_profit_and_loss_cells_use_a_share_colors(tmp_path, monkeypatch):
|
||||
window._fill_holdings_table()
|
||||
window.holdings_table.blockSignals(False)
|
||||
|
||||
profit_item = window.holdings_table.item(0, 10)
|
||||
loss_item = window.holdings_table.item(0, 11)
|
||||
profit_item = window.holdings_table.item(0, 8)
|
||||
loss_item = window.holdings_table.item(0, 9)
|
||||
assert profit_item.foreground().color() == QColor("#c62828")
|
||||
assert profit_item.font().bold()
|
||||
assert loss_item.foreground().color() == QColor("#2e7d32")
|
||||
@@ -210,12 +324,56 @@ def test_trade_dialog_labels_price_as_trade_price(tmp_path, monkeypatch):
|
||||
|
||||
labels = [label.text() for label in dialog.findChildren(QLabel)]
|
||||
assert "成交价" in labels
|
||||
assert "交易类型" in labels
|
||||
assert "分组" not in labels
|
||||
assert not hasattr(dialog, "group_edit")
|
||||
assert dialog.to_trade().trade_group is TradeGroup.OTHER
|
||||
|
||||
dialog.close()
|
||||
service.close()
|
||||
app.processEvents()
|
||||
|
||||
|
||||
def test_floating_monitor_renders_reached_target_offscreen(monkeypatch):
|
||||
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from grid_trading.ui.floating_monitor import FloatingMonitorWindow
|
||||
|
||||
app = QApplication.instance() or QApplication([])
|
||||
window = FloatingMonitorWindow()
|
||||
window.update_snapshot(
|
||||
GridMonitorSnapshot(
|
||||
items=[
|
||||
GridMonitorItem(
|
||||
target_key="manual:test",
|
||||
instrument_id=1,
|
||||
code="510300",
|
||||
name="沪深300ETF",
|
||||
side=TradeSide.BUY,
|
||||
target_price=Decimal("3.88"),
|
||||
quantity=1200,
|
||||
current_price=Decimal("3.87"),
|
||||
state=GridAlertState.REACHED,
|
||||
distance_pct=Decimal("-0.0025"),
|
||||
source=GridAlertSource.MANUAL,
|
||||
)
|
||||
],
|
||||
newly_reached=[],
|
||||
evaluated_at=datetime(2026, 7, 17, 10, 30),
|
||||
)
|
||||
)
|
||||
|
||||
assert window.table.rowCount() == 1
|
||||
assert window.table.item(0, 5).text() == "已到价"
|
||||
window.table.selectRow(0)
|
||||
assert window.remove_button.isEnabled()
|
||||
|
||||
window.close_permanently()
|
||||
app.processEvents()
|
||||
|
||||
|
||||
def _tab_titles(tabs):
|
||||
return [tabs.tabText(index) for index in range(tabs.count())]
|
||||
|
||||
@@ -246,12 +404,63 @@ def test_quote_refresh_does_not_block_main_window(tmp_path, monkeypatch):
|
||||
assert elapsed < 0.15
|
||||
|
||||
deadline = time.perf_counter() + 2
|
||||
while getattr(window, "_quote_thread", None) is not None and time.perf_counter() < deadline:
|
||||
while window.quote_monitor.is_refreshing and time.perf_counter() < deadline:
|
||||
app.processEvents()
|
||||
time.sleep(0.01)
|
||||
|
||||
assert getattr(window, "_quote_thread", None) is None
|
||||
assert not window.quote_monitor.is_refreshing
|
||||
|
||||
window.close()
|
||||
service.close()
|
||||
app.processEvents()
|
||||
|
||||
|
||||
def test_close_during_quote_refresh_does_not_block_main_window(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("QT_QPA_PLATFORM", "offscreen")
|
||||
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from grid_trading.services.trading_service import TradingService
|
||||
from grid_trading.ui.main_window import MainWindow
|
||||
|
||||
request_started = threading.Event()
|
||||
release_request = threading.Event()
|
||||
|
||||
class BlockingQuoteProvider:
|
||||
def fetch_quotes(self, instruments):
|
||||
request_started.set()
|
||||
release_request.wait(1)
|
||||
return {}
|
||||
|
||||
app = QApplication.instance() or QApplication([])
|
||||
service = TradingService(tmp_path / "grid.db", quote_provider=BlockingQuoteProvider())
|
||||
service.ensure_defaults()
|
||||
service.add_instrument(Instrument(id=None, code="000001", name="Ping An Bank"))
|
||||
window = MainWindow(service)
|
||||
window.show()
|
||||
window._refresh_quotes()
|
||||
|
||||
deadline = time.perf_counter() + 1
|
||||
while not request_started.is_set() and time.perf_counter() < deadline:
|
||||
app.processEvents()
|
||||
time.sleep(0.01)
|
||||
assert request_started.is_set()
|
||||
|
||||
started_at = time.perf_counter()
|
||||
window.close()
|
||||
elapsed = time.perf_counter() - started_at
|
||||
|
||||
assert elapsed < 0.15
|
||||
assert not window.isVisible()
|
||||
assert window.quote_monitor.is_refreshing
|
||||
|
||||
release_request.set()
|
||||
deadline = time.perf_counter() + 2
|
||||
while (window.quote_monitor.is_refreshing or window._close_pending) and time.perf_counter() < deadline:
|
||||
app.processEvents()
|
||||
time.sleep(0.01)
|
||||
|
||||
assert not window.quote_monitor.is_refreshing
|
||||
assert not window._close_pending
|
||||
service.close()
|
||||
app.processEvents()
|
||||
|
||||
Reference in New Issue
Block a user