Coverage for tests/utils/context/test_context.py: 71%
95 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
3from typing import Any, Optional
4from typing_extensions import override
6from .serializable_context import SerializableContext
9class TestContext(SerializableContext):
10 """Provides context or configuration for test methods to test a type."""
12 __test__ = False
14 has_json: Optional[bool] = None
15 """Expect presence of json field."""
16 is_pruned: Optional[bool] = None
17 """Expect pruning."""
18 is_full: Optional[bool] = None
19 """Expect complete model."""
20 is_confirmed: Optional[bool] = None
21 """Expect confirmations."""
22 is_miner_tx: Optional[bool] = None
23 """Expect miner tx."""
24 from_get_tx_pool: Optional[bool] = None
25 """Expect from tx pool."""
26 from_binary_block: Optional[bool] = None
27 """Expect from binary block."""
28 has_output_indices: Optional[bool] = None
29 """Expect output indices."""
30 do_not_test_copy: Optional[bool] = None
31 """Disable copy tests."""
32 has_txs: Optional[bool] = None
33 """Expect txs."""
34 has_hex: Optional[bool] = None
35 """Expect hex field."""
36 header_is_full: Optional[bool] = None
37 """Expect full header."""
38 tx_context: Optional[TestContext] = None
39 """Tx context."""
41 def __init__(self, ctx: Optional[TestContext] = None) -> None:
42 """Initialize a new test context.
44 :param TestContext | None ctx: test context to copy.
45 """
46 if ctx is not None:
47 # copy reference
48 self.has_json = ctx.has_json
49 self.is_pruned = ctx.is_pruned
50 self.is_full = ctx.is_full
51 self.is_confirmed = ctx.is_confirmed
52 self.is_miner_tx = ctx.is_miner_tx
53 self.from_get_tx_pool = ctx.from_get_tx_pool
54 self.from_binary_block = ctx.from_binary_block
55 self.has_output_indices = ctx.has_output_indices
56 self.do_not_test_copy = ctx.do_not_test_copy
57 self.has_txs = ctx.has_txs
58 self.header_is_full = ctx.header_is_full
59 self.tx_context = ctx.tx_context
61 @override
62 def to_dict(self) -> dict[str, Any]:
63 """Build the JSON object for this test context (defined fields only).
65 :returns dict[str, Any]: the context as a JSON-serializable dict.
66 """
67 root: dict[str, Any] = {}
68 self._put(root, "hasJson", self.has_json)
69 self._put(root, "isPruned", self.is_pruned)
70 self._put(root, "isFull", self.is_full)
71 self._put(root, "isConfirmed", self.is_confirmed)
72 self._put(root, "isMinerTx", self.is_miner_tx)
73 self._put(root, "fromGetTxPool", self.from_get_tx_pool)
74 self._put(root, "fromBinaryBlock", self.from_binary_block)
75 self._put(root, "hasOutputIndices", self.has_output_indices)
76 self._put(root, "doNotTestCopy", self.do_not_test_copy)
77 self._put(root, "hasTxs", self.has_txs)
78 self._put(root, "hasHex", self.has_hex)
79 self._put(root, "headerIsFull", self.header_is_full)
80 if self.tx_context is not None:
81 root["txContext"] = self.tx_context.to_dict()
82 return root
84 @override
85 @staticmethod
86 def from_dict(node: dict[str, Any], ctx: TestContext) -> None:
87 """Populate ``ctx`` from a parsed JSON object.
89 :param dict[str, Any] node: parsed JSON object.
90 :param TestContext ctx: instance to populate.
91 """
92 for key, value in node.items():
93 if key == "hasJson":
94 ctx.has_json = value
95 elif key == "isPruned":
96 ctx.is_pruned = value
97 elif key == "isFull":
98 ctx.is_full = value
99 elif key == "isConfirmed":
100 ctx.is_confirmed = value
101 elif key == "isMinerTx":
102 ctx.is_miner_tx = value
103 elif key == "fromGetTxPool":
104 ctx.from_get_tx_pool = value
105 elif key == "fromBinaryBlock":
106 ctx.from_binary_block = value
107 elif key == "hasOutputIndices":
108 ctx.has_output_indices = value
109 elif key == "doNotTestCopy":
110 ctx.do_not_test_copy = value
111 elif key == "hasTxs":
112 ctx.has_txs = value
113 elif key == "hasHex":
114 ctx.has_hex = value
115 elif key == "headerIsFull":
116 ctx.header_is_full = value
117 elif key == "txContext":
118 ctx.tx_context = TestContext()
119 TestContext.from_dict(value, ctx.tx_context)