Skip to content

Commit

Permalink
Merge pull request #242 from brightway-lca/generic-strategy-tests
Browse files Browse the repository at this point in the history
Add tests for "new" generic strategies
  • Loading branch information
cmutel authored Feb 20, 2024
2 parents 02d3d3a + 0b0d195 commit f101f15
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/strategies/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
tupleize_categories,
split_exchanges,
)
from bw2io.strategies.generic import overwrite_exchange_field_values, assign_default_location
from bw2data.tests import bw2test
from bw2data import Database
from copy import deepcopy
import numpy as np
Expand Down Expand Up @@ -397,3 +399,53 @@ def test_split_exchanges_no_changes():
}]}
]
assert split_exchanges(data, {'name': 'football'}, [{'location': 'A'}, {'location': 'B', 'cat': 'dog'}], [12/20, 8/20]) == expected


@bw2test
def test_overwrite_exchange_field_values():
db = Database("bio")
db.write(
{
("bio", "1"): {"code": 1, "name": "foo", "categories": ("air",)},
("bio", "2"): {"code": 2, "name": "foo", "categories": ("water",)},
("bio", "3"): {"code": 3, "name": "foo", "categories": ("soil",)},
}
)

# default fields -> should add name and overwrite categories
ds = [{"exchanges": [{"input": ("bio", "2"), "categories": "water"}]}]
got = overwrite_exchange_field_values(ds)
expected = [{"exchanges": [{"input": ("bio", "2"), "categories": ("water",), "name": "foo"}]}]
assert got == expected

# prevent adding name by specifying fields
ds = [{"exchanges": [{"input": ("bio", "2"), "categories": "water"}]}]
got = overwrite_exchange_field_values(ds.copy(), fields=["categories"])
expected = [{"exchanges": [{"input": ("bio", "2"), "categories": ("water",)}]}]
assert got == expected


def test_assign_default_location():
ds = [
{"name": "foo", "location": "DE"},
{"name": "bar"},
{"name": "con", "location": "CH"},
]

# default should add "GLO" as missing locations
got = assign_default_location(ds)
expected = [
{"name": "foo", "location": "DE"},
{"name": "bar", "location": "GLO"},
{"name": "con", "location": "CH"},
]
assert got == expected

# test overwrite with custom loc
got = assign_default_location(ds, default_loc="ES", overwrite=True)
expected = [
{"name": "foo", "location": "ES"},
{"name": "bar", "location": "ES"},
{"name": "con", "location": "ES"},
]
assert got == expected

0 comments on commit f101f15

Please sign in to comment.