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

99 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 MoneroDestination, MoneroTxConfig, MoneroTxPriority, MoneroUtils, MoneroWalletConfig 

7 

8from utils import AssertUtils, BaseTestClass, TestUtils 

9 

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

11 

12 

13@pytest.mark.unit 

14class TestMoneroWalletModelConfig(BaseTestClass): 

15 """Wallet config model unit tests.""" 

16 

17 def test_destination(self) -> None: 

18 amount: int = MoneroUtils.xmr_to_atomic_units(1) 

19 dest: MoneroDestination = MoneroDestination(TestUtils.ADDRESS, amount) 

20 logger.debug(f"Testing destination: {dest.serialize()}") 

21 copy: MoneroDestination = dest.copy() 

22 AssertUtils.assert_equals(dest, copy) 

23 

24 def test_wallet_config(self) -> None: 

25 config: MoneroWalletConfig = TestUtils.get_wallet_full_config(TestUtils.get_daemon_rpc_connection()) 

26 logger.debug(f"Testing wallet config: {config.serialize()}") 

27 copy: MoneroWalletConfig = config.copy() 

28 AssertUtils.assert_equals(config, copy) 

29 config_str: str = config.serialize() 

30 deserialized_config: MoneroWalletConfig = MoneroWalletConfig.deserialize(config_str) 

31 logger.debug(f"Deserialized config: {deserialized_config.serialize()}") 

32 AssertUtils.assert_equals(config, deserialized_config) 

33 

34 @pytest.mark.parametrize("json_fragment", ['{"networkType":-1}', '{"networkType":3}']) 

35 def test_wallet_config_invalid_network_type(self, json_fragment: str) -> None: 

36 with pytest.raises(Exception, match="Invalid network type"): 

37 MoneroWalletConfig.deserialize(json_fragment) 

38 

39 def test_tx_config(self) -> None: 

40 config: MoneroTxConfig = MoneroTxConfig() 

41 config.set_address(TestUtils.ADDRESS) 

42 config.amount = MoneroUtils.xmr_to_atomic_units(0.5) 

43 config.account_index = 0 

44 config.subaddress_indices = [i for i in range(10)] 

45 config.below_amount = MoneroUtils.xmr_to_atomic_units(0.1) 

46 config.can_split = True 

47 config.fee = MoneroUtils.xmr_to_atomic_units(0.00075) 

48 config.sweep_each_subaddress = False 

49 config.priority = MoneroTxPriority.ELEVATED 

50 config.key_image = "a" * 64 

51 

52 copy: MoneroTxConfig = config.copy() 

53 AssertUtils.assert_equals(config, copy) 

54 

55 config_str: str = config.serialize() 

56 logger.debug(f"Serialized tx config: {config_str}") 

57 

58 deserialized_config: MoneroTxConfig = MoneroTxConfig.deserialize(config_str) 

59 AssertUtils.assert_equals(config, deserialized_config) 

60 

61 @pytest.mark.parametrize("priority,priority_num", [ 

62 (MoneroTxPriority.DEFAULT, 0), 

63 (MoneroTxPriority.UNIMPORTANT, 1), 

64 (MoneroTxPriority.NORMAL, 2), 

65 (MoneroTxPriority.ELEVATED, 3), 

66 ]) 

67 def test_tx_config_priority_deserialize(self, priority: MoneroTxPriority, priority_num: int) -> None: 

68 config: MoneroTxConfig = MoneroTxConfig.deserialize('{"priority": %d}' % priority_num) 

69 assert config.priority == priority 

70 

71 def test_tx_config_invalid_priority_deserialize(self) -> None: 

72 with pytest.raises(RuntimeError) as exc_info: 

73 MoneroTxConfig.deserialize('{"priority": 4}') 

74 assert str(exc_info.value) == "Invalid priority number: 4" 

75 

76 def test_get_normalized_destinations_uses_destinations_when_address_and_amount_unset(self) -> None: 

77 config: MoneroTxConfig = MoneroTxConfig() 

78 config.destinations = [MoneroDestination(TestUtils.ADDRESS, 100), MoneroDestination(TestUtils.MINING_ADDRESS, 200)] 

79 assert config.get_normalized_destinations() == config.destinations 

80 

81 def test_get_normalized_destinations_builds_single_destination(self) -> None: 

82 config: MoneroTxConfig = MoneroTxConfig() 

83 config.address = TestUtils.ADDRESS 

84 config.amount = 500000 

85 assert len(config.destinations) == 0 # address/amount alone don't populate destinations 

86 

87 normalized: list[MoneroDestination] = config.get_normalized_destinations() 

88 assert len(normalized) == 1 

89 assert normalized[0].address == TestUtils.ADDRESS 

90 assert normalized[0].amount == 500000 

91 

92 def test_get_normalized_destinations_conflicts_with_multiple_destinations(self) -> None: 

93 config: MoneroTxConfig = MoneroTxConfig() 

94 config.address = TestUtils.ADDRESS 

95 config.amount = 500000 

96 config.destinations = [MoneroDestination(TestUtils.ADDRESS, 500000), MoneroDestination(TestUtils.MINING_ADDRESS, 1)] 

97 with pytest.raises(RuntimeError) as exc_info: 

98 config.get_normalized_destinations() 

99 assert str(exc_info.value) == "Invalid tx configuration: single destination address/amount incompatible with multiple destinations" 

100 

101 def test_get_normalized_destinations_address_mismatch(self) -> None: 

102 config: MoneroTxConfig = MoneroTxConfig() 

103 config.address = TestUtils.ADDRESS 

104 config.amount = 500000 

105 config.destinations = [MoneroDestination(TestUtils.MINING_ADDRESS, 500000)] 

106 with pytest.raises(RuntimeError) as exc_info: 

107 config.get_normalized_destinations() 

108 assert str(exc_info.value) == "Invalid tx configuration: single destination address does not match first destination address" 

109 

110 def test_get_normalized_destinations_amount_mismatch(self) -> None: 

111 config: MoneroTxConfig = MoneroTxConfig() 

112 config.address = TestUtils.ADDRESS 

113 config.amount = 500000 

114 config.destinations = [MoneroDestination(TestUtils.ADDRESS, 1)] 

115 with pytest.raises(RuntimeError) as exc_info: 

116 config.get_normalized_destinations() 

117 assert str(exc_info.value) == "Invalid tx configuration: single destination amount does not match first destination amount" 

118 

119 def test_get_normalized_destinations_matches_single_destination(self) -> None: 

120 config: MoneroTxConfig = MoneroTxConfig() 

121 config.address = TestUtils.ADDRESS 

122 config.amount = 500000 

123 config.destinations = [MoneroDestination(TestUtils.ADDRESS, 500000)] 

124 assert config.get_normalized_destinations() == config.destinations 

125 

126 def test_destination_deserialize(self) -> None: 

127 destination: MoneroDestination = MoneroDestination(TestUtils.ADDRESS, 500000) 

128 AssertUtils.assert_serialization_integrity(destination)