<commit_before>import os
from glob import glob

from astropy import units as u
import pytest

from tardis.io.config_validator import DefaultParser, Config, ConfigValueError


existing_configs = glob(os.path.join('docs', 'examples', '*.yml'))
existing_configs += glob(os.path.join('tardis', 'io', 'tests', 'data', '*.yml'))
config_definition = os.path.join('tardis', 'data', 'tardis_config_definition.yml')

test_config_definition = os.path.join('tardis', 'io', 'tests', 'data', 'conf_def.yml')
test_config = os.path.join('tardis', 'io', 'tests', 'data', 'conf_tes.yml')
existing_configs.remove(test_config_definition)
existing_configs.remove(test_config)


@pytest.mark.parametrize("config_filename", existing_configs)
def test_configread(config_filename):
    config = Config.from_yaml(config_filename, config_definition)


def test_configread_test_config():
    config = Config.from_yaml(test_config, test_config_definition)


def default_parser_helper(test_dic, default, wdefault, value, wvalue, container, mandatory, return_default=None,
                          return_value=None, value_as_string=True):
    test_ob = DefaultParser(test_dic)

    if return_value is None:
        return_value = value

    if return_default is None:
        return_default = default

    if not default == None:
        dhelper = True
    else:
        dhelper = False

    assert test_ob.has_default == dhelper
    assert test_ob.get_default() == return_default
    assert test_ob.is_leaf
    assert test_ob.is_container() == container
    assert test_ob.is_mandatory == mandatory

    #set good default
    test_ob.set_default(default)

    assert test_ob.get_default() == return_default
    #set bad default
    if wdefault is not None:
        with pytest.raises(ValueError):
            test_ob.set_default(wdefault)

    assert test_ob.get_value() == return_default

    #set good value
    test_ob.set_config_value(value)

    assert test_ob.get_value() == return_value

    #set bad value
    if wvalue is not None:
        with pytest.raises(ConfigValueError):
            test_ob.set_config_value(wvalue)
            test_ob.get_value()

    return 0


def test_default_parser_float():
    example_dic = {'default': 99.99,
                   'help': 'float value for testing',
                   'mandatory': True,
                   'property_type': 'float'}
    default = 99.99
    wdefault = "xx"
    value = 11.12
    wvalue = "yy"
    container = False
    mandatory = True
    ex = default_parser_helper(example_dic, default, wdefault, value, wvalue, container, mandatory)


def test_default_parser_integer():
    example_dic = {'default': 99,
                   'help': 'integer value for testing',
                   'mandatory': True,
                   'property_type': 'int'}

    default = 99
    wdefault = 9.15
    value = 11
    wvalue = 9.22
    container = False
    mandatory = True

    ex = default_parser_helper(example_dic, default, wdefault, value, wvalue, container, mandatory)


def test_default_parser_quantity():
    example_dic = {'default': '99.99 cm',
                   'help': 'quantity for testing',
                   'mandatory': True,
                   'property_type': 'quantity'}

    default = "99.99 cm"
    return_default = 99.99 * u.cm
    wdefault = "kl"
    value = "11.12 m"
    return_value = 11.12 * u.m
    wvalue = "yy"
    container = False
    mandatory = True

    ex = default_parser_helper(example_dic, default, wdefault, value, wvalue, container, mandatory,
                               return_default=return_default, return_value=return_value)




