#
# Copyright (c) Daniel Sheffield 2023
#
# All rights reserved
#
# THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
from datetime import datetime
from pytest import mark, raises
from pandas import DataFrame

from app.data.dataframe_util import(
    get_caption,
    get_divider,
    get_time_range,
    stats,
)

@mark.parametrize('data', [
    DataFrame([
        {'ts': datetime(1970, 1, 1)}, {'ts': datetime(2000, 2, 28)}
    ])
])
def test_get_timerange(data):
    assert ("01/01/1970", "28/02/2000") == get_time_range(data, 'ts')

@mark.parametrize('width, marker, expected', [
    #(0, '─', (AssertionError, "")),
    #(1, '─', (AssertionError, "")),
    #(2, '─', (AssertionError, "")),
    (0, '─', ' ─ '),
    (1, '─', ' ─ '),
    (2, '─', ' ─ '),
    (3, '─', ' ─ '),
    (4, '─', ' ── '),
    (5, '─', ' ─── '),
    (6, '─', '  ──  '),
    (7, '─', '  ───  '),
    (8, '─', '   ──   '),
])
def test_get_divider(width, marker, expected):
    if not isinstance(expected, tuple):
        assert expected == get_divider(width, marker=marker)
        return

    with raises(expected[0]):
        get_divider(width, marker=marker)

@mark.parametrize('data', [
    DataFrame([
        {'ts': 0, 'price': 3.0, 'amount': 10, '$/unit': 0.3 },
        {'ts': datetime(2000, 2, 28), 'price': 11, 'amount': 2, '$/unit': 5.5 },
    ])
])
def test_stats(data):
    assert [0.3, 2.9, 14/12, 5.5] == stats(data, 'price', 'amount', '$/unit')

@mark.parametrize('data', [
    DataFrame([
        {'ts': datetime(1970, 1, 1)}, {'ts': datetime(2000, 2, 28)}
    ])
])
@mark.parametrize('width, div', [
    (26, ' ── '),
    (27, ' ─── '),
])
def test_get_caption(data, width, div):
    assert f"01/01/1970 {div} 28/02/2000" == get_caption(get_time_range(data, 'ts'), width)
