Skip to content

Commit b805b18

Browse files
committed
Fix tests
1 parent 5201d35 commit b805b18

File tree

4 files changed

+68
-44
lines changed

4 files changed

+68
-44
lines changed

tests/test_aggregate.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from consensus_decentralization.helper import INTERIM_DIR
66
from consensus_decentralization.aggregate import aggregate, Aggregator, divide_timeframe
7+
from consensus_decentralization.helper import get_clustering_flag, get_aggregated_data_dir_name
78

89

910
@pytest.fixture
@@ -15,6 +16,8 @@ def setup_and_cleanup():
1516
"""
1617
# Set up
1718
test_io_dir = INTERIM_DIR / "test_output"
19+
# Mock return value of get_clustering_flag
20+
get_clustering_flag.return_value = True
1821
yield test_io_dir
1922
# Clean up
2023
shutil.rmtree(test_io_dir)
@@ -41,7 +44,7 @@ def mock_sample_bitcoin_mapped_data(setup_and_cleanup):
4144
'{"number": "649064", "timestamp": "2020-09-20 11:17:00 UTC", "reward_addresses": "0000000000000000000000000000000000000000", "creator": "TEST2", "mapping_method": "known_identifiers"},' \
4245
'{"number": "682736", "timestamp": "2021-05-09 11:12:32 UTC", "reward_addresses": "18cBEMRxXHqzWWCxZNtU91F5sbUNKhL5PX", "creator": "ViaBTC", "mapping_method": "known_identifiers"}' \
4346
']'
44-
with open(test_bitcoin_dir / 'mapped_data.json', 'w') as f:
47+
with open(test_bitcoin_dir / 'mapped_data_clustered.json', 'w') as f:
4548
f.write(mapped_data)
4649
return json.loads(mapped_data)
4750

@@ -64,7 +67,7 @@ def mock_sample_ethereum_mapped_data(setup_and_cleanup):
6467
'{"number":"11184329","timestamp":"2020-11-03 12:56:41 UTC","reward_addresses":"0x8595dd9e0438640b5e1254f9df579ac12a86865f","creator":"TEST", "mapping_method": "known_identifiers"},' \
6568
'{"number":"11183793","timestamp":"2020-11-03 10:56:07 UTC","reward_addresses":"0x8595dd9e0438640b5e1254f9df579ac12a86865f","creator":"TEST", "mapping_method": "known_identifiers"}' \
6669
']'
67-
with open(test_ethereum_dir / 'mapped_data.json', 'w') as f:
70+
with open(test_ethereum_dir / 'mapped_data_clustered.json', 'w') as f:
6871
f.write(mapped_data)
6972

7073

@@ -81,7 +84,7 @@ def mock_sample_cardano_mapped_data(setup_and_cleanup):
8184
'{"number":"00000000000","timestamp":"2020-12-31T06:42:00","creator":"Arrakis", "mapping_method": "known_identifiers"},' \
8285
'{"number":"55555555555","timestamp":"2020-12-31T06:42:01","creator":"1percentpool", "mapping_method": "known_identifiers"}' \
8386
']'
84-
with open(test_cardano_dir / 'mapped_data.json', 'w') as f:
87+
with open(test_cardano_dir / 'mapped_data_clustered.json', 'w') as f:
8588
f.write(mapped_data)
8689

8790

@@ -99,7 +102,7 @@ def mock_sample_tezos_mapped_data(setup_and_cleanup):
99102
'{"number": "1650474", "timestamp": "2021-08-30 06:11:58 UTC", "reward_addresses": "tz1Vd1rXpV8hTHbFXCXN3c3qzCsgcU5BZw1e", "creator": "TEST", "mapping_method": "known_addresses"},' \
100103
'{"number": "1651794", "timestamp": "2021-08-30 17:41:08 UTC", "reward_addresses": "None", "creator": "----- UNDEFINED BLOCK PRODUCER -----", "mapping_method": "fallback_mapping"}' \
101104
']'
102-
with open(test_tezos_dir / 'mapped_data.json', 'w') as f:
105+
with open(test_tezos_dir / 'mapped_data_clustered.json', 'w') as f:
103106
f.write(mapped_data)
104107

105108

@@ -109,7 +112,7 @@ def test_aggregate(setup_and_cleanup, mock_sample_bitcoin_mapped_data):
109112
timeframe = (datetime.date(2010, 1, 1), datetime.date(2010, 12, 31))
110113
aggregate(project='sample_bitcoin', output_dir=test_io_dir, timeframe=timeframe, estimation_window=31, frequency=31, force_aggregate=True)
111114

112-
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity/31_day_window_from_2010-01-01_to_2010-12'
115+
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity_clustered/31_day_window_from_2010-01-01_to_2010-12'
113116
'-31_sampled_every_31_days.csv')
114117
assert output_file.is_file() # there is no data from 2010 in the sample but the aggregator still creates the file when called with this timeframe
115118

@@ -119,20 +122,20 @@ def test_aggregate(setup_and_cleanup, mock_sample_bitcoin_mapped_data):
119122
aggregate(project='sample_bitcoin', output_dir=test_io_dir, timeframe=timeframe, estimation_window=30, frequency=30,
120123
force_aggregate=True)
121124

122-
output_file = test_io_dir / 'sample_bitcoin/blocks_per_entity/30_day_window_from_2018-02-01_to_2018-02-28_sampled_every_30_days.csv'
125+
output_file = test_io_dir / 'sample_bitcoin/blocks_per_entity_clustered/30_day_window_from_2018-02-01_to_2018-02-28_sampled_every_30_days.csv'
123126
assert not output_file.is_file()
124127

125128
timeframe = (datetime.date(2018, 3, 1), datetime.date(2018, 3, 31))
126129
aggregate(project='sample_bitcoin', output_dir=test_io_dir, timeframe=timeframe, estimation_window=31, frequency=31,
127130
force_aggregate=True)
128-
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity/31_day_window_from_2018-03-01_to_2018-03'
131+
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity_clustered/31_day_window_from_2018-03-01_to_2018-03'
129132
'-31_sampled_every_31_days.csv')
130133
assert output_file.is_file()
131134

132135
timeframe = (datetime.date(2021, 1, 1), datetime.date(2021, 12, 31))
133136
aggregate(project='sample_bitcoin', output_dir=test_io_dir, timeframe=timeframe, estimation_window=31, frequency=31,
134137
force_aggregate=True)
135-
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity/31_day_window_from_2021-01-01_to_2021-12'
138+
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity_clustered/31_day_window_from_2021-01-01_to_2021-12'
136139
'-31_sampled_every_31_days.csv')
137140
assert output_file.is_file()
138141

@@ -172,7 +175,7 @@ def test_bitcoin_aggregation(setup_and_cleanup, mock_sample_bitcoin_mapped_data)
172175
'GBMiners': '2\n'
173176
}
174177

175-
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity/30_day_window_from_2018-02-01_to_2018-03'
178+
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity_clustered/30_day_window_from_2018-02-01_to_2018-03'
176179
'-02_sampled_every_30_days.csv')
177180
with open(output_file) as f:
178181
for line in f.readlines():
@@ -194,7 +197,7 @@ def test_bitcoin_aggregation(setup_and_cleanup, mock_sample_bitcoin_mapped_data)
194197
'Bitmain': '1\n'
195198
}
196199

197-
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity/all_from_2020-01-01_to_2020-12-31.csv')
200+
output_file = test_io_dir / ('sample_bitcoin/blocks_per_entity_clustered/all_from_2020-01-01_to_2020-12-31.csv')
198201
with open(output_file) as f:
199202
for line in f.readlines():
200203
col_1, col_2 = line.split(',')
@@ -220,7 +223,7 @@ def test_ethereum_aggregation(setup_and_cleanup, mock_sample_ethereum_mapped_dat
220223
'0x45133a7e1cc7e18555ae8a4ee632a8a61de90df6': '1\n'
221224
}
222225

223-
output_file = test_io_dir / ('sample_ethereum/blocks_per_entity/30_day_window_from_2020-11-01_to_2020-11'
226+
output_file = test_io_dir / ('sample_ethereum/blocks_per_entity_clustered/30_day_window_from_2020-11-01_to_2020-11'
224227
'-30_sampled_every_30_days.csv')
225228
with open(output_file) as f:
226229
for line in f.readlines():
@@ -241,7 +244,7 @@ def test_ethereum_aggregation(setup_and_cleanup, mock_sample_ethereum_mapped_dat
241244
'MEV Builder: 0x3B...436': '1\n'
242245
}
243246

244-
output_file = test_io_dir / ('sample_ethereum/blocks_per_entity/365_day_window_from_2023-01-01_to_2023-12'
247+
output_file = test_io_dir / ('sample_ethereum/blocks_per_entity_clustered/365_day_window_from_2023-01-01_to_2023-12'
245248
'-31_sampled_every_365_days.csv')
246249
with open(output_file) as f:
247250
for line in f.readlines():
@@ -270,7 +273,7 @@ def test_cardano_aggregation(setup_and_cleanup, mock_sample_cardano_mapped_data)
270273
'1percentpool': '1\n'
271274
}
272275

273-
output_file = test_io_dir / ('sample_cardano/blocks_per_entity/31_day_window_from_2020-12-01_to_2020-12'
276+
output_file = test_io_dir / ('sample_cardano/blocks_per_entity_clustered/31_day_window_from_2020-12-01_to_2020-12'
274277
'-31_sampled_every_31_days.csv')
275278
with open(output_file) as f:
276279
for line in f.readlines():
@@ -298,7 +301,7 @@ def test_tezos_aggregation(setup_and_cleanup, mock_sample_tezos_mapped_data):
298301
'----- UNDEFINED BLOCK PRODUCER -----': '1\n'
299302
}
300303

301-
output_file = test_io_dir / ('sample_tezos/blocks_per_entity/31_day_window_from_2021-08-01_to_2021-08'
304+
output_file = test_io_dir / ('sample_tezos/blocks_per_entity_clustered/31_day_window_from_2021-08-01_to_2021-08'
302305
'-31_sampled_every_31_days.csv')
303306
with open(output_file) as f:
304307
for line in f.readlines():
@@ -319,7 +322,7 @@ def test_tezos_aggregation(setup_and_cleanup, mock_sample_tezos_mapped_data):
319322
'tz0000000000000000000000000000000000': '1\n'
320323
}
321324

322-
output_file = test_io_dir / ('sample_tezos/blocks_per_entity/365_day_window_from_2018-01-01_to_2018-12'
325+
output_file = test_io_dir / ('sample_tezos/blocks_per_entity_clustered/365_day_window_from_2018-01-01_to_2018-12'
323326
'-31_sampled_every_365_days.csv')
324327
with open(output_file) as f:
325328
for line in f.readlines():

tests/test_analyze.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import shutil
22
import pytest
3-
from consensus_decentralization.helper import INTERIM_DIR
3+
from consensus_decentralization.helper import INTERIM_DIR, get_clustering_flag
44
from consensus_decentralization.analyze import analyze
55

66

@@ -32,11 +32,16 @@ def setup_and_cleanup():
3232
'year_from_2010-01-01_to_2010-12-31':
3333
'Entity \\ Date,2010\n'
3434
}
35-
aggregated_data_path = test_bitcoin_dir / 'blocks_per_entity'
35+
aggregated_data_path = test_bitcoin_dir / 'blocks_per_entity_clustered'
3636
aggregated_data_path.mkdir(parents=True, exist_ok=True)
3737
for filename, content in csv_per_file.items():
38-
with open(test_bitcoin_dir / f'blocks_per_entity/{filename}.csv', 'w') as f:
38+
with open(aggregated_data_path / f'{filename}.csv', 'w') as f:
3939
f.write(content)
40+
# Create metrics directory
41+
metrics_dir = test_io_dir / "metrics"
42+
metrics_dir.mkdir(parents=True, exist_ok=True)
43+
# Mock return value of get_clustering_flag
44+
get_clustering_flag.return_value = True
4045
yield test_io_dir
4146
# Clean up
4247
shutil.rmtree(test_io_dir)
@@ -49,12 +54,14 @@ def test_analyze(setup_and_cleanup):
4954
analyze(
5055
projects=projects,
5156
aggregated_data_filename='year_from_2018-01-01_to_2018-12-31.csv',
52-
output_dir=test_output_dir
57+
input_dir=test_output_dir,
58+
output_dir=test_output_dir / 'metrics',
59+
population_windows=0
5360
)
5461

5562
metrics = ['gini', 'nakamoto_coefficient', 'entropy=1']
5663
for metric in metrics:
57-
output_file = test_output_dir / f'{metric}.csv'
64+
output_file = test_output_dir / 'metrics' / f'{metric}.csv'
5865
assert output_file.is_file()
5966

6067
with open(output_file) as f:
@@ -70,12 +77,14 @@ def test_analyze(setup_and_cleanup):
7077
analyze(
7178
projects=projects,
7279
aggregated_data_filename='month_from_2018-02-01_to_2018-03-31.csv',
73-
output_dir=test_output_dir
80+
input_dir=test_output_dir,
81+
output_dir=test_output_dir / 'metrics',
82+
population_windows=0
7483
)
7584

7685
metrics = ['gini', 'nakamoto_coefficient', 'entropy=1']
7786
for metric in metrics:
78-
output_file = test_output_dir / f'{metric}.csv'
87+
output_file = test_output_dir / 'metrics' / f'{metric}.csv'
7988
assert output_file.is_file()
8089

8190
with open(output_file) as f:
@@ -94,12 +103,14 @@ def test_analyze(setup_and_cleanup):
94103
analyze(
95104
projects=projects,
96105
aggregated_data_filename='year_from_2010-01-01_to_2010-12-31.csv',
97-
output_dir=test_output_dir
106+
input_dir=test_output_dir,
107+
output_dir=test_output_dir / 'metrics',
108+
population_windows=0
98109
)
99110

100111
metrics = ['gini', 'nakamoto_coefficient', 'entropy=1']
101112
for metric in metrics:
102-
output_file = test_output_dir / f'{metric}.csv'
113+
output_file = test_output_dir / 'metrics' / f'{metric}.csv'
103114
assert output_file.is_file()
104115

105116
with open(output_file) as f:

tests/test_end_to_end.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ def setup_and_cleanup():
2222
"""
2323
# Set up
2424
test_output_dir = INTERIM_DIR / "test_output"
25+
test_metrics_subdir = test_output_dir / "metrics"
2526
ledger_mapping['sample_bitcoin'] = DefaultMapping
2627
ledger_parser['sample_bitcoin'] = DefaultParser
2728
ledger_mapping['sample_cardano'] = CardanoMapping
2829
ledger_parser['sample_cardano'] = DummyParser
2930

