Skip to content

Commit 4ce8804

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 daa34cc commit 4ce8804

13 files changed

+368
-108
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_ModelicaDoEOMC_local(tmp_path, model_doe, param_doe):
7575
@skip_python_older_312
7676
def test_ModelicaDoEOMC_docker(tmp_path, model_doe, param_doe):
7777
omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal")
78-
assert omcs.sendExpression("getVersion()") == "OpenModelica 1.25.0"
78+
assert omcs.get_version() == "OpenModelica 1.25.0"
7979

8080
mod = OMPython.ModelicaSystemOMC(
8181
session=omcs,
@@ -98,7 +98,7 @@ def test_ModelicaDoEOMC_docker(tmp_path, model_doe, param_doe):
9898
@skip_python_older_312
9999
def test_ModelicaDoEOMC_WSL(tmp_path, model_doe, param_doe):
100100
omcs = OMPython.OMCSessionWSL()
101-
assert omcs.sendExpression("getVersion()") == "OpenModelica 1.25.0"
101+
assert omcs.get_version() == "OpenModelica 1.25.0"
102102

103103
mod = OMPython.ModelicaSystemOMC(
104104
session=omcs,
@@ -157,6 +157,6 @@ def _run_ModelicaDoEOMC(doe_mod):
157157
f"y[{row['p']}]": float(row['b']),
158158
}
159159

160-
for var in var_dict:
161-
assert var in sol['data']
162-
assert np.isclose(sol['data'][var][-1], var_dict[var])
160+
for key, val in var_dict.items():
161+
assert key in sol['data']
162+
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: 173 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,165 @@ 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+
assert omcs.get_version() == "OpenModelica 1.25.0"
163+
164+
# create a model using ModelicaSystem
165+
mod = OMPython.ModelicaSystemOMC(
166+
session=omcs,
167+
)
168+
mod.model(
169+
model_file=model_firstorder,
170+
model_name="M",
171+
)
172+
173+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
174+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
175+
176+
# run the model using only the runner class
177+
omcsr = OMPython.OMSessionRunner(
178+
version=mod.get_session().get_version(),
179+
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
180+
ompath_runner=OMPython.OMPathRunnerBash,
181+
model_execution_local=False,
182+
)
183+
modr = OMPython.ModelicaSystemRunner(
184+
session=omcsr,
185+
work_directory=mod.getWorkDirectory(),
186+
)
187+
modr.setup(
188+
model_name="M",
189+
)
190+
191+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
192+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
193+
194+
# cannot check the content as runner does not have the capability to open a result file
195+
assert resultfile_mod.size() == resultfile_modr.size()
196+
197+
# check results
198+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
199+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
200+
201+
202+
@pytest.mark.skip(reason="Not able to run WSL on github")
203+
@skip_python_older_312
204+
def test_ModelicaSystemDoE_WSL(tmp_path, model_doe, param_doe):
205+
omcs = OMPython.OMCSessionWSL()
206+
assert omcs.get_version() == "OpenModelica 1.25.0"
207+
208+
# create a model using ModelicaSystem
209+
mod = OMPython.ModelicaSystemOMC(
210+
session=omcs,
211+
)
212+
mod.model(
213+
model_file=model_firstorder,
214+
model_name="M",
215+
)
216+
217+
resultfile_mod = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_mod.mat"
218+
_run_simulation(mod=mod, resultfile=resultfile_mod, param=param)
219+
220+
# run the model using only the runner class
221+
omcsr = OMPython.OMSessionRunner(
222+
version=mod.get_session().get_version(),
223+
cmd_prefix=omcs.model_execution_prefix(cwd=mod.getWorkDirectory()),
224+
ompath_runner=OMPython.OMPathRunnerBash,
225+
model_execution_local=False,
226+
)
227+
modr = OMPython.ModelicaSystemRunner(
228+
session=omcsr,
229+
work_directory=mod.getWorkDirectory(),
230+
)
231+
modr.setup(
232+
model_name="M",
233+
)
234+
235+
resultfile_modr = mod.getWorkDirectory() / f"{mod.get_model_name()}_res_modr.mat"
236+
_run_simulation(mod=modr, resultfile=resultfile_modr, param=param)
237+
238+
# cannot check the content as runner does not have the capability to open a result file
239+
assert resultfile_mod.size() == resultfile_modr.size()
240+
241+
# check results
242+
_check_result(mod=mod, resultfile=resultfile_mod, param=param)
243+
_check_result(mod=mod, resultfile=resultfile_modr, param=param)
244+
245+
74246
def _run_simulation(mod, resultfile, param):
75247
simOptions = {"stopTime": param['stopTime'], "stepSize": 0.1, "tolerance": 1e-8}
76248
mod.setSimulationOptions(**simOptions)

0 commit comments

Comments
 (0)