Skip to content

Commit d62614b

Browse files
committed
Move some fixtures
1 parent c0acf46 commit d62614b

3 files changed

Lines changed: 45 additions & 78 deletions

File tree

tests/conftest.py

Lines changed: 7 additions & 70 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,17 +280,14 @@ def technologies(coords: Mapping) -> Dataset:
279280
Dataset with technology characteristics
280281
"""
281282
result = Dataset(coords=coords)
283+
result["comm_type"] = "commodity", coords["comm_type"]
284+
result["tech_type"] = "technology", coords["tech_type"]
285+
result["fuel"] = "technology", coords["fuel"]
286+
result = result.set_coords(("comm_type", "tech_type", "fuel"))
282287

283-
result["comm_type"] = ("commodity", coords["comm_type"])
284-
result["tech_type"] = "technology", ["solid", "liquid", "solid", "liquid"]
285-
result["fuel"] = "technology", ["person", "person", "oil", "person"]
286-
287-
result = result.set_coords(("comm_type", "fuel", "tech_type"))
288-
289-
# Generate random variables
288+
# We have a single agent with a share of 1 for all technologies
290289
result["agent_share"] = var_generator(result, ["technology", "region", "year"])
291290
result["agent_share"] /= np.sum(result.agent_share)
292-
result["agent_share_zero"] = result["agent_share"] * 0
293291

294292
# Create masks for technology characteristics
295293
fuels = result.comm_type == "energy"
@@ -638,54 +636,6 @@ def capacity(technologies: Dataset) -> DataArray:
638636
return create_fake_capacity(20, technologies)
639637

640638

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-
689639
@fixture(autouse=True)
690640
def warnings_as_errors(request):
691641
"""Configure warnings to be treated as errors during testing.
@@ -762,16 +712,3 @@ def saveme(module_name: str, registry_name: str):
762712
map(next, iterators)
763713
yield
764714
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
@@ -128,7 +128,7 @@ def test_similar_fuels(retro_agent, search_space, technologies):
128128
assert_tech_comparison(actual, search_space, "fuel", technologies)
129129

130130

131-
def test_currently_existing(retro_agent, search_space, technologies, agent_market, rng):
131+
def test_currently_existing(retro_agent, search_space, technologies, agent_market):
132132
# Test with zero capacity
133133
agent_market.capacity[:] = 0
134134
actual = currently_existing_tech(
@@ -147,9 +147,9 @@ def test_currently_existing(retro_agent, search_space, technologies, agent_marke
147147
assert actual.sel(replacement=in_market).all()
148148

149149
# Test with partial capacity
150-
techs = rng.choice(
150+
techs = np.random.choice(
151151
list(set(agent_market.technology.values)),
152-
1 + rng.choice(range(len(set(agent_market.technology.values)))),
152+
1 + np.random.choice(range(len(set(agent_market.technology.values)))),
153153
replace=False,
154154
)
155155
agent_market.capacity[:] = 0
@@ -210,13 +210,13 @@ def test_init_from_tech(demand_share, technologies, agent_market):
210210
assert not space.any()
211211

212212

213-
def test_init_from_asset(technologies, rng):
213+
def test_init_from_asset(technologies):
214214
# Create test data
215-
technology = rng.choice(technologies.technology, 5)
216-
installed = rng.choice((2020, 2025), len(technology))
215+
technology = np.random.choice(technologies.technology, 5)
216+
installed = np.random.choice((2020, 2025), len(technology))
217217
year = np.arange(2020, 2040, 5)
218218
capacity = xr.DataArray(
219-
rng.choice([0, 0, 1, 10], (len(technology), len(year))),
219+
np.random.choice([0, 0, 1, 10], (len(technology), len(year))),
220220
coords={
221221
"technology": ("asset", technology),
222222
"installed": ("asset", installed),
@@ -235,7 +235,7 @@ def test_init_from_asset(technologies, rng):
235235
assert set(space.asset.asset.values) == set(capacity.technology.values)
236236

237237

238-
def test_init_from_asset_no_assets(technologies, rng):
238+
def test_init_from_asset_no_assets(technologies):
239239
agent = namedtuple("DummyAgent", ["assets"])(
240240
xr.Dataset(dict(capacity=xr.DataArray(0)))
241241
)

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
@@ -8,6 +9,35 @@
89
from pytest import fixture, mark, raises
910

1011

12+
@fixture
13+
def settings(tmpdir) -> dict:
14+
"""Generate settings for testing.
15+
16+
Args:
17+
tmpdir: Temporary directory path
18+
19+
Returns:
20+
Dictionary with test settings
21+
"""
22+
import toml
23+
24+
from muse.readers import DEFAULT_SETTINGS_PATH
25+
from muse.readers.toml import format_paths
26+
27+
def drop_optionals(settings: dict) -> None:
28+
"""Remove optional settings from dictionary."""
29+
for k, v in list(settings.items()):
30+
if v == "OPTIONAL":
31+
settings.pop(k)
32+
elif isinstance(v, Mapping):
33+
drop_optionals(v)
34+
35+
settings = toml.load(DEFAULT_SETTINGS_PATH)
36+
drop_optionals(settings)
37+
settings = format_paths(settings, cwd=tmpdir, path=tmpdir, muse_sectors=tmpdir)
38+
return settings
39+
40+
1141
@fixture
1242
def user_data_files(settings: dict) -> None:
1343
"""Creates the files related to the user data."""

0 commit comments

Comments
 (0)