Coverage for tests/wallet/model/test_transfers.py: 100%

132 statements  

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

1from __future__ import annotations 

2 

3import logging 

4 

5import pytest 

6from monero import ( 

7 IncomingTransferComparator, 

8 MoneroDestination, 

9 MoneroIncomingTransfer, 

10 MoneroOutgoingTransfer, 

11 MoneroTxWallet, 

12) 

13 

14from utils import AssertUtils, BaseTestClass, TestUtils 

15 

16logger: logging.Logger = logging.getLogger("TestMoneroWalletModelTransfers") 

17 

18 

19@pytest.mark.unit 

20class TestMoneroWalletModelTransfers(BaseTestClass): 

21 """Wallet transfer model unit tests.""" 

22 

23 def test_incoming_transfer_deserialize(self) -> None: 

24 transfer: MoneroIncomingTransfer = MoneroIncomingTransfer() 

25 transfer.amount = 500000 

26 transfer.account_index = 0 

27 transfer.subaddress_index = 1 

28 transfer.address = TestUtils.ADDRESS 

29 transfer.num_suggested_confirmations = 10 

30 AssertUtils.assert_serialization_integrity(transfer) 

31 

32 def test_outgoing_transfer_deserialize(self) -> None: 

33 transfer: MoneroOutgoingTransfer = MoneroOutgoingTransfer() 

34 transfer.amount = 500000 

35 transfer.account_index = 0 

36 transfer.subaddress_indices = [0, 1] 

37 transfer.addresses = [TestUtils.ADDRESS] 

38 transfer.destinations = [MoneroDestination(TestUtils.ADDRESS, 500000)] 

39 AssertUtils.assert_serialization_integrity(transfer) 

40 

41 def test_incoming_transfer_copy(self) -> None: 

42 transfer: MoneroIncomingTransfer = MoneroIncomingTransfer() 

43 transfer.amount = 500000 

44 transfer.account_index = 0 

45 transfer.subaddress_index = 1 

46 transfer.address = TestUtils.ADDRESS 

47 transfer.num_suggested_confirmations = 10 

48 

49 copy: MoneroIncomingTransfer = transfer.copy() 

50 assert copy is not transfer 

51 assert copy.serialize() == transfer.serialize() 

52 

53 def test_incoming_transfer_merge(self) -> None: 

54 a: MoneroIncomingTransfer = MoneroIncomingTransfer() 

55 a.amount = 500000 

56 a.account_index = 0 

57 a.subaddress_index = 1 

58 # a.tx is left unset on both sides so merge() won't recurse into tx merge 

59 

60 b: MoneroIncomingTransfer = a.copy() 

61 b.address = TestUtils.ADDRESS # a.address is unset -> merge fills the gap 

62 a.merge(b) 

63 assert a.address == TestUtils.ADDRESS 

64 

65 def test_incoming_transfer_merge_recurses_into_tx_merge(self) -> None: 

66 tx_a: MoneroTxWallet = MoneroTxWallet() 

67 tx_a.hash = "a" * 64 

68 tx_a.is_confirmed = True 

69 

70 tx_b: MoneroTxWallet = MoneroTxWallet() 

71 tx_b.hash = "a" * 64 

72 tx_b.is_confirmed = True 

73 tx_b.num_confirmations = 9 # tx_a's num_confirmations is unset -> merge fills the gap 

74 

75 a: MoneroIncomingTransfer = MoneroIncomingTransfer() 

76 a.tx = tx_a 

77 a.account_index = 0 

78 a.amount = 100 

79 

80 b: MoneroIncomingTransfer = MoneroIncomingTransfer() 

81 b.tx = tx_b 

82 b.account_index = 0 

83 b.amount = 100 

84 

85 # transfers on different txs -> merge delegates to tx merge (which comes back to merging transfers) 

86 a.merge(b) 

87 assert tx_a.num_confirmations == 9 

88 

89 def test_incoming_transfer_merge_zero_amount_conflict_keeps_original(self) -> None: 

90 tx: MoneroTxWallet = MoneroTxWallet() 

91 tx.hash = "a" * 64 

92 

