@@ -62,6 +62,56 @@ def test_block_height(self):
6262 self .mocked_websocket .return_value .get_message_property_to_hex .assert_called_once_with (
6363 'number' )
6464
65+ def test_finalized_block_height (self ):
66+ """Tests the finalized_block_height function uses the correct call and args to get finalized block height"""
67+ payload = {
68+ "jsonrpc" : "2.0" ,
69+ "method" : "eth_getBlockByNumber" ,
70+ "params" : ["finalized" , False ],
71+ "id" : self .chain_id
72+ }
73+ self .evm_collector .finalized_block_height ()
74+ self .mocked_websocket .return_value .query .assert_called_once_with (payload )
75+
76+ def test_finalized_block_height_return_none_when_query_none (self ):
77+ """Tests that finalized_block_height returns None if the query returns None"""
78+ self .mocked_websocket .return_value .query .return_value = None
79+ result = self .evm_collector .finalized_block_height ()
80+ self .assertEqual (None , result )
81+
82+ def test_finalized_block_height_return_none_when_no_number_field (self ):
83+ """Tests that finalized_block_height returns None if the response has no 'number' field"""
84+ self .mocked_websocket .return_value .query .return_value = {"hash" : "0x123" }
85+ result = self .evm_collector .finalized_block_height ()
86+ self .assertEqual (None , result )
87+
88+ def test_finalized_block_height_return (self ):
89+ """Tests that finalized_block_height converts hex block number to integer correctly"""
90+ mock_block_response = {
91+ "number" : "0x1a2b3c" ,
92+ "hash" : "0x456def"
93+ }
94+ self .mocked_websocket .return_value .query .return_value = mock_block_response
95+ result = self .evm_collector .finalized_block_height ()
96+ # 0x1a2b3c = 1715004 in decimal
97+ self .assertEqual (1715004 , result )
98+
99+ def test_finalized_block_height_hex_conversion (self ):
100+ """Tests that finalized_block_height handles various hex values correctly"""
101+ test_cases = [
102+ ("0x0" , 0 ),
103+ ("0x1" , 1 ),
104+ ("0xff" , 255 ),
105+ ("0x1000" , 4096 )
106+ ]
107+
108+ for hex_value , expected_int in test_cases :
109+ with self .subTest (hex_value = hex_value ):
110+ mock_block_response = {"number" : hex_value }
111+ self .mocked_websocket .return_value .query .return_value = mock_block_response
112+ result = self .evm_collector .finalized_block_height ()
113+ self .assertEqual (expected_int , result )
114+
65115 def test_client_version (self ):
66116 """Tests the client_version function uses the correct call and args to get client version"""
67117 payload = {
@@ -735,8 +785,8 @@ def test_latency(self):
735785 self .mocked_connection .return_value .latest_query_latency = 0.123
736786 self .assertEqual (0.123 , self .aptos_collector .latency ())
737787
738- class TestTronCollector (TestCase ):
739- """Tests the Tron collector class"""
788+ class TestEvmHttpCollector (TestCase ):
789+ """Tests the EvmHttp collector class"""
740790
741791 def setUp (self ):
742792 self .url = "https://test.com"
@@ -747,7 +797,7 @@ def setUp(self):
747797 self .client_params = {
748798 "open_timeout" : self .open_timeout , "ping_timeout" : self .ping_timeout }
749799 with mock .patch ('collectors.HttpsInterface' ) as mocked_connection :
750- self .tron_collector = collectors .TronCollector (
800+ self .evmhttp_collector = collectors .EvmHttpCollector (
751801 self .url , self .labels , self .chain_id , ** self .client_params )
752802 self .mocked_connection = mocked_connection
753803
@@ -756,7 +806,7 @@ def test_logger_metadata(self):
756806 expected_metadata = {
757807 'component' : 'TronCollector' , 'url' : 'test.com' }
758808 self .assertEqual (expected_metadata ,
759- self .tron_collector ._logger_metadata )
809+ self .evmhttp_collector ._logger_metadata )
760810
761811 def test_https_interface_created (self ):
762812 """Tests that the Tron collector calls the https interface with the correct args"""
@@ -765,52 +815,52 @@ def test_https_interface_created(self):
765815
766816 def test_interface_attribute_exists (self ):
767817 """Tests that the interface attribute exists."""
768- self .assertTrue (hasattr (self .tron_collector , 'interface' ))
818+ self .assertTrue (hasattr (self .evmhttp_collector , 'interface' ))
769819
770820 def test_alive_call (self ):
771821 """Tests the alive function uses the correct call"""
772- self .tron_collector .alive ()
822+ self .evmhttp_collector .alive ()
773823 self .mocked_connection .return_value .cached_json_rpc_post .assert_called_once_with (
774- self .tron_collector .client_version_payload )
824+ self .evmhttp_collector .client_version_payload )
775825
776826 def test_alive_false (self ):
777827 """Tests the alive function returns false when post returns None"""
778828 self .mocked_connection .return_value .cached_json_rpc_post .return_value = None
779- result = self .tron_collector .alive ()
829+ result = self .evmhttp_collector .alive ()
780830 self .assertFalse (result )
781831
782832 def test_block_height (self ):
783833 """Tests the block_height function uses the correct call to get block height"""
784834 self .mocked_connection .return_value .cached_json_rpc_post .return_value = "0x1a2b3c"
785- result = self .tron_collector .block_height ()
835+ result = self .evmhttp_collector .block_height ()
786836 self .mocked_connection .return_value .cached_json_rpc_post .assert_called_once_with (
787- self .tron_collector .block_height_payload )
837+ self .evmhttp_collector .block_height_payload )
788838 self .assertEqual (result , 1715004 )
789839
790840 def test_block_height_raises_value_error (self ):
791841 """Tests that the block height raises ValueError if result is invalid"""
792842 self .mocked_connection .return_value .cached_json_rpc_post .return_value = "invalid"
793843 with self .assertRaises (ValueError ):
794- self .tron_collector .block_height ()
844+ self .evmhttp_collector .block_height ()
795845
796846 def test_client_version (self ):
797847 """Tests the client_version function uses the correct call to get client version"""
798848 self .mocked_connection .return_value .cached_json_rpc_post .return_value = "Tron/v1.0.0"
799- result = self .tron_collector .client_version ()
849+ result = self .evmhttp_collector .client_version ()
800850 self .mocked_connection .return_value .cached_json_rpc_post .assert_called_once_with (
801- self .tron_collector .client_version_payload )
851+ self .evmhttp_collector .client_version_payload )
802852 self .assertEqual (result , {"client_version" : "Tron/v1.0.0" })
803853
804854 def test_client_version_returns_none (self ):
805855 """Tests that the client_version returns None if cached_json_rpc_post returns None"""
806856 self .mocked_connection .return_value .cached_json_rpc_post .return_value = None
807- result = self .tron_collector .client_version ()
857+ result = self .evmhttp_collector .client_version ()
808858 self .assertIsNone (result )
809859
810860 def test_latency (self ):
811861 """Tests that the latency is obtained from the interface based on latest_query_latency"""
812862 self .mocked_connection .return_value .latest_query_latency = 0.123
813- self .assertEqual (0.123 , self .tron_collector .latency ())
863+ self .assertEqual (0.123 , self .evmhttp_collector .latency ())
814864
815865class TestXRPLCollector (TestCase ):
816866 """Tests the XRPL collector class"""
0 commit comments