@@ -62,6 +62,44 @@ 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 that finalized_block_height uses correct call and args to get finalized block"""
67+ # Mock with hex string, not integer
68+ mock_block_response = {"number" : "0x1a2b3c" }
69+ self .mocked_websocket .return_value .query .return_value = mock_block_response
70+
71+ payload = {
72+ "jsonrpc" : "2.0" ,
73+ "method" : "eth_getBlockByNumber" ,
74+ "params" : ["finalized" , False ],
75+ "id" : self .chain_id
76+ }
77+ self .evm_collector .finalized_block_height ()
78+ self .mocked_websocket .return_value .query .assert_called_once_with (payload )
79+
80+ def test_finalized_block_height_return_none_when_query_none (self ):
81+ """Tests that finalized_block_height returns None if the query returns None"""
82+ self .mocked_websocket .return_value .query .return_value = None
83+ result = self .evm_collector .finalized_block_height ()
84+ self .assertEqual (None , result )
85+
86+ def test_finalized_block_height_return_none_when_no_number_field (self ):
87+ """Tests that finalized_block_height returns None if the response has no 'number' field"""
88+ self .mocked_websocket .return_value .query .return_value = {"hash" : "0x123" }
89+ result = self .evm_collector .finalized_block_height ()
90+ self .assertEqual (None , result )
91+
92+ def test_finalized_block_height_return (self ):
93+ """Tests that finalized_block_height converts hex block number to integer correctly"""
94+ mock_block_response = {
95+ "number" : "0x1a2b3c" , # Hex string as your code expects
96+ "hash" : "0x456def"
97+ }
98+ self .mocked_websocket .return_value .query .return_value = mock_block_response
99+ result = self .evm_collector .finalized_block_height ()
100+ # 0x1a2b3c = 1715004 in decimal
101+ self .assertEqual (1715004 , result )
102+
65103 def test_client_version (self ):
66104 """Tests the client_version function uses the correct call and args to get client version"""
67105 payload = {
@@ -735,8 +773,8 @@ def test_latency(self):
735773 self .mocked_connection .return_value .latest_query_latency = 0.123
736774 self .assertEqual (0.123 , self .aptos_collector .latency ())
737775
738- class TestTronCollector (TestCase ):
739- """Tests the Tron collector class"""
776+ class TestEvmHttpCollector (TestCase ):
777+ """Tests the EvmHttp collector class"""
740778
741779 def setUp (self ):
742780 self .url = "https://test.com"
@@ -747,70 +785,73 @@ def setUp(self):
747785 self .client_params = {
748786 "open_timeout" : self .open_timeout , "ping_timeout" : self .ping_timeout }
749787 with mock .patch ('collectors.HttpsInterface' ) as mocked_connection :
750- self .tron_collector = collectors .TronCollector (
788+ self .evmhttp_collector = collectors .EvmHttpCollector (
751789 self .url , self .labels , self .chain_id , ** self .client_params )
752790 self .mocked_connection = mocked_connection
753791
754792 def test_logger_metadata (self ):
755793 """Validate logger metadata. Makes sure url is stripped by helpers.strip_url function."""
756794 expected_metadata = {
757- 'component' : 'TronCollector ' , 'url' : 'test.com' }
795+ 'component' : 'EvmHttpCollector ' , 'url' : 'test.com' }
758796 self .assertEqual (expected_metadata ,
759- self .tron_collector ._logger_metadata )
797+ self .evmhttp_collector ._logger_metadata )
760798
761799 def test_https_interface_created (self ):
762- """Tests that the Tron collector calls the https interface with the correct args"""
800+ """Tests that the EvmHttp collector calls the https interface with the correct args"""
763801 self .mocked_connection .assert_called_once_with (
764802 self .url , self .open_timeout , self .ping_timeout )
765803
766804 def test_interface_attribute_exists (self ):
767805 """Tests that the interface attribute exists."""
768- self .assertTrue (hasattr (self .tron_collector , 'interface' ))
806+ self .assertTrue (hasattr (self .evmhttp_collector , 'interface' ))
769807
770808 def test_alive_call (self ):
771809 """Tests the alive function uses the correct call"""
772- self .tron_collector .alive ()
810+ self .evmhttp_collector .alive ()
773811 self .mocked_connection .return_value .cached_json_rpc_post .assert_called_once_with (
774- self .tron_collector .client_version_payload )
812+ self .evmhttp_collector .client_version_payload )
775813
776814 def test_alive_false (self ):
777815 """Tests the alive function returns false when post returns None"""
778816 self .mocked_connection .return_value .cached_json_rpc_post .return_value = None
779- result = self .tron_collector .alive ()
817+ result = self .evmhttp_collector .alive ()
780818 self .assertFalse (result )
781819
782820 def test_block_height (self ):
783821 """Tests the block_height function uses the correct call to get block height"""
784822 self .mocked_connection .return_value .cached_json_rpc_post .return_value = "0x1a2b3c"
785- result = self .tron_collector .block_height ()
823+ result = self .evmhttp_collector .block_height ()
786824 self .mocked_connection .return_value .cached_json_rpc_post .assert_called_once_with (
787- self .tron_collector .block_height_payload )
825+ self .evmhttp_collector .block_height_payload )
788826 self .assertEqual (result , 1715004 )
789827
790828 def test_block_height_raises_value_error (self ):
791829 """Tests that the block height raises ValueError if result is invalid"""
792830 self .mocked_connection .return_value .cached_json_rpc_post .return_value = "invalid"
793831 with self .assertRaises (ValueError ):
794- self .tron_collector .block_height ()
832+ self .evmhttp_collector .block_height ()
795833
796834 def test_client_version (self ):
797- """Tests the client_version function uses the correct call to get client version"""
798- self .mocked_connection .return_value .cached_json_rpc_post .return_value = "Tron/v1.0.0"
799- result = self .tron_collector .client_version ()
835+ """Tests the client_version function uses the correct call and args to get client version"""
836+ payload = {
837+ "jsonrpc" : "2.0" ,
838+ "method" : "web3_clientVersion" ,
839+ "id" : 1
840+ }
841+ self .evmhttp_collector .client_version ()
800842 self .mocked_connection .return_value .cached_json_rpc_post .assert_called_once_with (
801- self .tron_collector .client_version_payload )
802- self .assertEqual (result , {"client_version" : "Tron/v1.0.0" })
843+ payload )
803844
804845 def test_client_version_returns_none (self ):
805846 """Tests that the client_version returns None if cached_json_rpc_post returns None"""
806847 self .mocked_connection .return_value .cached_json_rpc_post .return_value = None
807- result = self .tron_collector .client_version ()
848+ result = self .evmhttp_collector .client_version ()
808849 self .assertIsNone (result )
809850
810851 def test_latency (self ):
811852 """Tests that the latency is obtained from the interface based on latest_query_latency"""
812853 self .mocked_connection .return_value .latest_query_latency = 0.123
813- self .assertEqual (0.123 , self .tron_collector .latency ())
854+ self .assertEqual (0.123 , self .evmhttp_collector .latency ())
814855
815856class TestXRPLCollector (TestCase ):
816857 """Tests the XRPL collector class"""
0 commit comments