1- from tokenomics_decentralization .analyze import analyze_snapshot , analyze , get_entries
2- from unittest .mock import call
1+ from tokenomics_decentralization .analyze import analyze_snapshot , analyze , get_entries , analyze_ledger_snapshot
2+ from unittest .mock import call , Mock
33import pathlib
44
55
@@ -129,20 +129,25 @@ def test_get_entries(mocker):
129129
130130
131131def test_analyze (mocker ):
132+ get_concurrency_mock = mocker .patch ('tokenomics_decentralization.helper.get_concurrency_per_ledger' )
133+ get_concurrency_mock .return_value = {'bitcoin' : 2 , 'ethereum' : 2 }
134+
135+ write_csv_output_mock = mocker .patch ('tokenomics_decentralization.helper.write_csv_output' )
136+
137+ analyze (['bitcoin' ], ['2010-01-01' ])
138+ assert len (write_csv_output_mock .call_args_list ) == 1
139+
140+
141+ def test_analyze_ledger_snapshot (mocker ):
132142 get_input_directories_mock = mocker .patch ('tokenomics_decentralization.helper.get_input_directories' )
133143 get_input_directories_mock .return_value = [pathlib .Path ('/' ).resolve ()]
134144
135145 is_file_mock = mocker .patch ('os.path.isfile' )
136146 is_file_mock .side_effect = {
137147 pathlib .Path ('/bitcoin_2010-01-01_raw_data.csv' ).resolve (): True ,
138- pathlib .Path ('/bitcoin_2011-01-01_raw_data.csv' ).resolve (): False ,
139148 pathlib .Path ('/ethereum_2010-01-01_raw_data.csv' ).resolve (): False ,
140- pathlib .Path ('/ethereum_2011-01-01_raw_data.csv' ).resolve (): True ,
141149 }.get
142150
143- get_db_connector_mock = mocker .patch ('tokenomics_decentralization.db_helper.get_connector' )
144- get_db_connector_mock .return_value = 'connector'
145-
146151 get_entries_mock = mocker .patch ('tokenomics_decentralization.analyze.get_entries' )
147152 entries = [1 , 2 ]
148153 get_entries_mock .return_value = entries
@@ -153,36 +158,26 @@ def test_analyze(mocker):
153158 get_output_row_mock = mocker .patch ('tokenomics_decentralization.helper.get_output_row' )
154159 get_output_row_mock .return_value = 'row'
155160
156- write_csv_output_mock = mocker . patch ( 'tokenomics_decentralization.helper.write_csv_output' )
161+ sema = Mock ( )
157162
158- get_input_dirs_calls = []
159163 get_entries_calls = []
160164 analyze_snapshot_calls = []
161165 get_row_calls = []
162- write_output_calls = []
166+ sema_release_calls = []
163167
164- analyze (['bitcoin' ], ['2010-01-01' ])
165- get_input_dirs_calls .append (call ())
166- assert get_input_directories_mock .call_args_list == get_input_dirs_calls
168+ analyze_ledger_snapshot ('bitcoin' , '2010-01-01' , [], sema )
167169 get_entries_calls .append (call ('bitcoin' , '2010-01-01' , pathlib .Path ('/bitcoin_2010-01-01_raw_data.csv' ).resolve ()))
168170 assert get_entries_mock .call_args_list == get_entries_calls
169171 analyze_snapshot_calls .append (call (entries ))
170172 assert analyze_snapshot_mock .call_args_list == analyze_snapshot_calls
171173 get_row_calls .append (call ('bitcoin' , '2010-01-01' , {'hhi' : 1 }))
172174 assert get_output_row_mock .call_args_list == get_row_calls
173- write_output_calls .append (call ([ 'row' ] ))
174- assert write_csv_output_mock . call_args_list == write_output_calls
175+ sema_release_calls .append (call ())
176+ assert sema . release . call_args_list == sema_release_calls
175177
176- analyze (['bitcoin' , 'ethereum' ], ['2010-01-01' , '2011-01-01' ])
177- get_input_dirs_calls += 4 * [call ()]
178- assert get_input_directories_mock .call_args_list == get_input_dirs_calls
179- get_entries_calls .append (call ('bitcoin' , '2010-01-01' , pathlib .Path ('/bitcoin_2010-01-01_raw_data.csv' ).resolve ()))
180- get_entries_calls .append (call ('ethereum' , '2011-01-01' , pathlib .Path ('/ethereum_2011-01-01_raw_data.csv' ).resolve ()))
178+ analyze_ledger_snapshot ('ethereum' , '2010-01-01' , [], sema )
181179 assert get_entries_mock .call_args_list == get_entries_calls
182- analyze_snapshot_calls += 2 * [call (entries )]
183180 assert analyze_snapshot_mock .call_args_list == analyze_snapshot_calls
184- get_row_calls .append (call ('bitcoin' , '2010-01-01' , {'hhi' : 1 }))
185- get_row_calls .append (call ('ethereum' , '2011-01-01' , {'hhi' : 1 }))
186181 assert get_output_row_mock .call_args_list == get_row_calls
187- write_output_calls .append (call ([ 'row' , 'row' ]))
188- assert write_csv_output_mock . call_args_list == write_output_calls
182+ sema_release_calls .append (call ()) # Test that semaphore release is called even if file does not exist
183+ assert sema . release . call_args_list == sema_release_calls
0 commit comments