Skip to content

Migrate to sund/models/ to adhere to python formatting

Currently compiled models are saved in sund/Models/but it should be sund/models/ if we want to adhere to ruff. This change was postponed due to an issue in Windows/Mac where the compiled (.pyd, .so) got locked when chaining installation. This seems to be due to a confusing between the models/ dir and the _Models dir.

The tests that initially was used for checking the upgrade functionality is attached below

import subprocess
import sys
from pathlib import Path

import pytest


@pytest.mark.upgrade_sund2
def test_upgrade_from_193_to_200():
    """Test full upgrade path from 1.9.3 to 2.0.0 including legacy directory cleanup"""

    # Ensure pip is available
    subprocess.check_call([sys.executable, "-m", "ensurepip"])  # noqa: S603
    subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])  # noqa: S603

    # Install old version (1.9.3)
    subprocess.check_call(  # noqa: S603
        [
            sys.executable,
            "-m",
            "pip",
            "install",
            "dist/sund-1.9.3.tar.gz",
        ],
    )

    # Import and verify old version
    import sund  # noqa: PLC0415

    assert sund.version() == "1.9.3"

    # Manually create the legacy Models directory to mimic 1.9.3 installation
    # We skip actual model installation due to C++ API compatibility issues
    old_models_dir = Path(sund.__file__).parent / "Models"
    old_models_dir.mkdir(exist_ok=True)

    # Create some dummy files to mimic installed models from 1.9.3
    (old_models_dir / "legacy_model_1.hash").write_text("v1:dummy_hash_193_model1")
    (old_models_dir / "legacy_model_2.hash").write_text("v1:dummy_hash_193_model2")

    # Verify that old Models directory exists
    assert old_models_dir.exists(), "Old Models/ directory should exist after 1.9.3 setup"

    # Upgrade to current version (2.0.0)
    subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "."])  # noqa: S603

    # The old Models directory should still exist at this point
    assert old_models_dir.exists(), "Old Models/ directory should still exist before migration"


@pytest.mark.upgrade_sund2
def test_migration_function():
    """Test the migrate_models_dir function directly"""

    # Ensure we have sund 2.0.0
    import sund  # noqa: PLC0415

    if sund.version() != "2.0.0":
        subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "."])  # noqa: S603
        import sund  # noqa: PLC0415

    assert sund.version() == "2.0.0"

    # install models to trigger cleanup/migration
    sund.install_model("tests/test_models/minimal_dae_test_model.txt")

    # Verify that the hash files exist in the new models directory
    new_models_dir = Path(sund.__file__).parent / "models"
    assert new_models_dir.exists(), "New models/ directory should exist after migration"
    assert (new_models_dir / "minimal_dae_test_model.hash").exists(), (
        "Hash file for minimal_dae_test_model should exist"
    )

    # Verify that the old Models directory does not exist
    # Check for any case variants that might exist
    parent_dir = Path(sund.__file__).parent
    models_variants = [d for d in parent_dir.iterdir() if d.is_dir() and d.name.lower() == "models"]
    # Should only have the new "models" directory, not "Models"
    assert len(models_variants) == 1, (
        f"Expected only 'models' directory, found: {[d.name for d in models_variants]}"
    )
    assert models_variants[0].name == "models", (
        f"Expected 'models', got '{models_variants[0].name}'"
    )

    # cleanup created models
    sund.uninstall_model(all=True)
Edited by Henrik Podéus Derelöv