3031
force_map_flag = config['execution_flags']['force_map']
3132
config['execution_flags']['force_map'] = True
33+
config['analyze_flags']['clustering'] = True
3234

3335
mapping_info_dir = pathlib.Path(__file__).resolve().parent.parent / 'mapping_information'
3436
for project in ['bitcoin', 'cardano']:
@@ -53,7 +55,7 @@ def setup_and_cleanup():
5355
)
5456
except FileNotFoundError:
5557
pass
56-
yield test_output_dir
58+
yield test_output_dir, test_metrics_subdir
5759
# Clean up
5860
shutil.rmtree(test_output_dir)
5961
for project in ['sample_bitcoin', 'sample_cardano']:
@@ -74,21 +76,23 @@ def setup_and_cleanup():
7476

7577

7678
def test_end_to_end(setup_and_cleanup):
77-
test_output_dir = setup_and_cleanup
79+
test_output_dir, test_metrics_dir = setup_and_cleanup
7880

7981
main(
8082
['sample_bitcoin', 'sample_cardano'],
8183
(datetime.date(2010, 1, 1), datetime.date(2010, 12, 31)),
8284
estimation_window=None,
8385
frequency=None,
84-
interim_dir=test_output_dir
86+
interim_dir=test_output_dir,
87+
results_dir=test_output_dir,
88+
population_windows=0
8589
)
8690

