class WritingExcelFile(unittest.TestCase): def setUp(self): self.whale = WhaleAlert(TEST_WORKING_DIR) self.config = self.whale.get_configuration() self.status = self.whale.get_status() self.database = self.whale.get_database() def tearDown(self): cleanup_working_directories() def test_return_false_if_no_working_directory(self): whale = WhaleAlert() success = whale.to_excel() self.assertEqual(success, False) def test_return_false_no_databases_exist(self): success = self.whale.to_excel() self.assertEqual(success, False) def test_writes_an_excel_file(self): self.whale.transactions.get_transactions = mock.MagicMock().method() self.whale.transactions.get_transactions.return_value = ( True, good_transactions, good_status) success = self.whale.fetch_and_store_data(0) success = self.whale.to_excel() exists = os.path.exists('whaleAlert.xlsx') self.assertEqual(exists, True) self.assertEqual(success, True) os.remove("whaleAlert.xlsx")
def test_no_api_key_and_no_working_directory_returns_false_and_doesnt_call( self): whale = WhaleAlert() success = whale.fetch_and_store_data(0, 0) whale.transactions.get_transactions = mock.MagicMock().method() self.assertEqual(len(whale.transactions.get_transactions.mock_calls), 0) self.assertIs(success, False)
class FetchAndStoreData(unittest.TestCase): def setUp(self): self.whale = WhaleAlert(TEST_WORKING_DIR) self.config = self.whale.get_configuration() self.status = self.whale.get_status() self.database = self.whale.get_database() def tearDown(self): cleanup_working_directories() def test_no_api_key_and_no_working_directory_returns_false_and_doesnt_call( self): whale = WhaleAlert() success = whale.fetch_and_store_data(0, 0) whale.transactions.get_transactions = mock.MagicMock().method() self.assertEqual(len(whale.transactions.get_transactions.mock_calls), 0) self.assertIs(success, False) def test_no_api_key_with_working_directory_calls_using_saved_api_key(self): self.config.set_value(settings.API_section_name, settings.API_option_private_key, 'zxcv') self.whale.transactions.get_transactions = mock.MagicMock().method() self.whale.transactions.get_transactions.return_value = ( True, good_transactions, good_status) success = self.whale.fetch_and_store_data(0) expected = [mock.call(0, None, 'zxcv', None, 500000, 100)] self.assertEqual(self.whale.transactions.get_transactions.mock_calls, expected) self.assertIs(success, True) def test_supplying_api_key_with_working_directory_calls_using_supplied_api_key( self): self.config.set_value(settings.API_section_name, settings.API_option_private_key, 'zxcv') self.whale.transactions.get_transactions = mock.MagicMock().method() self.whale.transactions.get_transactions.return_value = ( True, good_transactions, good_status) success = self.whale.fetch_and_store_data(0, api_key='poiu') expected = [mock.call(0, None, 'poiu', None, 500000, 100)] self.assertEqual(self.whale.transactions.get_transactions.mock_calls, expected) self.assertIs(success, True) def test_a_successful_call_causes_the_database_to_be_written(self): self.whale.transactions.get_transactions = mock.MagicMock().method() self.whale.transactions.get_transactions.return_value = ( True, good_transactions, good_status) success = self.whale.fetch_and_store_data(0) database = self.whale.get_database() status = self.whale.get_status() successful_calls = status.get_value( settings.status_file_current_session_section_name, settings.status_file_option_successful_calls) failed_calls = status.get_value( settings.status_file_current_session_section_name, settings.status_file_option_failed_calls) df = database.table_to_df('bitcoin') self.assertEqual(len(df), 1) self.assertEqual(successful_calls, 1) self.assertEqual(failed_calls, 0) self.assertIs(success, True) def test_a_successful_call_with_failed_keys_causes_no_write_ands_a_failed_status( self): self.whale.transactions.get_transactions = mock.MagicMock().method() self.whale.transactions.get_transactions.return_value = ( True, bad_transactions, good_status) success = self.whale.fetch_and_store_data(0) database = self.whale.get_database() status = self.whale.get_status() successful_calls = status.get_value( settings.status_file_current_session_section_name, settings.status_file_option_successful_calls) failed_calls = status.get_value( settings.status_file_current_session_section_name, settings.status_file_option_failed_calls) error_code = status.get_value( settings.status_file_last_failed_secion_name, settings.status_file_option_error_code) df = database.table_to_df('bitcoin') self.assertEqual(df.empty, True) self.assertEqual(successful_calls, 0) self.assertEqual(failed_calls, 1) self.assertIs(success, False) self.assertIs(error_code, 20) def test_a_failed_call_causes_no_write_to_database_and_failed_status(self): self.whale.transactions.get_transactions = mock.MagicMock().method() self.whale.transactions.get_transactions.return_value = ( False, good_transactions, bad_status) success = self.whale.fetch_and_store_data(0) database = self.whale.get_database() status = self.whale.get_status() successful_calls = status.get_value( settings.status_file_current_session_section_name, settings.status_file_option_successful_calls) failed_calls = status.get_value( settings.status_file_current_session_section_name, settings.status_file_option_failed_calls) df = database.table_to_df('bitcoin') self.assertEqual( df, None ) # Nothing should have been written, so the table doesn't exist and none is returned self.assertEqual(successful_calls, 0) self.assertEqual(failed_calls, 1) self.assertIs(success, False)