MoPyRegtest 0.5.0__tar.gz → 0.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1/MoPyRegtest.egg-info}/PKG-INFO +1 -1
  2. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/MoPyRegtest.egg-info/SOURCES.txt +1 -0
  3. {MoPyRegtest-0.5.0/MoPyRegtest.egg-info → mopyregtest-0.5.1}/PKG-INFO +1 -1
  4. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/mopyregtest/cli.py +11 -1
  5. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/mopyregtest/generator.py +15 -2
  6. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/mopyregtest/modelicaregressiontest.py +57 -17
  7. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/setup.py +1 -1
  8. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/tests/test_cli.py +43 -0
  9. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/tests/test_compare_results.py +26 -22
  10. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/tests/test_metrics.py +5 -1
  11. mopyregtest-0.5.1/tests/test_modelerror.py +46 -0
  12. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/tests/test_unify_timestamps.py +5 -1
  13. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/LICENSE +0 -0
  14. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/MoPyRegtest.egg-info/dependency_links.txt +0 -0
  15. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/MoPyRegtest.egg-info/entry_points.txt +0 -0
  16. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/MoPyRegtest.egg-info/requires.txt +0 -0
  17. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/MoPyRegtest.egg-info/top_level.txt +0 -0
  18. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/README.md +0 -0
  19. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/mopyregtest/__init__.py +0 -0
  20. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/mopyregtest/metrics.py +0 -0
  21. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/mopyregtest/templates/omc/model_import.mos.template +0 -0
  22. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/mopyregtest/templates/omc/model_simulate.mos.template +0 -0
  23. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/mopyregtest/utils.py +0 -0
  24. {MoPyRegtest-0.5.0 → mopyregtest-0.5.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: MoPyRegtest
3
- Version: 0.5.0
3
+ Version: 0.5.1
4
4
  Summary: A Python enabled simple regression testing framework for Modelica models
5
5
  Home-page: https://github.com/pstelzig/mopyregtest
6
6
  Author: Philipp Emanuel Stelzig
@@ -18,4 +18,5 @@ mopyregtest/templates/omc/model_simulate.mos.template
18
18
  tests/test_cli.py
19
19
  tests/test_compare_results.py
20
20
  tests/test_metrics.py
21
+ tests/test_modelerror.py
21
22
  tests/test_unify_timestamps.py
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: MoPyRegtest
3
- Version: 0.5.0
3
+ Version: 0.5.1
4
4
  Summary: A Python enabled simple regression testing framework for Modelica models
5
5
  Home-page: https://github.com/pstelzig/mopyregtest
6
6
  Author: Philipp Emanuel Stelzig
@@ -36,7 +36,17 @@ def generate(args):
36
36
  models_in_package = args.models_in_package.split(",")
37
37
 
38
38
  if args.references is not None:
39
- references = {r.split(":")[0] : r.split(":")[1] for r in args.references.split(",")}
39
+ ref_pairs = args.references.split(",")
40
+ references = {}
41
+ for i in range(0, len(ref_pairs)):
42
+ sep_idx = ref_pairs[i].find(":")
43
+ if sep_idx == -1 or sep_idx == len(ref_pairs[i])-1:
44
+ raise ValueError(f"The argument {args.references} has no valid reference string format. "
45
+ "Must be like <model name1>:</path/to/ref1.csv>,<model name2>:</path/to/ref2.csv>")
46
+
47
+ k = ref_pairs[i][0:sep_idx]
48
+ v = ref_pairs[i][sep_idx+1:]
49
+ references[k] = v
40
50
  else:
41
51
  references = None
42
52
 
@@ -10,6 +10,7 @@ import shutil
10
10
  import os
11
11
  import pathlib
12
12
  import tempfile
13
+ import sys
13
14
 
14
15
  from . import metrics
15
16
  from . import utils
@@ -168,11 +169,23 @@ if __name__ == '__main__':
168
169
  result_folder=tmp_res_folder, tool=tool,
169
170
  modelica_version=self.modelica_version,
170
171
  dependencies=self.dependencies)
171
- regtest._import_and_simulate()
172
+
173
+ try:
174
+ regtest._import_and_simulate()
175
+ except Exception as e:
176
+ print(f"Failed to generate the test:\n\n{e}\n\n")
177
+ print("Aborting test generation.")
178
+ sys.exit(1)
172
179
 
173
180
  # Copy reference result file from temporary folder into reference target folder
174
181
  res_file = f"{model_in_package}_res.csv"
