-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_registries.py
More file actions
235 lines (204 loc) · 10.9 KB
/
test_registries.py
File metadata and controls
235 lines (204 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""Tests the registries module"""
import os
from unittest import TestCase, mock
from structlog.testing import capture_logs
from registries import Endpoint, EndpointRegistry, CollectorRegistry
class TestEndpoint(TestCase): # pylint: disable=too-many-instance-attributes
"""Tests the Endpoint class"""
def setUp(self):
"""Creates dummy data for a test endpoint"""
self.url = "http://test.com"
self.provider = "provider"
self.blockchain = "test_chain"
self.network_name = "test_network"
self.network_type = "ETH"
self.integration_maturity = "development"
self.canonical_name = "test-chain-network"
self.chain_selector = 121212
self.chain_id = 123
self.client_params = {"dummy": "data"}
self.endpoint = Endpoint(self.url, self.provider, self.blockchain,
self.network_name, self.network_type,
self.integration_maturity, self.canonical_name,
self.chain_selector,
self.chain_id, **self.client_params)
def test_url_attribute(self):
"""Tests the url attribute is set correctly"""
self.assertEqual(self.url, self.endpoint.url)
def test_chain_id_attribute(self):
"""Tests the chain_id attribute is set correctly"""
self.assertEqual(self.chain_id, self.endpoint.chain_id)
def test_labels_attribute(self):
"""Tests the labels attribute is set correctly"""
labels = [self.url, self.provider, self.blockchain,
self.network_name, self.network_type,
self.integration_maturity, self.canonical_name,
str(self.chain_selector),
str(self.chain_id)]
self.assertEqual(labels, self.endpoint.labels)
class TestEndpointRegistry(TestCase):
"""Tests the EndpointRegistry class"""
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_bitcoin.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def setUp(self):
self.endpoint_registry = EndpointRegistry()
def test_logger_metadata(self):
"""Tests that the logger metadata is correct"""
expected_metadata = {'component': 'Registries'}
self.assertDictEqual(
expected_metadata,
self.endpoint_registry._logger_metadata) # pylint: disable=protected-access
def test_blockchain_property(self):
"""Tests the blockchain property returns the correct value"""
self.assertEqual('bitcoin', self.endpoint_registry.blockchain)
def test_collector_property(self):
"""Tests the collector property returns the correct value"""
self.assertEqual('bitcoin', self.endpoint_registry.collector)
class TestCollectorRegistry(TestCase):
"""Tests the CollectorRegistry class"""
def setUp(self):
self.collector_registry = None
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_conflux.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_conflux(self):
"""Tests that the conflux collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.ConfluxCollector', new=mock.Mock()) as collector:
helper_test_collector_registry(self, collector)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_cardano.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_cardano(self):
"""Tests that the cardano collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.CardanoCollector', new=mock.Mock()) as collector:
helper_test_collector_registry(self, collector)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_bitcoin.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_bitcoin(self):
"""Tests that the bitcoin collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.BitcoinCollector', new=mock.Mock()) as collector:
helper_test_collector_registry(self, collector)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_filecoin.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_filecoin(self):
"""Tests that the filecoin collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.FilecoinCollector', new=mock.Mock()) as collector:
helper_test_collector_registry(self, collector)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_solana.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_solana(self):
"""Tests that the solana collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.SolanaCollector', new=mock.Mock()) as collector:
helper_test_collector_registry(self, collector)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_starknet.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_starknet(self):
"""Tests that the starknet collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.StarknetCollector', new=mock.Mock()) as collector:
helper_test_collector_registry(self, collector)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_aptos.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_aptos(self):
"""Tests that the aptos collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
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_evmhttp.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_evmhttp(self):
"""Tests that the EvmHttp collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.EvmHttpCollector', new=mock.Mock()) as collector:
helper_test_collector_registry(self, collector)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_xrpl.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_xrpl(self):
"""Tests that the XRPL collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.XRPLCollector', 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"
})
def test_get_collector_registry_for_evm(self):
"""Tests that the evm collector is called with the correct args"""
self.collector_registry = CollectorRegistry()
with mock.patch('collectors.EvmCollector', new=mock.Mock()) as collector:
helper_test_collector_registry(self, collector)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_unsupported_blockchain.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_unsupported_chain_exit(self):
"""Tests that the program exits when loading a config file with an unsupported chain"""
with self.assertRaises(SystemExit) as raises_context:
self.collector_registry = CollectorRegistry()
self.collector_registry.get_collector_registry # pylint: disable=pointless-statement
self.assertEqual(1, raises_context.exception.code)
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_unsupported_blockchain.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_for_unsupported_chain_error_log(self):
"""Tests that an error is logged when loading a config file with an unsupported chain"""
try:
with capture_logs() as captured:
self.collector_registry = CollectorRegistry()
self.collector_registry.get_collector_registry # pylint: disable=pointless-statement
except SystemExit:
# Catch and pass on expected SystemExit so tests keep running
pass
self.assertTrue(any(
log['log_level'] == "error" for log in captured)) # pylint: disable=duplicate-code
@mock.patch.dict(os.environ, {
"CONFIG_FILE_PATH": "tests/fixtures/configuration_bitcoin.yaml",
"VALIDATION_FILE_PATH": "tests/fixtures/validation.yaml"
})
def test_get_collector_registry_no_error_log(self):
"""Tests that no error is logged when loading a valid config file"""
try:
with capture_logs() as captured:
self.collector_registry = CollectorRegistry()
self.collector_registry.get_collector_registry # pylint: disable=pointless-statement
except SystemExit:
# Catch and pass on expected SystemExit so tests keep running
pass
self.assertFalse(
any(log['log_level'] == "error" for log in captured))
def helper_test_collector_registry(test_collector_registry, mock_collector):
"""Helper function to check the collector list calls the mock and
ensure the called collector uses the correct args.
This function is to avoid duplicating the code for each collector type"""
collector_list = test_collector_registry.collector_registry.get_collector_registry
test_collector_registry.assertTrue(
all(isinstance(col, mock.Mock) for col in collector_list))
calls = []
for item in test_collector_registry.collector_registry.get_endpoint_registry:
calls.append(mock.call(item.url, item.labels, item.chain_id,
**test_collector_registry.collector_registry.client_parameters))
mock_collector.assert_has_calls(calls, False)