from dqc_wrapper.utils.utils_files import *
from dqc_wrapper.unit_tests.conftest import dirs
from pathlib import Path
from configparser import RawConfigParser
import pytest


def test_check_param_config():
    c_file = Path(dirs.test_dir, "..", "check_tests", "conf_utils_unit_tests.ini")
    cc = CheckerConfig(str(c_file))
    cc.read_config(execute=False)
    res = cc.check_param_config("general", "input")
    assert res == "/tmp"
    with pytest.raises(Exception):
        cc.check_param_config("general", "type")


def test_check_opt_param_config():
    c_file = Path(dirs.test_dir, "..", "check_tests", "conf_utils_unit_tests.ini")
    cc = CheckerConfig(str(c_file))
    cc.read_config(execute=False)
    res = cc.check_opt_param_config("general", "input")
    assert res == "/tmp"
    # Not found
    response = cc.check_opt_param_config("general", "type", default="test")
    assert response == "test"


def test_check_datatype_config():
    c_file = Path(dirs.test_dir, "..", "check_tests", "conf_utils_unit_tests.ini")
    cc = CheckerConfig(str(c_file))
    cc.read_config(execute=False)
    res = cc.check_datatype_config("general", "input")
    assert res == ['/tmp']


def test_save_config(capsys):
    c_file = Path(dirs.test_dir, "..", "check_tests", "conf_utils_unit_tests.ini")
    cc = CheckerConfig(str(c_file))
    cc.read_config(execute=False)
    cc.save_config("general", {'res': 'ok', 'msg': 'ok'})
    captured = capsys.readouterr()
    assert 'INFO: ok' in captured.out


def test_dir_or_file():
    response = dir_or_file(str(Path(dirs.nc_file).parent), "*.nc")
    assert len(response) == 2  #sample.nc and mask.nc


def test_format_float_single():
    assert '5.326' == format_float_single(5.326)
    assert '1.00E-02' == format_float_single(0.01)


def test_format_float():
    assert '[5.326, 1.00E-02,]' == format_float([5.326, 0.01])


def test_get_zip_fname():
    zipfname = get_zip_fname("test", "test.ini")
    assert zipfname == "test/test.zip"


def test_zip_results():
    zfile = "test.zip"
    zip_results(zfile, [dirs.nc_file])
    assert Path(zfile)
    # Deleting
    Path(zfile).unlink()


def test_save_result():
    save_result(RawConfigParser(), dirs.log_dir, "test")
    assert Path(dirs.log_dir, "test").exists()


def test_initiate_logging():
    logging_reset()
    def_path = initiate_logging(dirs.log_dir, dirs.log_file, '0')
    assert Path(def_path).exists()


def test_get_dates_from_files():
    file_list = ["file_20191120.fake", "file_20200101.fake"]
    response = get_dates_from_files(file_list)
    assert response[0] < response[1]
    with pytest.raises(Exception):
        file_list = ["fff"]
        get_dates_from_files(file_list)


def test_filter_dates():
    res = filter_dates([datetime(2019, 11, 20), datetime(2020, 11, 20)],
                       ["file_20191120.fake", "file_20200101.fake"])
    assert res[0] == datetime(2019, 11, 20)
    assert len(res) == 1


def test_file_pattern():
    res = file_pattern("file_20191120.fake")
    assert res == 'file_'


def test_combine_tests():
    test_res1 = {'res': 'ok', 'msg': 'This is test 1'}
    test_res2 = {'res': 'err', 'msg': 'This is test 2'}
    res = combine_tests(test_res1, test_res2)
    assert res['res'] == 'err'
    assert res['msg'] == 'This is test 1. This is test 2'


def test_overall_result():
    res = overall_result(1, 2, 1, '', 'current')
    assert res['res'] == 'warn'
    assert res['msg'] == 'current'
