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', '', '', 'fc', '', dirs.test_dir, 'tas', 'Celsius')
    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', '', '', 'fc', '', dirs.test_dir, 'tas', 'Celsius')
    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, 'fc', '', dirs.test_dir, 'tas', 'Celsius')
    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, 'fc', '', dirs.test_dir, 'tas', 'Celsius')
    res, msg = test.check_valid_ranges()
    assert res == "err"
