Coverage for tests/utils/rpc_connection_utils.py: 93%

42 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1import logging 

2 

3import pytest 

4 

5from abc import ABC 

6 

7from monero import SerializableStruct, MoneroRpcConnection, MoneroConnectionType 

8 

9logger: logging.Logger = logging.getLogger("RpcConnectionUtils") 

10 

11 

12class RpcConnectionUtils(ABC): 

13 """Test utils for rpc connections.""" 

14 

15 NETWORK_ERROR_MSG: str = "Network error: no response from offline_server_uri/json_rpc (timeout: 180000ms)" 

16 """Default network error message.""" 

17 

18 @classmethod 

19 def setup_rpc_connection(cls, connection: MoneroRpcConnection) -> None: 

20 """Setup and check rpc connection. 

21 

22 :param MoneroRpcConnection connection: rpc connection to setup. 

23 """ 

24 if connection.is_connected() is not None: 

25 return 

26 cls.test_check_rpc_connection(connection, True) 

27 

28 @classmethod 

29 def test_check_rpc_connection(cls, connection: MoneroRpcConnection, connected: bool) -> None: 

30 """Test rpc connection check. 

31 

32 :param MoneroRpcConnection connection: rpc connection to check. 

33 :param bool connected: expected rpc connection to be connected after check. 

34 """ 

35 # check connection 

36 assert connection.check_connection() 

37 assert not connection.check_connection() 

38 assert connection.is_connected() == connected 

39 assert connection.is_online() == connected 

40 

41 if connected: 

42 assert connection.response_time is not None 

43 assert connection.response_time > 0 

44 logger.debug(f"Rpc connection response time: {connection.response_time} ms") 

45 else: 

46 assert connection.response_time is None 

47 

48 @classmethod 

49 def test_rpc_connection( 

50 cls, 

51 connection: MoneroRpcConnection | None, 

52 uri: str | None, 

53 connected: bool, 

54 connection_type: MoneroConnectionType | None 

55 ) -> None: 

56 """Test a monero rpc connection. 

57 

58 :param MoneroRpcConnection | None connection: rpc connection to test. 

59 :param str | None uri: rpc uri of the connection to test. 

60 :param bool connected: checks if rpc is connected or not. 

61 :param MoneroConnectionType | None connection_type: type of rpc connection to test. 

62 :raises AssertionError: raises an error if rpc connection is not as expected. 

63 """ 

64 # check expected values from rpc connection 

65 assert connection is not None 

66 assert isinstance(connection, SerializableStruct) 

67 assert isinstance(connection, MoneroRpcConnection) 

68 assert uri is not None 

69 assert len(uri) > 0 

70 assert connection.uri == uri 

71 

72 # test check connection 

73 cls.test_check_rpc_connection(connection, connected) 

74 

75 # response_time is a read-only property 

76 with pytest.raises(Exception): 

77 connection.response_time = 0 # type: ignore 

78 

79 # test connection type 

80 if connection_type == MoneroConnectionType.I2P: 

81 assert connection.is_i2p() 

82 elif connection_type == MoneroConnectionType.TOR: 

83 assert connection.is_onion() 

84 elif connection_type is not None: 

85 assert not connection.is_i2p() 

86 assert not connection.is_onion()