import collections
import logging

from pymedphys import _config
from pymedphys._vendor.patchlogging import apply_logging_patch
from pymedphys.cli import run_logging_basic_config

Args = collections.namedtuple("Args", ["logging_verbose", "logging_debug"])


def test_setting_logging():
    try:
        apply_logging_patch()
    except ValueError:
        pass
    else:
        raise AssertionError("Logging patch should raise an error if not within CLI")

    _config.is_cli = True
    apply_logging_patch()

    try:
        apply_logging_patch()
    except ValueError:
        pass
    else:
        raise AssertionError("Logging patch should not be able to be run twice")

    _config.is_cli = False

    args = Args(logging_verbose=False, logging_debug=False)
    run_logging_basic_config(args, {"level": logging.DEBUG})

    assert logging.root.getEffectiveLevel() == logging.DEBUG

    run_logging_basic_config(args, {"level": "error"})

    assert logging.root.getEffectiveLevel() == logging.ERROR

    run_logging_basic_config(args, {})

    assert logging.root.getEffectiveLevel() == logging.WARNING

    args_verbose = Args(logging_verbose=True, logging_debug=False)
    run_logging_basic_config(args_verbose, {})

    assert logging.root.getEffectiveLevel() == logging.INFO

    args_debug = Args(logging_verbose=False, logging_debug=True)
    run_logging_basic_config(args_debug, {})

    assert logging.root.getEffectiveLevel() == logging.DEBUG

    run_logging_basic_config(args, {"level": 5})

    assert logging.root.getEffectiveLevel() == 5

    args_both = Args(logging_verbose=True, logging_debug=True)
    run_logging_basic_config(args_both, {})

    assert logging.root.getEffectiveLevel() == logging.DEBUG
