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