From 06c5648294917f8204b7c3e8cab25ac7fb9ef4d0 Mon Sep 17 00:00:00 2001 From: "Benjamin W. Portner" Date: Mon, 13 Feb 2023 15:32:32 +0100 Subject: [PATCH] add test for optional unit definition --- tests/pint_parameters.py | 45 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/tests/pint_parameters.py b/tests/pint_parameters.py index cc8af4e7..d8915099 100644 --- a/tests/pint_parameters.py +++ b/tests/pint_parameters.py @@ -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") @@ -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):