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