-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test-platform
fixture for ixmp4 tests
#832
Merged
danielhuppmann
merged 10 commits into
IAMconsortium:main
from
danielhuppmann:ixmp4/test-platform
Mar 8, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c501a0a
Add a test for `read_ixmp4()` with non-default scenarios
danielhuppmann a4df1a1
Drop `version` column when writing to the database
danielhuppmann 437f220
Refactor to `test_platform` conftest featue
danielhuppmann d0ee817
Minor fixes
danielhuppmann a520a8d
Add to release notes
danielhuppmann b838beb
Fix failing test
danielhuppmann b42d529
Make ruff
danielhuppmann 3759f87
Add a test per suggestion by @phackstock
danielhuppmann 10e45ba
Minor fixes
danielhuppmann 6ed7b56
Another fix
danielhuppmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,71 @@ | ||
import pytest | ||
from ixmp4.core import Platform | ||
from ixmp4.core.region import RegionModel | ||
from ixmp4.core.unit import UnitModel | ||
from ixmp4.data.backend import SqliteTestBackend | ||
|
||
import pyam | ||
from pyam import read_ixmp4 | ||
|
||
|
||
def test_to_ixmp4_missing_region_raises(test_df_year): | ||
def test_to_ixmp4_missing_region_raises(test_platform, test_df_year): | ||
"""Writing to platform raises if region not defined""" | ||
platform = Platform(_backend=SqliteTestBackend()) | ||
with pytest.raises(RegionModel.NotFound, match="World. Use `Platform.regions."): | ||
test_df_year.to_ixmp4(platform=platform) | ||
test_df_year.rename(region={"World": "foo"}, inplace=True) | ||
with pytest.raises(RegionModel.NotFound, match="foo. Use `Platform.regions."): | ||
test_df_year.to_ixmp4(platform=test_platform) | ||
|
||
|
||
def test_to_ixmp4_missing_unit_raises(test_df_year): | ||
def test_to_ixmp4_missing_unit_raises(test_platform, test_df_year): | ||
"""Writing to platform raises if unit not defined""" | ||
platform = Platform(_backend=SqliteTestBackend()) | ||
platform.regions.create(name="World", hierarchy="common") | ||
with pytest.raises(UnitModel.NotFound, match="EJ/yr. Use `Platform.units."): | ||
test_df_year.to_ixmp4(platform=platform) | ||
test_df_year.rename(unit={"EJ/yr": "foo"}, inplace=True) | ||
with pytest.raises(UnitModel.NotFound, match="foo. Use `Platform.units."): | ||
test_df_year.to_ixmp4(platform=test_platform) | ||
|
||
|
||
def test_ixmp4_time_not_implemented(test_df): | ||
def test_ixmp4_time_not_implemented(test_platform, test_df): | ||
"""Writing an IamDataFrame with datetime-data is not implemented""" | ||
platform = Platform(_backend=SqliteTestBackend()) | ||
platform.regions.create(name="World", hierarchy="common") | ||
platform.units.create(name="EJ/yr") | ||
|
||
if test_df.time_domain != "year": | ||
with pytest.raises(NotImplementedError): | ||
test_df.to_ixmp4(platform=platform) | ||
test_df.to_ixmp4(platform=test_platform) | ||
|
||
|
||
def test_ixmp4_integration(test_df_year): | ||
def test_ixmp4_integration(test_platform, test_df_year): | ||
"""Write an IamDataFrame to the platform""" | ||
platform = Platform(_backend=SqliteTestBackend()) | ||
platform.regions.create(name="World", hierarchy="common") | ||
platform.units.create(name="EJ/yr") | ||
|
||
# test writing to platform | ||
test_df_year.to_ixmp4(platform=platform) | ||
test_df_year.to_ixmp4(platform=test_platform) | ||
|
||
# read only default scenarios (runs) - version number added as meta indicator | ||
obs = read_ixmp4(platform=platform) | ||
obs = read_ixmp4(platform=test_platform) | ||
exp = test_df_year.copy() | ||
exp.set_meta(1, "version") # add version number added from ixmp4 | ||
pyam.assert_iamframe_equal(exp, obs) | ||
|
||
# make one scenario a non-default scenario, make sure that it is not included | ||
test_platform.runs.get("model_a", "scen_b").unset_as_default() | ||
obs = read_ixmp4(platform=test_platform) | ||
pyam.assert_iamframe_equal(exp.filter(scenario="scen_a"), obs) | ||
|
||
# read all scenarios (runs) - version number used as additional index dimension | ||
obs = read_ixmp4(platform=platform, default_only=False) | ||
obs = read_ixmp4(platform=test_platform, default_only=False) | ||
data = test_df_year.data | ||
data["version"] = 1 | ||
meta = test_df_year.meta.reset_index() | ||
meta["version"] = 1 | ||
exp = pyam.IamDataFrame(data, meta=meta, index=["model", "scenario", "version"]) | ||
pyam.assert_iamframe_equal(exp, obs) | ||
|
||
|
||
@pytest.mark.parametrize("drop_meta", (True, False)) | ||
def test_ixmp4_reserved_columns(test_platform, test_df_year, drop_meta): | ||
"""Make sure that a 'version' column in `meta` is not written to the platform""" | ||
|
||
if drop_meta: | ||
test_df_year = pyam.IamDataFrame(test_df_year.data) | ||
|
||
# test writing to platform with a version-number as meta indicator | ||
test_df_year.set_meta(1, "version") # add version number added from ixmp4 | ||
test_df_year.to_ixmp4(platform=test_platform) | ||
|
||
if drop_meta: | ||
assert len(test_platform.runs.get("model_a", "scen_a").meta) == 0 | ||
else: | ||
assert "version" not in test_platform.runs.get("model_a", "scen_a").meta |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this result in an empty data frame except for the model and scenario index?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, added a test-variant for this case