1111from consensus_decentralization .mappings .cardano_mapping import CardanoMapping
1212from consensus_decentralization .helper import INTERIM_DIR , config
1313import pytest
14+ import csv
1415
1516
1617@pytest .fixture
@@ -28,8 +29,6 @@ def setup_and_cleanup():
2829 ledger_mapping ['sample_cardano' ] = CardanoMapping
2930 ledger_parser ['sample_cardano' ] = DummyParser
3031
31- force_map_flag = config ['force_map' ]
32- config ['force_map' ] = True
3332 config ['clustering' ] = True
3433
3534 mapping_info_dir = pathlib .Path (__file__ ).resolve ().parent .parent / 'mapping_information'
@@ -72,69 +71,21 @@ def setup_and_cleanup():
7271 except FileNotFoundError :
7372 pass
7473
75- config ['force_map' ] = force_map_flag
76-
7774
7875def test_end_to_end (setup_and_cleanup ):
7976 test_output_dir , test_metrics_dir = setup_and_cleanup
8077
81- main (
82- ['sample_bitcoin' , 'sample_cardano' ],
83- (datetime .date (2010 , 1 , 1 ), datetime .date (2010 , 12 , 31 )),
84- estimation_window = None ,
85- frequency = None ,
86- interim_dir = test_output_dir ,
87- results_dir = test_output_dir ,
88- population_windows = 0
89- )
90-
91- expected_entropy = [
92- 'timeframe,sample_bitcoin,sample_cardano\n ' ,
93- '2010-07-02,,\n '
94- ]
95- with open (test_metrics_dir / 'entropy=1.csv' ) as f :
96- lines = f .readlines ()
97- for idx , line in enumerate (lines ):
98- assert line == expected_entropy [idx ]
99-
100- expected_gini = [
101- 'timeframe,sample_bitcoin,sample_cardano\n ' ,
102- '2010-07-02,,\n '
103- ]
104- with open (test_metrics_dir / 'gini.csv' ) as f :
105- lines = f .readlines ()
106- for idx , line in enumerate (lines ):
107- assert line == expected_gini [idx ]
108-
109- expected_nc = [
110- 'timeframe,sample_bitcoin,sample_cardano\n ' ,
111- '2010-07-02,,\n '
112- ]
113- with open (test_metrics_dir / 'nakamoto_coefficient.csv' ) as f :
114- lines = f .readlines ()
115- for idx , line in enumerate (lines ):
116- assert line == expected_nc [idx ]
117-
11878 main (
11979 ['sample_bitcoin' , 'sample_cardano' ],
12080 (datetime .date (2018 , 2 , 1 ), datetime .date (2018 , 3 , 31 )),
12181 estimation_window = 30 ,
12282 frequency = 30 ,
12383 interim_dir = test_output_dir ,
12484 results_dir = test_output_dir ,
125- population_windows = 0
85+ population_windows = 0 ,
86+ force_map = True
12687 )
12788
128- expected_entropy = [
129- 'timeframe,sample_bitcoin,sample_cardano\n ' ,
130- '2018-02-15,1.5,\n ' ,
131- '2018-03-17,0.0,\n ' ,
132- ]
133- with open (test_metrics_dir / 'entropy=1.csv' ) as f :
134- lines = f .readlines ()
135- for idx , line in enumerate (lines ):
136- assert line == expected_entropy [idx ]
137-
13889 # todo fix test (remake calculations from sample files given the new window/frequency)
13990 # expected_gini = [
14091 # 'timeframe,sample_bitcoin,sample_cardano\n',
@@ -146,14 +97,18 @@ def test_end_to_end(setup_and_cleanup):
14697 # for idx, line in enumerate(lines):
14798 # assert line == expected_gini[idx]
14899
149- expected_nc = [
150- 'timeframe,sample_bitcoin,sample_cardano\n ' ,
151- '2018-02-15,1,\n ' , '2018-03-17,1,\n '
152- ]
153- with open (test_metrics_dir / 'nakamoto_coefficient.csv' ) as f :
154- lines = f .readlines ()
155- for idx , line in enumerate (lines ):
156- assert line == expected_nc [idx ]
100+ output_file = test_metrics_dir / 'output_clustered.csv'
101+ assert output_file .is_file ()
102+ with open (output_file ) as f :
103+ rows = list (csv .reader (f ))
104+ header = rows [0 ]
105+ ent_idx = header .index ('entropy=1' )
106+ nc_idx = header .index ('nakamoto_coefficient' )
107+
108+ # build mapping ledger+date -> row
109+ row_map = {(r [0 ], r [1 ]): r for r in rows [1 :]}
110+ assert row_map [('sample_bitcoin' , '2018-02-15' )][ent_idx ] == '1.5'
111+ assert row_map [('sample_bitcoin' , '2018-02-15' )][nc_idx ] == '1'
157112
158113 main (
159114 ['sample_bitcoin' , 'sample_cardano' ],
@@ -162,32 +117,19 @@ def test_end_to_end(setup_and_cleanup):
162117 frequency = 31 ,
163118 interim_dir = test_output_dir ,
164119 results_dir = test_output_dir ,
165- population_windows = 0
120+ population_windows = 0 ,
121+ force_map = True
166122 )
167123
168- expected_entropy = [
169- 'timeframe,sample_bitcoin,sample_cardano\n ' ,
170- '2020-12-16,,1.9219280948873623\n '
171- ]
172- with open (test_metrics_dir / 'entropy=1.csv' ) as f :
173- lines = f .readlines ()
174- for idx , line in enumerate (lines ):
175- assert line == expected_entropy [idx ]
176-
177- expected_gini = [
178- 'timeframe,sample_bitcoin,sample_cardano\n ' ,
179- '2020-12-16,,0.15\n '
180- ]
181- with open (test_metrics_dir / 'gini.csv' ) as f :
182- lines = f .readlines ()
183- for idx , line in enumerate (lines ):
184- assert line == expected_gini [idx ]
185-
186- expected_nc = [
187- 'timeframe,sample_bitcoin,sample_cardano\n ' ,
188- '2020-12-16,,2\n '
189- ]
190- with open (test_metrics_dir / 'nakamoto_coefficient.csv' ) as f :
191- lines = f .readlines ()
192- for idx , line in enumerate (lines ):
193- assert line == expected_nc [idx ]
124+ output_file = test_metrics_dir / 'output_clustered.csv'
125+ assert output_file .is_file ()
126+ with open (output_file ) as f :
127+ rows = list (csv .reader (f ))
128+ header = rows [0 ]
129+ ent_idx = header .index ('entropy=1' )
130+ gini_idx = header .index ('gini' )
131+ nc_idx = header .index ('nakamoto_coefficient' )
132+ row_map = {(r [0 ], r [1 ]): r for r in rows [1 :]}
133+ assert row_map [('sample_cardano' , '2020-12-16' )][ent_idx ] == '1.9219280948873623'
134+ assert row_map [('sample_cardano' , '2020-12-16' )][gini_idx ] == '0.15'
135+ assert row_map [('sample_cardano' , '2020-12-16' )][nc_idx ] == '2'
0 commit comments