@@ -734,3 +734,80 @@ def test_latency(self):
734
734
"""Tests that the latency is obtained from the interface based on latest_query_latency"""
735
735
self .mocked_connection .return_value .latest_query_latency = 0.123
736
736
self .assertEqual (0.123 , self .aptos_collector .latency ())
737
+
738
+ class TestTronCollector (TestCase ):
739
+ """Tests the Tron collector class"""
740
+
741
+ def setUp (self ):
742
+ self .url = "https://test.com"
743
+ self .labels = ["dummy" , "labels" ]
744
+ self .chain_id = 123
745
+ self .open_timeout = 8
746
+ self .ping_timeout = 9
747
+ self .client_params = {
748
+ "open_timeout" : self .open_timeout , "ping_timeout" : self .ping_timeout }
749
+ with mock .patch ('collectors.HttpsInterface' ) as mocked_connection :
750
+ self .tron_collector = collectors .TronCollector (
751
+ self .url , self .labels , self .chain_id , ** self .client_params )
752
+ self .mocked_connection = mocked_connection
753
+
754
+ def test_logger_metadata (self ):
755
+ """Validate logger metadata. Makes sure url is stripped by helpers.strip_url function."""
756
+ expected_metadata = {
757
+ 'component' : 'TronCollector' , 'url' : 'test.com' }
758
+ self .assertEqual (expected_metadata ,
759
+ self .tron_collector ._logger_metadata )
760
+
761
+ def test_https_interface_created (self ):
762
+ """Tests that the Tron collector calls the https interface with the correct args"""
763
+ self .mocked_connection .assert_called_once_with (
764
+ self .url , self .open_timeout , self .ping_timeout )
765
+
766
+ def test_interface_attribute_exists (self ):
767
+ """Tests that the interface attribute exists."""
768
+ self .assertTrue (hasattr (self .tron_collector , 'interface' ))
769
+
770
+ def test_alive_call (self ):
771
+ """Tests the alive function uses the correct call"""
772
+ self .tron_collector .alive ()
773
+ self .mocked_connection .return_value .cached_json_rpc_post .assert_called_once_with (
774
+ self .tron_collector .client_version_payload )
775
+
776
+ def test_alive_false (self ):
777
+ """Tests the alive function returns false when post returns None"""
778
+ self .mocked_connection .return_value .cached_json_rpc_post .return_value = None
779
+ result = self .tron_collector .alive ()
780
+ self .assertFalse (result )
781
+
782
+ def test_block_height (self ):
783
+ """Tests the block_height function uses the correct call to get block height"""
784
+ self .mocked_connection .return_value .cached_json_rpc_post .return_value = "0x1a2b3c"
785
+ result = self .tron_collector .block_height ()
786
+ self .mocked_connection .return_value .cached_json_rpc_post .assert_called_once_with (
787
+ self .tron_collector .block_height_payload )
788
+ self .assertEqual (result , 1715004 )
789
+
790
+ def test_block_height_raises_value_error (self ):
791
+ """Tests that the block height raises ValueError if result is invalid"""
792
+ self .mocked_connection .return_value .cached_json_rpc_post .return_value = "invalid"
793
+ with self .assertRaises (ValueError ):
794
+ self .tron_collector .block_height ()
795
+
796
+ 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 ()
800
+ 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" })
803
+
804
+ def test_client_version_returns_none (self ):
805
+ """Tests that the client_version returns None if cached_json_rpc_post returns None"""
806
+ self .mocked_connection .return_value .cached_json_rpc_post .return_value = None
807
+ result = self .tron_collector .client_version ()
808
+ self .assertIsNone (result )
809
+
810
+ def test_latency (self ):
811
+ """Tests that the latency is obtained from the interface based on latest_query_latency"""
812
+ self .mocked_connection .return_value .latest_query_latency = 0.123
813
+ self .assertEqual (0.123 , self .tron_collector .latency ())
0 commit comments