|
| 1 | +"""Tests for BinanceRegionException and region validation.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from binance.client import Client |
| 5 | +from binance.async_client import AsyncClient |
| 6 | +from binance.exceptions import BinanceRegionException |
| 7 | + |
| 8 | + |
| 9 | +class TestBinanceRegionException: |
| 10 | + """Tests for the BinanceRegionException class itself.""" |
| 11 | + |
| 12 | + def test_exception_attributes(self): |
| 13 | + """Test that exception has correct attributes.""" |
| 14 | + exc = BinanceRegionException("us", "com", "test_endpoint") |
| 15 | + assert exc.required_tld == "us" |
| 16 | + assert exc.actual_tld == "com" |
| 17 | + assert exc.endpoint_name == "test_endpoint" |
| 18 | + |
| 19 | + def test_exception_message_format(self): |
| 20 | + """Test that exception message is properly formatted.""" |
| 21 | + exc = BinanceRegionException("us", "com", "get_staking_asset_us") |
| 22 | + assert "binance.us" in str(exc) |
| 23 | + assert "binance.com" in str(exc) |
| 24 | + assert "get_staking_asset_us" in str(exc) |
| 25 | + |
| 26 | + def test_exception_default_endpoint_name(self): |
| 27 | + """Test that endpoint_name defaults to 'endpoint'.""" |
| 28 | + exc = BinanceRegionException("us", "com") |
| 29 | + assert exc.endpoint_name == "endpoint" |
| 30 | + assert "endpoint is only available" in str(exc) |
| 31 | + |
| 32 | + |
| 33 | +class TestSyncClientRegionValidation: |
| 34 | + """Tests for region validation in synchronous Client.""" |
| 35 | + |
| 36 | + def test_get_staking_asset_us_wrong_tld(self): |
| 37 | + """Test that get_staking_asset_us raises exception for non-US client.""" |
| 38 | + client = Client("test_key", "test_secret", tld="com", ping=False) |
| 39 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 40 | + client.get_staking_asset_us() |
| 41 | + assert exc_info.value.required_tld == "us" |
| 42 | + assert exc_info.value.actual_tld == "com" |
| 43 | + assert exc_info.value.endpoint_name == "get_staking_asset_us" |
| 44 | + |
| 45 | + def test_stake_asset_us_wrong_tld(self): |
| 46 | + """Test that stake_asset_us raises exception for non-US client.""" |
| 47 | + client = Client("test_key", "test_secret", tld="com", ping=False) |
| 48 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 49 | + client.stake_asset_us() |
| 50 | + assert exc_info.value.required_tld == "us" |
| 51 | + assert exc_info.value.endpoint_name == "stake_asset_us" |
| 52 | + |
| 53 | + def test_unstake_asset_us_wrong_tld(self): |
| 54 | + """Test that unstake_asset_us raises exception for non-US client.""" |
| 55 | + client = Client("test_key", "test_secret", tld="com", ping=False) |
| 56 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 57 | + client.unstake_asset_us() |
| 58 | + assert exc_info.value.required_tld == "us" |
| 59 | + assert exc_info.value.endpoint_name == "unstake_asset_us" |
| 60 | + |
| 61 | + def test_get_staking_balance_us_wrong_tld(self): |
| 62 | + """Test that get_staking_balance_us raises exception for non-US client.""" |
| 63 | + client = Client("test_key", "test_secret", tld="com", ping=False) |
| 64 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 65 | + client.get_staking_balance_us() |
| 66 | + assert exc_info.value.required_tld == "us" |
| 67 | + assert exc_info.value.endpoint_name == "get_staking_balance_us" |
| 68 | + |
| 69 | + def test_get_staking_history_us_wrong_tld(self): |
| 70 | + """Test that get_staking_history_us raises exception for non-US client.""" |
| 71 | + client = Client("test_key", "test_secret", tld="com", ping=False) |
| 72 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 73 | + client.get_staking_history_us() |
| 74 | + assert exc_info.value.required_tld == "us" |
| 75 | + assert exc_info.value.endpoint_name == "get_staking_history_us" |
| 76 | + |
| 77 | + def test_get_staking_rewards_history_us_wrong_tld(self): |
| 78 | + """Test that get_staking_rewards_history_us raises exception for non-US client.""" |
| 79 | + client = Client("test_key", "test_secret", tld="com", ping=False) |
| 80 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 81 | + client.get_staking_rewards_history_us() |
| 82 | + assert exc_info.value.required_tld == "us" |
| 83 | + assert exc_info.value.endpoint_name == "get_staking_rewards_history_us" |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.asyncio |
| 87 | +class TestAsyncClientRegionValidation: |
| 88 | + """Tests for region validation in asynchronous AsyncClient.""" |
| 89 | + |
| 90 | + async def test_get_staking_asset_us_wrong_tld_async(self): |
| 91 | + """Test that async get_staking_asset_us raises exception for non-US client.""" |
| 92 | + client = AsyncClient("test_key", "test_secret", tld="com") |
| 93 | + try: |
| 94 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 95 | + await client.get_staking_asset_us() |
| 96 | + assert exc_info.value.required_tld == "us" |
| 97 | + assert exc_info.value.actual_tld == "com" |
| 98 | + assert exc_info.value.endpoint_name == "get_staking_asset_us" |
| 99 | + finally: |
| 100 | + await client.close_connection() |
| 101 | + |
| 102 | + async def test_stake_asset_us_wrong_tld_async(self): |
| 103 | + """Test that async stake_asset_us raises exception for non-US client.""" |
| 104 | + client = AsyncClient("test_key", "test_secret", tld="com") |
| 105 | + try: |
| 106 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 107 | + await client.stake_asset_us() |
| 108 | + assert exc_info.value.required_tld == "us" |
| 109 | + assert exc_info.value.endpoint_name == "stake_asset_us" |
| 110 | + finally: |
| 111 | + await client.close_connection() |
| 112 | + |
| 113 | + async def test_unstake_asset_us_wrong_tld_async(self): |
| 114 | + """Test that async unstake_asset_us raises exception for non-US client.""" |
| 115 | + client = AsyncClient("test_key", "test_secret", tld="com") |
| 116 | + try: |
| 117 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 118 | + await client.unstake_asset_us() |
| 119 | + assert exc_info.value.required_tld == "us" |
| 120 | + assert exc_info.value.endpoint_name == "unstake_asset_us" |
| 121 | + finally: |
| 122 | + await client.close_connection() |
| 123 | + |
| 124 | + async def test_get_staking_balance_us_wrong_tld_async(self): |
| 125 | + """Test that async get_staking_balance_us raises exception for non-US client.""" |
| 126 | + client = AsyncClient("test_key", "test_secret", tld="com") |
| 127 | + try: |
| 128 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 129 | + await client.get_staking_balance_us() |
| 130 | + assert exc_info.value.required_tld == "us" |
| 131 | + assert exc_info.value.endpoint_name == "get_staking_balance_us" |
| 132 | + finally: |
| 133 | + await client.close_connection() |
| 134 | + |
| 135 | + async def test_get_staking_history_us_wrong_tld_async(self): |
| 136 | + """Test that async get_staking_history_us raises exception for non-US client.""" |
| 137 | + client = AsyncClient("test_key", "test_secret", tld="com") |
| 138 | + try: |
| 139 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 140 | + await client.get_staking_history_us() |
| 141 | + assert exc_info.value.required_tld == "us" |
| 142 | + assert exc_info.value.endpoint_name == "get_staking_history_us" |
| 143 | + finally: |
| 144 | + await client.close_connection() |
| 145 | + |
| 146 | + async def test_get_staking_rewards_history_us_wrong_tld_async(self): |
| 147 | + """Test that async get_staking_rewards_history_us raises exception for non-US client.""" |
| 148 | + client = AsyncClient("test_key", "test_secret", tld="com") |
| 149 | + try: |
| 150 | + with pytest.raises(BinanceRegionException) as exc_info: |
| 151 | + await client.get_staking_rewards_history_us() |
| 152 | + assert exc_info.value.required_tld == "us" |
| 153 | + assert exc_info.value.endpoint_name == "get_staking_rewards_history_us" |
| 154 | + finally: |
| 155 | + await client.close_connection() |
0 commit comments