Skip to content

Commit

Permalink
♻️ refactor: Avoid running ixmp4 tests on Python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Jan 21, 2025
1 parent 7a55225 commit 2bf2493
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
14 changes: 7 additions & 7 deletions ixmp/backend/ixmp4.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@
from collections.abc import Generator, MutableMapping, Sequence
from typing import TYPE_CHECKING, Any, Optional

from ixmp4 import Platform
from ixmp4.conf.base import PlatformInfo
from ixmp4.data.backend import SqliteTestBackend
from ixmp4.data.backend.base import Backend as ixmp4_backend

from ixmp.backend.base import CachingBackend

if TYPE_CHECKING:
import ixmp4
from ixmp4.data.backend.base import Backend as ixmp4_backend

log = logging.getLogger(__name__)

Expand All @@ -19,9 +15,13 @@ class IXMP4Backend(CachingBackend):
"""Backend using :mod:`ixmp4`."""

_platform: "ixmp4.Platform"
_backend: ixmp4_backend
_backend: "ixmp4_backend"

def __init__(self, _backend: Optional["ixmp4_backend"] = None) -> None:
from ixmp4 import Platform
from ixmp4.conf.base import PlatformInfo
from ixmp4.data.backend import SqliteTestBackend

def __init__(self, _backend: Optional[ixmp4_backend] = None) -> None:
# Create a default backend if None is provided
if not _backend:
log.warning("Falling back to default SqliteBackend 'ixmp4-local'")
Expand Down
16 changes: 11 additions & 5 deletions ixmp/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import logging
import os
import shutil
import sys
from collections.abc import Generator
from contextlib import contextmanager, nullcontext
from copy import deepcopy
Expand All @@ -45,8 +46,6 @@
import pint
import pytest
from click.testing import CliRunner
from ixmp4.conf.base import PlatformInfo
from ixmp4.data.backend import SqliteTestBackend

from ixmp import BACKENDS, Platform, cli
from ixmp import config as ixmp_config
Expand Down Expand Up @@ -89,6 +88,12 @@
"tmp_env",
]

# Parametrize platforms for jdbc and ixmp4
backends = list(BACKENDS.keys())
# ixmp4 is not published for Python 3.9
if sys.version_info < (3, 10):
backends.remove("ixmp4")


# Pytest hooks

Expand Down Expand Up @@ -158,7 +163,7 @@ def test_data_path() -> Path:
return Path(__file__).parents[1].joinpath("tests", "data")


@pytest.fixture(scope="module", params=list(BACKENDS.keys()))
@pytest.fixture(scope="module", params=backends)
def test_mp(
request: pytest.FixtureRequest, tmp_env, test_data_path
) -> Generator[Platform, Any, None]:
Expand Down Expand Up @@ -252,7 +257,7 @@ def protect_rename_dims():
RENAME_DIMS.update(saved)


@pytest.fixture(scope="function", params=list(BACKENDS.keys()))
@pytest.fixture(scope="function", params=backends)
def test_mp_f(request: pytest.FixtureRequest, tmp_env, test_data_path):
"""An empty :class:`Platform` connected to a temporary, in-memory database.
Expand Down Expand Up @@ -410,7 +415,8 @@ def _platform_fixture(
platform_name, backend, "hsqldb", url=f"jdbc:hsqldb:mem:{platform_name}"
)
elif backend == "ixmp4":
# import ixmp4.conf
from ixmp4.conf.base import PlatformInfo
from ixmp4.data.backend import SqliteTestBackend

# Setup ixmp4 backend and run DB migrations
sqlite = SqliteTestBackend(
Expand Down
11 changes: 8 additions & 3 deletions ixmp/tests/core/test_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import re
import sys
from sys import getrefcount
from typing import TYPE_CHECKING
from weakref import getweakrefcount
Expand All @@ -13,17 +14,21 @@

import ixmp
from ixmp.backend import FIELDS
from ixmp.testing import DATA, assert_logs, models
from ixmp.testing import DATA, assert_logs, backends, models

if TYPE_CHECKING:
from ixmp import Platform

min_ixmp4_version = pytest.mark.skipif(
sys.version_info < (3, 10), reason="ixmp4 requires Python 3.10 or higher"
)


class TestPlatform:
def test_init0(self):
with pytest.raises(
ValueError,
match=re.escape("backend class 'foo' not among ['ixmp4', 'jdbc']"),
match=re.escape(f"backend class 'foo' not among {backends}"),
):
ixmp.Platform(backend="foo")

Expand All @@ -35,7 +40,7 @@ def test_init0(self):
"backend, backend_args",
(
("jdbc", dict(driver="hsqldb", url="jdbc:hsqldb:mem:TestPlatform")),
("ixmp4", dict()),
pytest.param("ixmp4", dict(), marks=min_ixmp4_version),
),
)
def test_init1(self, backend, backend_args):
Expand Down

0 comments on commit 2bf2493

Please sign in to comment.