def test_default_parser_quantity_range():
    example_dic = {'default': ['1 cm', '5 cm'],<commit_msg>added test for log_lsun<commit_after>import os
from glob import glob

from astropy import units as u
import pytest

from tardis.io.config_validator import DefaultParser, Config, ConfigValueError


existing_configs = glob(os.path.join('docs', 'examples', '*.yml'))
existing_configs += glob(os.path.join('tardis', 'io', 'tests', 'data', '*.yml'))
config_definition = os.path.join('tardis', 'data', 'tardis_config_definition.yml')

test_config_definition = os.path.join('tardis', 'io', 'tests', 'data', 'conf_def.yml')
test_config = os.path.join('tardis', 'io', 'tests', 'data', 'conf_tes.yml')
existing_configs.remove(test_config_definition)
existing_configs.remove(test_config)


@pytest.mark.parametrize("config_filename", existing_configs)
def test_configread(config_filename):
    config = Config.from_yaml(config_filename, config_definition)


def test_configread_test_config():
    config = Config.from_yaml(test_config, test_config_definition)


def default_parser_helper(test_dic, default, wdefault, value, wvalue, container, mandatory, return_default=None,
                          return_value=None, value_as_string=True):
    test_ob = DefaultParser(test_dic)

    if return_value is None:
        return_value = value

    if return_default is None:
        return_default = default

    if not default == None:
        dhelper = True
    else:
        dhelper = False

    assert test_ob.has_default == dhelper
    assert test_ob.get_default() == return_default
    assert test_ob.is_leaf
    assert test_ob.is_container() == container
    assert test_ob.is_mandatory == mandatory

    #set good default
    test_ob.set_default(default)

    assert test_ob.get_default() == return_default
    #set bad default
    if wdefault is not None:
        with pytest.raises(ValueError):
            test_ob.set_default(wdefault)

    assert test_ob.get_value() == return_default

    #set good value
    test_ob.set_config_value(value)

    assert test_ob.get_value() == return_value

    #set bad value
    if wvalue is not None:
        with pytest.raises(ConfigValueError):
            test_ob.set_config_value(wvalue)
            test_ob.get_value()

    return 0


def test_default_parser_float():
    example_dic = {'default': 99.99,
                   'help': 'float value for testing',
                   'mandatory': True,
                   'property_type': 'float'}
    default = 99.99
    wdefault = "xx"
    value = 11.12
    wvalue = "yy"
    container = False
    mandatory = True
    ex = default_parser_helper(example_dic, default, wdefault, value, wvalue, container, mandatory)


def test_default_parser_integer():
    example_dic = {'default': 99,
                   'help': 'integer value for testing',
                   'mandatory': True,
                   'property_type': 'int'}

    default = 99
    wdefault = 9.15
    value = 11
    wvalue = 9.22
    container = False
    mandatory = True

    ex = default_parser_helper(example_dic, default, wdefault, value, wvalue, container, mandatory)


def test_default_parser_quantity_log_lsun():
    example_dic = {'default': '10 solLum',
                   'help': 'quantity for testing',
                   'mandatory': True,
                   'property_type': 'quantity'}

    default = "10 erg/s"
    return_default = 10 * u.erg / u.s
    wdefault = "kl"
    value = "9 log_lsun"
    return_value = 3.8459999999999732e+42 * u.erg / u.s
    wvalue = "yy"
    container = False
    mandatory = True

    ex = default_parser_helper(example_dic, default, wdefault, value, wvalue, container, mandatory,
                               return_default=return_default, return_value=return_value)



def test_default_parser_quantity():
    example_dic = {'default': '99.99 cm',
                   'help': 'quantity for testing',
                   'mandatory': True,
                   'property_type': 'quantity'}

    default = "99.99 cm"
    return_default = 99.99 * u.cm
    wdefault = "kl"
    value = "11.12 m"
    return_value = 11.12 * u.m
    wvalue = "yy"
    container = False
    mandatory = True

    ex = default_parser_helper(example_dic, default, wdefault, value, wvalue, container, mandatory,
                               return_default=return_default, return_value=return_value)


def test_default_parser_quantity_range():
    example_dic = {'default': ['1 cm', '5 cm'],
{'fraction_non_alphanumeric': 0.10525663277956857, 'fraction_numerical': 0.014877262583684603, 'mean_word_length': 2.948604992657856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '963648', 'n_tokens_mistral': 2613, 'n_tokens_neox': 2451, 'n_words': 588}