Skip to content

Commit

Permalink
add test for optional unit definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin W. Portner committed Feb 13, 2023
1 parent dfe0110 commit 06c5648
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions tests/pint_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
ProjectParameter,
Interpreter,
ParameterManager,
Group
)
from bw2data import config, Database, projects
import shutil

bw2parameters = pytest.importorskip("bw2parameters", "1.0.0")

# repeat tests in tests/parameters.py with PintInterpreter and PintParameterSet
if bw2parameters.PintWrapper.pint_installed:
config.use_pint_parameters = True
from .parameters import * # noqa
# # repeat tests in tests/parameters.py with PintInterpreter and PintParameterSet
# if bw2parameters.PintWrapper.pint_installed:
# config.use_pint_parameters = True
# from .parameters import * # noqa


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -173,6 +174,42 @@ def test_mix_parameters_with_and_without_units(use_pint):
DatabaseParameter.recalculate("some_db")


def test_mix_parameters_optional_units(use_pint):
# delete existing project parameters because some of them contain units
ProjectParameter.delete().execute()
# define parameters
ProjectParameter.create(
name="p_proj3",
amount=1,
data={"unit": "kilogram"},
)
ProjectParameter.create(
name="p_proj4",
amount=200,
data={"unit": "gram"},
)
ProjectParameter.create(
name="p_proj5",
formula="p_proj3 + p_proj4",
)
# solve with pint
config.use_pint_parameters = True
ProjectParameter.recalculate()
obj = ProjectParameter.get(name="p_proj5")
assert obj.amount == 1.2
assert obj.dict["unit"] == "kilogram"
# solve without pint
config.use_pint_parameters = False
group = Group.get(name="project")
group.fresh = False
group.save()
ProjectParameter.recalculate()
obj = ProjectParameter.get(name="p_proj5")
assert obj.amount == 201
assert obj.dict["unit"] == "kilogram"
config.use_pint_parameters = True


def test_error_if_recalculated_without_pint(use_pint):
config.use_pint_parameters = False
with pytest.raises(bw2parameters.MissingName):
Expand Down

0 comments on commit 06c5648

Please sign in to comment.