175
- shutil.copyfile(pathlib.Path(tmp_res_folder) / res_file, str(pathlib.Path(reference_folder) / res_file))
182
+ ref_src = pathlib.Path(tmp_res_folder) / res_file
183
+ ref_dst = pathlib.Path(reference_folder) / res_file
184
+
185
+ if ref_dst.exists():
186
+ os.remove(ref_dst)
187
+
188
+ shutil.copyfile(ref_src, ref_dst)
176
189
 
177
190
  # Cleanup temporary directory with simulation data
178
191
  if do_cleanup:
@@ -7,6 +7,8 @@ MIT License. See the project's LICENSE file.
7
7
  """
8
8
 
9
9
  import os
10
+ import re
11
+ import subprocess
10
12
  import platform
11
13
  import pathlib
12
14
  import shutil
@@ -49,11 +51,12 @@ class RegressionTest:
49
51
  dependency is an entire package, it must be the path to the respective package's package.mo.
50
52
  """
51
53
 
54
+ self.initial_cwd = os.getcwd()
55
+
52
56
  self.template_folder_path = pathlib.Path(__file__).parent.absolute() / "templates"
53
- self.package_folder_path = pathlib.Path(package_folder).absolute()
57
+ self.package_folder_path = self._make_path_absolut(package_folder)
54
58
  self.model_in_package = model_in_package
55
- self.result_folder_path = pathlib.Path(result_folder).absolute()
56
- self.initial_cwd = os.getcwd()
59
+ self.result_folder_path = self._make_path_absolut(result_folder)
57
60
  self.modelica_version = modelica_version
58
61
  self.dependencies = dependencies
59
62
 
@@ -64,6 +67,18 @@ class RegressionTest:
64
67
 
65
68
  self.result_folder_created = False
66
69
 
70
+ def _make_path_absolut(self, path):
71
+ path = os.path.expanduser(path)
72
+
73
+ if not pathlib.PurePath(path).is_absolute():
74
+ path_abs = pathlib.Path(self.initial_cwd) / path
75
+ else:
76
+ path_abs = pathlib.Path(path)
77
+
78
+ path_abs = path_abs.absolute()
79
+
80
+ return path_abs
81
+
67
82
  @staticmethod
68
83
  def _unify_timestamps(results: List[pd.DataFrame], fill_in_method="ffill"):
69
84
  """
@@ -207,13 +222,9 @@ class RegressionTest:
207
222
  pathlib.Path.mkdir(self.result_folder_path)
208
223
  self.result_folder_created = True
209
224
 
210
- os.chdir(self.result_folder_path)
211
-
212
225
  # Run the scripts for import and simulation
213
226
  self._run_model()
214
227
 
215
- os.chdir(self.initial_cwd)
216
-
217
228
  return
218
229
 
219
230
  @staticmethod
@@ -442,6 +453,8 @@ class RegressionTest:
442
453
  out : None
443
454
 
444
455
  """
456
+ os.chdir(self.result_folder_path)
457
+
445
458
  for tool in self.tools:
446
459
  tool_executable = tool
447
460
 
@@ -477,20 +490,20 @@ class RegressionTest:
477
490
  utils.replace_in_file(self.result_folder_path / model_import_mos, repl_dict)
478
491
 
479
492
  # Run the import script and write the output of the OpenModelica Compiler (omc) to omc_output
480
- os.system(tool_executable + " {} > {}".format(model_import_mos, tool_output))
481
-
482
- # Read simulation options from the simulation_options_file
483
- model_import_output_file = open(tool_output, 'r')
484
- omc_messages = model_import_output_file.readlines()
485
- model_import_output_file.close()
493
+ proc_return = subprocess.run([tool_executable, model_import_mos], check=True, capture_output=True)
494
+ omc_messages = proc_return.stdout.decode("utf-8").strip("\'").strip("\n")
486
495
 
487
- (start_time, stop_time, tolerance, num_intervals, interval) = omc_messages[-1].lstrip('(').rstrip(')').split(',')
496
+ (start_time, stop_time, tolerance, num_intervals, interval) = omc_messages.split("\n")[-1].lstrip('(').rstrip(')').split(',')
488
497
 
489
498
  # Modify the simulation template
490
499
  if platform.system() == 'Windows':
491
- repl_dict["SIMULATION_BINARY"] = "{}.exe".format(self.model_in_package)
500
+ sim_binary = self.model_in_package + ".bat"
501
+ repl_dict["SIMULATION_BINARY"] = sim_binary
492
502
  elif platform.system() == 'Linux':
493
- repl_dict["SIMULATION_BINARY"] = "./{}".format(self.model_in_package)
503
+ sim_binary = self.model_in_package
504
+ repl_dict["SIMULATION_BINARY"] = "./" + sim_binary
505
+ else:
506
+ raise ValueError(f"Platform {platform.system()} not supported")
494
507
  repl_dict["START_TIME"] = start_time
