From 97cbfbb08682d1a8f342f872d8fd7c02a435e2d2 Mon Sep 17 00:00:00 2001 From: rodgerjohnson Date: Fri, 30 Aug 2024 21:39:45 +0900 Subject: [PATCH] Initial tests --- src/test_collectors.py | 77 ++++++++++++++++++++++ src/test_registries.py | 11 ++++ src/tests/fixtures/configuration_tron.yaml | 12 ++++ 3 files changed, 100 insertions(+) create mode 100644 src/tests/fixtures/configuration_tron.yaml diff --git a/src/test_collectors.py b/src/test_collectors.py index e296289..5ade458 100644 --- a/src/test_collectors.py +++ b/src/test_collectors.py @@ -734,3 +734,80 @@ def test_latency(self): """Tests that the latency is obtained from the interface based on latest_query_latency""" self.mocked_connection.return_value.latest_query_latency = 0.123 self.assertEqual(0.123, self.aptos_collector.latency()) + +class TestTronCollector(TestCase): + """Tests the Tron collector class""" + + def setUp(self): + self.url = "https://test.com" + self.labels = ["dummy", "labels"] + self.chain_id = 123 + self.open_timeout = 8 + self.ping_timeout = 9 + self.client_params = { + "open_timeout": self.open_timeout, "ping_timeout": self.ping_timeout} + with mock.patch('collectors.HttpsInterface') as mocked_connection: + self.tron_collector = collectors.TronCollector( + self.url, self.labels, self.chain_id, **self.client_params) + self.mocked_connection = mocked_connection + + def test_logger_metadata(self): + """Validate logger metadata. Makes sure url is stripped by helpers.strip_url function.""" + expected_metadata = { + 'component': 'TronCollector', 'url': 'test.com'} + self.assertEqual(expected_metadata, + self.tron_collector._logger_metadata) + + def test_https_interface_created(self): + """Tests that the Tron collector calls the https interface with the correct args""" + self.mocked_connection.assert_called_once_with( + self.url, self.open_timeout, self.ping_timeout) + + def test_interface_attribute_exists(self): + """Tests that the interface attribute exists.""" + self.assertTrue(hasattr(self.tron_collector, 'interface')) + + def test_alive_call(self): + """Tests the alive function uses the correct call""" + self.tron_collector.alive() + self.mocked_connection.return_value.cached_json_rpc_post.assert_called_once_with( + self.tron_collector.client_version_payload) + + def test_alive_false(self): + """Tests the alive function returns false when post returns None""" + self.mocked_connection.return_value.cached_json_rpc_post.return_value = None + result = self.tron_collector.alive() + self.assertFalse(result) + + def test_block_height(self): + """Tests the block_height function uses the correct call to get block height""" + self.mocked_connection.return_value.cached_json_rpc_post.return_value = "0x1a2b3c" + result = self.tron_collector.block_height() + self.mocked_connection.return_value.cached_json_rpc_post.assert_called_once_with( + self.tron_collector.block_height_payload) + self.assertEqual(result, 1715004) + + def test_block_height_raises_value_error(self): + """Tests that the block height raises ValueError if result is invalid""" + self.mocked_connection.return_value.cached_json_rpc_post.return_value = "invalid" + with self.assertRaises(ValueError): + self.tron_collector.block_height() + + def test_client_version(self): + """Tests the client_version function uses the correct call to get client version""" + self.mocked_connection.return_value.cached_json_rpc_post.return_value = "Tron/v1.0.0" + result = self.tron_collector.client_version() + self.mocked_connection.return_value.cached_json_rpc_post.assert_called_once_with( + self.tron_collector.client_version_payload) + self.assertEqual(result, {"client_version": "Tron/v1.0.0"}) + + def test_client_version_returns_none(self): + """Tests that the client_version returns None if cached_json_rpc_post returns None""" + self.mocked_connection.return_value.cached_json_rpc_post.return_value = None + result = self.tron_collector.client_version() + self.assertIsNone(result) + + def test_latency(self): + """Tests that the latency is obtained from the interface based on latest_query_latency""" + self.mocked_connection.return_value.latest_query_latency = 0.123 + self.assertEqual(0.123, self.tron_collector.latency()) diff --git a/src/test_registries.py b/src/test_registries.py index b202efc..a728962 100644 --- a/src/test_registries.py +++ b/src/test_registries.py @@ -139,6 +139,17 @@ def test_get_collector_registry_for_aptos(self): with mock.patch('collectors.AptosCollector', new=mock.Mock()) as collector: helper_test_collector_registry(self, collector) + @mock.patch.dict(os.environ, { + "CONFIG_FILE_PATH": "tests/fixtures/configuration_tron.yaml", + "VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml" + }) + def test_get_collector_registry_for_tron(self): + """Tests that the Tron collector is called with the correct args""" + self.collector_registry = CollectorRegistry() + with mock.patch('collectors.TronCollector', new=mock.Mock()) as collector: + helper_test_collector_registry(self, collector) + + @mock.patch.dict(os.environ, { "CONFIG_FILE_PATH": "tests/fixtures/configuration_evm.yaml", "VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml" diff --git a/src/tests/fixtures/configuration_tron.yaml b/src/tests/fixtures/configuration_tron.yaml new file mode 100644 index 0000000..aa4f380 --- /dev/null +++ b/src/tests/fixtures/configuration_tron.yaml @@ -0,0 +1,12 @@ +blockchain: "Tron" +chain_id: 1234 +network_name: "Testnet" +network_type: "Testnet" +collector: "tron" +endpoints: + - url: https://test1.com + provider: TestProvider1 + - url: https://test2.com + provider: TestProvider2 + - url: https://test3.com + provider: TestProvider3