8791
expected_entropy = [
8892
'timeframe,sample_bitcoin,sample_cardano\n',
8993
'2010-07-02,,\n'
9094
]
91-
with open(test_output_dir / 'entropy=1.csv') as f:
95+
with open(test_metrics_dir / 'entropy=1.csv') as f:
9296
lines = f.readlines()
9397
for idx, line in enumerate(lines):
9498
assert line == expected_entropy[idx]
@@ -97,7 +101,7 @@ def test_end_to_end(setup_and_cleanup):
97101
'timeframe,sample_bitcoin,sample_cardano\n',
98102
'2010-07-02,,\n'
99103
]
100-
with open(test_output_dir / 'gini.csv') as f:
104+
with open(test_metrics_dir / 'gini.csv') as f:
101105
lines = f.readlines()
102106
for idx, line in enumerate(lines):
103107
assert line == expected_gini[idx]
@@ -106,7 +110,7 @@ def test_end_to_end(setup_and_cleanup):
106110
'timeframe,sample_bitcoin,sample_cardano\n',
107111
'2010-07-02,,\n'
108112
]
109-
with open(test_output_dir / 'nakamoto_coefficient.csv') as f:
113+
with open(test_metrics_dir / 'nakamoto_coefficient.csv') as f:
110114
lines = f.readlines()
111115
for idx, line in enumerate(lines):
112116
assert line == expected_nc[idx]
@@ -116,15 +120,17 @@ def test_end_to_end(setup_and_cleanup):
116120
(datetime.date(2018, 2, 1), datetime.date(2018, 3, 31)),
117121
estimation_window=30,
118122
frequency=30,
119-
interim_dir=test_output_dir
123+
interim_dir=test_output_dir,
124+
results_dir=test_output_dir,
125+
population_windows=0
120126
)
121127

