Coverage for tests/wallet/test_support.py: 98%
58 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 logging
4from pathlib import Path
6import pytest
7import yaml
8from monero import MoneroWallet, MoneroWalletConfig, MoneroWalletFull, MoneroWalletKeys, MoneroWalletRpc
10from utils import BaseTestClass, TestUtils as Utils, WalletCall, WALLET_CALLS
12logger: logging.Logger = logging.getLogger("TestMoneroWalletSupport")
14# the support matrix of the docs, which these tests keep true
15SUPPORT_FILE: Path = Path(__file__).parent / "wallet_support.yml"
16WALLETS: tuple[str, ...] = ("full", "rpc", "keys")
17STATES: tuple[str, ...] = ("supported", "partial", "todo", "unsupported")
18# every method once, in the order of the calls: close() is the last one
19METHODS: list[str] = list(dict.fromkeys(call.method for call in WALLET_CALLS))
22@pytest.mark.unit
23class TestMoneroWalletSupport(BaseTestClass):
24 """Checks the support matrix of the docs (tests/wallet/wallet_support.yml) against the bindings: every method of the interface is
25 called on a full, an rpc and a keys-only wallet, and the methods that a wallet does not implement raise "not supported"."""
27 @pytest.fixture(scope="class")
28 @classmethod
29 def support(cls) -> dict[str, dict[str, str]]:
30 """The entries of the matrix by method, without their sections."""
31 with open(SUPPORT_FILE, encoding="utf-8") as file:
32 sections: dict[str, dict[str, dict[str, str]]] = yaml.safe_load(file)
33 return {method: entry for methods in sections.values() for method, entry in methods.items()}
35 @pytest.fixture(scope="class")
36 @classmethod
37 def wallets(cls) -> dict[str, MoneroWallet]:
38 """A wallet of each type, of its own: the calls change and close them. None of them reaches the network: the full one has no
39 daemon and the rpc one has no server, so their calls fail at once when they are implemented."""
40 full_config: MoneroWalletConfig = MoneroWalletConfig()
41 full_config.path = ""
42 full_config.password = Utils.WALLET_PASSWORD
43 full_config.network_type = Utils.NETWORK_TYPE
44 keys_config: MoneroWalletConfig = MoneroWalletConfig()
45 keys_config.network_type = Utils.NETWORK_TYPE
46 return {
47 "full": MoneroWalletFull.create_wallet(full_config),
48 "rpc": MoneroWalletRpc(),
49 "keys": MoneroWalletKeys.create_wallet_random(keys_config),
50 }
52 #region Tests
54 # Every public method of MoneroWallet has a call, so a new one has a row in the matrix
55 def test_every_method_is_called(self) -> None:
56 methods: set[str] = {name for name in dir(MoneroWallet) if not name.startswith("_") and callable(getattr(MoneroWallet, name))}
57 assert methods - set(METHODS) == set(), "methods of MoneroWallet with no call in WALLET_CALLS"
58 assert set(METHODS) - methods == set(), "calls in WALLET_CALLS of methods that MoneroWallet does not have"
60 # Every method is in the matrix once, with a known state for each wallet
61 def test_matrix_is_complete(self, support: dict[str, dict[str, str]]) -> None:
62 assert set(METHODS) - set(support) == set(), f"methods missing from {SUPPORT_FILE.name}"
63 assert set(support) - set(METHODS) == set(), f"methods in {SUPPORT_FILE.name} that MoneroWallet does not have"
64 for method, entry in support.items():
65 for wallet in WALLETS:
66 assert entry.get(wallet) in STATES, f"{method}: the state of the {wallet} wallet is {entry.get(wallet)}, not one of {STATES}"
67 partial: bool = "partial" in (entry[wallet] for wallet in WALLETS)
68 assert partial == ("note" in entry), f"{method}: a note is for a partial support, and a partial support needs one"
70 # Each wallet implements the methods that the matrix says, and not the others
71 @pytest.mark.parametrize("method", METHODS)
72 def test_support(self, wallets: dict[str, MoneroWallet], support: dict[str, dict[str, str]], method: str) -> None:
73 calls: list[WalletCall] = [call for call in WALLET_CALLS if call.method == method]
74 for name, wallet in wallets.items():
75 state: str = support[method][name]
76 if state in ("supported", "partial") and any(call.waits for call in calls):
77 # it would wait for the network: the wallet is said to implement it, and the other wallets are checked
78 continue
79 implemented: list[bool] = [call.is_implemented_by(wallet) for call in calls]
80 if state == "supported":
81 assert all(implemented), f"{method} on the {name} wallet: supported, but it does not implement {self._missing(calls, implemented)}"
82 elif state == "partial":
83 assert any(implemented) and not all(implemented), \
84 f"{method} on the {name} wallet: partial, but it implements {'every' if all(implemented) else 'no'} overload"
85 else:
86 assert not any(implemented), f"{method} on the {name} wallet: {state}, but it implements it"
88 #endregion
90 @staticmethod
91 def _missing(calls: list[WalletCall], implemented: list[bool]) -> str:
92 return ", ".join(str(call) for call, done in zip(calls, implemented) if not done)