Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RWTH-EBC
GitHub Repository: RWTH-EBC/ebcpy
Path: blob/master/tests/test_modelica.py
505 views
1
"""Test-module for all classes inside
2
ebcpy.optimization."""
3
4
import unittest
5
import os
6
from pathlib import Path
7
import pandas as pd
8
from ebcpy.modelica import manipulate_ds, \
9
get_expressions, \
10
get_names_and_values_of_lines
11
from ebcpy.modelica.simres import mat_to_pandas
12
13
14
class TestToPandas(unittest.TestCase):
15
"""Test-class for simres module"""
16
17
def setUp(self):
18
"""Called before every test.
19
Used to setup relevant paths and APIs etc."""
20
data_dir = Path(__file__).parent.joinpath("data")
21
self.example_mat_dir = data_dir.joinpath("example_mat_data.mat")
22
self.example_mo_dir = data_dir.joinpath("HeatPumpSystem.mo")
23
24
def test_mat_to_pandas(self):
25
"""Test function for the function to_pandas"""
26
df = mat_to_pandas(fname=self.example_mat_dir)
27
first_col_name = df.columns[0]
28
self.assertIsInstance(df, pd.DataFrame)
29
df = mat_to_pandas(fname=self.example_mat_dir, with_unit=False)
30
first_col_name_without_unit = df.columns[0]
31
self.assertIsInstance(df, pd.DataFrame)
32
self.assertTrue(first_col_name.startswith(first_col_name_without_unit))
33
df = mat_to_pandas(fname=self.example_mat_dir,
34
with_unit=False,
35
names=['combiTimeTable.y[6]'])
36
self.assertEqual(len(df.columns), 1)
37
38
def test_get_variable_code(self):
39
"""Test function get variable code"""
40
exp = get_expressions(filepath_model=self.example_mo_dir)
41
self.assertEqual(len(exp), 24)
42
exp = get_expressions(filepath_model=self.example_mo_dir, modelica_type="replaceable model")
43
self.assertEqual(len(exp), 2)
44
exp = get_expressions(filepath_model=self.example_mo_dir, modelica_type="variables")
45
self.assertEqual(len(exp), 0)
46
exp, exp_pr = get_expressions(filepath_model=self.example_mo_dir, get_protected=True)
47
self.assertEqual(len(exp), 16)
48
self.assertEqual(len(exp_pr), 8)
49
50
def test_get_variable_values(self):
51
"""Test get variable names and values"""
52
exp = get_expressions(filepath_model=self.example_mo_dir)
53
for var_name, var_value in get_names_and_values_of_lines(exp).items():
54
self.assertTrue(" " not in var_name)
55
if var_value is not None:
56
self.assertIsInstance(var_value, (float, int, bool))
57
# Test doctest
58
lines = ['parameter Boolean my_boolean=true "Some description"',
59
'parameter Real my_real=12.0 "Some description" annotation("Some annotation")']
60
self.assertEqual(get_names_and_values_of_lines(lines=lines),
61
{'my_boolean': True, 'my_real': 12.0})
62
lines = ['parameter Boolean my_boolean=true "Some description"',
63
'//parameter Real my_real=12.0 "Some description" annotation("Some annotation")']
64
self.assertEqual(get_names_and_values_of_lines(lines=lines),
65
{'my_boolean': True})
66
67
68
class TestManipulateDS(unittest.TestCase):
69
"""Test-class for manipulate_ds module."""
70
71
def setUp(self):
72
"""Called before every test.
73
Used to setup relevant paths and APIs etc."""
74
self.ds_paths = []
75
for file in ["dsfinal_old.txt", "dsin_2023.txt", "dsfinal_2023.txt"]:
76
self.ds_paths.append(Path(__file__).parent.joinpath("data", "ds_files", file))
77
78
def test_convert_ds_file_to_dataframe(self):
79
"""Test function for the function convert_ds_file_to_dataframe"""
80
for ds_path in self.ds_paths:
81
df = manipulate_ds.convert_ds_file_to_dataframe(ds_path)
82
self.assertIsInstance(df, pd.DataFrame)
83
84
def test_eliminate_parameters_from_ds_file(self):
85
"""Test function for the function eliminate_parameters_from_ds_file."""
86
for ds_path in self.ds_paths:
87
self._single_file_eliminate_parameters_from_ds_file(ds_path)
88
89
def _single_file_eliminate_parameters_from_ds_file(self, ds_path):
90
"""Test function for the function eliminate_parameters_from_ds_file."""
91
manipulate_ds.eliminate_parameters_from_ds_file(ds_path,
92
"dummy_dsout.txt",
93
[],
94
del_aux_paras=True)
95
self.assertTrue(os.path.isfile("dummy_dsout.txt"))
96
os.remove("dummy_dsout.txt")
97
# Test dont remove aux
98
manipulate_ds.eliminate_parameters_from_ds_file(ds_path,
99
"dummy_dsout.txt",
100
[],
101
del_aux_paras=False)
102
self.assertTrue(os.path.isfile("dummy_dsout.txt"))
103
os.remove("dummy_dsout.txt")
104
# Test wring input:
105
with self.assertRaises(TypeError):
106
manipulate_ds.eliminate_parameters_from_ds_file(
107
filename=ds_path,
108
savepath="dummy_dsout.kdk",
109
exclude_paras=[]
110
)
111
with self.assertRaises(TypeError):
112
manipulate_ds.eliminate_parameters_from_ds_file(
113
filename=ds_path,
114
savepath="dummy_dsout.txt",
115
exclude_paras={"Not a": "list"}
116
)
117
118
119
if __name__ == "__main__":
120
unittest.main()
121
122