|
1 | 1 | import shutil |
2 | 2 | import pytest |
| 3 | +import csv |
3 | 4 | from consensus_decentralization.helper import INTERIM_DIR, get_clustering_flag |
4 | 5 | from consensus_decentralization.analyze import analyze |
5 | 6 |
|
@@ -59,62 +60,49 @@ def test_analyze(setup_and_cleanup): |
59 | 60 | population_windows=0 |
60 | 61 | ) |
61 | 62 |
|
62 | | - metrics = ['gini', 'nakamoto_coefficient', 'entropy=1'] |
63 | | - for metric in metrics: |
64 | | - output_file = test_output_dir / 'metrics' / f'{metric}.csv' |
65 | | - assert output_file.is_file() |
| 63 | + output_file = test_output_dir / 'metrics' / 'output_clustered.csv' |
| 64 | + assert output_file.is_file() |
66 | 65 |
|
67 | | - with open(output_file) as f: |
68 | | - lines = f.readlines() |
69 | | - assert lines[0] == 'timeframe,sample_bitcoin\n' |
70 | | - if metric == 'gini': |
71 | | - assert lines[1] == '2018,0.25\n' |
72 | | - elif metric == 'nakamoto_coefficient': |
73 | | - assert lines[1] == '2018,2\n' |
74 | | - elif metric == 'entropy=1': |
75 | | - assert lines[1] == '2018,1.836591668108979\n' |
| 66 | + with open(output_file) as f: |
| 67 | + reader = list(csv.reader(f)) |
| 68 | + header = reader[0] |
| 69 | + # find metric column indices |
| 70 | + gini_idx = header.index('gini') |
| 71 | + nc_idx = header.index('nakamoto_coefficient') |
| 72 | + ent_idx = header.index('entropy=1') |
76 | 73 |
|
77 | | - analyze( |
78 | | - projects=projects, |
79 | | - aggregated_data_filename='month_from_2018-02-01_to_2018-03-31.csv', |
80 | | - input_dir=test_output_dir, |
81 | | - output_dir=test_output_dir / 'metrics', |
82 | | - population_windows=0 |
83 | | - ) |
84 | | - |
85 | | - metrics = ['gini', 'nakamoto_coefficient', 'entropy=1'] |
86 | | - for metric in metrics: |
87 | | - output_file = test_output_dir / 'metrics' / f'{metric}.csv' |
88 | | - assert output_file.is_file() |
89 | | - |
90 | | - with open(output_file) as f: |
91 | | - lines = f.readlines() |
92 | | - assert lines[0] == 'timeframe,sample_bitcoin\n' |
93 | | - if metric == 'gini': |
94 | | - assert lines[1] == 'Feb-2018,0.16666666666666666\n' |
95 | | - assert lines[2] == 'Mar-2018,0.0\n' |
96 | | - elif metric == 'nakamoto_coefficient': |
97 | | - assert lines[1] == 'Feb-2018,1\n' |
98 | | - assert lines[2] == 'Mar-2018,1\n' |
99 | | - elif metric == 'entropy=1': |
100 | | - assert lines[1] == 'Feb-2018,1.5\n' |
101 | | - assert lines[2] == 'Mar-2018,0.0\n' |
| 74 | + # find the row for sample_bitcoin and 2018 |
| 75 | + data_row = None |
| 76 | + for row in reader[1:]: |
| 77 | + if row[0] == 'sample_bitcoin' and row[1] == '2018': |
| 78 | + data_row = row |
| 79 | + break |
| 80 | + assert data_row is not None |
| 81 | + assert data_row[gini_idx] == '0.25' |
| 82 | + assert data_row[nc_idx] == '2' |
| 83 | + assert data_row[ent_idx] == '1.836591668108979' |
102 | 84 |
|
103 | 85 | analyze( |
104 | 86 | projects=projects, |
105 | | - aggregated_data_filename='year_from_2010-01-01_to_2010-12-31.csv', |
| 87 | + aggregated_data_filename='month_from_2018-02-01_to_2018-03-31.csv', |
106 | 88 | input_dir=test_output_dir, |
107 | 89 | output_dir=test_output_dir / 'metrics', |
108 | 90 | population_windows=0 |
109 | 91 | ) |
110 | 92 |
|
111 | | - metrics = ['gini', 'nakamoto_coefficient', 'entropy=1'] |
112 | | - for metric in metrics: |
113 | | - output_file = test_output_dir / 'metrics' / f'{metric}.csv' |
114 | | - assert output_file.is_file() |
| 93 | + output_file = test_output_dir / 'metrics' / 'output_clustered.csv' |
| 94 | + assert output_file.is_file() |
| 95 | + with open(output_file) as f: |
| 96 | + reader = list(csv.reader(f)) |
| 97 | + header = reader[0] |
| 98 | + gini_idx = header.index('gini') |
| 99 | + nc_idx = header.index('nakamoto_coefficient') |
| 100 | + ent_idx = header.index('entropy=1') |
115 | 101 |
|
116 | | - with open(output_file) as f: |
117 | | - lines = f.readlines() |
118 | | - assert len(lines) == 2 |
119 | | - assert lines[0] == 'timeframe,sample_bitcoin\n' |
120 | | - assert lines[1] == '2010,\n' |
| 102 | + rows_for_project = {row[1]: row for row in reader[1:] if row[0] == 'sample_bitcoin'} |
| 103 | + assert rows_for_project['Feb-2018'][gini_idx] == '0.16666666666666666' |
| 104 | + assert rows_for_project['Mar-2018'][gini_idx] == '0.0' |
| 105 | + assert rows_for_project['Feb-2018'][nc_idx] == '1' |
| 106 | + assert rows_for_project['Mar-2018'][nc_idx] == '1' |
| 107 | + assert rows_for_project['Feb-2018'][ent_idx] == '1.5' |
| 108 | + assert rows_for_project['Mar-2018'][ent_idx] == '0.0' |
0 commit comments