from shutil import copyfile
from pathlib import Path
from tempfile import TemporaryDirectory

from requests.exceptions import ConnectionError
import pandas as pd

from boo.main import download, read_dataframe, inspect
from boo.path import RawFile


DATA_FOLDER = Path(__file__).parent / "data"


def raw_content():
    return (DATA_FOLDER / "raw0.csv").read_text(encoding="cp1251")


def copy(filename, destination_folder):
    src = DATA_FOLDER / filename
    dst = Path(destination_folder) / filename
    copyfile(src, dst)


def test_download():
    try:
        with TemporaryDirectory() as temp_dir:
            download(year=0, directory=temp_dir)
            r = RawFile(year=0, directory=temp_dir)
            assert r.content() == raw_content()
    # test passes if no internet connection available
    except ConnectionError:
        pass



def test_read_dataframe():
    with TemporaryDirectory() as temp_dir:
        copy("raw0.csv", temp_dir)
        df = read_dataframe(year=0, directory=temp_dir)
        assert isinstance(df, pd.DataFrame)
        assert sum(df.cf) == -7032726


def test_inspect_on_existing_data_is_callable(ARGS_YEAR_0):
    year, folder = ARGS_YEAR_0
    inspect(year, folder)


def test_inspect_on_non_existing_data_is_callable():
    with TemporaryDirectory() as temp_dir:
        inspect(2012, directory=temp_dir)
