Skip to content

Commit a73e331

Browse files
committed
(E001) update tests (v4.x.x)
[test_*] reorder imports [tests_ModelicaDoE*] fix pylint hint * use .items() [tests_*] use OMSessionABC.get_version() [test_ModelicaSystemCmd] use get_model_name() instead of access to private variable _model_name [test_ModelicaSystemOMC] read file using utf-8 encoding / linter fix [test_ModelicaSystemRunner] update test case * ModelicaSystemRunner & OMCPath * ModelicaSystemRunner & OMPathRunnerLocal * ModelicaSystemRunner & OMPathRunnerBash * ModelicaSystemRunner & OMPathRunnerBash using docker * ModelicaSystemRunner & OMPathRunnerBash using WSL (not tested!) [test_OMCPath] update test case * OMCPath & OMCSessionZMQ * OMCPath & OMCSessionLocal * OMCPath & OMCSessionDocker * OMCPath & OMCSessionWSL (not tested!) * OMPathLocal & OMCSessionRunner * OMPathBash & OMCSessionRunner * OMPathBash & OMCSessionRunner in docker * OMPathBash & OMCSessionRunner in WSL (not tested!) add workflow to run unittests in ./tests [test_OMParser] use only the public interface => om_parser_basic() [test_OMTypedParser] rename file / use om_parser_typed() update tests - do NOT run test_FMIRegression.py reason: * it is only a test for OMC / not OMPython specific * furthermore, it is run automatically via cron job (= FMITest) [test_ModelExecutionCmd] rename from test_ModelicaSystemCmd
1 parent 1fb3681 commit a73e331

13 files changed

+365
-101
lines changed

OMPython/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
OMPathABC,
3131
OMCPath,
3232

33+
OMSessionABC,
3334
OMSessionRunner,
3435

3536
OMCSessionABC,
@@ -77,6 +78,7 @@
7778
'OMPathABC',
7879
'OMCPath',
7980

81+
'OMSessionABC',
8082
'OMSessionRunner',
8183

8284
'OMCSessionABC',

tests/test_FMIExport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import shutil
21
import os
32
import pathlib
3+
import shutil
44

55
import OMPython
66

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def mscmd_firstorder(model_firstorder):
2929
cmd_local=mod.get_session().model_execution_local,
3030
cmd_windows=mod.get_session().model_execution_windows,
3131
cmd_prefix=mod.get_session().model_execution_prefix(cwd=mod.getWorkDirectory()),
32-
model_name=mod._model_name,
32+
model_name=mod.get_model_name(),
3333
)
3434

3535
return mscmd

tests/test_ModelicaDoEOMC.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,6 @@ def _run_ModelicaDoEOMC(doe_mod):
159159
f"y[{row['p']}]": float(row['b']),
160160
}
161161

162-
for var in var_dict:
163-
assert var in sol['data']
164-
assert np.isclose(sol['data'][var][-1], var_dict[var])
162+
for key, val in var_dict.items():
163+
assert key in sol['data']
164+
assert np.isclose(sol['data'][key][-1], val)

tests/test_ModelicaDoERunner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,6 @@ def _check_runner_result(mod, doe_mod):
153153
'b': float(row['b']),
154154
}
155155

156-
for var in var_dict:
157-
assert var in sol['data']
158-
assert np.isclose(sol['data'][var][-1], var_dict[var])
156+
for key, val in var_dict.items():
157+
assert key in sol['data']
158+
assert np.isclose(sol['data'][key][-1], val)

tests/test_ModelicaSystemOMC.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def test_simulate_inputs(tmp_path):
495495
}
496496
mod.setInputs(**inputs)
497497
csv_file = mod._createCSVData()
498-
assert pathlib.Path(csv_file).read_text() == """time,u1,u2,end
498+
assert pathlib.Path(csv_file).read_text(encoding='utf-8') == """time,u1,u2,end
499499
0.0,0.0,0.0,0
500500
0.25,0.25,0.5,0
501501
0.5,0.5,1.0,0

tests/test_ModelicaSystemRunner.py

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
import sys
2+
13
import numpy as np
24
import pytest
35

46
import OMPython
57

68