122128
expected_entropy = [
123129
'timeframe,sample_bitcoin,sample_cardano\n',
124130
'2018-02-15,1.5,\n',
125131
'2018-03-17,0.0,\n',
126132
]
127-
with open(test_output_dir / 'entropy=1.csv') as f:
133+
with open(test_metrics_dir / 'entropy=1.csv') as f:
128134
lines = f.readlines()
129135
for idx, line in enumerate(lines):
130136
assert line == expected_entropy[idx]
@@ -135,7 +141,7 @@ def test_end_to_end(setup_and_cleanup):
135141
# '2018-02-15,0.375,\n',
136142
# '2018-03-17,0.75,\n'
137143
# ]
138-
# with open(test_output_dir / 'gini.csv') as f:
144+
# with open(test_metrics_dir / 'gini.csv') as f:
139145
# lines = f.readlines()
140146
# for idx, line in enumerate(lines):
141147
# assert line == expected_gini[idx]
@@ -144,7 +150,7 @@ def test_end_to_end(setup_and_cleanup):
144150
'timeframe,sample_bitcoin,sample_cardano\n',
145151
'2018-02-15,1,\n', '2018-03-17,1,\n'
146152
]
147-
with open(test_output_dir / 'nakamoto_coefficient.csv') as f:
153+
with open(test_metrics_dir / 'nakamoto_coefficient.csv') as f:
148154
lines = f.readlines()
149155
for idx, line in enumerate(lines):
150156
assert line == expected_nc[idx]
@@ -154,14 +160,16 @@ def test_end_to_end(setup_and_cleanup):
154160
(datetime.date(2020, 12, 1), datetime.date(2020, 12, 31)),
155161
estimation_window=31,
156162
frequency=31,
157-
interim_dir=test_output_dir
163+
interim_dir=test_output_dir,
164+
results_dir=test_output_dir,
165+
population_windows=0
158166
)
159167

