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
import configparser


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_opt_param_config("general", "input")
    assert res == '/tmp'


def test_save_config():
    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'})


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_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_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)