495
508
  repl_dict["STOP_TIME"] = stop_time
496
509
  repl_dict["TOLERANCE"] = tolerance
@@ -498,8 +511,35 @@ class RegressionTest:
498
511
 
499
512
  utils.replace_in_file(self.result_folder_path / model_simulate_mos, repl_dict)
500
513
 
514
+ # Delete old simulation binary and old simulation result
515
+ sim_result_path = self.result_folder_path / (self.model_in_package + "_res.csv")
516
+ if sim_result_path.exists():
517
+ os.remove(sim_result_path)
518
+
519
+ sim_binary_path = self.result_folder_path / sim_binary
520
+ if sim_binary_path.exists():
521
+ os.remove(sim_binary_path)
522
+
501
523
  # Run the simulation script and append the output of the OpenModelica Compiler (omc) to omc_output
502
- os.system(tool_executable + " {} >> {}".format(model_simulate_mos, tool_output))
524
+ proc_return = subprocess.run([tool_executable, model_simulate_mos], check=True, capture_output=True)
525
+ omc_messages = proc_return.stdout.decode("utf-8").strip("\'").strip("\n")
526
+
527
+ # Check output: Both simulation binary and simulation result must exist now
528
+ if not sim_binary_path.exists():
529
+ os.chdir(self.initial_cwd)
530
+
531
+ raise AssertionError(
532
+ f"The expected simulation binary at {sim_result_path} does not exist. "
533
+ + f"Please check the output from the simulation tool:\n\n{omc_messages}")
534
+
535
+ if not sim_result_path.exists():
536
+ os.chdir(self.initial_cwd)
537
+
538
+ raise AssertionError(
539
+ f"The expected simulation result at {sim_result_path} does not exist. "
540
+ + f"Please check the output from the simulation tool:\n\n{omc_messages}")
541
+
542
+ os.chdir(self.initial_cwd)
503
543
 
504
544
  return
505
545
 
@@ -8,7 +8,7 @@ with open("README.md") as fh:
8
8
 
