Coverage for tests/utils/integration_test_utils.py: 91%

64 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1import logging 

2 

3from abc import ABC 

4from time import sleep 

5from monero import MoneroWallet, MoneroTxWallet, MoneroTxQuery, MoneroSyncResult 

6 

7from .wallet_test_utils import WalletTestUtils 

8from .mining_utils import MiningUtils 

9from .blockchain_utils import BlockchainUtils 

10from .wallet_type import WalletType 

11from .test_utils import TestUtils 

12 

13logger: logging.Logger = logging.getLogger("IntegrationTestUtils") 

14 

15 

16class IntegrationTestUtils(ABC): 

17 """Integration test utilities.""" 

18 

19 __test__ = False 

20 

21 _LAST_SETUP: WalletType | None = None 

22 """Wallet type that the integration environment was set up for last, in this session.""" 

23 

24 @classmethod 

25 def setup_blockchain(cls) -> None: 

26 """Setup blockchain for integration tests.""" 

27 BlockchainUtils.setup_blockchain(TestUtils.NETWORK_TYPE) 

28 

29 @classmethod 

30 def setup(cls, wallet_type: WalletType) -> None: 

31 """Setup integration test environment: mines the blockchain until 

32 `TestUtils.MIN_BLOCK_HEIGHT` and fund test wallet by integration test wallet type. 

33 

34 :param MoneroWallet wallet_type: wallet type to use in integration tests. 

35 """ 

36 if wallet_type == WalletType.KEYS or wallet_type == WalletType.UNDEFINED: 

37 return 

38 # once for each run of classes of the same type, as when all the tests of a wallet type were in a class: the classes of the 

39 # topics that came from it would do the same work again. It is not once for the whole session: the wallets of the types have 

40 # the same seed, so the tests of a type spend the funds of the next one, which is funded again when its classes start 

41 if wallet_type == cls._LAST_SETUP: 

42 return 

43 cls.setup_blockchain() 

44 # get test wallet 

45 wallet: MoneroWallet 

46 type_str: str = "FULL" 

47 if wallet_type == WalletType.FULL: 

48 wallet = TestUtils.get_wallet_full() 

49 elif wallet_type == WalletType.RPC: 

50 wallet = TestUtils.get_wallet_rpc() 

51 type_str = "RPC" 

52 else: 

53 raise ValueError("Only RPC and FULL wallet are supported for integration tests") 

54 

55 wallet_txs: list[MoneroTxWallet] = wallet.get_txs() 

56 num_wallet_txs: int = len(wallet_txs) 

57 # fund wallet with mined coins and wait for unlocked balance 

58 txs: list[MoneroTxWallet] = cls.fund_wallet_and_wait_for_unlocked(wallet) 

59 

60 # setup first receive height 

61 tx: MoneroTxWallet = txs[0] if num_wallet_txs == 0 else wallet_txs[0] 

62 tx_height: int | None = tx.get_height() 

63 assert tx_height is not None 

64 TestUtils.FIRST_RECEIVE_HEIGHT = tx_height 

65 logger.debug(f"Test wallet first receive height: {tx_height}") 

66 

67 if num_wallet_txs < len(txs): 

68 logger.info(f"Funded test wallet {type_str}") 

69 

70 cls._LAST_SETUP = wallet_type 

71 

72 @classmethod 

73 def fund_wallet_and_wait_for_unlocked(cls, wallet: MoneroWallet) -> list[MoneroTxWallet]: 

74 """Fund wallet used for integration tests and wait for unlocked balance. 

75 

76 :param MoneroWallet wallet: wallet to use for an integration test. 

77 :returns list[MoneroTxWallet]: list of transactions used to fund test wallet. 

78 """ 

79 # fund wallet 

80 txs: list[MoneroTxWallet] = WalletTestUtils.fund_wallet(wallet) 

81 if len(txs) > 0: 

82 # mine an output to wallet primary address 

83 MiningUtils.generate_blocks(wallet.get_primary_address(), 1) 

84 # mine blocks to confirm txs 

85 block_height: int = BlockchainUtils.wait_for_blocks(11) 

86 

87 # sync wallet 

88 while wallet.get_height() < block_height: 

89 sync_result: MoneroSyncResult = wallet.sync() 

90 assert sync_result.num_blocks_fetched is not None 

91 if sync_result.num_blocks_fetched > 0: 

92 logger.debug(f"Sync result from funded wallet: {sync_result.serialize()}") 

93 

94 sleep(TestUtils.SYNC_PERIOD_IN_MS / 1000) 

95 

96 # check for txs 

97 query: MoneroTxQuery = MoneroTxQuery() 

98 for tx in txs: 

99 assert tx.hash is not None 

100 query.hashes.append(tx.hash) 

101 

102 num_txs: int = len(txs) 

103 txs = wallet.get_txs(query) 

104 

105 assert len(txs) == num_txs, f"Expected {num_txs} txs, but got {len(txs)}" 

106 

107 # assert txs are unlocked 

108 for tx in txs: 

109 assert tx.is_locked is False, f"Expected tx to be unlocked: {tx.serialize()}" 

110 

111 return txs