Coverage for tests/utils/output_utils.py: 99%
105 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1import logging
3from abc import ABC
4from typing import Optional
6from monero import (
7 MoneroWallet, MoneroOutputQuery, MoneroTx,
8 MoneroOutput, MoneroKeyImage, MoneroOutputWallet,
9 MoneroOutputDistributionEntry, MoneroOutputHistogramEntry
10)
12from .gen_utils import GenUtils
13from .assert_utils import AssertUtils
14from .context import TestContext
16logger: logging.Logger = logging.getLogger("OutputUtils")
19class OutputUtils(ABC):
21 @classmethod
22 def test_key_image(cls, image: Optional[MoneroKeyImage], context: Optional[TestContext] = None) -> None:
23 """Test monero key image.
25 :param MoneroKeyImage | None image: key image to test.
26 :param TestContext context: test context (default `None`).
27 """
28 assert image is not None
29 logger.debug(f"Testing key image: {image.serialize()}")
30 assert image.hex is not None
31 assert len(image.hex) > 0
32 if image.signature is not None:
33 assert len(image.signature) > 0
35 @classmethod
36 def test_output_distribution_entry(cls, entry: MoneroOutputDistributionEntry) -> None:
37 """Test daemon output distribution entry.
39 :param MoneroOutputDistributionEntry entry: daemon output distribution entry to test.
40 """
41 logger.debug(f"Testing output distribution entry: {entry.serialize()}")
42 GenUtils.test_unsigned_big_integer(entry.amount)
43 assert entry.base is not None
44 assert entry.base >= 0
45 assert len(entry.distribution) > 0
46 assert entry.start_height is not None
47 assert entry.start_height >= 0
49 @classmethod
50 def test_output_histogram_entry(cls, entry: MoneroOutputHistogramEntry) -> None:
51 """Test daemon output histogram entry.
53 :param MoneroOutputHistogramEntry entry: daemon output histogram entry to test.
54 """
55 logger.debug(f"Testing output histogram entry: {entry.serialize()}")
56 GenUtils.test_unsigned_big_integer(entry.amount)
57 assert entry.num_instances is not None
58 assert entry.num_instances >= 0
59 assert entry.unlocked_instances is not None
60 assert entry.unlocked_instances >= 0
61 assert entry.recent_instances is not None
62 assert entry.recent_instances >= 0
64 @classmethod
65 def test_output(cls, output: Optional[MoneroOutput], context: Optional[TestContext] = None) -> None:
66 """Test monero output.
68 :param MoneroOutput | None output: output to test.
69 :param TestContext | None context: test context (default `None`).
70 """
71 assert output is not None
72 GenUtils.test_unsigned_big_integer(output.amount)
73 if context is None:
74 return
75 assert output.tx is not None
76 ctx: TestContext = TestContext(context)
77 if output.tx.in_tx_pool or ctx.has_output_indices is False:
78 assert output.index is None
79 else:
80 assert output.index is not None
81 assert output.index >= 0
82 assert output.stealth_public_key is not None
83 assert len(output.stealth_public_key) > 0
85 @classmethod
86 def test_input(cls, xmr_input: Optional[MoneroOutput], ctx: Optional[TestContext]) -> None:
87 """Test monero input.
89 :param MoneroOutput | None xmr_input: input to test.
90 :param TestContext | None ctx: test context (default `None`).
91 """
92 assert xmr_input is not None
93 cls.test_output(xmr_input)
94 cls.test_key_image(xmr_input.key_image, ctx)
95 assert len(xmr_input.ring_output_indices) > 0
97 @classmethod
98 def test_input_wallet(cls, xmr_input: Optional[MoneroOutputWallet]) -> None:
99 """Test monero input wallet.
101 :param MoneroOutputWallet xmr_input: wallet input to test.
102 """
103 assert xmr_input is not None
104 assert xmr_input.key_image is not None
105 assert xmr_input.key_image.hex is not None
106 assert len(xmr_input.key_image.hex) > 0
107 assert xmr_input.amount is None
109 @classmethod
110 def test_output_wallet(cls, output: Optional[MoneroOutputWallet]) -> None:
111 """Test monero output wallet.
113 :param MoneroOutputWallet | None output: wallet output to test.
114 """
115 assert output is not None
116 assert output.account_index is not None
117 assert output.account_index >= 0
118 assert output.subaddress_index is not None
119 assert output.subaddress_index >= 0
120 assert output.index is not None
121 assert output.index >= 0
122 assert output.is_spent is not None
123 # TODO implement is_locked
124 #assert output.is_locked is not None
125 assert output.is_frozen is not None
126 assert output.key_image is not None
127 assert output.key_image.hex is not None
128 assert len(output.key_image.hex) > 0
129 GenUtils.test_unsigned_big_integer(output.amount, True)
131 # output has circular reference to its transaction which has some initialized fields
132 tx: MoneroTx | None = output.tx
133 assert tx is not None
134 assert output in tx.outputs
135 assert tx.hash is not None
136 # TODO implement is_locked
137 #assert tx.is_locked is not None
138 # TODO monero-wallet-rpc: possible to get unconfirmed outputs?
139 assert tx.is_confirmed is True
140 assert tx.is_relayed is True
141 assert tx.is_failed is False
142 tx_height: int | None = tx.get_height()
143 assert tx_height is not None
144 assert tx_height > 0
146 # test copying
147 copy: MoneroOutputWallet = output.copy()
148 assert copy != output
149 AssertUtils.assert_equals(copy, output)
150 # TODO: should output copy do deep copy of tx so models are graph instead of tree? Would need to work out circular references
151 # monero-cpp gives non-null output tx
152 # assert copy.tx is None
153 assert copy.tx is not None
154 assert copy.tx == output.tx
156 @classmethod
157 def get_and_test_outputs(cls, wallet: MoneroWallet, query: Optional[MoneroOutputQuery], is_expected: Optional[bool]) -> list[MoneroOutputWallet]:
158 """Fetches and tests wallet outputs (i.e. wallet tx outputs) according to the given query.
160 :param MoneroWallet wallet: wallet to get outputs from.
161 :param MoneroOutputQuery | None query: output query.
162 :param bool | None is_expected: expected non-empty outputs.
163 :returns list[MoneroOutputWallet]: the fetched, tested outputs.
164 """
166 copy: MoneroOutputQuery | None = query.copy() if query is not None else None
167 outputs: list[MoneroOutputWallet] = wallet.get_outputs(query) if query is not None else wallet.get_outputs(MoneroOutputQuery())
168 AssertUtils.assert_equals(copy, query)
170 if is_expected is False:
171 assert len(outputs) == 0
172 elif is_expected is True:
173 assert len(outputs) > 0, "Outputs were expected but not found; run send tests"
175 for output in outputs:
176 OutputUtils.test_output_wallet(output)
178 return outputs