Пример #1
0
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")
Пример #2
0
 def test_status_database_config_are_none_with_no_working_directory(self):
     whale = WhaleAlert()
     config = whale.get_configuration()
     status = whale.get_status()
     database = whale.get_database()
     self.assertIs(config, None)
     self.assertIs(status, None)
     self.assertIs(database, None)
Пример #3
0
class WrintingLastCallStats(unittest.TestCase):
    def setUp(self):
        self.whale = WhaleAlert(working_directory=TEST_WORKING_DIR)
        self.writer = Writer(self.whale.get_status(),
                             self.whale.get_database())

    def tearDown(self):
        cleanup_working_directories()

    def reload_status(self):
        target_file = os.path.join(TEST_WORKING_DIR,
                                   settings.data_file_directory,
                                   settings.status_file_name)
        self.writer.get_status_object().set_configuration_file(target_file)

    def get_status_value(self, section, key):
        return self.writer.get_status_object().get_value(section, key)

    def test_successful_call_writen_ok(self):
        status = successful_call_with_trans
        success = self.writer.write_status(status)
        self.reload_status()
        self.assertEqual(
            self.get_status_value(
                settings.status_file_last_good_call_section_name,
                settings.status_file_option_timeStamp),
            successful_call_with_trans['timestamp'])
        self.assertEqual(
            self.get_status_value(
                settings.status_file_last_good_call_section_name,
                settings.status_file_option_transaction_count),
            successful_call_with_trans['transaction_count'])
        self.assertEqual(success, True)

    def test_error_call_writen_ok(self):
        status = failed_call
        success = self.writer.write_status(status)
        self.reload_status()
        self.assertEqual(
            self.get_status_value(settings.status_file_last_failed_secion_name,
                                  settings.status_file_option_timeStamp),
            failed_call['timestamp'])
        self.assertEqual(
            self.get_status_value(settings.status_file_last_failed_secion_name,
                                  settings.status_file_option_error_code),
            failed_call['error_code'])
        self.assertEqual(
            self.get_status_value(settings.status_file_last_failed_secion_name,
                                  settings.status_file_option_error_message),
            failed_call['error_message'])
        self.assertEqual(success, True)
Пример #4
0
class HandlingErrors(unittest.TestCase):
    def setUp(self):
        self.whale = WhaleAlert(working_directory=TEST_WORKING_DIR)
        self.writer = Writer(self.whale.get_status(),
                             self.whale.get_database())

    def tearDown(self):
        cleanup_working_directories()

    def test_status_key_error(self):
        success = self.writer.write_status(bad_status)
        self.assertIs(success, False)

    def test_status_handle_missing_status_file(self):
        cleanup_working_directories()
        success = self.writer.write_status(successful_call_with_trans)
        self.assertIs(success, False)

    def test_handle_bad_type(self):
        writer = Writer(None, None)
        success = writer.write_status(successful_call_with_trans)
        self.assertIs(success, False)
