Exemple #1
0
def test_zip_content(tmp_path):

    script_name = "pythonslave.py"
    script_file = Path(__file__).parent / "slaves" / script_name
    fmu = FmuBuilder.build_FMU(script_file, dest=tmp_path)
    assert fmu.exists()
    assert zipfile.is_zipfile(fmu)

    with zipfile.ZipFile(fmu) as files:
        names = files.namelist()

        assert "modelDescription.xml" in names
        assert "/".join(("resources", script_name)) in names
        module_file = "/".join(("resources", "slavemodule.txt"))
        assert module_file in names

        nfiles = 20
        if FmuBuilder.has_binary():
            assert ("/".join(
                ("binaries", get_platform(), f"PythonSlave.{lib_extension}"))
                    in names)
        else:
            nfiles -= 1

        # Check sources
        src_folder = Path(pythonfmu.__path__[0]) / "pythonfmu-export"
        for f in itertools.chain(
                src_folder.rglob("*.hpp"),
                src_folder.rglob("*.cpp"),
                src_folder.rglob("CMakeLists.txt"),
        ):
            assert "/".join(
                ("sources", f.relative_to(src_folder).as_posix())) in names

        # Check pythonfmu is embedded
        pkg_folder = Path(pythonfmu.__path__[0])
        for f in pkg_folder.rglob("*.py"):
            relative_f = f.relative_to(pkg_folder).as_posix()
            if "test" not in relative_f:
                assert "/".join(
                    ("resources", "pythonfmu", relative_f)) in names

        assert len(
            names
        ) >= nfiles  # Library + python script + XML + module name + sources

        with files.open(module_file) as myfile:
            assert myfile.read() == b"pythonslave"
import math
from pathlib import Path

import pytest

from pythonfmu.builder import FmuBuilder

pytestmark = pytest.mark.skipif(
    not FmuBuilder.has_binary(),
    reason="No binary available for the current platform.")
pyfmi = pytest.importorskip(
    "pyfmi", reason="pyfmi is required for testing the produced FMU")

DEMO = "pythonslave.py"


@pytest.mark.integration
def test_integration_demo(tmp_path):
    script_file = Path(__file__).parent / DEMO

    fmu = FmuBuilder.build_FMU(script_file,
                               dest=tmp_path,
                               needsExecutionTool="false")
    assert fmu.exists()
    model = pyfmi.load_fmu(str(fmu))
    res = model.simulate(final_time=0.5)

    assert res["realOut"][-1] == pytest.approx(res["time"][-1], rel=1e-7)


@pytest.mark.integration
Exemple #3
0
import math
import pytest

from pythonfmu.builder import FmuBuilder

pytestmark = pytest.mark.skipif(
    not FmuBuilder.has_binary(), reason="No binary available for the current platform."
)
pyfmi = pytest.importorskip(
    "pyfmi", reason="pyfmi is required for testing the produced FMU"
)
# fmpy = pytest.importorskip(
#     "fmpy", reason="fmpy is not available for testing the produced FMU"
# )


@pytest.mark.integration
def test_integration_multiple_fmus_pyfmi(tmp_path):
    slave1_code = """import math
from pythonfmu.fmi2slave import Fmi2Slave, Fmi2Causality, Integer, Real, Boolean, String


class Slave1(Fmi2Slave):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.realIn = 22.0
        self.realOut = 0.0
        self.register_variable(Real("realIn", causality=Fmi2Causality.input))
        self.register_variable(Real("realOut", causality=Fmi2Causality.output))