Skip to content

Commit 416ae1f

Browse files
author
Jeff Schroeder
committed
Full test coverage of PythMappingAccount
1 parent 3cd14e6 commit 416ae1f

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

tests/test_mapping_account.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import pytest
2+
3+
from pythclient.pythaccounts import PythMappingAccount, _VERSION_2
4+
from pythclient.solana import SolanaPublicKey
5+
6+
7+
@pytest.fixture
8+
def mapping_account_bytes():
9+
"""
10+
Put a debugger breakpoint in PythMappingAccount.update_from() at the top.
11+
Get the mapping account number of entries and 3 keys:
12+
13+
fmt_size = struct.calcsize(fmt)
14+
intermediate_buffer = buffer[offset:offset + fmt_size + (SolanaPublicKey.LENGTH * 3)]
15+
16+
Replace the num_keys bytes with 3:
17+
18+
num_entries_bytes = int(3).to_bytes(4, 'little')
19+
product_account_bytes = num_entries_bytes + intermediate_buffer[struct.calcsize("<I"):]
20+
21+
Render those into a pasteable form with:
22+
23+
print(list(product_account_bytes))
24+
25+
"""
26+
return bytes(
27+
[
28+
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30+
72, 214, 3, 61, 115, 62, 39, 149, 12, 46, 3, 81, 226, 80,
31+
84, 145, 205, 145, 84, 130, 79, 113, 109, 149, 19, 81, 76,
32+
116, 185, 249, 143, 88, 200, 12, 11, 20, 138, 186, 153, 75,
33+
37, 102, 234, 84, 167, 143, 240, 142, 250, 61, 94, 14, 195,
34+
208, 206, 234, 12, 250, 160, 63, 236, 37, 238, 162, 53, 21,
35+
179, 134, 30, 143, 233, 62, 95, 84, 11, 164, 7, 124, 33,
36+
100, 4, 120, 43, 134, 213, 231, 128, 119, 179, 203, 253,
37+
39, 49, 58, 179, 188
38+
]
39+
)
40+
41+
42+
@pytest.fixture
43+
def entries():
44+
return [
45+
SolanaPublicKey("5uKdRzB3FzdmwyCHrqSGq4u2URja617jqtKkM71BVrkw"),
46+
SolanaPublicKey("ETuC4VK6kuHfxc9MCU14dASfnGBfzgFUVCs1oVowawHb"),
47+
SolanaPublicKey("4aDoSXJ5o3AuvL7QFeR6h44jALQfTmUUCTVGDD6aoJTM"),
48+
]
49+
50+
51+
@pytest.fixture
52+
def mapping_key():
53+
return SolanaPublicKey("AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J")
54+
55+
56+
def test_mapping_account_upate_from(
57+
solana_client, mapping_key, mapping_account_bytes, entries
58+
):
59+
account = PythMappingAccount(
60+
key=mapping_key,
61+
solana=solana_client,
62+
)
63+
assert account.entries == []
64+
assert account.next_account_key is None
65+
66+
account.update_from(
67+
buffer=mapping_account_bytes,
68+
version=_VERSION_2,
69+
offset=0,
70+
)
71+
72+
assert account.entries == entries
73+
assert account.next_account_key is None
74+
75+
76+
def test_mapping_account_upate_from_null_key(
77+
solana_client, mapping_key, mapping_account_bytes, entries
78+
):
79+
account = PythMappingAccount(
80+
key=mapping_key,
81+
solana=solana_client,
82+
)
83+
assert account.entries == []
84+
assert account.next_account_key is None
85+
86+
# Replace the last key with a null key
87+
null_key_bytes = b"\0" * SolanaPublicKey.LENGTH
88+
89+
# Length of bytes with the last pubkey trimmed
90+
offset = len(mapping_account_bytes) - SolanaPublicKey.LENGTH
91+
92+
# Take the original bytes and add a null key to the end
93+
bad_bytes = mapping_account_bytes[:offset] + null_key_bytes
94+
95+
account.update_from(
96+
buffer=bad_bytes,
97+
version=_VERSION_2,
98+
offset=0,
99+
)
100+
101+
# The last key in the list is null, so remove it
102+
expected = entries[: len(entries) - 1]
103+
104+
assert account.entries == expected
105+
assert account.next_account_key is None
106+
107+
108+
def test_mapping_account_str(mapping_key, solana_client):
109+
actual = str(
110+
PythMappingAccount(
111+
key=mapping_key,
112+
solana=solana_client,
113+
)
114+
)
115+
expected = f"PythMappingAccount ({mapping_key})"
116+
assert actual == expected

0 commit comments

Comments
 (0)