# Path: calliope/test/common/util.py
# def build_test_model(
#     override_dict=None,
#     scenario=None,
#     model_file="model.yaml",
#     timeseries_dataframes=None,
# ):
#     return calliope.Model(
#         os.path.join(os.path.dirname(__file__), "test_model", model_file),
#         override_dict=override_dict,
#         scenario=scenario,
#         timeseries_dataframes=timeseries_dataframes,
#     )
# 
# Path: calliope/backend/pyomo/util.py
# def get_domain(var: xr.DataArray) -> str:
#     def check_sign(var):
#         if re.match("resource|node_coordinates|cost*", var.name):
#             return ""
#         else:
#             return "NonNegative"
# 
#     if var.dtype.kind == "b":
#         return "Boolean"
#     elif is_numeric_dtype(var.dtype):
#         return check_sign(var) + "Reals"
#     else:
#         return "Any"
# 
# @memoize
# def get_param(backend_model, var, dims):
#     """
#     Get an input parameter held in a Pyomo object, or held in the defaults
#     dictionary if that Pyomo object doesn't exist.
# 
#     Parameters
#     ----------
#     backend_model : Pyomo model instance
#     var : str
#     dims : single value or tuple
# 
#     """
#     try:
#         return getattr(backend_model, var)[dims]
#     except AttributeError:  # i.e. parameter doesn't exist at all
#         logger.debug(
#             "get_param: var {} and dims {} leading to default lookup".format(var, dims)
#         )
#         return backend_model.__calliope_defaults[var]
#     except KeyError:  # try removing timestep
#         try:
#             if len(dims) > 2:
#                 return getattr(backend_model, var)[dims[:-1]]
#             else:
#                 return getattr(backend_model, var)[dims[0]]
#         except KeyError:  # Static default value
#             logger.debug(
#                 "get_param: var {} and dims {} leading to default lookup".format(
#                     var, dims
#                 )
#             )
#             return backend_model.__calliope_defaults[var]
# 
# def invalid(val) -> bool:
#     if isinstance(val, po.base.param._ParamData):
#         return val._value == po.Param.NoValue or po.value(val) is None
#     elif val == po.Param.NoValue:
#         return True
#     else:
#         return pd.isnull(val)


import pytest  # noqa: F401
import pyomo.core as po
from calliope.test.common.util import build_test_model as build_model
from calliope.backend.pyomo.util import get_domain, get_param, invalid
            )
            get_param(m._backend_model, "random_param", ("b", "test_supply_elec"))


class TestGetDomain:
    @pytest.mark.parametrize(
        "var, domain",
        (
            ("energy_cap_max", "NonNegativeReals"),
            ("resource", "Reals"),
            ("cost_energy_cap", "Reals"),
            ("force_resource", "NonNegativeReals"),
            ("name", "Any"),
        ),
    )
    def test_dtypes(self, model, var, domain):
        assert get_domain(model._model_data[var]) == domain


class TestInvalid:
    def test_invalid(self):
        pyomo_model = po.ConcreteModel()
        pyomo_model.new_set = po.Set(initialize=["a", "b"])
        pyomo_model.new_param = po.Param(
            pyomo_model.new_set,
            initialize={"a": 1},
            mutable=True,
            within=po.NonNegativeReals,
        )


# Path: calliope/test/common/util.py
# def build_test_model(
#     override_dict=None,
#     scenario=None,
#     model_file="model.yaml",
#     timeseries_dataframes=None,
# ):
#     return calliope.Model(
#         os.path.join(os.path.dirname(__file__), "test_model", model_file),
#         override_dict=override_dict,
#         scenario=scenario,
#         timeseries_dataframes=timeseries_dataframes,
#     )
# 
# Path: calliope/backend/pyomo/util.py
# def get_domain(var: xr.DataArray) -> str:
#     def check_sign(var):
#         if re.match("resource|node_coordinates|cost*", var.name):
#             return ""
#         else:
#             return "NonNegative"
# 
#     if var.dtype.kind == "b":
#         return "Boolean"
#     elif is_numeric_dtype(var.dtype):
#         return check_sign(var) + "Reals"
#     else:
#         return "Any"
# 
# @memoize
# def get_param(backend_model, var, dims):
#     """
#     Get an input parameter held in a Pyomo object, or held in the defaults
#     dictionary if that Pyomo object doesn't exist.
# 
#     Parameters
#     ----------
#     backend_model : Pyomo model instance
#     var : str
#     dims : single value or tuple
# 
#     """
#     try:
#         return getattr(backend_model, var)[dims]
#     except AttributeError:  # i.e. parameter doesn't exist at all
#         logger.debug(
#             "get_param: var {} and dims {} leading to default lookup".format(var, dims)
#         )
#         return backend_model.__calliope_defaults[var]
#     except KeyError:  # try removing timestep
#         try:
#             if len(dims) > 2:
#                 return getattr(backend_model, var)[dims[:-1]]
#             else:
#                 return getattr(backend_model, var)[dims[0]]
#         except KeyError:  # Static default value
#             logger.debug(
#                 "get_param: var {} and dims {} leading to default lookup".format(
#                     var, dims
#                 )
#             )
#             return backend_model.__calliope_defaults[var]
# 
# def invalid(val) -> bool:
#     if isinstance(val, po.base.param._ParamData):
#         return val._value == po.Param.NoValue or po.value(val) is None
#     elif val == po.Param.NoValue:
#         return True
#     else:
#         return pd.isnull(val)


# Path: calliope/test/test_backend_pyomo_util.py
import pytest  # noqa: F401
import pyomo.core as po
from calliope.test.common.util import build_test_model as build_model
from calliope.backend.pyomo.util import get_domain, get_param, invalid

            )
            get_param(m._backend_model, "random_param", ("b", "test_supply_elec"))


class TestGetDomain:
    @pytest.mark.parametrize(
        "var, domain",
        (
            ("energy_cap_max", "NonNegativeReals"),
            ("resource", "Reals"),
            ("cost_energy_cap", "Reals"),
            ("force_resource", "NonNegativeReals"),
            ("name", "Any"),
        ),
    )
    def test_dtypes(self, model, var, domain):
        assert get_domain(model._model_data[var]) == domain


class TestInvalid:
    def test_invalid(self):
        pyomo_model = po.ConcreteModel()
        pyomo_model.new_set = po.Set(initialize=["a", "b"])
        pyomo_model.new_param = po.Param(
            pyomo_model.new_set,
            initialize={"a": 1},
            mutable=True,
            within=po.NonNegativeReals,
        )


        assert invalid(pyomo_model.new_param["a"]) is False