Skip to content

Commit 0dc6784

Browse files
Code quality fixes
1 parent 50764b6 commit 0dc6784

6 files changed

Lines changed: 73 additions & 28 deletions

File tree

tests/test_monero_wallet_full.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ def test_get_height_by_date_regtest(self, wallet: MoneroWallet):
694694

695695
#region Disabled Tests
696696

697-
@pytest.mark.skip(reason="TODO disabled because importing key images deletes corresponding incoming transfers: https://github.com/monero-project/monero/issues/5812")
697+
@pytest.mark.skip(reason="TODO disabled because importing key images deletes corresponding incoming transfers: #5812")
698698
@override
699699
def test_import_key_images(self, wallet: MoneroWallet):
700700
return super().test_import_key_images(wallet)
@@ -719,8 +719,20 @@ def _test_multisig_sample(self, m: int, n: int) -> None:
719719
tester: MultisigSampleCodeTester = MultisigSampleCodeTester(m, wallets)
720720
tester.test()
721721

722-
def _test_sync_seed(self, daemon: MoneroDaemonRpc, wallet: MoneroWalletFull, start_height: Optional[int], restore_height: Optional[int], skip_gt_comparison: bool = False, test_post_sync_notifications: bool = False) -> None:
723-
tester: SyncSeedTester = SyncSeedTester(daemon, wallet, self._create_wallet, start_height, restore_height, skip_gt_comparison, test_post_sync_notifications)
722+
def _test_sync_seed(
723+
self,
724+
daemon: MoneroDaemonRpc,
725+
wallet: MoneroWalletFull,
726+
start_height: Optional[int],
727+
restore_height: Optional[int],
728+
skip_gt_comparison: bool = False,
729+
test_post_sync_notifications: bool = False
730+
) -> None:
731+
tester: SyncSeedTester = SyncSeedTester(
732+
daemon, wallet, self._create_wallet,
733+
start_height, restore_height,
734+
skip_gt_comparison, test_post_sync_notifications
735+
)
724736
tester.test()
725737

726738
#endregion

