Test Binance Spot REST API client.
()
| 146 | |
| 147 | |
| 148 | def test_spot_client(): |
| 149 | """Test Binance Spot REST API client.""" |
| 150 | print("=" * 60) |
| 151 | print("Testing Binance Spot REST API Client") |
| 152 | print("=" * 60) |
| 153 | |
| 154 | # Get API credentials from environment |
| 155 | api_key = os.getenv("BINANCE_API_KEY", "") |
| 156 | api_secret = os.getenv("BINANCE_SECRET_KEY", "") |
| 157 | testnet = os.getenv("BINANCE_TESTNET", "true").lower() == "true" |
| 158 | |
| 159 | if not api_key or not api_secret: |
| 160 | print("⚠️ BINANCE_API_KEY and BINANCE_SECRET_KEY not found in environment variables") |
| 161 | print(" Skipping Spot Client tests...\n") |
| 162 | return |
| 163 | |
| 164 | try: |
| 165 | # Initialize Spot client |
| 166 | spot_client = BinanceSpotClient( |
| 167 | api_key=api_key, |
| 168 | api_secret=api_secret, |
| 169 | testnet=testnet |
| 170 | ) |
| 171 | |
| 172 | print(f"✅ Spot Client initialized (testnet={testnet})") |
| 173 | print(f" Base URL: {spot_client.base_url}\n") |
| 174 | |
| 175 | # Test 1: Get exchange info (no authentication required) |
| 176 | print("Test 1: Getting exchange info (public endpoint)...") |
| 177 | try: |
| 178 | exchange_info = spot_client.exchange_info() |
| 179 | symbols_count = len(exchange_info.get('symbols', [])) |
| 180 | print(f"✅ Exchange info retrieved successfully") |
| 181 | print(f" Total symbols: {symbols_count}") |
| 182 | if symbols_count > 0: |
| 183 | sample_symbol = exchange_info['symbols'][0] |
| 184 | print(f" Sample symbol: {sample_symbol.get('symbol')} ({sample_symbol.get('status')})") |
| 185 | except Exception as e: |
| 186 | print(f"❌ Failed to get exchange info: {e}\n") |
| 187 | return |
| 188 | |
| 189 | print() |
| 190 | |
| 191 | # Test 2: Get account info (authentication required) |
| 192 | print("Test 2: Getting account info (authenticated endpoint)...") |
| 193 | try: |
| 194 | account_info = spot_client.account() |
| 195 | print(f"✅ Account info retrieved successfully") |
| 196 | print(f" Account type: {account_info.get('accountType')}") |
| 197 | print(f" Permissions: {account_info.get('permissions', [])}") |
| 198 | balances = account_info.get('balances', []) |
| 199 | non_zero_balances = [b for b in balances if float(b.get('free', 0)) + float(b.get('locked', 0)) > 0] |
| 200 | print(f" Non-zero balances: {len(non_zero_balances)}") |
| 201 | if non_zero_balances: |
| 202 | for balance in non_zero_balances[:3]: # Show first 3 |
| 203 | asset = balance.get('asset') |
| 204 | free = balance.get('free') |
| 205 | locked = balance.get('locked') |
nothing calls this directly
no test coverage detected