Coverage for tests/utils/context/tx_context.py: 74%
77 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
1from __future__ import annotations
3import json
5from typing import Any, Optional
6from typing_extensions import override
7from monero import MoneroWallet, MoneroTxConfig
9from .serializable_context import SerializableContext
12class TxContext(SerializableContext):
13 """Provides context or configuration for test methods to test a type."""
15 wallet: Optional[MoneroWallet] = None
16 """Context wallet."""
17 config: Optional[MoneroTxConfig] = None
18 """Transaction configuration."""
19 has_outgoing_transfer: Optional[bool] = None
20 """Expect outgoing transfer in tx."""
21 has_incoming_transfers: Optional[bool] = None
22 """Expect incoming transfers in tx."""
23 has_destinations: Optional[bool] = None
24 """Expect destinations in tx."""
25 is_copy: Optional[bool] = None
26 """Indicates if a copy is being tested which means back references won't be the same."""
27 include_outputs: Optional[bool] = None
28 """Expects outputs in tx."""
29 is_send_response: Optional[bool] = None
30 """Expect newly created tx."""
31 is_sweep_response: Optional[bool] = None
32 """Expect newly created tx from sweep action."""
33 is_sweep_output_response: Optional[bool] = None
34 """Expect newly created tx from specific output sweep."""
35 # TODO monero-wallet-rpc: this only necessary because sweep_output does not return account index
37 def __init__(self, ctx: Optional[TxContext] = None) -> None:
38 """Initialize a new tx context.
40 :param TxContext | None ctx: Transaction context to copy.
41 """
42 if ctx is not None:
43 # copy reference
44 self.wallet = ctx.wallet
45 self.config = ctx.config
46 self.has_outgoing_transfer = ctx.has_outgoing_transfer
47 self.has_incoming_transfers = ctx.has_incoming_transfers
48 self.has_destinations = ctx.has_destinations
49 self.is_copy = ctx.is_copy
50 self.include_outputs = ctx.include_outputs
51 self.is_send_response = ctx.is_send_response
52 self.is_sweep_response = ctx.is_sweep_response
53 self.is_sweep_output_response = ctx.is_sweep_output_response
55 @override
56 def to_dict(self) -> dict[str, Any]:
57 """Build the JSON object for this tx context (defined fields only).
59 ``wallet`` is a live wallet handle rather than serializable data, so it
60 is only reflected by the ``hasWallet`` flag and is not restored on
61 :meth:`deserialize`.
63 :returns dict[str, Any]: the context as a JSON-serializable dict.
64 """
65 root: dict[str, Any] = {}
66 if self.wallet is not None:
67 root["hasWallet"] = True
68 if self.config is not None:
69 root["config"] = json.loads(self.config.serialize())
70 self._put(root, "hasOutgoingTransfer", self.has_outgoing_transfer)
71 self._put(root, "hasIncomingTransfers", self.has_incoming_transfers)
72 self._put(root, "hasDestinations", self.has_destinations)
73 self._put(root, "isCopy", self.is_copy)
74 self._put(root, "includeOutputs", self.include_outputs)
75 self._put(root, "isSendResponse", self.is_send_response)
76 self._put(root, "isSweepResponse", self.is_sweep_response)
77 self._put(root, "isSweepOutputResponse", self.is_sweep_output_response)
78 return root
80 @override
81 @staticmethod
82 def from_dict(node: dict[str, Any], ctx: TxContext) -> None:
83 """Populate ``ctx`` from a parsed JSON object.
85 :param dict[str, Any] node: parsed JSON object.
86 :param TxContext ctx: instance to populate.
87 """
88 for key, value in node.items():
89 if key == "config":
90 ctx.config = MoneroTxConfig.deserialize(json.dumps(value))
91 elif key == "hasOutgoingTransfer":
92 ctx.has_outgoing_transfer = value
93 elif key == "hasIncomingTransfers":
94 ctx.has_incoming_transfers = value
95 elif key == "hasDestinations":
96 ctx.has_destinations = value
97 elif key == "isCopy":
98 ctx.is_copy = value
99 elif key == "includeOutputs":
100 ctx.include_outputs = value
101 elif key == "isSendResponse":
102 ctx.is_send_response = value
103 elif key == "isSweepResponse":
104 ctx.is_sweep_response = value
105 elif key == "isSweepOutputResponse":
106 ctx.is_sweep_output_response = value
107 # "hasWallet" is informational only; the live wallet handle cannot be restored