test_client_id_orders.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import annotations
  2. from pathlib import Path
  3. import pytest
  4. from exec_mcp import storage
  5. from exec_mcp import repo
  6. from exec_mcp.services_orders import get_open_orders, place_order
  7. class _FakeTrading:
  8. def buy_order(self, **kwargs):
  9. return {"id": "12345", "status": "open", "client_order_id": "broker-client-1", **kwargs}
  10. def buy_gtd_order(self, **kwargs):
  11. return {"id": "12345", "status": "open", "client_order_id": "broker-client-1", **kwargs}
  12. def sell_order(self, **kwargs):
  13. return {"id": "12345", "status": "open", "client_order_id": "broker-client-1", **kwargs}
  14. def sell_gtd_order(self, **kwargs):
  15. return {"id": "12345", "status": "open", "client_order_id": "broker-client-1", **kwargs}
  16. class _FakeClient:
  17. def __init__(self):
  18. self.trading = _FakeTrading()
  19. @pytest.fixture()
  20. def temp_db(tmp_path, monkeypatch):
  21. db_path = tmp_path / "exec.sqlite3"
  22. monkeypatch.setattr(storage, "DB_PATH", db_path)
  23. storage.init_db()
  24. return db_path
  25. def test_place_order_rejects_non_string_client_id(temp_db, monkeypatch):
  26. monkeypatch.setattr("exec_mcp.services_orders._get_client", lambda account_id: _FakeClient())
  27. monkeypatch.setattr("exec_mcp.services_orders.load_market_by_symbol", lambda market: {"base_decimals": 8, "counter_decimals": 2, "minimum_order_value": "0", "counter_currency": "USD"})
  28. with pytest.raises(Exception) as exc:
  29. place_order(
  30. account_id="acct-1",
  31. market="xrpusd",
  32. side="buy",
  33. order_type="market",
  34. amount="10",
  35. client_id=123,
  36. )
  37. assert "client_id must be a string" in str(exc.value)
  38. def test_place_order_persists_client_id_and_get_open_orders_filters(temp_db, monkeypatch):
  39. monkeypatch.setattr("exec_mcp.services_orders._get_client", lambda account_id: _FakeClient())
  40. monkeypatch.setattr("exec_mcp.services_orders.load_market_by_symbol", lambda market: {"base_decimals": 8, "counter_decimals": 2, "minimum_order_value": "0", "counter_currency": "USD"})
  41. repo.create_account(display_name="strategy", venue="bitstamp", venue_account_ref="ref-1", api_key="k", api_secret="s")
  42. result = place_order(
  43. account_id=repo.list_accounts(enabled_only=False)[0]["id"],
  44. market="xrpusd",
  45. side="buy",
  46. order_type="market",
  47. amount="10",
  48. client_id="strategy-a:v1:abc123",
  49. )
  50. assert result["ok"] is True
  51. orders = get_open_orders(account_id=repo.list_accounts(enabled_only=False)[0]["id"], client_id="strategy-a:v1:abc123")
  52. assert orders["ok"] is True
  53. assert orders["client_id"] == "strategy-a:v1:abc123"
  54. assert len(orders["orders"]) == 1
  55. assert orders["orders"][0]["client_id"] == "strategy-a:v1:abc123"