test_reporting_tools.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from __future__ import annotations
  2. import json
  3. import pytest
  4. from exec_mcp import repo, storage
  5. from exec_mcp.server import report_balance_snapshots, report_recent_trades
  6. @pytest.fixture()
  7. def temp_db(tmp_path, monkeypatch):
  8. db_path = tmp_path / "exec.sqlite3"
  9. monkeypatch.setattr(storage, "DB_PATH", db_path)
  10. storage.init_db()
  11. return db_path
  12. def _create_account() -> str:
  13. repo.create_account(display_name="strategy", venue="bitstamp", venue_account_ref="ref-1", api_key="k", api_secret="s")
  14. return repo.list_accounts(enabled_only=False)[0]["id"]
  15. def test_report_recent_trades_defaults_to_filled_and_sorts_newest_first(temp_db):
  16. account_id = _create_account()
  17. with storage.get_connection() as conn:
  18. rows = [
  19. ("rec-1", "filled", "client-a", "1994292738961401", "2026-04-09T08:20:50+00:00"),
  20. ("rec-2", "cancelled", "client-a", "1994292738961402", "2026-04-09T09:20:50+00:00"),
  21. ("rec-3", "filled", "client-b", "1994292738961403", "2026-04-09T10:20:50+00:00"),
  22. ("rec-4", "open", "client-a", "1994292738961404", "2026-04-09T11:20:50+00:00"),
  23. ]
  24. for record_id, status, client_id, order_id, stamp in rows:
  25. conn.execute(
  26. """
  27. INSERT INTO order_records
  28. (id, account_id, market, side, order_type, amount, price, expire_time, status, bitstamp_order_id, client_id, client_order_id, raw_json, created_at, updated_at)
  29. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  30. """,
  31. (
  32. record_id,
  33. account_id,
  34. "xrpusd",
  35. "buy",
  36. "limit",
  37. "10",
  38. "2.00",
  39. None,
  40. status,
  41. order_id,
  42. client_id,
  43. None,
  44. json.dumps({"id": order_id, "status": status}),
  45. stamp,
  46. stamp,
  47. ),
  48. )
  49. conn.commit()
  50. result = report_recent_trades(account_id=account_id)
  51. assert result["ok"] is True
  52. assert result["status"] == "filled"
  53. assert result["count"] == 2
  54. assert [row["bitstamp_order_id"] for row in result["orders"]] == ["1994292738961403", "1994292738961401"]
  55. def test_report_recent_trades_supports_all_statuses_and_client_filter(temp_db):
  56. account_id = _create_account()
  57. with storage.get_connection() as conn:
  58. for idx, (status, client_id, stamp) in enumerate(
  59. [
  60. ("filled", "client-a", "2026-04-09T08:20:50+00:00"),
  61. ("cancelled", "client-a", "2026-04-09T09:20:50+00:00"),
  62. ("filled", "client-b", "2026-04-09T10:20:50+00:00"),
  63. ],
  64. start=1,
  65. ):
  66. order_id = f"19942927389614{idx}"
  67. conn.execute(
  68. """
  69. INSERT INTO order_records
  70. (id, account_id, market, side, order_type, amount, price, expire_time, status, bitstamp_order_id, client_id, client_order_id, raw_json, created_at, updated_at)
  71. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  72. """,
  73. (
  74. f"rec-{idx}",
  75. account_id,
  76. "xrpusd",
  77. "sell",
  78. "limit",
  79. "10",
  80. "2.00",
  81. None,
  82. status,
  83. order_id,
  84. client_id,
  85. None,
  86. json.dumps({"id": order_id, "status": status}),
  87. stamp,
  88. stamp,
  89. ),
  90. )
  91. conn.commit()
  92. result = report_recent_trades(account_id=account_id, status="all", client_id="client-a", limit=10)
  93. assert result["ok"] is True
  94. assert result["status"] == "all"
  95. assert result["client_id"] == "client-a"
  96. assert [row["status"] for row in result["orders"]] == ["cancelled", "filled"]
  97. def test_report_balance_snapshots_returns_history_newest_first(temp_db):
  98. account_id = _create_account()
  99. repo.save_balance_snapshot(account_id=account_id, asset_code="BTC", balance=1.25, balance_value=11250.0, value_currency="USD")
  100. repo.save_balance_snapshot(account_id=account_id, asset_code="USD", balance=500.0, balance_value=500.0, value_currency="USD")
  101. result = report_balance_snapshots(account_id=account_id)
  102. assert result["ok"] is True
  103. assert result["count"] == 2
  104. assert [row["asset_code"] for row in result["snapshots"]] == ["USD", "BTC"]