93 a: MoneroIncomingTransfer = MoneroIncomingTransfer() 

94 a.tx = tx 

95 a.account_index = 0 

96 a.amount = 500 

97 

98 b: MoneroIncomingTransfer = MoneroIncomingTransfer() 

99 b.tx = tx 

100 b.account_index = 0 

101 b.amount = 0 

102 

103 # conflicting amounts where one side is 0 are a known monero-project quirk (failed tx in 

104 # pool): merge() warns and leaves the amount as-is rather than reconciling it 

105 a.merge(b) 

106 assert a.amount == 500 

107 

108 def test_incoming_transfer_lt_comparator(self) -> None: 

109 t1: MoneroIncomingTransfer = MoneroIncomingTransfer() 

110 t1.tx = MoneroTxWallet() 

111 t1.account_index = 0 

112 t1.subaddress_index = 0 

113 

114 t2: MoneroIncomingTransfer = MoneroIncomingTransfer() 

115 t2.tx = MoneroTxWallet() 

116 t2.account_index = 0 

117 t2.subaddress_index = 1 

118 

119 assert t1 < t2 

120 assert not (t2 < t1) 

121 assert IncomingTransferComparator.compare(t1, t2) 

122 assert not IncomingTransferComparator.compare(t2, t1) 

123 

124 transfers: list[MoneroIncomingTransfer] = [t2, t1] 

125 transfers.sort() 

126 assert transfers[0] is t1 

127 assert transfers[1] is t2 

128 

129 def test_outgoing_transfer_copy(self) -> None: 

130 transfer: MoneroOutgoingTransfer = MoneroOutgoingTransfer() 

131 transfer.amount = 500000 

132 transfer.account_index = 0 

133 transfer.addresses = [TestUtils.ADDRESS] 

134 transfer.subaddress_indices = [0] 

135 transfer.destinations = [MoneroDestination(TestUtils.ADDRESS, 500000)] 

136 

137 copy: MoneroOutgoingTransfer = transfer.copy() 

138 assert copy is not transfer 

139 assert copy.serialize() == transfer.serialize() 

140 

141 def test_outgoing_transfer_merge(self) -> None: 

142 a: MoneroOutgoingTransfer = MoneroOutgoingTransfer() 

143 a.amount = 500000 

144 a.account_index = 0 

145 # a.addresses/subaddress_indices/destinations left empty on both sides so far 

146 

147 b: MoneroOutgoingTransfer = a.copy() 

148 b.addresses = [TestUtils.ADDRESS] 

149 b.subaddress_indices = [0] 

150 b.destinations = [MoneroDestination(TestUtils.ADDRESS, 500000)] 

151 a.merge(b) # a's lists are empty -> merge adopts b's 

152 assert a.addresses == [TestUtils.ADDRESS] 

153 assert a.subaddress_indices == [0] 

154 assert len(a.destinations) == 1 

155 

156 def test_outgoing_transfer_merge_destinations_with_unset_fields(self) -> None: 

157 # destinations with unset address/amount are a reachable state; merge() 

158 # must compare them without dereferencing the empty optionals 

159 a: MoneroOutgoingTransfer = MoneroOutgoingTransfer() 

160 a.amount = 500000 

161 a.account_index = 0 

162 a.destinations = [MoneroDestination()] 

163 b: MoneroOutgoingTransfer = a.copy() 

164 b.destinations = [MoneroDestination()] 

165 a.merge(b) # equal (empty) destinations -> no error 

166 assert len(a.destinations) == 1 

167 

168 def test_outgoing_transfer_merge_conflicting_destinations_raises(self) -> None: 

169 a: MoneroOutgoingTransfer = MoneroOutgoingTransfer() 

170 a.amount = 500000 

171 a.account_index = 0 

172 a.destinations = [MoneroDestination(TestUtils.ADDRESS, 1)] 

173 b: MoneroOutgoingTransfer = a.copy() 

174 b.destinations = [MoneroDestination(TestUtils.ADDRESS, 2)] 

175 with pytest.raises(RuntimeError) as exc_info: 

176 a.merge(b) 

177 assert str(exc_info.value) == "Destination vectors are different"