-
Notifications
You must be signed in to change notification settings - Fork 113
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
Implement ixmp4 shim #516
Closed
Closed
Implement ixmp4 shim #516
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
66366ec
Add ixmp4 to pyproject.toml optional deps
khaeru 18f5832
Add minimal/empty .backend.ixmp4.IXMP4Backend
khaeru 284d5c1
Adjust test_platform.test_init
khaeru dd43b40
Add TestPlatform.test_init1()
khaeru f8aa167
Add stub/missing methods to IXMP4Backend
khaeru 5c7dce2
Implement IXMP4Backend.__init__
khaeru 892000b
Add parametrized TestPlatform.mp fixture
khaeru 3a6d6f3
Add parametrized TestPlatform.test_scenario_list()
khaeru f512ea6
Implement IXMP4Backend.get_scenarios()
khaeru 0c9022f
Add ixmp4 to type checking packages
khaeru 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ repos: | |
- mypy >= 1.9.0 | ||
- genno | ||
- GitPython | ||
- ixmp4 | ||
- nbclient | ||
- pandas-stubs | ||
- pytest | ||
|
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from ixmp.backend.base import CachingBackend | ||
|
||
if TYPE_CHECKING: | ||
import ixmp4 | ||
|
||
|
||
class IXMP4Backend(CachingBackend): | ||
"""Backend using :mod:`ixmp4`.""" | ||
|
||
_platform: "ixmp4.Platform" | ||
|
||
def __init__(self): | ||
import ixmp4 | ||
|
||
# TODO Obtain `name` from the ixmp.Platform creating this Backend | ||
name = "test" | ||
|
||
# Add an ixmp4.Platform using ixmp4's own configuration code | ||
# TODO Move this to a test fixture | ||
# NB ixmp.tests.conftest.test_sqlite_mp exists, but is not importable (missing | ||
# __init__.py) | ||
import ixmp4.conf | ||
|
||
dsn = "sqlite:///:memory:" | ||
try: | ||
ixmp4.conf.settings.toml.add_platform(name, dsn) | ||
except ixmp4.core.exceptions.PlatformNotUnique: | ||
pass | ||
|
||
# Instantiate and store | ||
self._platform = ixmp4.Platform(name) | ||
|
||
def get_scenarios(self, default, model, scenario): | ||
# Current fails with: | ||
# sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: run | ||
# [SQL: SELECT DISTINCT run.model__id, run.scenario__id, run.version, | ||
# run.is_default, run.id | ||
# FROM run | ||
# WHERE run.is_default = 1 ORDER BY run.id ASC] | ||
return self._platform.runs.list() | ||
|
||
# The below methods of base.Backend are not yet implemented | ||
def _ni(self, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
add_model_name = _ni | ||
add_scenario_name = _ni | ||
cat_get_elements = _ni | ||
cat_list = _ni | ||
cat_set_elements = _ni | ||
check_out = _ni | ||
clear_solution = _ni | ||
clone = _ni | ||
commit = _ni | ||
delete = _ni | ||
delete_geo = _ni | ||
delete_item = _ni | ||
delete_meta = _ni | ||
discard_changes = _ni | ||
get = _ni | ||
get_data = _ni | ||
get_doc = _ni | ||
get_geo = _ni | ||
get_meta = _ni | ||
get_model_names = _ni | ||
get_nodes = _ni | ||
get_scenario_names = _ni | ||
get_timeslices = _ni | ||
get_units = _ni | ||
has_solution = _ni | ||
init = _ni | ||
init_item = _ni | ||
is_default = _ni | ||
item_delete_elements = _ni | ||
item_get_elements = _ni | ||
item_index = _ni | ||
item_set_elements = _ni | ||
last_update = _ni | ||
list_items = _ni | ||
remove_meta = _ni | ||
run_id = _ni | ||
set_as_default = _ni | ||
set_data = _ni | ||
set_doc = _ni | ||
set_geo = _ni | ||
set_meta = _ni | ||
set_node = _ni | ||
set_timeslice = _ni | ||
set_unit = _ni |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import logging | ||
import re | ||
from sys import getrefcount | ||
from typing import TYPE_CHECKING | ||
from typing import TYPE_CHECKING, Generator | ||
from weakref import getweakrefcount | ||
|
||
import pandas as pd | ||
|
@@ -20,21 +20,48 @@ | |
|
||
|
||
class TestPlatform: | ||
def test_init(self): | ||
@pytest.fixture(params=list(ixmp.BACKENDS)) | ||
def mp(self, request, test_mp) -> Generator[ixmp.Platform, None, None]: | ||
"""Fixture that yields 2 different platforms: one JDBC-backed, one ixmp4.""" | ||
backend = request.param | ||
|
||
if backend == "jdbc": | ||
yield test_mp | ||
elif backend == "ixmp4": | ||
# TODO Use a fixture similar to test_mp (with same contents) backed by ixmp4 | ||
yield ixmp.Platform(backend="ixmp4") | ||
|
||
def test_init0(self): | ||
with pytest.raises( | ||
ValueError, match=re.escape("backend class 'foo' not among ['jdbc']") | ||
ValueError, | ||
match=re.escape("backend class 'foo' not among ['ixmp4', 'jdbc']"), | ||
): | ||
ixmp.Platform(backend="foo") | ||
|
||
# name="default" is used, referring to "local" | ||
mp = ixmp.Platform() | ||
assert "local" == mp.name | ||
|
||
@pytest.mark.parametrize( | ||
"backend, backend_args", | ||
( | ||
("jdbc", dict(driver="hsqldb", url="jdbc:hsqldb:mem:TestPlatform")), | ||
("ixmp4", dict()), | ||
), | ||
) | ||
def test_init1(self, backend, backend_args): | ||
# Platform can be instantiated | ||
ixmp.Platform(backend=backend, **backend_args) | ||
|
||
def test_getattr(self, test_mp): | ||
"""Test __getattr__.""" | ||
with pytest.raises(AttributeError): | ||
test_mp.not_a_direct_backend_method | ||
|
||
def test_scenario_list(self, mp): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently fails (e.g. here) with:
|
||
scenario = mp.scenario_list() | ||
assert isinstance(scenario, pd.DataFrame) | ||
|
||
|
||
@pytest.fixture | ||
def log_level_mp(test_mp): | ||
|
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
Oops, something went wrong.
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.
FYI @glatterf42, the idea is that to remove lines like
name = _ni
below as each method gets a concrete implementation, in parallel with tests that exercise it.