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


def test_check_param_config():
    c_file = Path(dirs.test_dir, "..", "conf", "conf_test00_6.ini")
    config = configparser.ConfigParser()
    config.read(c_file)
    type = check_param_config(config, "general", "type")
    assert type == "grib"
    with pytest.raises(Exception):
        check_param_config(None, "general", "type")

def test_tempdir():
    c_file = Path(dirs.test_dir, "..", "conf", "conf_test00_6.ini")
    config = configparser.ConfigParser()
    config.read(c_file)
    type = check_opt_param_config(config, "general", "type")
    assert type == "grib"
    # Not found
    response = check_opt_param_config(None, "general", "type",
                                      default="test")
    assert response == "test"


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(ConfigParser(), 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)