import pytest
from dqc_wrapper.unit_tests.conftest import dirs
from dqc_wrapper.utils.utils_forms import *
import json


def test_get_forms_dates():
    response = get_forms_dates('', 'tas', '', '', dirs.test_dir)
    assert len(response) == 2
    with pytest.raises(Exception):
        get_forms_dates('asd', 'tas', '', '', dirs.test_dir)


def test_get_param_id():
    response = get_param_id('', 'temperature', dirs.test_dir)
    assert response == '130'
    with pytest.raises(Exception):
        get_param_id('asd', 'temperature', dirs.test_dir)
    with pytest.raises(Exception):
        get_param_id('', 'fake_var', dirs.test_dir)


def test_get_shortname_url():
    response = get_shortname_url('https://apps.ecmwf.int/codes/grib/param-db/?id=1')
    assert response == 'strf'


def test_get_shortname_grib():
    with open(dirs.grib_json, 'r') as gf:
        grib_def = json.load(gf)
    res = get_shortname_grib(grib_def, grib_def, '131060')
    assert res == 'tpg1'


def test_get_dataset_information():
    with open(dirs.grib_json, 'r') as gf:
        grib_def = json.load(gf)
    var_codes, prod_types = get_dataset_information('', dirs.test_dir, grib_def, grib_def)
    assert 'temperature' in var_codes
    assert len(prod_types)


def test_get_metadata():
    res =get_metadata(dirs.test_dir, '', 'temperature')
    assert res['varname'].lower() == 'temperature'
    assert 'unit' in res
    assert 'cf' in res
    assert 'grib_codes' in res


def test_get_variables_info_c3s():
    with open(dirs.form_json, 'r') as f:
        form = json.load(f)
    res = get_variables_info_c3s(form)
    assert 'temperature' in res