Пример #5
0
class WritingCustomStatus(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_writing_well_formated_status_returns_true(self):
        success = self.whale.write_custom_status(good_status)
        self.assertEqual(success, True)

    def test_writing_badly_formated_status_returns_false(self):
        success = self.whale.write_custom_status(bad_formatted_status)
        self.assertEqual(success, False)

    def test_writing_status_with_no_working_directory_returns_false(self):
        whale = WhaleAlert()
        success = whale.write_custom_status(good_status)
        self.assertEqual(success, False)
Пример #6
0
class GettingLoggerStatus(unittest.TestCase):
    def setUp(self):
        self.whale = WhaleAlert(working_directory=TEST_WORKING_DIR)
        self.writer = Writer(self.whale.get_status(),
                             self.whale.get_database())
        self.reader = Reader(self.whale.get_status(),
                             self.whale.get_database(),
                             self.whale.get_configuration())

    def tearDown(self):
        cleanup_working_directories()

    def write_good_status(self, unix_timestamp):
        status = dict()
        status['timestamp'] = Reader.to_local_time(unix_timestamp)
        status['error_code'] = 200
        status['error_message'] = 'A dud message'
        status['transaction_count'] = 0
        self.writer.write_status(status)

    def write_bad_status(self, unix_timestamp):
        status = dict()
        status['timestamp'] = Reader.to_local_time(unix_timestamp)
        status['error_code'] = 1
        status['error_message'] = 'A dud message'
        status['transaction_count'] = 0
        self.writer.write_status(status)

    def test_getting_status_stdout(self):
        last_good_call_seconds_ago = 300
        last_good_call_minutes = int(round(last_good_call_seconds_ago / 60, 0))
        health = round(
            100 * (settings.health_list_length - 1) /
            settings.health_list_length, 1)
        self.write_bad_status(int(time.time()) - 200)
        self.write_good_status(int(time.time()) - last_good_call_seconds_ago)
        output = self.reader.status_request()
        expected_std_status = "Last successful call {} minutes ago, health {}%".format(
            last_good_call_minutes, health)
        self.assertEqual(expected_std_status, output)

    def test_getting_status_dict(self):
        last_good_call_seconds_ago = 100
        last_good_call_minutes = int(round(last_good_call_seconds_ago / 60, 0))
        health = round(
            100 * (settings.health_list_length - 1) /
            settings.health_list_length, 1)
        self.write_bad_status(int(time.time()) - 200)
        self.write_good_status(int(time.time()) - last_good_call_seconds_ago)
        output = self.reader.status_request(as_dict=True)
        expected_status = {
            'last_call': last_good_call_minutes,
            'health': health,
            'status': 'Ok'
        }
        self.assertEqual(expected_status, output)

    def test_getting_status_dict_logger_not_ok(self):
        last_good_call_seconds_ago = 700
        last_good_call_minutes = int(round(last_good_call_seconds_ago / 60, 0))
        health = round(
            100 * (settings.health_list_length - 1) /
            settings.health_list_length, 1)
        self.write_bad_status(int(time.time()) - 200)
        self.write_good_status(int(time.time()) - last_good_call_seconds_ago)
        output = self.reader.status_request(as_dict=True)
        expected_status = {
            'last_call': last_good_call_minutes,
            'health': health,
            'status': 'Error'
        }
        self.assertEqual(expected_status, output)

    def test_request_status_with_no_working_directory_returns_none(self):
        reader = Reader()
        expected_status = None
        output = reader.status_request()
        self.assertEqual(expected_status, output)

    def test_request_status_with_no_working_directory_returns_none(self):
        output = self.reader.status_request()
        self.assertEqual(None, output)
Пример #7
0
class RequestingStatusByExchange(unittest.TestCase):
    def setUp(self):
        self.whale = WhaleAlert(working_directory=TEST_WORKING_DIR)
        self.writer = Writer(self.whale.get_status(),
                             self.whale.get_database())
        self.reader = Reader(self.whale.get_status(),
                             self.whale.get_database())

    def tearDown(self):
        cleanup_working_directories()

    def add_call_to_database(self, data):
        self.writer.write_transactions(data)

    def test_single_blockchain_single_symbol_request_one_result(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['bitcoin']
        request[settings.request_symbols] = ['BTC']
        request[settings.request_maximum_results] = 1
        request[settings.request_from_time] = 0
        data = test_good_data[0]
        expected_output = format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_single_blockchain_multiple_symbols_request_one_result(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['ethereum']
        request[settings.request_symbols] = ['USDT', "ETH"]
        request[settings.request_maximum_results] = 1
        request[settings.request_from_time] = 0
        data = test_good_data[4]
        expected_output = format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_single_blockchain_multiple_symbols_request_multiple_results(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['ethereum']
        request[settings.request_symbols] = ['USDT', "ETH"]
        request[settings.request_maximum_results] = 2
        request[settings.request_from_time] = 0
        data = test_good_data[1]
        expected_output = format_output(data)
        data = test_good_data[4]
        expected_output = expected_output + format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_multiple_blockchain_multiple_symbols_request_multiple_results(
            self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['ethereum', 'tron']
        request[settings.request_symbols] = ['USDT', "ETH"]
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        data = test_good_data[1]
        expected_output = format_output(data)
        data = test_good_data[2]
        expected_output = expected_output + format_output(data)
        data = test_good_data[4]
        expected_output = expected_output + format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_multiple_blockchain_multiple_symbols_request_one_result(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['ethereum', 'tron']
        request[settings.request_symbols] = ['USDT', "ETH"]
        request[settings.request_maximum_results] = 1
        request[settings.request_from_time] = 0
        data = test_good_data[4]
        expected_output = format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_invalid_blockchain_is_ignored(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['ethereum', 'not_a_blockchain']
        request[settings.request_symbols] = ['USDT', "ETH"]
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        data = test_good_data[1]
        expected_output = format_output(data)
        data = test_good_data[4]
        expected_output = expected_output + format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_invalid_symbols_are_ignored(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['ethereum', 'tron']
        request[settings.request_symbols] = ["ETH"]
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        data = test_good_data[4]
        expected_output = format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_values_only_taken_after_timestamp(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['ethereum', 'tron']
        request[settings.request_symbols] = ['USDT', "ETH"]
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 1488874451
        data = test_good_data[4]
        expected_output = format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_blockchain_wildcard_gets_all_blockchain(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['*']
        request[settings.request_symbols] = ['USDT']
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        data = test_good_data[1]
        expected_output = format_output(data)
        data = test_good_data[2]
        expected_output = expected_output + format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_symbol_widcard_gets_all_symbols(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['ethereum']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        data = test_good_data[1]
        expected_output = format_output(data)
        data = test_good_data[4]
        expected_output = expected_output + format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_both_wildcards_return_all_results(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['*']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        data = test_good_data[0]
        expected_output = format_output(data)
        data = test_good_data[1]
        expected_output = expected_output + format_output(data)
        data = test_good_data[2]
        expected_output = expected_output + format_output(data)
        data = test_good_data[3]
        expected_output = expected_output + format_output(data)
        data = test_good_data[4]
        expected_output = expected_output + format_output(data)
        expected_output = expected_output[:-1]
        output = self.reader.data_request(request)
        self.assertEqual(output, expected_output)

    def test_both_wildcards_return_all_results_as_dict(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['*']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 1
        request[settings.request_from_time] = 0
        data = test_good_data[4]
        expected_output = format_output(data, as_dict=True)
        output = self.reader.data_request(request, as_dict=True)
        self.assertEqual(output, [expected_output])

    def test_single_result_pretty_format(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['*']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 1
        request[settings.request_from_time] = 0
        output = self.reader.data_request(request, pretty=True)
        self.assertEqual([output], [expected_pretty_output])

    def test_single_result_pretty_format_medium_sum(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['neo']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 1
        request[settings.request_from_time] = 0
        output = self.reader.data_request(request, pretty=True)
        self.assertEqual([output], [expected_pretty_large_sum])

    def test_single_pretty_format_huge_sum(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['tron']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 1
        request[settings.request_from_time] = 0
        output = self.reader.data_request(request, pretty=True)
        self.assertEqual([output], [expected_pretty_huge_sum])

    def test_calling_without_database_returns_empty_string(self):
        reader = Reader()
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['*']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        output = reader.data_request(request)
        self.assertEqual(output, '')

    def test_calling_with_empty_database_returns_empty_list(self):
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['*']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        output = self.reader.data_request(request, as_dict=True)
        self.assertEqual(output, [])

    def test_calling_with_empty_database_returns_empty_dataFrame(self):
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['*']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        output = self.reader.data_request(request, as_df=True)
        self.assertIs(output.empty, True)

    def test_bad_data_request_returns_empty_string(self):
        bad_request = {
            'bad_key': 'sfd',
            'another_bad_key': 'sfd',
        }
        output = self.reader.data_request(bad_request)
        self.assertEqual(output, '')

    def test_getting_all_data_as_df(self):
        self.add_call_to_database(test_good_data)
        request = dict(settings.request_format)
        request[settings.request_blockchain] = ['*']
        request[settings.request_symbols] = ['*']
        request[settings.request_maximum_results] = 5
        request[settings.request_from_time] = 0
        expected_output = pd.DataFrame(flattern_transactions(test_good_data))
        expected_output = expected_output.reindex(sorted(
            expected_output.columns),
                                                  axis=1)
        output = self.reader.data_request(request, as_df=True)
        pd.testing.assert_frame_equal(expected_output, output)
Пример #8
0
class WritingData(unittest.TestCase):
    def setUp(self):
        self.whale = WhaleAlert(working_directory=TEST_WORKING_DIR)
        self.writer = Writer(self.whale.get_status(),
                             self.whale.get_database())

    def tearDown(self):
        cleanup_working_directories()

    def test_writing_new_data_adds_tables_to_database(self):
        transactions = test_good_data
        success = self.writer.write_transactions(transactions)
        database = self.writer.get_database()
        tables = database.get_table_names()
        expected_tables = ['bitcoin', 'ethereum', 'tron', 'neo']
        self.assertEqual(success, True)
        self.assertEqual(tables, expected_tables)

    def test_writing_new_data_adds_correct_columns_to_table(self):
        transactions = test_good_data
        success = self.writer.write_transactions(transactions)
        database = self.writer.get_database()
        columns = database.get_column_names('bitcoin')
        expected_columns = sorted(list(settings.database_columns.keys()))
        self.assertEqual(success, True)
        self.assertEqual(expected_columns, columns)

    def test_writing_new_data_last_time_entry_is_correct(self):
        transactions = test_good_data
        success = self.writer.write_transactions(transactions)
        database = self.writer.get_database()
        last_entry = database.get_last_time_entry('bitcoin')
        expected_entry = {
            'blockchain': 'bitcoin',
            'symbol': 'btc',
            'id': '662472177',
            'transaction_type': 'transfer',
            'hash':
            '8d5ae34805f70d0a412964dca4dbd3f48bc103700686035a61b293cb91fe750d',
            'from_address': 'f2103b01cd7957f3a9d9726bbb74c0ccd3f355d3',
            'from_owner': 'binance',
            'from_owner_type': 'exchange',
            'to_address': '3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be',
            'to_owner': 'binance',
            'to_owner_type': 'exchange',
            'timestamp': 1588874414,
            'amount': 3486673,
            'amount_usd': 3508660.2,
            'transaction_count': 1
        }
        self.assertEqual(success, True)
        self.assertEqual(expected_entry, last_entry)

    def test_writing_new_data_extra_key_returns_false(self):
        transaction = test_extra_key
        success = self.writer.write_transactions(transaction)
        self.assertEqual(success, False)

    def test_writing_empty_transactions_returns_false(self):
        transactions = []
        success = self.writer.write_transactions(transactions)
        self.assertEqual(success, False)

    def test_writing_with_a_bad_key_returns_false(self):
        transactions = test_bad_key
        success = self.writer.write_transactions(transactions)
        self.assertEqual(success, False)

    def test_writing_with_a_bad_dictionary_returns_false(self):
        transactions = dict(test_bad_key[0])
        success = self.writer.write_transactions(transactions)
        self.assertEqual(success, False)

    def test_writing_successive_entries(self):
        transactions = test_good_data
        success = self.writer.write_transactions(transactions)
        success = self.writer.write_transactions(transactions)
        success = self.writer.write_transactions(transactions)
        database = self.writer.get_database()
        df = database.table_to_df('bitcoin')
        self.assertEqual(len(df), 3)

        # There are two ethereum transaction in the good data
        df = database.table_to_df('ethereum')
        self.assertEqual(len(df), 6)

        self.assertEqual(success, True)

    def test_writing_wrong_data_type(self):
        transactions = 'A string'
        success = self.writer.write_transactions(transactions)
        self.assertEqual(success, False)
Пример #9
0
class WritingCurrentAndAllTimeStatus(unittest.TestCase):
    def setUp(self):
        self.whale = WhaleAlert(working_directory=TEST_WORKING_DIR)
        self.writer = Writer(self.whale.get_status(),
                             self.whale.get_database())

    def tearDown(self):
        cleanup_working_directories()

    def test_writing_status_with_no_working_directory_handled_ok(self):
        writer = Writer(self.whale.get_status(), self.whale.get_database())
        writer.write_status(successful_call_no_trans)

    def test_writing_a_successful_call_updates_success_no_trans(self):
        previous_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_successful_calls)
        previous_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_successful_calls)
        success = self.writer.write_status(successful_call_no_trans)
        self.reload_status()
        new_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_successful_calls)
        new_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_successful_calls)
        self.assertEqual(success, True)
        self.assertEqual(new_value_all_time, previous_value_all_time + 1)
        self.assertEqual(new_value_current, previous_value_current + 1)

    def test_writing_a_successful_call_updates_success_with_trans(self):
        previous_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_successful_calls)
        previous_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_successful_calls)
        success = self.writer.write_status(successful_call_with_trans)
        self.reload_status()
        new_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_successful_calls)
        new_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_successful_calls)
        self.assertEqual(success, True)
        self.assertEqual(new_value_all_time, previous_value_all_time + 1)
        self.assertEqual(new_value_current, previous_value_current + 1)

    def test_writing_a_successful_call_doesnt_change_failed_calls(self):
        previous_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_failed_calls)
        previous_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_failed_calls)
        success = self.writer.write_status(successful_call_no_trans)
        self.reload_status()
        new_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_failed_calls)
        new_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_failed_calls)
        self.assertEqual(success, True)
        self.assertEqual(new_value_all_time, previous_value_all_time)
        self.assertEqual(new_value_current, previous_value_current)

    def test_writing_a_successful_call_doesnt_change_failed_calls_with_trans(
            self):
        previous_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_failed_calls)
        previous_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_failed_calls)
        success = self.writer.write_status(successful_call_with_trans)
        self.reload_status()
        new_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_failed_calls)
        new_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_failed_calls)
        self.assertEqual(success, True)
        self.assertEqual(new_value_all_time, previous_value_all_time)
        self.assertEqual(new_value_current, previous_value_current)

    def test_writing_a_failed_call_updates_failed(self):
        previous_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_failed_calls)
        previous_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_failed_calls)
        success = self.writer.write_status(failed_call)
        self.reload_status()
        new_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_failed_calls)
        new_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_failed_calls)
        self.assertEqual(success, True)
        self.assertEqual(new_value_all_time, previous_value_all_time + 1)
        self.assertEqual(new_value_current, previous_value_current + 1)

    def test_writing_a_failed_call_doesnt_change_successful_calls(self):
        previous_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_successful_calls)
        previous_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_successful_calls)
        success = self.writer.write_status(failed_call)
        self.reload_status()
        new_value_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_successful_calls)
        new_value_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_successful_calls)
        self.assertEqual(success, True)
        self.assertEqual(new_value_all_time, previous_value_all_time)
        self.assertEqual(new_value_current, previous_value_current)

    def test_writing_failed_and_successfull_calls_calculates_percent(self):
        self.writer.get_status_object().set_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_success_rate, 0)
        self.writer.get_status_object().set_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_success_rate, 0)
        self.writer.write_status(successful_call_no_trans)
        self.writer.write_status(successful_call_no_trans)
        self.writer.write_status(successful_call_no_trans)
        self.writer.write_status(failed_call)
        success = self.writer.write_status(failed_call)
        self.reload_status()
        percent_all_time = self.get_status_value(
            settings.status_file_all_time_section_name,
            settings.status_file_option_success_rate)
        percent_current = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_success_rate)
        self.assertEqual(percent_all_time, 60.00)
        self.assertEqual(percent_current, 60.00)
        self.assertEqual(success, True)

    def test_writing_health(self):

        health_length = settings.health_list_length
        self.assertGreater(health_length, 10)
        self.writer.write_status(failed_call)
        success = self.writer.write_status(failed_call)
        expected_health = round(100 * (health_length - 2) / health_length, 1)
        self.reload_status()
        current_health = self.get_status_value(
            settings.status_file_current_session_section_name,
            settings.status_file_option_health)
        self.assertEqual(current_health, expected_health)
        self.assertEqual(success, True)

    def reload_status(self):
        target_file = os.path.join(TEST_WORKING_DIR,
                                   settings.data_file_directory,
                                   settings.status_file_name)
        self.writer.get_status_object().set_configuration_file(target_file)

    def get_status_value(self, section, key):
        return self.writer.get_status_object().get_value(section, key)