9
9
  setuptools.setup(
10
10
  name="MoPyRegtest",
11
- version="0.5.0",
11
+ version="0.5.1",
12
12
  author="Philipp Emanuel Stelzig",
13
13
  description="A Python enabled simple regression testing framework for Modelica models",
14
14
  long_description=long_description,
@@ -1,3 +1,4 @@
1
+ import shutil
1
2
  import unittest
2
3
  import pathlib
3
4
  from mopyregtest import cli
@@ -53,6 +54,44 @@ class TestCli(unittest.TestCase):
53
54
 
54
55
  return
55
56
 
57
+ def test_generate4(self):
58
+ """
59
+ Testing for generation to fail if the simulation model for which a test is
60
+ being generated fails to build.
61
+ """
62
+ cmd_args = ["generate",
63
+ str(this_folder / "data/gentests"),
64
+ "FlawedModels_DoesNotBuild",
65
+ str(this_folder / "data/FlawedModels"),
66
+ "FlawedModels.DoesNotBuild"]
67
+
68
+ with self.assertRaises(SystemExit) as e:
69
+ cli.parse_args(cmd_args)
70
+
71
+ self.assertEqual(e.exception.code, 1)
72
+
73
+ # Clean up the generated regression tests as otherwise they will confuse unittest discovery
74
+ shutil.rmtree(this_folder / "data/gentests")
75
+
76
+ return
77
+
78
+ def test_generate5(self):
79
+ """
80
+
81
+ """
82
+ cmd_args = ["generate",
83
+ str(this_folder / "data/gentests"),
84
+ "FlawedModels_DoesNotFinish",
85
+ str(this_folder / "data/FlawedModels"),
86
+ "FlawedModels.DoesNotFinish"]
87
+
88
+ cli.parse_args(cmd_args)
89
+
90
+ # Clean up the generated regression tests as otherwise they will confuse unittest discovery
91
+ shutil.rmtree(this_folder / "data/gentests")
92
+
93
+ return
94
+
56
95
  def test_compare1(self):
57
96
  """
58
97
  Testing for default arguments in CLI: mopyregtest compare
@@ -129,3 +168,7 @@ class TestCli(unittest.TestCase):
129
168
  (this_folder / "../examples/test_user_defined_metrics/references/Sine_res_comparison.csv").exists())
130
169
 
131
170
  return
171
+
172
+
173
+ if __name__ == '__main__':
174
+ unittest.main()
@@ -12,8 +12,8 @@ class TestCompareResults(unittest.TestCase):
12
12
  """
13
13
 
14
14
  self.assertRaises(ValueError, mopyregtest.RegressionTest.compare_csv_files,
15
- reference_result="../examples/test_user_defined_metrics/references/Sine_res.csv",
16
- simulation_result="../examples/test_user_defined_metrics/references/Sine_res.csv",
15
+ reference_result=this_folder / "../examples/test_user_defined_metrics/references/Sine_res.csv",
16
+ simulation_result=this_folder / "../examples/test_user_defined_metrics/references/Sine_res.csv",
17
17
  validated_cols=["time"])
18
18
 
19
19
  def test_disjoint_cols(self):
@@ -21,8 +21,8 @@ class TestCompareResults(unittest.TestCase):
21
21
  Validate that test of two CSV result files that have no common result variable besides time fails
22
22
  """
23
23
  self.assertRaises(ValueError, mopyregtest.RegressionTest.compare_csv_files,
24
- reference_result="../examples/test_Modelica_Electrical_Analog_Examples/references/Modelica.Electrical.Analog.Examples.CharacteristicIdealDiodes_res.csv",
25
- simulation_result="../examples/test_user_defined_metrics/references/Sine_res.csv")
24
+ reference_result=this_folder / "../examples/test_Modelica_Electrical_Analog_Examples/references/Modelica.Electrical.Analog.Examples.CharacteristicIdealDiodes_res.csv",
25
+ simulation_result=this_folder / "../examples/test_user_defined_metrics/references/Sine_res.csv")
26
26
  return
27
27
 
28
28
  def test_validated_cols_not_contained1(self):
@@ -32,8 +32,8 @@ class TestCompareResults(unittest.TestCase):
32
32
  """
33
33
 
34
34
  self.assertRaises(ValueError, mopyregtest.RegressionTest.compare_csv_files,
35
- reference_result="../examples/test_user_defined_metrics/references/Sine_res.csv",
36
- simulation_result="../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
35
+ reference_result=this_folder / "../examples/test_user_defined_metrics/references/Sine_res.csv",
36
+ simulation_result=this_folder / "../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
37
37
  validated_cols=["y", "not_present_var"])
38
38
  return
39
39
 
@@ -44,8 +44,8 @@ class TestCompareResults(unittest.TestCase):
44
44
  """
45
45
 
46
46
  self.assertRaises(ValueError, mopyregtest.RegressionTest.compare_csv_files,
47
- reference_result="../examples/test_user_defined_metrics/references/Sine_res.csv",
48
- simulation_result="../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
47
+ reference_result=this_folder / "../examples/test_user_defined_metrics/references/Sine_res.csv",
48
+ simulation_result=this_folder / "../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
49
49
  validated_cols=["y", "uniformNoise.y"])
50
50
  return
51
51
 
@@ -56,8 +56,8 @@ class TestCompareResults(unittest.TestCase):
56
56
  """
57
57
 
58
58
  self.assertRaises(ValueError, mopyregtest.RegressionTest.compare_csv_files,
59
- reference_result="../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
60
- simulation_result="../examples/test_user_defined_metrics/references/Sine_res.csv",
59
+ reference_result=this_folder / "../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
60
+ simulation_result=this_folder / "../examples/test_user_defined_metrics/references/Sine_res.csv",
61
61
  validated_cols=["y", "uniformNoise.y"])
62
62
  return
63
63
 
@@ -67,18 +67,18 @@ class TestCompareResults(unittest.TestCase):
67
67
  argument.
68
68
  """
69
69
 
70
- compar_file_path = (this_folder / "../examples/test_user_defined_metrics/references/Sine_res_comparison.csv")
70
+ compare_file_path = (this_folder / "../examples/test_user_defined_metrics/references/Sine_res_comparison.csv")
71
71
 
72
- if compar_file_path.exists():
73
- os.remove(compar_file_path)
72
+ if compare_file_path.exists():
73
+ os.remove(compare_file_path)
74
74
 
75
75
  self.assertRaises(AssertionError, mopyregtest.RegressionTest.compare_csv_files,
76
- reference_result="../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
77
- simulation_result="../examples/test_user_defined_metrics/references/Sine_res.csv",
76
+ reference_result=this_folder / "../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
77
+ simulation_result=this_folder / "../examples/test_user_defined_metrics/references/Sine_res.csv",
78
78
  tol=1e-5,
79
79
  validated_cols=["y"],
80
80
  write_comparison=False)
81
- self.assertFalse(compar_file_path.exists())
81
+ self.assertFalse(compare_file_path.exists())
82
82
 
83
83
  return
84
84
 
@@ -87,16 +87,20 @@ class TestCompareResults(unittest.TestCase):
87
87
  Validate that a comparison timeseries is written on a failed test by default.
88
88
  """
89
89
 
90
- compar_file_path = (this_folder / "../examples/test_user_defined_metrics/references/Sine_res_comparison.csv")
90
+ compare_file_path = (this_folder / "../examples/test_user_defined_metrics/references/Sine_res_comparison.csv")
91
91
 
92
- if compar_file_path.exists():
93
- os.remove(compar_file_path)
92
+ if compare_file_path.exists():
93
+ os.remove(compare_file_path)
94
94
 
95
95
  self.assertRaises(AssertionError, mopyregtest.RegressionTest.compare_csv_files,
96
- reference_result="../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
97
- simulation_result="../examples/test_user_defined_metrics/references/Sine_res.csv",
96
+ reference_result=this_folder / "../examples/test_user_defined_metrics/references/SineNoisy_res.csv",
97
+ simulation_result=this_folder / "../examples/test_user_defined_metrics/references/Sine_res.csv",
98
98
  tol=1e-5,
99
99
  validated_cols=["y"])
100
- self.assertTrue(compar_file_path.exists())
100
+ self.assertTrue(compare_file_path.exists())
101
101
 
102
102
  return
103
+
104
+
105
+ if __name__ == '__main__':
106
+ unittest.main()
@@ -150,4 +150,8 @@ class TestMetrics(unittest.TestCase):
150
150
 
151
151
  self.assertRaises(ValueError, mopyregtest.metrics.norm_infty_dist, f1=f1, f2=f3)
152
152
 
153
- self.assertRaises(ValueError, mopyregtest.metrics.norm_infty_dist, f1=f1, f2=f4)
153
+ self.assertRaises(ValueError, mopyregtest.metrics.norm_infty_dist, f1=f1, f2=f4)
154
+
155
+
156
+ if __name__ == '__main__':
157
+ unittest.main()
@@ -0,0 +1,46 @@
1
+ import os
2
+ import unittest
3
+ import pathlib
4
+ import mopyregtest
5
+
6
+ this_folder = pathlib.Path(__file__).absolute().parent
7
+ initial_cwd = os.getcwd()
8
+
9
+ class TestModelErrors(unittest.TestCase):
10
+ def test_build_errors(self):
11
+ # Exception in RegressionTest._import_and_simulate raised before RegressionTest switches back to initial_cwd
12
+ os.chdir(initial_cwd)
13
+
14
+ tester = mopyregtest.RegressionTest(package_folder=this_folder / "data/FlawedModels",
15
+ model_in_package="FlawedModels.DoesNotBuild",
16
+ result_folder=this_folder / "data/FlawedModels/results")
17
+
18
+ self.assertRaises(AssertionError, tester._import_and_simulate)
19
+
20
+ tester.cleanup(ask_confirmation=False)
21
+
22
+ return
23
+
24
+ def test_simulate_errors1(self):
25
+ """
26
+ This test runs a simulation model that will build and run, but the simulation will not reach the
27
+ targeted simulation end time. An error will _not_ be thrown by _import_and_simulate. This is because
28
+ for regression we do not care about simulation end times, but about reproducibility.
29
+ """
30
+
31
+ # Exception in RegressionTest._import_and_simulate raised before RegressionTest switches back to initial_cwd
32
+ os.chdir(initial_cwd)
33
+
34
+ tester = mopyregtest.RegressionTest(package_folder=this_folder / "data/FlawedModels",
35
+ model_in_package="FlawedModels.DoesNotFinish",
36
+ result_folder=this_folder / "data/FlawedModels/results")
37
+
38
+ tester._import_and_simulate()
39
+
40
+ tester.cleanup(ask_confirmation=False)
41
+
42
+ return
43
+
44
+
45
+ if __name__ == '__main__':
46
+ unittest.main()
@@ -99,4 +99,8 @@ class TestUnifyTimestamps(unittest.TestCase):
99
99
  results_ext = mopyregtest.RegressionTest._unify_timestamps([res1, res2])
100
100
 
101
101
  self.assertIsNone(pd.testing.assert_frame_equal(res1_ext_expect, results_ext[0]))
102
- self.assertIsNone(pd.testing.assert_frame_equal(res2_ext_expect, results_ext[1]))
102
+ self.assertIsNone(pd.testing.assert_frame_equal(res2_ext_expect, results_ext[1]))
103
+
104
+
105
+ if __name__ == '__main__':
106
+ unittest.main()
File without changes
File without changes
File without changes