From 5cbeb9ad0b210ca6329b651b323377e1ba65a520 Mon Sep 17 00:00:00 2001 From: Chris Mutel Date: Mon, 25 Nov 2024 08:49:09 +0100 Subject: [PATCH] Remove composite "MFP:" names where possible --- multifunctional/allocation.py | 4 ++ tests/conftest.py | 12 ++++- tests/fixtures/name_change.py | 45 +++++++++++++++++++ tests/test_allocation.py | 21 +++++++++ ...test_readonly_process_creation_deletion.py | 10 ++--- 5 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/name_change.py diff --git a/multifunctional/allocation.py b/multifunctional/allocation.py index 836a5e5..b2a4d72 100644 --- a/multifunctional/allocation.py +++ b/multifunctional/allocation.py @@ -127,6 +127,10 @@ def generic_allocation( del allocated_process["id"] if strategy_label: allocated_process["mf_strategy_label"] = act["mf_strategy_label"] = strategy_label + if "name" in allocated_process and allocated_process["name"].startswith("MFP: "): + allocated_process["name"] = (product or new_exc).get( + "name", allocated_process["name"] + ) + " (read-only process)" allocated_process["code"] = process_code allocated_process["mf_parent_key"] = (act["database"], act["code"]) # Used to filter out previous read only processes diff --git a/tests/conftest.py b/tests/conftest.py index 8e563cf..0369226 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,9 +5,10 @@ from fixtures.basic import DATA as BASIC_DATA from fixtures.errors import DATA as ERRORS_DATA from fixtures.internal_linking import DATA as INTERNAL_LINKING_DATA +from fixtures.many_products import DATA as MANY_PRODUCTS_DATA +from fixtures.name_change import DATA as NAME_CHANGE_DATA from fixtures.product_properties import DATA as PP_DATA from fixtures.products import DATA as PRODUCT_DATA -from fixtures.many_products import DATA as MANY_PRODUCTS_DATA from multifunctional import MultifunctionalDatabase, allocation_before_writing @@ -26,6 +27,15 @@ def basic(): return db +@pytest.fixture +@bw2test +def name_change(): + db = MultifunctionalDatabase("name_change") + db.write(deepcopy(NAME_CHANGE_DATA), process=False) + db.metadata["dirty"] = True + return db + + @pytest.fixture @bw2test def products(): diff --git a/tests/fixtures/name_change.py b/tests/fixtures/name_change.py new file mode 100644 index 0000000..1005071 --- /dev/null +++ b/tests/fixtures/name_change.py @@ -0,0 +1,45 @@ +DATA = { + ("name_change", "a"): { + "name": "flow - a", + "code": "a", + "unit": "kg", + "type": "emission", + "categories": ("air",), + }, + ("name_change", "1"): { + "name": "MFP: Longer name because like⧺Long name look here wut", + "code": "1", + "location": "first", + "type": "multifunctional", + "exchanges": [ + { + "functional": True, + "type": "production", + "name": "Longer name because like reasons", + "desired_code": "first - generated", + "amount": 4, + "properties": { + "price": 7, + "mass": 6, + }, + }, + { + "functional": True, + "type": "production", + "name": "Long name look here wut, wut", + "desired_code": "second - generated", + "amount": 4, + "properties": { + "price": 7, + "mass": 6, + }, + }, + { + "type": "biosphere", + "name": "flow - a", + "amount": 10, + "input": ("name_change", "a"), + }, + ], + }, +} diff --git a/tests/test_allocation.py b/tests/test_allocation.py index dc2c021..b069e96 100644 --- a/tests/test_allocation.py +++ b/tests/test_allocation.py @@ -247,3 +247,24 @@ def test_allocation_zero_factor_still_gives_process(): (exc.input, exc["amount"]) for exc in bd.get_node(name="process - 1", unit="megajoule").edges() ] + + +def test_name_replacement_mfp(name_change): + name_change.metadata["default_allocation"] = "price" + bd.get_node(code="1").allocate() + assert {ds["name"] for ds in name_change} == { + "flow - a", + "MFP: Longer name because like⧺Long name look here wut", + "Longer name because like reasons (read-only process)", + "Long name look here wut, wut (read-only process)", + } + + +def test_name_replacement_not_mfp(name_change): + name_change.metadata["default_allocation"] = "price" + + node = bd.get_node(code="1") + node['name'] = "Replace me" + node.save() + node.allocate() + assert sorted(ds["name"] for ds in name_change) == ["Replace me"] * 3 + ["flow - a"] diff --git a/tests/test_readonly_process_creation_deletion.py b/tests/test_readonly_process_creation_deletion.py index e39fbe1..1b396d8 100644 --- a/tests/test_readonly_process_creation_deletion.py +++ b/tests/test_readonly_process_creation_deletion.py @@ -99,30 +99,30 @@ def test_change_multifunctional_reduce_num_still_multifunctional(many_products): assert bd.get_node(code="1")["type"] == "multifunctional" assert len(many_products) == 8 - exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input['code'] == 'p1'][0] + exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input["code"] == "p1"][0] assert exc["functional"] exc["functional"] = False exc.save() bd.get_node(code="1").allocate() assert bd.get_node(code="1")["mf_was_once_allocated"] - assert bd.get_node(code="1")["type"] == 'multifunctional' + assert bd.get_node(code="1")["type"] == "multifunctional" assert len(many_products) == 7 - exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input['code'] == 'p1'][0] + exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input["code"] == "p1"][0] exc["functional"] = True exc.save() bd.get_node(code="1").allocate() assert bd.get_node(code="1")["type"] == "multifunctional" assert len(many_products) == 8 - exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input['code'] == 'p1'][0] + exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input["code"] == "p1"][0] exc["functional"] = False exc.save() bd.get_node(code="1").allocate() assert bd.get_node(code="1")["type"] == "multifunctional" assert len(many_products) == 7 - exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input['code'] == 'p1'][0] + exc = [exc for exc in bd.get_node(code="1").exchanges() if exc.input["code"] == "p1"][0] exc["functional"] = True exc.save() bd.get_node(code="1").allocate()