from dqc_wrapper.test02_spatial_completeness import SpatialCompletenessTest
from dqc_wrapper.unit_tests.conftest import dirs
import xarray as xr
from pathlib import Path
import numpy as np


def test_read_masks():
    test = SpatialCompletenessTest([dirs.nc_file], 'tas', 'nc', 'reanalysis-era5-single-levels',
                                   '', 0, dirs.masks_dir, 'yes', 'yes')
    res = test.read_masks()
    assert res.keys()


def test_missing_values():
    test = SpatialCompletenessTest([dirs.nc_masks_file], 'tas', 'nc', 'reanalysis-era5-single-levels',
                                   '', 0, dirs.masks_dir, 'yes', 'yes')
    masks = test.read_masks()
    mv, uv, msg = test.missing_values_xarray(dirs.nc_masks_file, masks)
    assert mv == 0 and uv == 3213185
    # Without mask
    test = SpatialCompletenessTest([dirs.nc_masks_file], 'tas', 'nc', 'reanalysis-era5-single-levels',
                                   '', 0, dirs.masks_dir, 'no', 'no')
    mv, uv, msg = test.missing_values_xarray(dirs.nc_file, {})
    assert mv == 0 and uv == 0


def test_check_spatial_completeness():
    test = SpatialCompletenessTest([dirs.nc_masks_file], 'tas', 'nc', 'reanalysis-era5-single-levels',
                                   '', 0, dirs.masks_dir, 'yes', 'yes')
    # 1: Warning because of unnecesary values
    res, msg = test.check_spatial_completeness()
    assert res == "warn"
    # 2: Without mask
    test = SpatialCompletenessTest([dirs.nc_masks_file], 'tas', 'nc', 'reanalysis-era5-single-levels',
                                   '', 0, dirs.masks_dir, 'no', 'no')
    res, msg = test.check_spatial_completeness()
    assert res == "ok"
    # 3: ok
    test = SpatialCompletenessTest([dirs.nc_masks_file], 'tas', 'nc', 'reanalysis-era5-single-levels',
                                   '', 0, dirs.masks_dir, 'no', 'yes')
    res, msg = test.check_spatial_completeness()
    assert res == "ok"
