# Path: calliope/exceptions.py
# def _formatwarning(message, category, filename, lineno, line=None):
# def warn(message, _class=ModelWarning):
# def print_warnings_and_raise_errors(warnings=None, errors=None):
# class ModelError(Exception):
# class BackendError(Exception):
# class ModelWarning(Warning):
# class BackendWarning(Warning):
# 
# Path: calliope/test/common/util.py
# def check_error_or_warning(error_warning, test_string_or_strings):
#     if hasattr(error_warning, "list"):
#         output = ",".join(
#             str(error_warning.list[i]) for i in range(len(error_warning.list))
#         )
#     else:
#         output = str(error_warning.value)
# 
#     if isinstance(test_string_or_strings, list):
#         result = all(test_string in output for test_string in test_string_or_strings)
#     else:
#         result = test_string_or_strings in output
# 
#     return result


import os
import pytest  # noqa: F401
import calliope
from calliope import exceptions
from calliope.test.common.util import check_error_or_warning




def build_model(override_dict, scenario):
    model_path = os.path.join(
        os.path.dirname(__file__), "common", "test_model", "model.yaml"
    )

    return calliope.Model(model_path, override_dict=override_dict, scenario=scenario)


class TestExistsFalse:
    """
    Test removal of techs, nodes, links, and transmission techs
    with the ``exists: False`` configuration option.

    """

    def test_tech_exists_false(self):
        overrides = {"techs.test_storage.exists": False}
        with pytest.warns(exceptions.ModelWarning) as excinfo:
            model = build_model(overrides, "simple_storage,one_day,investment_costs")
        model.run()

        # Ensure what should be gone is gone
        assert "test_storage" not in model.results.coords["techs"].values

        # Ensure warnings were raised

# Path: calliope/exceptions.py
# def _formatwarning(message, category, filename, lineno, line=None):
# def warn(message, _class=ModelWarning):
# def print_warnings_and_raise_errors(warnings=None, errors=None):
# class ModelError(Exception):
# class BackendError(Exception):
# class ModelWarning(Warning):
# class BackendWarning(Warning):
# 
# Path: calliope/test/common/util.py
# def check_error_or_warning(error_warning, test_string_or_strings):
#     if hasattr(error_warning, "list"):
#         output = ",".join(
#             str(error_warning.list[i]) for i in range(len(error_warning.list))
#         )
#     else:
#         output = str(error_warning.value)
# 
#     if isinstance(test_string_or_strings, list):
#         result = all(test_string in output for test_string in test_string_or_strings)
#     else:
#         result = test_string_or_strings in output
# 
#     return result


# Path: calliope/test/test_model_manipulation.py
import os
import pytest  # noqa: F401
import calliope
from calliope import exceptions
from calliope.test.common.util import check_error_or_warning





def build_model(override_dict, scenario):
    model_path = os.path.join(
        os.path.dirname(__file__), "common", "test_model", "model.yaml"
    )

    return calliope.Model(model_path, override_dict=override_dict, scenario=scenario)


class TestExistsFalse:
    """
    Test removal of techs, nodes, links, and transmission techs
    with the ``exists: False`` configuration option.

    """

    def test_tech_exists_false(self):
        overrides = {"techs.test_storage.exists": False}
        with pytest.warns(exceptions.ModelWarning) as excinfo:
            model = build_model(overrides, "simple_storage,one_day,investment_costs")
        model.run()

        # Ensure what should be gone is gone
        assert "test_storage" not in model.results.coords["techs"].values

        # Ensure warnings were raised

        assert check_error_or_warning(