test_bitstamp_private_ws.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from __future__ import annotations
  2. from exec_mcp import repo, storage
  3. from exec_mcp.bitstamp_private_ws import _get_token
  4. from exec_mcp.services_bitstamp import clear_bitstamp_trading_client
  5. class _FakeBitstampClient:
  6. constructions = 0
  7. def __init__(self, username, api_key, api_secret):
  8. type(self).constructions += 1
  9. self.username = username
  10. self.api_key = api_key
  11. self.api_secret = api_secret
  12. def websocket_token(self):
  13. return {"token": "token-123", "user_id": "user-456"}
  14. def test_get_token_reuses_cached_bitstamp_client(tmp_path, monkeypatch):
  15. db_path = tmp_path / "exec.sqlite3"
  16. monkeypatch.setattr(storage, "DB_PATH", db_path)
  17. storage.init_db()
  18. repo.create_account(display_name="strategy", venue="bitstamp", venue_account_ref="ref-1", api_key="k", api_secret="s")
  19. account_id = repo.list_accounts(enabled_only=False)[0]["id"]
  20. clear_bitstamp_trading_client(account_id)
  21. monkeypatch.setattr("exec_mcp.services_bitstamp.BitstampClient", _FakeBitstampClient)
  22. _FakeBitstampClient.constructions = 0
  23. first = _get_token(account_id)
  24. second = _get_token(account_id)
  25. assert first == {"token": "token-123", "user_id": "user-456"}
  26. assert second == first
  27. assert _FakeBitstampClient.constructions == 1