from dqc_wrapper.test06_valid_ranges import ValidRangesTest
from dqc_wrapper.unit_tests.conftest import dirs
import xarray as xr
import numpy as np
import pandas as pd


def test_get_hist_hadley_xarray():
    df_hist = pd.DataFrame(index=['N_polar'], columns=np.arange(15))
    test = ValidRangesTest(dirs.log_dir, 'testname.ini', [dirs.nc_file],
                           'tas', '', '', 0, '', dirs.test_dir, 'tas')
    ds = xr.open_dataset(dirs.nc_file)
    test.get_hist_hadley_xarray(df_hist, np.arange(10, 26), ds, 0, 60, 90, 'N_polar',
                         'lat')
    assert df_hist.shape == (1, 15)


def test_get_bins_init():
    test = ValidRangesTest(dirs.log_dir, 'testname.ini', [dirs.nc_file],
                           'tas', '', '', 0, '', dirs.test_dir, 'tas')
    hist = test.get_bins_init([dirs.nc_file])
    assert len(hist) == 4


def test_check_valid_ranges():
    test = ValidRangesTest(dirs.log_dir, 'testname.ini', [dirs.nc_file],
                           'tas', 0, 500, 0, '', dirs.test_dir, 'tas')
    res, msg = test.check_valid_ranges()
    assert res == "ok"
    # Min and max outside range
    test = ValidRangesTest(dirs.log_dir, 'testname.ini', [dirs.nc_file],
                           'tas', 100, 200, 0, '', dirs.test_dir, 'tas')
    res, msg = test.check_valid_ranges()
    assert res == "err"