Пример #10
0
class WhaleAlertInit(unittest.TestCase):
    """ Testing whale alert initialisation"""
    def setUp(self):
        self.whale = WhaleAlert(TEST_WORKING_DIR, log_level=logging.DEBUG)
        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_configuration_directories_and_files_created_ok_with_good_directory(
            self):
        whale = WhaleAlert(working_directory=TEST_WORKING_DIR,
                           log_level=logging.DEBUG)
        self.check_directories_are(TEST_WORKING_DIR, True)

    def test_configuration_directories_and_files_created_ok_with_bad_directory_permissions(
            self):
        self.assertRaises(PermissionError,
                          WhaleAlert,
                          TEST_WORKING_DIR_BAD_PERMISSIONS,
                          log_level=logging.DEBUG)

    def test_configuration_directories_and_files_created_ok_with_bad_directory_permissions(
            self):
        self.assertRaises(NotADirectoryError,
                          WhaleAlert,
                          TEST_WORKING_DIR_ALREADY_EXISTS,
                          log_level=logging.DEBUG)

    def check_directories_are(self, directory, status):
        config_exists = os.path.exists(
            os.path.join(directory, settings.input_configuation_filename))
        data_dir_exists = os.path.exists(
            os.path.join(directory, settings.data_file_directory))
        database_exists = os.path.exists(
            os.path.join(directory, settings.data_file_directory,
                         settings.database_file_name))
        status_exists = os.path.exists(
            os.path.join(directory, settings.data_file_directory,
                         settings.status_file_name))
        logs_exists = os.path.exists(
            os.path.join(directory, settings.data_file_directory,
                         settings.log_file_name))
        self.assertEqual(config_exists, status)
        self.assertEqual(data_dir_exists, status)
        self.assertEqual(database_exists, status)
        self.assertEqual(status_exists, status)

    def test_status_database_config_are_none_with_no_working_directory(self):
        whale = WhaleAlert()
        config = whale.get_configuration()
        status = whale.get_status()
        database = whale.get_database()
        self.assertIs(config, None)
        self.assertIs(status, None)
        self.assertIs(database, None)

    def test_config_values_are_default(self):
        self.assertEqual(
            self.config.get_value(settings.API_section_name,
                                  settings.API_option_private_key),
            settings.API_option_privatate_key_default)
        self.assertEqual(
            self.config.get_value(settings.API_section_name,
                                  settings.API_option_interval),
            settings.API_option_interval_default)
        self.assertEqual(
            self.config.get_value(settings.API_section_name,
                                  settings.API_option_minimum_value),
            settings.API_option_minimum_value_default)
        self.assertEqual(
            self.config.get_value(settings.API_section_name,
                                  settings.API_option_historical_limit),
            settings.API_option_historical_limit_default)

    def test_status_values_are_default(self):
        self.assertEqual(
            self.status.get_value(
                settings.status_file_last_good_call_section_name,
                settings.status_file_option_timeStamp), '')
        self.assertEqual(
            self.status.get_value(
                settings.status_file_last_good_call_section_name,
                settings.status_file_option_transaction_count), 0)

        self.assertEqual(
            self.status.get_value(settings.status_file_last_failed_secion_name,
                                  settings.status_file_option_timeStamp), '')
        self.assertEqual(
            self.status.get_value(settings.status_file_last_failed_secion_name,
                                  settings.status_file_option_error_code), 0)
        self.assertEqual(
            self.status.get_value(settings.status_file_last_failed_secion_name,
                                  settings.status_file_option_error_message),
            '')

        self.assertEqual(
            self.status.get_value(
                settings.status_file_current_session_section_name,
                settings.status_file_option_successful_calls), 0)
        self.assertEqual(
            self.status.get_value(
                settings.status_file_current_session_section_name,
                settings.status_file_option_failed_calls), 0)
        self.assertEqual(
            self.status.get_value(
                settings.status_file_current_session_section_name,
                settings.status_file_option_success_rate), 0.0)
        self.assertEqual(
            self.status.get_value(
                settings.status_file_current_session_section_name,
                settings.status_file_option_health), 100.0)

        self.assertEqual(
            self.status.get_value(
                settings.status_file_all_time_section_name,
                settings.status_file_option_successful_calls), 0)
        self.assertEqual(
            self.status.get_value(settings.status_file_all_time_section_name,
                                  settings.status_file_option_failed_calls), 0)
        self.assertEqual(
            self.status.get_value(settings.status_file_all_time_section_name,
                                  settings.status_file_option_success_rate),
            0.0)
