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

105 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 MoneroKeyImage, MoneroOutputWallet, MoneroTx, OutputComparator 

7 

8from utils import AssertUtils, BaseTestClass 

9 

10logger: logging.Logger = logging.getLogger("TestMoneroWalletModelOutputs") 

11 

12 

13@pytest.mark.unit 

14class TestMoneroWalletModelOutputs(BaseTestClass): 

15 """Wallet output model unit tests.""" 

16 

17 def test_output_wallet_deserialize(self) -> None: 

18 output_wallet: MoneroOutputWallet = MoneroOutputWallet() 

19 output_wallet.amount = 1000000 

20 output_wallet.index = 2 

21 key_image: MoneroKeyImage = MoneroKeyImage() 

22 key_image.hex = "a" * 64 

23 key_image.signature = "b" * 128 

24 output_wallet.key_image = key_image 

25 output_wallet.account_index = 0 

26 output_wallet.subaddress_index = 1 

27 output_wallet.is_spent = False 

28 output_wallet.is_frozen = False 

29 AssertUtils.assert_serialization_integrity(output_wallet) 

30 

31 def test_output_wallet_copy(self) -> None: 

32 output: MoneroOutputWallet = MoneroOutputWallet() 

33 output.amount = 1000000 

34 output.index = 2 

35 output.account_index = 0 

36 output.subaddress_index = 1 

37 output.is_spent = False 

38 output.is_frozen = False 

39 

40 copy: MoneroOutputWallet = output.copy() 

41 assert copy is not output 

42 assert copy.serialize() == output.serialize() 

43 

44 def test_output_wallet_merge(self) -> None: 

45 a: MoneroOutputWallet = MoneroOutputWallet() 

46 a.amount = 1000000 

47 a.index = 2 

48 a.account_index = 0 

49 a.subaddress_index = 1 

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

51 

52 b: MoneroOutputWallet = a.copy() 

53 b.is_spent = True # a.is_spent is unset -> merge fills the gap 

54 a.merge(b) 

55 assert a.is_spent is True 

56 

57 def test_output_wallet_lt_comparator(self) -> None: 

58 o1: MoneroOutputWallet = MoneroOutputWallet() 

59 o1.tx = MoneroTx() 

60 o1.account_index = 0 

61 o1.subaddress_index = 0 

62 o1.index = 0 

63 o1.key_image = MoneroKeyImage() 

64 o1.key_image.hex = "a" * 64 

65 

66 o2: MoneroOutputWallet = MoneroOutputWallet() 

67 o2.tx = MoneroTx() 

68 o2.account_index = 0 

69 o2.subaddress_index = 0 

70 o2.index = 1 

71 o2.key_image = MoneroKeyImage() 

72 o2.key_image.hex = "b" * 64 

73 

74 assert o1 < o2 

75 assert not (o2 < o1) 

76 assert OutputComparator.compare(o1, o2) 

77 assert not OutputComparator.compare(o2, o1) 

78 

79 def test_output_wallet_lt_comparator_account_index(self) -> None: 

80 tx: MoneroTx = MoneroTx() 

81 o1: MoneroOutputWallet = MoneroOutputWallet() 

82 o1.tx = tx 

83 o1.account_index = 0 

84 o1.subaddress_index = 0 

85 o1.index = 0 

86 o1.key_image = MoneroKeyImage() 

87 o1.key_image.hex = "a" * 64 

88 

89 o2: MoneroOutputWallet = o1.copy() 

90 o2.account_index = 1 # only the account index differs 

91 

92 assert OutputComparator.compare(o1, o2) 

93 assert not OutputComparator.compare(o2, o1) 

94 

95 def test_output_wallet_lt_comparator_subaddress_index(self) -> None: 

96 tx: MoneroTx = MoneroTx() 

97 o1: MoneroOutputWallet = MoneroOutputWallet() 

98 o1.tx = tx 

99 o1.account_index = 0 

100 o1.subaddress_index = 0 

101 o1.index = 0 

102 o1.key_image = MoneroKeyImage() 

103 o1.key_image.hex = "a" * 64 

104 

105 o2: MoneroOutputWallet = o1.copy() 

106 o2.subaddress_index = 1 # same account index, only the subaddress index differs 

107 

108 assert OutputComparator.compare(o1, o2) 

109 assert not OutputComparator.compare(o2, o1) 

110 

111 def test_output_wallet_lt_comparator_key_image(self) -> None: 

112 tx: MoneroTx = MoneroTx() 

113 o1: MoneroOutputWallet = MoneroOutputWallet() 

114 o1.tx = tx 

115 o1.account_index = 0 

116 o1.subaddress_index = 0 

117 o1.index = 0 

118 o1.key_image = MoneroKeyImage() 

119 o1.key_image.hex = "a" * 64 

120 

121 o2: MoneroOutputWallet = o1.copy() 

122 o2.key_image = MoneroKeyImage() 

123 o2.key_image.hex = "b" * 64 # account/subaddress/index tied -> falls back to key image hex 

124 

125 assert OutputComparator.compare(o1, o2) 

126 assert not OutputComparator.compare(o2, o1) 

127 

128 outputs: list[MoneroOutputWallet] = [o2, o1] 

129 outputs.sort() 

130 assert outputs[0] is o1 

131 assert outputs[1] is o2