tests/test_monero_wallet_keys.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -661,31 +661,14 @@ def test_create_wallet_from_keys(self, daemon: MoneroDaemonRpc, wallet: MoneroWa
661661
config.private_view_key = private_view_key
662662
config.private_spend_key = private_spend_key
663663
w: MoneroWallet = self._create_wallet(config)
664-
665-
try:
666-
assert primary_address == w.get_primary_address()
667-
assert private_view_key == w.get_private_view_key()
668-
assert private_spend_key == w.get_private_spend_key()
669-
MoneroUtils.validate_mnemonic(w.get_seed())
670-
assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language()
671-
finally:
672-
self._close_wallet(w)
664+
WalletUtils.test_wallet_keys(primary_address, private_view_key, private_spend_key, w)
673665

674666
# recreate test wallet from spend key
675667
config = MoneroWalletConfig()
676668
config.primary_address = primary_address
677669
config.private_spend_key = private_spend_key
678670
w = self._create_wallet(config)
679-
680-
try:
681-
assert primary_address == w.get_primary_address()
682-
assert private_view_key == w.get_private_view_key()
683-
assert private_spend_key == w.get_private_spend_key()
684-
# TODO monero-wallet-rpc: cannot get seed from wallet created from keys?
685-
MoneroUtils.validate_mnemonic(w.get_seed())
686-
assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language()
687-
finally:
688-
self._close_wallet(w)
671+
WalletUtils.test_wallet_keys(primary_address, private_view_key, private_spend_key, w)
689672

690673
@pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
691674
@override

tests/utils/block_utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BlockUtils(ABC):
1717
"""Block utilities"""
1818

1919
@classmethod
20-
def test_block_header(cls, header: Optional[MoneroBlockHeader], is_full: Optional[bool]):
20+
def test_block_header(cls, header: Optional[MoneroBlockHeader], is_full: Optional[bool]) -> None:
2121
"""
2222
Test a block header
2323
@@ -46,6 +46,17 @@ def test_block_header(cls, header: Optional[MoneroBlockHeader], is_full: Optiona
4646
assert header.nonce > 0
4747
# never seen defined
4848
assert header.pow_hash is None
49+
# test full header details
50+
cls.test_full_header(header, is_full)
51+
52+
@classmethod
53+
def test_full_header(cls, header: MoneroBlockHeader, is_full: Optional[bool]) -> None:
54+
"""
55+
Test full header details.
56+
57+
:param MoneroBlockHeader header: header to test full details.
58+
:param bool | None is_full: indicates if `header`'s full details should be defined.
59+
"""
4960
if is_full:
5061
# check full block
5162
assert header.size is not None
@@ -133,7 +144,13 @@ def test_get_blocks_range(
133144
# fetch blocks by range
134145
real_start_height: int = 0 if start_height is None else start_height
135146
real_end_height: int = chain_height - 1 if end_height is None else end_height
136-
blocks: list[MoneroBlock] = daemon.get_blocks_by_range_chunked(start_height, end_height) if chunked else daemon.get_blocks_by_range(start_height, end_height)
147+
blocks: list[MoneroBlock]
148+
149+
if chunked:
150+
blocks = daemon.get_blocks_by_range_chunked(start_height, end_height)
151+
else:
152+
blocks = daemon.get_blocks_by_range(start_height, end_height)
153+
137154
num_blocks: int = len(blocks)
138155
expected_num_blocks: int = real_end_height - real_start_height + 1
139156
assert expected_num_blocks == num_blocks, f"Expected {expected_num_blocks} block(s), got {num_blocks}"

tests/utils/sync_with_pool_submit_tester.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
class SyncWithPoolSubmitTester:
1515
"""Test wallet capability to sync transactions in the pool."""
1616

17+
BALANCE_ERR: str = "Wallet balance should revert to original after flushing tx from pool without relaying"
18+
UNLOCKED_BALANCE_ERR: str = "Wallet unlocked balance should revert to original after flushing tx from pool without relaying"
19+
1720
daemon: MoneroDaemon
1821
"""Daemon instance."""
1922
wallet: MoneroWallet
@@ -56,9 +59,9 @@ def run_failing_core_code(self, config_no_relay: MoneroTxConfig) -> None:
5659
return
5760

5861
# wallet balances should change
59-
assert self.balance_before != self.wallet.get_balance(), "Wallet balance should revert to original after flushing tx from pool without relaying"
62+
assert self.balance_before != self.wallet.get_balance(), self.BALANCE_ERR
6063
# TODO: test exact amounts, maybe in ux test
61-
assert self.unlocked_balance_before != self.wallet.get_unlocked_balance(), "Wallet unlocked balance should revert to original after flushing tx from pool without relaying"
64+
assert self.unlocked_balance_before != self.wallet.get_unlocked_balance(), self.UNLOCKED_BALANCE_ERR
6265

6366
# create tx using same config which is no longer double spend
6467
tx2: MoneroTxWallet = self.wallet.create_tx(config_no_relay)

tests/utils/tx_utils.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,14 @@ def test_signature_header_error(cls, ex: Exception) -> None:
867867
assert msg == "Signature header check error", msg
868868

869869
@classmethod
870-
def get_and_test_txs(cls, wallet: MoneroWallet, query: Optional[MoneroTxQuery], ctx: Optional[TxContext], is_expected: Optional[bool], regtest: bool) -> list[MoneroTxWallet]:
870+
def get_and_test_txs(
871+
cls,
872+
wallet: MoneroWallet,
873+
query: Optional[MoneroTxQuery],
874+
ctx: Optional[TxContext],
875+
is_expected: Optional[bool],
876+
regtest: bool
877+
) -> list[MoneroTxWallet]:
871878
"""Get and test txs from wallet"""
872879
copy: Optional[MoneroTxQuery] = query.copy() if query is not None else None
873880
txs = wallet.get_txs(query) if query is not None else wallet.get_txs()
@@ -888,7 +895,13 @@ def get_and_test_txs(cls, wallet: MoneroWallet, query: Optional[MoneroTxQuery],
888895
return txs
889896

890897
@classmethod
891-
def get_and_test_transfers(cls, wallet: MoneroWallet, query: Optional[MoneroTransferQuery], ctx: Optional[TxContext], is_expected: Optional[bool]) -> list[MoneroTransfer]:
898+
def get_and_test_transfers(
899+
cls,
900+
wallet: MoneroWallet,
901+
query: Optional[MoneroTransferQuery],
902+
ctx: Optional[TxContext],
903+
is_expected: Optional[bool]
904+
) -> list[MoneroTransfer]:
892905
copy: Optional[MoneroTransferQuery] = query.copy() if query is not None else None
893906
transfers = wallet.get_transfers(query) if query is not None else wallet.get_transfers(MoneroTransferQuery())
894907

tests/utils/wallet_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,20 @@ def test_sweep_wallet(cls, wallet: MoneroWallet, sweep_each_subaddress: Optional
483483
"""
484484
sweeper: WalletSweeper = WalletSweeper(wallet, sweep_each_subaddress)
485485
sweeper.sweep()
486+
487+
@classmethod
488+
def test_wallet_keys(cls, address: str, view_key: str, spend_key: str, w: MoneroWallet) -> None:
489+
"""
490+
Test wallet keys.
491+
492+
:param str address: expected primary address.
493+
:param str view_key: expected private view key.
494+
:param str spend_key: expected private spend key.
495+
:param MoneroWallet wallet: wallet to test keys.
496+
"""
497+
498+
assert address == w.get_primary_address()
499+
assert view_key == w.get_private_view_key()
500+
assert spend_key == w.get_private_spend_key()
501+
MoneroUtils.validate_mnemonic(w.get_seed())
502+
assert MoneroWallet.DEFAULT_LANGUAGE == w.get_seed_language()

0 commit comments

Comments
 (0)