|
| 1 | +from _pytest.logging import LogCaptureFixture |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pytest_mock import MockerFixture |
| 5 | + |
| 6 | +from mock import AsyncMock |
| 7 | + |
| 8 | + |
| 9 | +from pythclient.solana import SolanaAccount, SolanaPublicKey, SolanaClient |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def solana_pubkey() -> SolanaPublicKey: |
| 14 | + return SolanaPublicKey("AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J") |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def solana_account(solana_pubkey: SolanaPublicKey, solana_client: SolanaClient) -> SolanaAccount: |
| 19 | + return SolanaAccount( |
| 20 | + key=solana_pubkey, |
| 21 | + solana=solana_client, |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture() |
| 26 | +def mock_get_account_info(mocker: MockerFixture) -> AsyncMock: |
| 27 | + async_mock = AsyncMock() |
| 28 | + mocker.patch('pythclient.solana.SolanaClient.get_account_info', side_effect=async_mock) |
| 29 | + return async_mock |
| 30 | + |
| 31 | + |
| 32 | +def test_solana_account_update_with_rpc_response( |
| 33 | + solana_pubkey: SolanaPublicKey, solana_client: SolanaClient |
| 34 | +) -> None: |
| 35 | + actual = SolanaAccount( |
| 36 | + key=solana_pubkey, |
| 37 | + solana=solana_client, |
| 38 | + ) |
| 39 | + assert actual.slot is None |
| 40 | + assert actual.lamports is None |
| 41 | + |
| 42 | + slot = 106498726 |
| 43 | + value = { |
| 44 | + "lamports": 1000000000 |
| 45 | + } |
| 46 | + |
| 47 | + actual.update_with_rpc_response(slot=slot, value=value) |
| 48 | + |
| 49 | + assert actual.slot == slot |
| 50 | + assert actual.lamports == value["lamports"] |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.asyncio |
| 54 | +async def test_solana_account_update_success(mock_get_account_info: AsyncMock, |
| 55 | + solana_pubkey: SolanaPublicKey, solana_client: SolanaClient) -> None: |
| 56 | + actual = SolanaAccount( |
| 57 | + key=solana_pubkey, |
| 58 | + solana=solana_client, |
| 59 | + ) |
| 60 | + |
| 61 | + mock_get_account_info.return_value = {'context': {'slot': 93752509}, 'value': {'lamports': 1000000001}} |
| 62 | + |
| 63 | + await actual.update() |
| 64 | + assert actual.slot == mock_get_account_info.return_value['context']['slot'] |
| 65 | + assert actual.lamports == mock_get_account_info.return_value['value']['lamports'] |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.asyncio |
| 69 | +async def test_solana_account_update_fail(mock_get_account_info: AsyncMock, |
| 70 | + caplog: LogCaptureFixture, |
| 71 | + solana_pubkey: SolanaPublicKey, |
| 72 | + solana_client: SolanaClient) -> None: |
| 73 | + actual = SolanaAccount( |
| 74 | + key=solana_pubkey, |
| 75 | + solana=solana_client, |
| 76 | + ) |
| 77 | + mock_get_account_info.return_value = {'value': {'lamports': 1000000001}} |
| 78 | + exc_message = f'error while updating account {solana_pubkey}' |
| 79 | + await actual.update() |
| 80 | + assert exc_message in caplog.text |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.asyncio |
| 84 | +async def test_solana_account_update_null(mock_get_account_info: AsyncMock, |
| 85 | + caplog: LogCaptureFixture, |
| 86 | + solana_pubkey: SolanaPublicKey, |
| 87 | + solana_client: SolanaClient) -> None: |
| 88 | + actual = SolanaAccount( |
| 89 | + key=solana_pubkey, |
| 90 | + solana=solana_client, |
| 91 | + ) |
| 92 | + mock_get_account_info.return_value = {'context': {'slot': 93752509}} |
| 93 | + exc_message = f'got null value from Solana getAccountInfo for {solana_pubkey}; ' \ |
| 94 | + + f'non-existent account? {mock_get_account_info.return_value}' |
| 95 | + await actual.update() |
| 96 | + assert exc_message in caplog.text |
| 97 | + |
| 98 | + |
| 99 | +def test_solana_account_str(solana_account: SolanaAccount) -> None: |
| 100 | + actual = str(solana_account) |
| 101 | + expected = f"SolanaAccount ({solana_account.key})" |
| 102 | + assert actual == expected |
0 commit comments