Пример #11
0
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)
Пример #12
0
class WhaleAlertAPICall(unittest.TestCase):
    """ Testing setup for whale alert api call"""
    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_configuration_values_are_used_if_working_directory_supplied(self):
        self.whale.transactions.get_transactions = mock.MagicMock().method()
        self.whale.transactions.get_transactions.return_value = (False, {}, {})
        self.config.set_value(settings.API_section_name,
                              settings.API_option_private_key, '1234')
        self.config.set_value(settings.API_section_name,
                              settings.API_option_minimum_value, 10)
        expected = [mock.call(0, None, '1234', None, 10, 100)]
        self.whale.get_transactions(0)
        self.assertEqual(self.whale.transactions.get_transactions.mock_calls,
                         expected)

    def test_configuration_values_are_overwrite_config_values_if_supplied(
            self):
        self.whale.transactions.get_transactions = mock.MagicMock().method()
        self.whale.transactions.get_transactions.return_value = (False, {}, {})
        self.config.set_value(settings.API_section_name,
                              settings.API_option_private_key, '1234')
        self.config.set_value(settings.API_section_name,
                              settings.API_option_minimum_value, 10)
        expected = [mock.call(0, None, '7890', None, 500, 100)]
        self.whale.get_transactions(0, api_key='7890', min_value=500)
        self.assertEqual(self.whale.transactions.get_transactions.mock_calls,
                         expected)

    def test_no_config_value_raises_exception_if_api_key_not_supplied(self):
        whale = WhaleAlert()
        self.assertRaises(ValueError, whale.get_transactions, 0)

    def test_raises_exception_if_start_time_is_not_int(self):
        whale = WhaleAlert()
        self.assertRaises(ValueError,
                          whale.get_transactions,
                          'not_an_int',
                          api_key='1234')

    def test_raises_exception_if_end_time_is_not_int(self):
        whale = WhaleAlert()
        self.assertRaises(ValueError,
                          whale.get_transactions,
                          0,
                          end_time='not_an_int',
                          api_key='1234')

    def test_no_config_get_transactions_works_as_normal(self):
        whale = WhaleAlert()
        whale.transactions.get_transactions = mock.MagicMock().method()
        whale.transactions.get_transactions.return_value = (False, {}, {})
        expected = [mock.call(0, None, 'asdf', None, 500000, 100)]
        whale.get_transactions(0, api_key='asdf')
        self.assertEqual(whale.transactions.get_transactions.mock_calls,
                         expected)