from __future__ import annotations from unittest.mock import MagicMock import pytest @pytest.mark.asyncio async def test_init_paper_mode(client, mock_trading): assert client.paper is True assert client._trading is mock_trading @pytest.mark.asyncio async def test_get_account_calls_trading(client, mock_trading): mock_trading.get_account.return_value = MagicMock( model_dump=lambda: {"equity": 100000, "cash": 50000} ) result = await client.get_account() mock_trading.get_account.assert_called_once() assert result["equity"] == 100000 @pytest.mark.asyncio async def test_get_positions_returns_list(client, mock_trading): pos_mock = MagicMock(model_dump=lambda: {"symbol": "AAPL", "qty": 10}) mock_trading.get_all_positions.return_value = [pos_mock] result = await client.get_positions() assert len(result) == 1 assert result[0]["symbol"] == "AAPL" @pytest.mark.asyncio async def test_place_market_order_stocks(client, mock_trading): order_mock = MagicMock(model_dump=lambda: {"id": "o123", "symbol": "AAPL"}) mock_trading.submit_order.return_value = order_mock result = await client.place_order( symbol="AAPL", side="buy", qty=1, order_type="market", asset_class="stocks", ) assert result["id"] == "o123" assert mock_trading.submit_order.called @pytest.mark.asyncio async def test_place_limit_order_requires_price(client): with pytest.raises(ValueError, match="limit_price"): await client.place_order( symbol="AAPL", side="buy", qty=1, order_type="limit", ) @pytest.mark.asyncio async def test_cancel_order(client, mock_trading): mock_trading.cancel_order_by_id.return_value = None result = await client.cancel_order("o1") mock_trading.cancel_order_by_id.assert_called_once_with("o1") assert result == {"order_id": "o1", "canceled": True} @pytest.mark.asyncio async def test_close_position_no_options(client, mock_trading): order_mock = MagicMock(model_dump=lambda: {"id": "close-1"}) mock_trading.close_position.return_value = order_mock result = await client.close_position("AAPL") assert mock_trading.close_position.called assert result["id"] == "close-1" @pytest.mark.asyncio async def test_get_clock(client, mock_trading): clock_mock = MagicMock(model_dump=lambda: {"is_open": True, "next_close": "2026-04-21T20:00:00Z"}) mock_trading.get_clock.return_value = clock_mock result = await client.get_clock() assert result["is_open"] is True @pytest.mark.asyncio async def test_invalid_asset_class(client): with pytest.raises(ValueError, match="invalid asset_class"): await client.get_ticker("AAPL", asset_class="forex")