Skip to content

add tests for PythAccount and PythClient #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions tests/test_pyth_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import pytest

from _pytest.logging import LogCaptureFixture

from pythclient.solana import SolanaClient, SolanaPublicKey
from pythclient.pythaccounts import PythAccount, PythAccountType, PythProductAccount


BCH_PRODUCT_ACCOUNT_KEY_DEVNET = '89GseEmvNkzAMMEXcW9oTYzqRPXTsJ3BmNerXmgA1osV'
BCH_PRICE_ACCOUNT_KEY_DEVNET = '4EQrNZYk5KR1RnjyzbaaRbHsv8VqZWzSUtvx58wLsZbj'

PRODUCT_ACCOUNT_B64_DATA = ('1MOyoQIAAAACAAAAlwAAADAClHlZh5cpDjY4oXEsKb3iNn0OynlPd4sltaRy8ZLeBnN5bWJvbAdCQ0gv'
'VVNECmFzc2V0X3R5cGUGQ3J5cHRvDnF1b3RlX2N1cnJlbmN5A1VTRAtkZXNjcmlwdGlvbgdCQ0gvVVNE'
'DmdlbmVyaWNfc3ltYm9sBkJDSFVTRARiYXNlA0JDSA==')
Comment on lines +9 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make these into fixtures



@pytest.fixture
def solana_pubkey():
return SolanaPublicKey("AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J")


@pytest.fixture
def pyth_account(solana_pubkey, solana_client):
return PythAccount(
key=solana_pubkey,
solana=solana_client,
)


@ pytest.fixture
def product_account(solana_client: SolanaClient) -> PythProductAccount:
product_account = PythProductAccount(
key=SolanaPublicKey(BCH_PRODUCT_ACCOUNT_KEY_DEVNET),
solana=solana_client,
)
product_account.slot = 96866599
product_account.attrs = {
'asset_type': 'Crypto',
'symbol': 'BCH/USD',
'quote_currency': 'USD',
'description': 'BCH/USD',
'generic_symbol': 'BCHUSD',
'base': 'BCH'
}
product_account.first_price_account_key = SolanaPublicKey(
BCH_PRICE_ACCOUNT_KEY_DEVNET,
)
return product_account


def test_product_account_update_with_rpc_response_with_data(
product_account: PythProductAccount
):
assert product_account.slot == 96866599
assert product_account.lamports is None

slot = 96866600
value = {
'lamports': 1000000000,
'data': [
PRODUCT_ACCOUNT_B64_DATA,
'base64'
]
}

product_account.update_with_rpc_response(slot=slot, value=value)
assert product_account.slot == 96866600


def test_pyth_account_update_with_rpc_response_wrong_type(
pyth_account: PythAccount,
caplog: LogCaptureFixture
):
assert pyth_account.slot is None
assert pyth_account.lamports is None

slot = 96866600
value = {
'lamports': 1000000000,
'data': [
PRODUCT_ACCOUNT_B64_DATA,
'base64'
]
}

exc_message = f"wrong Pyth account type {PythAccountType.PRODUCT} for {type(pyth_account)}"
with pytest.raises(ValueError, match=exc_message):
pyth_account.update_with_rpc_response(slot=slot, value=value)


def test_pyth_account_update_with_rpc_response_no_data(
pyth_account: PythAccount,
caplog: LogCaptureFixture
):
assert pyth_account.slot is None
assert pyth_account.lamports is None

slot = 106498726
value = {
"lamports": 1000000000
}

exc_message = f'invalid account data response from Solana for key {pyth_account.key}: {value}'
with pytest.raises(ValueError, match=exc_message):
pyth_account.update_with_rpc_response(slot=slot, value=value)
assert exc_message in caplog.text
Loading