9+
skip_on_windows = pytest.mark.skipif(
10+
sys.platform.startswith("win"),
11+
reason="OpenModelica Docker image is Linux-only; skipping on Windows.",
12+
)
13+
14+
skip_python_older_312 = pytest.mark.skipif(
15+
sys.version_info < (3, 12),
16+
reason="OMCPath(non-local) only working for Python >= 3.12.",
17+
)
18+
19+
720
@pytest.fixture
821
def model_firstorder_content():
922
return """
@@ -37,7 +50,7 @@ def param():
3750
}
3851

3952

40-
def test_runner(model_firstorder, param):
53+
def test_ModelicaSystemRunner_OMC(model_firstorder, param):
4154
# create a model using ModelicaSystem
4255
mod = OMPython.ModelicaSystemOMC()
4356
mod.model(
@@ -71,6 +84,167 @@ def test_runner(model_firstorder, param):
7184
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
7285

7386

87+
def test_ModelicaSystemRunner_local(model_firstorder, param):
88+
# create a model using ModelicaSystem
89+
mod = OMPython.ModelicaSystemOMC()
90+
mod.model(
91+
model_file=model_firstorder,
92+
model_name="M",
93+
)
94+
95+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
96+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
97+
98+
# run the model using only the runner class
99+
omcs = OMPython.OMSessionRunner(
100+
version=mod.get_session().get_version(),
101+
ompath_runner=OMPython.OMPathRunnerLocal,
102+
)
103+
modr = OMPython.ModelicaSystemRunner(
104+
session=omcs,
105+
work_directory=mod.getWorkDirectory(),
106+
)
107+
modr.setup(
108+
model_name="M",
109+
)
110+
111+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
112+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
113+
114+
# cannot check the content as runner does not have the capability to open a result file
115+
assert resultfile_mod.size() == resultfile_modr.size()
116+
117+
# check results
118+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
119+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
120+
121+
122+
@skip_on_windows
123+
def test_ModelicaSystemRunner_bash(model_firstorder, param):
124+
# create a model using ModelicaSystem
125+
mod = OMPython.ModelicaSystemOMC()
126+
mod.model(
127+
model_file=model_firstorder,
128+
model_name="M",
129+
)
130+
131+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
132+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
133+
134+
# run the model using only the runner class
135+
omcsr = OMPython.OMSessionRunner(
136+
version=mod.get_session().get_version(),
137+
ompath_runner=OMPython.OMPathRunnerBash,
138+
)
139+
modr = OMPython.ModelicaSystemRunner(
140+
session=omcsr,
141+
work_directory=mod.getWorkDirectory(),
142+
)
143+
modr.setup(
144+
model_name="M",
145+
)
146+
147+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
148+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
149+
150+
# cannot check the content as runner does not have the capability to open a result file
151+
assert resultfile_mod.size() == resultfile_modr.size()
152+
153+
# check results
154+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
155+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
156+
157+
158+
@skip_on_windows
159+
@skip_python_older_312
160+
def test_ModelicaSystemRunner_bash_docker(model_firstorder, param):
161+
omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
162+
omversion = omcs.sendExpression("getVersion()")
163+
assert isinstance(omversion, str) and omversion.startswith("OpenModelica")
164+
165+
# create a model using ModelicaSystem
166+
mod = OMPython.ModelicaSystemOMC(
167+
session=omcs,
168+
)
169+
mod.model(
170+
model_file=model_firstorder,
171+
model_name="M",
172+
)
173+
174+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
175+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
176+
177+
# run the model using only the runner class
178+
omcsr = OMPython.OMSessionRunner(
179+
version=mod.get_session().get_version(),
180+
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
181+
ompath_runner=OMPython.OMPathRunnerBash,
182+
model_execution_local=False,
183+
)
184+
modr = OMPython.ModelicaSystemRunner(
185+
session=omcsr,
186+
work_directory=mod.getWorkDirectory(),
187+
)
188+
modr.setup(
189+
model_name="M",
190+
)
191+
192+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
193+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
194+
195+
# cannot check the content as runner does not have the capability to open a result file
196+
assert resultfile_mod.size() == resultfile_modr.size()
197+
198+
# check results
199+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
200+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
201+
202+
203+
@pytest.mark.skip(reason="Not able to run WSL on github")
204+
@skip_python_older_312
205+
def test_ModelicaSystemDoE_WSL(tmp_path, model_doe, param_doe):
206+
omcs = OMPython.OMCSessionWSL()
207+
omversion = omcs.sendExpression("getVersion()")
208+
assert isinstance(omversion, str) and omversion.startswith("OpenModelica")
209+
210+
# create a model using ModelicaSystem
211+
mod = OMPython.ModelicaSystemOMC(
212+
session=omcs,
213+
)
214+
mod.model(
215+
model_file=model_firstorder,
216+
model_name="M",
217+
)
218+
219+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
220+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
221+
222+
# run the model using only the runner class
223+
omcsr = OMPython.OMSessionRunner(
224+
version=mod.get_session().get_version(),
225+
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
226+
ompath_runner=OMPython.OMPathRunnerBash,
227+
model_execution_local=False,
228+
)
229+
modr = OMPython.ModelicaSystemRunner(
230+
session=omcsr,
231+
work_directory=mod.getWorkDirectory(),
232+
)
233+
modr.setup(
234+
model_name="M",
235+
)
236+
237+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
238+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
239+
240+
# cannot check the content as runner does not have the capability to open a result file
241+
assert resultfile_mod.size() == resultfile_modr.size()
242+
243+
# check results
244+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
245+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
246+
247+
74248
def _run_simulation(mod, resultfile, param):
75249
simOptions = {"stopTime": param['stopTime'], "stepSize": 0.1, "tolerance": 1e-8}
76250
mod.setSimulationOptions(**simOptions)

0 commit comments

Comments
 (0)