Skip to content

Commit

Permalink
Complete testing for add_exchange_input_if_missing
Browse files Browse the repository at this point in the history
  • Loading branch information
cmutel committed Jul 5, 2024
1 parent 27b66d2 commit 0288169
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
5 changes: 4 additions & 1 deletion multifunctional/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ def generic_allocation(act: MaybeMultifunctionalProcess, func: Callable) -> List

# Change input from artificial one added by `add_exchange_input_if_missing`
# to the actual code needed
if new_functional_exchange.get("mf_artificial_code") and "input" in new_functional_exchange:
if (
new_functional_exchange.get("mf_artificial_code")
and "input" in new_functional_exchange
):
new_functional_exchange["input"] = (act["database"], process_code)

allocated_process["exchanges"] = [new_functional_exchange]
Expand Down
16 changes: 12 additions & 4 deletions multifunctional/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from pprint import pformat
from typing import List

from bw2data import get_node
from bw2data.backends import Exchange
from bw2data.backends.schema import ExchangeDataset
from bw2data.errors import UnknownObject
from loguru import logger


def add_exchange_input_if_missing(data: dict) -> dict:
Expand All @@ -16,12 +18,18 @@ def add_exchange_input_if_missing(data: dict) -> dict:
for exc in ds.get("exchanges", []):
if not exc.get("functional"):
continue
if exc.get('input'):
if "code" not in exc:
exc["code"] = exc["input"][1]
if exc.get("input"):
if "code" in exc and exc["code"] != exc["input"][1]:
logger.critical(
"Mismatch in exchange: given 'code' is '{c}' but 'input' code is '{i}' in exchange:\n{e}",
c=exc["code"],
i=exc["input"][1],
e=pformat(exc),
)
exc["code"] = exc["input"][1]
else:
if "code" in exc:
exc["input"] = (exc.get('database') or ds["database"], exc['code'])
exc["input"] = (exc.get("database") or key[0], exc["code"])
else:
exc["input"] = key
exc["code"] = exc["input"][1]
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ testing = [
"bw2calc>=2.0.dev17",
"pytest",
"pytest-cov",
"python-coveralls"
"pytest-loguru",
"python-coveralls",
]
dev = [
"build",
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Fixtures for bw_simapro_csv"""

from copy import deepcopy

import pytest
from bw2data.tests import bw2test
from fixtures.basic import DATA as BASIC_DATA
from copy import deepcopy


@pytest.fixture
Expand Down
62 changes: 34 additions & 28 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
from multifunctional.utils import add_exchange_input_if_missing


def test_add_exchange_input_if_missing_no_code():
def test_add_exchange_input_if_missing(caplog):
given = {
("db", "code"): {
"exchanges": [
{"functional": True},
{"functional": False},
{},
{"functional": True, "input": ("db", "other")},
{"functional": True, "input": ("db", "foo"), "code": "bar"},
{"functional": True, "code": "foo"},
{"functional": True, "code": "foo", "database": "something"},
{"functional": True},
]
}
},
("db", "more"): {
"database": "me",
"exchanges": [
{"functional": True, "code": "foo"},
],
},
}
expected = {
("db", "code"): {
"exchanges": [
{"functional": True, "input": ("db", "code"), "code": "code", 'mf_artificial_code': True},
{"functional": False},
{},
{"functional": True, "input": ("db", "other"), "code": "other"},
{"functional": True, "input": ("db", "foo"), "code": "foo"},
{"functional": True, "input": ("db", "foo"), "code": "foo"},
{
"functional": True,
"input": ("something", "foo"),
"code": "foo",
"database": "something",
},
{
"functional": True,
"input": ("db", "code"),
"code": "code",
"mf_artificial_code": True,
},
]
}
}
assert add_exchange_input_if_missing(given) == expected


def test_add_exchange_input_if_missing_code_present():
given = {
("db", "code"): {
"database": "1",
},
("db", "more"): {
"database": "me",
"exchanges": [
{"functional": True, "code": "2"},
{},
{"functional": True, "input": ("db", "other")},
]
}
}
expected = {
("db", "code"): {
"database": "1",
"exchanges": [
{"functional": True, "code": "2", "input": ("1", "2")},
{},
{"functional": True, "input": ("db", "other"), "code": "other"},
]
}
{"functional": True, "code": "foo", "input": ("db", "foo")},
],
},
}
assert add_exchange_input_if_missing(given) == expected
print(caplog.text)
assert "given 'code' is 'bar' but 'input' code is 'foo'" in caplog.text

0 comments on commit 0288169

Please sign in to comment.