Coverage for tests/utils/tx_utils.py: 55%

53 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 typing import Optional 

5from monero import MoneroTx, MoneroBlock 

6 

7from .context import TestContext 

8from .assert_utils import AssertUtils 

9from .tx_tester import TxTester 

10 

11logger: logging.Logger = logging.getLogger("TxUtils") 

12 

13 

14class TxUtils(ABC): 

15 """Tx utils for tests.""" 

16 

17 __test__ = False 

18 

19 @classmethod 

20 def test_tx_copy(cls, tx: Optional[MoneroTx], context: Optional[TestContext]) -> None: 

21 """Test monero tx copy. 

22 

23 :param MoneroTx | None tx: transaction to test copy. 

24 :param TestContext | None context: test context. 

25 """ 

26 # copy tx and assert deep equality 

27 assert tx is not None 

28 copy: MoneroTx = tx.copy() 

29 assert isinstance(copy, MoneroTx) 

30 assert copy.block is None 

31 if tx.block is not None: 

32 block_copy: MoneroBlock = tx.block.copy() 

33 block_copy.txs = [copy] 

34 

35 AssertUtils.assert_equals(tx, copy) 

36 assert copy != tx 

37 

38 # test different input references 

39 if len(copy.inputs) == 0: 

40 assert len(tx.inputs) == 0 

41 else: 

42 assert copy.inputs != tx.inputs 

43 for i, output in enumerate(copy.outputs): 

44 assert tx.outputs[i].amount == output.amount 

45 

46 # test copied tx 

47 ctx: TestContext = TestContext(context) 

48 ctx.do_not_test_copy = True # to prevent infinite recursion 

49 if tx.block is not None: 

50 block_copy = tx.block.copy() 

51 block_copy.txs = [copy] 

52 copy.block = block_copy 

53 

54 cls.test_tx(copy, ctx) 

55 

56 # test merging with copy 

57 merged: MoneroTx = copy 

58 merged.merge(copy.copy()) 

59 assert str(tx) == str(merged) 

60 

61 @classmethod 

62 def test_tx(cls, tx: MoneroTx | None, ctx: TestContext) -> None: 

63 """Test monero tx. 

64 

65 :param MoneroTx | None tx: transaction to test. 

66 :param TestContext ctx: test context. 

67 """ 

68 assert tx is not None, "No tx provided" 

69 logger.debug(f"Testing tx: {tx.serialize()}. Context: {ctx.serialize()}") 

70 tester: TxTester = TxTester(tx, ctx) 

71 tester.run() 

72 

73 @classmethod 

74 def test_miner_tx(cls, miner_tx: Optional[MoneroTx]) -> None: 

75 """Test monero miner tx. 

76 

77 :param MoneroTx | None miner_tx: miner transaction to test. 

78 """ 

79 assert miner_tx is not None 

80 assert miner_tx.is_miner_tx is not None 

81 assert miner_tx.version is not None 

82 assert miner_tx.version >= 0 

83 assert miner_tx.extra is not None 

84 assert len(miner_tx.extra) > 0 

85 assert miner_tx.unlock_time is not None 

86 assert miner_tx.unlock_time >= 0 

87 assert miner_tx.is_confirmed 

88 # TODO binary blocks doesn't have depth? 

89 # assert miner_tx.num_confirmations is not None 

90 # assert miner_tx.num_confirmations > 0 

91 

92 # TODO: miner tx does not have hashes in binary requests so this will fail, need to derive using prunable data 

93 # ctx = new TestContext() 

94 # ctx.has_json = false 

95 # ctx.is_pruned = true 

96 # ctx.is_full = false 

97 # ctx.is_confirmed = true 

98 # ctx.is_miner = true 

99 # ctx.from_get_tx_pool = true 

100 # cls.test_tx(miner_tx, ctx)