160168
expected_entropy = [
161169
'timeframe,sample_bitcoin,sample_cardano\n',
162170
'2020-12-16,,1.9219280948873623\n'
163171
]
164-
with open(test_output_dir / 'entropy=1.csv') as f:
172+
with open(test_metrics_dir / 'entropy=1.csv') as f:
165173
lines = f.readlines()
166174
for idx, line in enumerate(lines):
167175
assert line == expected_entropy[idx]
@@ -170,7 +178,7 @@ def test_end_to_end(setup_and_cleanup):
170178
'timeframe,sample_bitcoin,sample_cardano\n',
171179
'2020-12-16,,0.15\n'
172180
]
173-
with open(test_output_dir / 'gini.csv') as f:
181+
with open(test_metrics_dir / 'gini.csv') as f:
174182
lines = f.readlines()
175183
for idx, line in enumerate(lines):
176184
assert line == expected_gini[idx]
@@ -179,7 +187,7 @@ def test_end_to_end(setup_and_cleanup):
179187
'timeframe,sample_bitcoin,sample_cardano\n',
180188
'2020-12-16,,2\n'
181189
]
182-
with open(test_output_dir / 'nakamoto_coefficient.csv') as f:
190+
with open(test_metrics_dir / 'nakamoto_coefficient.csv') as f:
183191
lines = f.readlines()
184192
for idx, line in enumerate(lines):
185193
assert line == expected_nc[idx]

0 commit comments

Comments
 (0)