Skip to content

Commit bf5d453

Browse files
committed
Move some fixtures
1 parent 389fd5e commit bf5d453

3 files changed

Lines changed: 44 additions & 76 deletions

File tree

tests/conftest.py

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from muse.commodities import CommodityUsage
2323
from muse.timeslices import setup_module
2424

25-
# Constants
2625
RANDOM_SEED = 123
2726

2827
DEFAULT_TIMESLICES = """
@@ -207,6 +206,8 @@ def coords() -> Mapping:
207206
"""
208207
return {
209208
"technology": ["burger_flipper", "soda_shaker", "deep_frier", "salad_arranger"],
209+
"tech_type": ["solid", "liquid", "solid", "liquid"],
210+
"fuel": ["person", "person", "oil", "person"],
210211
"region": ["ASEAN", "USA"],
211212
"year": [2010, 2030],
212213
"commodity": [
@@ -279,16 +280,14 @@ def technologies(coords: Mapping) -> Dataset:
279280
Dataset with technology characteristics
280281
"""
281282
result = Dataset(coords=coords)
282-
283-
result["comm_type"] = ("commodity", coords["comm_type"])
284-
result["tech_type"] = "technology", ["solid", "liquid", "solid", "liquid"]
285-
283+
result["comm_type"] = "commodity", coords["comm_type"]
284+
result["tech_type"] = "technology", coords["tech_type"]
285+
result["fuel"] = "technology", coords["fuel"]
286286
result = result.set_coords(("comm_type", "tech_type"))
287287

288-
# Generate random variables
288+
# We have a single agent with a share of 1 for all technologies
289289
result["agent_share"] = var_generator(result, ["technology", "region", "year"])
290290
result["agent_share"] /= np.sum(result.agent_share)
291-
result["agent_share_zero"] = result["agent_share"] * 0
292291

293292
# first create a mask so each tech will have consistent inputs/outputs across years
294293
# and regions
@@ -638,54 +637,6 @@ def capacity(technologies: Dataset) -> DataArray:
638637
return create_fake_capacity(20, technologies)
639638

640639

641-
@fixture
642-
def settings(tmpdir) -> dict:
643-
"""Generate settings for testing.
644-
645-
Args:
646-
tmpdir: Temporary directory path
647-
648-
Returns:
649-
Dictionary with test settings
650-
"""
651-
import toml
652-
653-
from muse.readers import DEFAULT_SETTINGS_PATH
654-
from muse.readers.toml import format_paths
655-
656-
def drop_optionals(settings: dict) -> None:
657-
"""Remove optional settings from dictionary."""
658-
for k, v in list(settings.items()):
659-
if v == "OPTIONAL":
660-
settings.pop(k)
661-
elif isinstance(v, Mapping):
662-
drop_optionals(v)
663-
664-
settings = toml.load(DEFAULT_SETTINGS_PATH)
665-
drop_optionals(settings)
666-
out = format_paths(settings, cwd=tmpdir, path=tmpdir, muse_sectors=tmpdir)
667-
668-
# Add required settings
669-
required = {
670-
"time_framework": [2010, 2015, 2020],
671-
"regions": ["MEX"],
672-
"equilibrium": False,
673-
"maximum_iterations": 3,
674-
"tolerance": 0.1,
675-
"interpolation_mode": "linear",
676-
}
677-
out.update(required)
678-
679-
# Add required carbon budget settings
680-
carbon_budget_required = {
681-
"budget": [420000, 413000, 403000],
682-
"commodities": ["CO2f", "CO2r", "CH4", "N2O"],
683-
}
684-
out["carbon_budget_control"].update(carbon_budget_required)
685-
686-
return out
687-
688-
689640
@fixture(autouse=True)
690641
def warnings_as_errors(request):
691642
"""Configure warnings to be treated as errors during testing.
@@ -762,16 +713,3 @@ def saveme(module_name: str, registry_name: str):
762713
map(next, iterators)
763714
yield
764715
map(next, iterators)
765-
766-
767-
@fixture
768-
def rng(request):
769-
"""Create a random number generator for testing.
770-
771-
Args:
772-
request: Pytest request object
773-
774-
Returns:
775-
Random number generator instance
776-
"""
777-
return default_rng(getattr(request.config.option, "randomly_seed", None))

tests/test_filters.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def test_similar_fuels(retro_agent, search_space, technologies):
146146
assert (actual == expected).all()
147147

148148

149-
def test_currently_existing(retro_agent, search_space, technologies, agent_market, rng):
149+
def test_currently_existing(retro_agent, search_space, technologies, agent_market):
150150
# Test with zero capacity
151151
agent_market.capacity[:] = 0
152152
actual = currently_existing_tech(
@@ -165,9 +165,9 @@ def test_currently_existing(retro_agent, search_space, technologies, agent_marke
165165
assert actual.sel(replacement=in_market).all()
166166

167167
# Test with partial capacity
168-
techs = rng.choice(
168+
techs = np.random.choice(
169169
list(set(agent_market.technology.values)),
170-
1 + rng.choice(range(len(set(agent_market.technology.values)))),
170+
1 + np.random.choice(range(len(set(agent_market.technology.values)))),
171171
replace=False,
172172
)
173173
agent_market.capacity[:] = 0
@@ -228,13 +228,13 @@ def test_init_from_tech(demand_share, technologies, agent_market):
228228
assert not space.any()
229229

230230

231-
def test_init_from_asset(technologies, rng):
231+
def test_init_from_asset(technologies):
232232
# Create test data
233-
technology = rng.choice(technologies.technology, 5)
234-
installed = rng.choice((2020, 2025), len(technology))
233+
technology = np.random.choice(technologies.technology, 5)
234+
installed = np.random.choice((2020, 2025), len(technology))
235235
year = np.arange(2020, 2040, 5)
236236
capacity = xr.DataArray(
237-
rng.choice([0, 0, 1, 10], (len(technology), len(year))),
237+
np.random.choice([0, 0, 1, 10], (len(technology), len(year))),
238238
coords={
239239
"technology": ("asset", technology),
240240
"installed": ("asset", installed),
@@ -253,7 +253,7 @@ def test_init_from_asset(technologies, rng):
253253
assert set(space.asset.asset.values) == set(capacity.technology.values)
254254

255255

256-
def test_init_from_asset_no_assets(technologies, rng):
256+
def test_init_from_asset_no_assets(technologies):
257257
agent = namedtuple("DummyAgent", ["assets"])(
258258
xr.Dataset(dict(capacity=xr.DataArray(0)))
259259
)

tests/test_readers.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections.abc import Mapping
12
from itertools import chain, permutations
23
from pathlib import Path
34
from unittest.mock import patch
@@ -16,6 +17,35 @@
1617
]
1718

1819

20+
@fixture
21+
def settings(tmpdir) -> dict:
22+
"""Generate settings for testing.
23+
24+
Args:
25+
tmpdir: Temporary directory path
26+
27+
Returns:
28+
Dictionary with test settings
29+
"""
30+
import toml
31+
32+
from muse.readers import DEFAULT_SETTINGS_PATH
33+
from muse.readers.toml import format_paths
34+
35+
def drop_optionals(settings: dict) -> None:
36+
"""Remove optional settings from dictionary."""
37+
for k, v in list(settings.items()):
38+
if v == "OPTIONAL":
39+
settings.pop(k)
40+
elif isinstance(v, Mapping):
41+
drop_optionals(v)
42+
43+
settings = toml.load(DEFAULT_SETTINGS_PATH)
44+
drop_optionals(settings)
45+
settings = format_paths(settings, cwd=tmpdir, path=tmpdir, muse_sectors=tmpdir)
46+
return settings
47+
48+
1949
@fixture
2050
def user_data_files(settings: dict) -> None:
2151
"""Creates test files related to user data."""

0 commit comments

Comments
 (0)