|
| 1 | +import warnings |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from pyinjective.core.network import Network |
| 5 | + |
| 6 | + |
| 7 | +class TestAsyncClientDeprecationWarnings: |
| 8 | + @patch("pyinjective.async_client.asyncio.get_event_loop") |
| 9 | + @patch("pyinjective.async_client.ChainGrpcChainStream") |
| 10 | + @patch("pyinjective.async_client.ChainGrpcExchangeApi") |
| 11 | + @patch("pyinjective.async_client.ChainGrpcDistributionApi") |
| 12 | + @patch("pyinjective.async_client.ChainGrpcAuthZApi") |
| 13 | + @patch("pyinjective.async_client.ChainGrpcAuthApi") |
| 14 | + @patch("pyinjective.async_client.ChainGrpcBankApi") |
| 15 | + @patch("pyinjective.async_client.IndexerClient") |
| 16 | + def test_async_client_deprecation_warning(self, *mocks): |
| 17 | + """Test that creating an AsyncClient instance raises a deprecation warning with correct details.""" |
| 18 | + # Create a mock network to avoid actual network initialization |
| 19 | + mock_network = MagicMock(spec=Network) |
| 20 | + mock_network.chain_cookie_assistant = MagicMock() |
| 21 | + mock_network.create_chain_grpc_channel = MagicMock() |
| 22 | + mock_network.create_chain_stream_grpc_channel = MagicMock() |
| 23 | + mock_network.official_tokens_list_url = "https://example.com/tokens.json" |
| 24 | + |
| 25 | + # Import here to avoid early import issues |
| 26 | + from pyinjective.async_client import AsyncClient |
| 27 | + |
| 28 | + # Capture warnings |
| 29 | + with warnings.catch_warnings(record=True) as warning_list: |
| 30 | + warnings.simplefilter("always") # Ensure all warnings are captured |
| 31 | + |
| 32 | + # Create AsyncClient instance - this should trigger the deprecation warning |
| 33 | + client = AsyncClient(network=mock_network) |
| 34 | + |
| 35 | + # Find the AsyncClient deprecation warning |
| 36 | + async_client_warnings = [ |
| 37 | + w |
| 38 | + for w in warning_list |
| 39 | + if issubclass(w.category, DeprecationWarning) |
| 40 | + and "AsyncClient from pyinjective.async_client is deprecated" in str(w.message) |
| 41 | + ] |
| 42 | + |
| 43 | + # Should have exactly one warning |
| 44 | + assert len(async_client_warnings) == 1 |
| 45 | + |
| 46 | + warning = async_client_warnings[0] |
| 47 | + # Check warning message contains migration advice |
| 48 | + assert "Please use AsyncClient from pyinjective.async_client_v2 instead" in str(warning.message) |
| 49 | + # Check warning category |
| 50 | + assert warning.category == DeprecationWarning |
| 51 | + # Check stacklevel is working correctly (should point to this test file) |
| 52 | + assert "test_async_client_deprecation_warnings.py" in warning.filename |
| 53 | + |
| 54 | + # Verify the client was still created successfully |
| 55 | + assert client is not None |
| 56 | + assert hasattr(client, "network") |
| 57 | + assert client.network == mock_network |
0 commit comments