Skip to content

Commit 5321cf9

Browse files
committed
Add test_report.exp_len_all fixture
1 parent 81289fc commit 5321cf9

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

message_ix/tests/test_report.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pytest
1313
from genno.testing import assert_qty_equal
1414
from ixmp import Platform
15+
from ixmp.backend import ItemType
1516
from ixmp.backend.jdbc import JDBCBackend
1617
from ixmp.report import Reporter as ixmp_Reporter
1718
from ixmp.testing import assert_logs
@@ -20,6 +21,7 @@
2021
from pandas.testing import assert_frame_equal, assert_series_equal
2122

2223
from message_ix import Scenario
24+
from message_ix.models import MESSAGE
2325
from message_ix.report import Reporter, configure
2426
from message_ix.testing import SCENARIO, make_dantzig, make_westeros
2527

@@ -152,6 +154,22 @@ def test_reporter_from_scenario(
152154
assert_qty_equal(vom, rep.get(vom_key))
153155

154156

157+
@pytest.fixture(scope="module")
158+
def exp_len_all(test_mp: Platform) -> int:
159+
"""Expected items collected under the "all" reporting key."""
160+
161+
# - Sets not included in "all".
162+
# - 1 item each for PAR and VAR.
163+
# - 2 items (values and marginals) for EQU.
164+
count = {ItemType.SET: 0, ItemType.PAR: 1, ItemType.EQU: 2, ItemType.VAR: 1}
165+
N = sum(count[i.type] for i in MESSAGE.items.values())
166+
167+
# With IXMP4Backend, scenarios do not include variables 'C' and 'I'
168+
N += 0 if is_ixmp4backend(test_mp._backend) else 2
169+
170+
return N
171+
172+
155173
#: Expected number of data points for items in the make_dantzig() scenario.
156174
EXP_LEN_DANTZIG = defaultdict(
157175
lambda: 0,
@@ -175,7 +193,7 @@ def test_reporter_from_scenario(
175193

176194

177195
def test_reporter_from_dantzig(
178-
request: pytest.FixtureRequest, test_mp: Platform
196+
request: pytest.FixtureRequest, test_mp: Platform, exp_len_all: int
179197
) -> None:
180198
scen = make_dantzig(test_mp, solve=True, quiet=True, request=request)
181199

@@ -186,12 +204,11 @@ def test_reporter_from_dantzig(
186204
result = rep.get("all")
187205

188206
# Result contains data for expected number of model data items
189-
# With IXMP4Backend, `scen` and thus `result` does not include variables 'C' and 'I'
190-
assert 268 + (0 if is_ixmp4backend(test_mp._backend) else 2) == len(result)
207+
assert exp_len_all == len(result)
191208

192209
# Items have expected length
193210
for qty in result:
194-
assert EXP_LEN_DANTZIG[qty.name] == len(qty)
211+
assert EXP_LEN_DANTZIG[qty.name] == len(qty), f"{qty.name}: {qty.to_string()}"
195212

196213

197214
#: Expected number of data points for items in the make_westeros() scenario.
@@ -227,7 +244,7 @@ def test_reporter_from_dantzig(
227244

228245

229246
def test_reporter_from_westeros(
230-
request: pytest.FixtureRequest, test_mp: Platform
247+
request: pytest.FixtureRequest, test_mp: Platform, exp_len_all: int
231248
) -> None:
232249
scen = make_westeros(test_mp, emissions=True, solve=True, request=request)
233250

@@ -242,7 +259,7 @@ def test_reporter_from_westeros(
242259

243260
# Result contains data for expected number of model data items
244261
# With IXMP4Backend, `scen` and thus `result` does not include variables 'C' and 'I'
245-
assert 266 + (0 if is_ixmp4backend(test_mp._backend) else 2) == len(result)
262+
assert exp_len_all == len(result)
246263

247264
# Items have expected length
248265
for qty in result:

message_ix/tests/test_tutorials.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def _t(
107107
_t("w0", f"{W}_sankey"),
108108
# NB This is the same count as in test_report.test_reporter_from_scenario.
109109
# Using len(MISSING_IXMP4) for atol allows the test to pass when the value is
110-
# 16010 - 8 = 16002.
111-
_t(None, f"{W}_report", check=[("len-rep-graph", 16010, 1e-7, len(MISSING_IXMP4))]),
110+
# N - 8.
111+
_t(None, f"{W}_report", check=[("len-rep-graph", 17387, 1e-7, len(MISSING_IXMP4))]),
112112
_t("at0", "austria", check=[("solve-objective-value", 206321.90625)]),
113113
_t(
114114
"at0", "austria_single_policy", check=[("solve-objective-value", 205310.34375)]

0 commit comments

Comments
 (0)