Exemplo n.º 1
0
def test_create_csv_file(data_object: Data, descending, army_time, start_date,
                         expected_file_to_match):
    """
    Test create CSV file functionality.
    :param data_object: Data object to leverage to test this function.
    :param descending: Boolean that decides whether values in CSV are in descending format or not.
    :param army_time: Boolean that dictates whether dates will be written in army-time format or not.
    :param start_date: Date to have CSV data from.
    """
    remove_test_data()
    data_object.create_table()

    insert_test_data_to_database()
    data_object.data = data_object.get_data_from_database()

    start_date = parser.parse(start_date).date()

    path = data_object.create_csv_file(descending=descending,
                                       army_time=army_time,
                                       start_date=start_date)
    path_to_equal = os.path.join(ROOT_DIR, 'tests', 'data',
                                 'test_create_csv_file_data',
                                 expected_file_to_match)

    assert filecmp.cmp(path, path_to_equal)
Exemplo n.º 2
0
def test_dump_to_table(data_object: Data):
    """
    Testing dumping to table functionality.
    :param data_object: Data object to leverage to test this function.
    """
    remove_test_data()
    data_object.create_table()

    normalized_csv_data = get_normalized_csv_data()
    result = data_object.dump_to_table(normalized_csv_data)
    assert result is True, "Expected all data to dump successfully."

    def get_rows():
        with closing(sqlite3.connect(DATABASE_FILE_PATH)) as connection:
            with closing(connection.cursor()) as cursor:
                db_rows = cursor.execute(
                    f"SELECT * FROM {DATABASE_TABLE} ORDER BY date_utc"
                ).fetchall()
                return [
                    get_normalized_data(row, parse_date=True)
                    for row in db_rows
                ]

    rows = get_rows()
    assert normalized_csv_data == rows, "Values entered are not equal."

    # Dumping several, duplicate values multiple times. Nothing should be dumped due to duplicates.
    data_object.dump_to_table(normalized_csv_data)
    data_object.dump_to_table(normalized_csv_data)

    assert len(rows) == len(
        get_rows()), "Expected count of data to be the same."
Exemplo n.º 3
0
def test_create_table(data_object: Data):
    """
    Test to ensure create table works as intended.
    """
    remove_test_data()
    assert os.path.exists(
        DATABASE_FILE_PATH
    ) is False, f"Expected {DATABASE_FILE_PATH} to not exist for testing."

    data_object.create_table()
    with closing(sqlite3.connect(data_object.database_file)) as connection:
        with closing(connection.cursor()) as cursor:
            table_info = cursor.execute(
                f'PRAGMA TABLE_INFO({data_object.database_table})').fetchall()

    # Each tuple in table_info contains one column's information like this: (0, 'date_utc', 'TEXT', 0, None, 1)
    expected_columns = {
        'date_utc', 'open_price', 'high_price', 'low_price', 'close_price',
        'volume', 'quote_asset_volume', 'number_of_trades',
        'taker_buy_base_asset', 'taker_buy_quote_asset'
    }

    table_columns = {col[1] for col in table_info}
    assert table_columns == expected_columns, f"Expected: {expected_columns}. Got: {table_columns}"
    assert all(col[2] == 'TEXT' for col in
               table_info), "Expected all columns to have the TEXT data type."
Exemplo n.º 4
0
def test_get_data_from_database(data_object: Data):
    """
    Test to ensure get data from database works appropriately.
    :param data_object: Data object to leverage to test this function.
    """
    normalized_csv_data = get_normalized_csv_data()

    remove_test_data()
    data_object.create_table()
    data_object.dump_to_table(normalized_csv_data)
    result = data_object.get_data_from_database()

    # Reverse because data is in ascending order whereas CSV data is not.
    assert normalized_csv_data == result, "Expected data to equal."
Exemplo n.º 5
0
def test_get_latest_database_row(data_object: Data):
    """
    Test get latest database row functionality.
    :param data_object: Data object to leverage to test this function.
    """
    data_object.create_table()
    result = data_object.get_latest_database_row()
    assert result == {}, "Expected an empty dictionary."

    insert_test_data_to_database()
    result = data_object.get_latest_database_row()
    expected = {
        'close': 3.7763,
        'date_utc': convert_str_to_utc_datetime('03/06/2021 01:43 AM'),
        'high': 3.7763,
        'low': 3.7729,
        'number_of_trades': 6192.345082,
        'open': 3.7729,
        'quote_asset_volume': 1614995039999.0,
        'taker_buy_base_asset': 25.0,
        'taker_buy_quote_asset': 1635.85,
        'volume': 1640.75
    }
    assert result == expected, f'Expected: {expected}. Got: {result}'