-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_profile_preprocessor.py
112 lines (95 loc) · 3.7 KB
/
test_profile_preprocessor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import unittest
import os
import json
from profile_preprocessor import (
ProfilePreprocessor,
FormatException,
UndefinedVariableException,
VariableTypeException,
)
import logging
# Setup logging as required by your ProfilePreprocessor implementation
logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)
class TestProfilePreprocessor(unittest.TestCase):
def setUp(self):
os.environ["LOG_PATH"] = "/tmp"
os.environ["CONFIG_PATH"] = "/tmp"
# A basic profile structure to be modified in different tests
self.profile = {
"variables": [
{
"name": "Pressure",
"key": "pressure_1",
"type": "pressure",
"value": 9,
},
{"name": "Time", "key": "time_1", "type": "time", "value": 30},
],
"stages": [
{
"name": "Test Stage",
"type": "pressure",
"dynamics": {
"points": [["$time_1", "$pressure_1"]],
"over": "time",
"interpolation": "linear",
},
"exit_triggers": [{"type": "time", "value": "$time_1"}],
"limits": [
{"type": "flow", "value": 2.1},
{"type": "pressure", "value": "$pressure_1"},
],
}
],
}
def test_successful_processing(self):
processed_profile = ProfilePreprocessor.processVariables(self.profile)
self.assertEqual(
processed_profile["stages"][0]["dynamics"]["points"][0],
[30, 9],
"Points were not processed correctly",
)
self.assertEqual(
processed_profile["stages"][0]["exit_triggers"][0]["value"],
30,
"Exit were triggers not processed correctly",
)
self.assertEqual(
processed_profile["stages"][0]["limits"][1]["value"],
9,
"Limits were not processed correctly",
)
logger.warning(json.dumps(processed_profile, indent=2))
def test_missing_variables(self):
# Remove variables
self.profile["variables"] = []
with self.assertRaises(UndefinedVariableException):
ProfilePreprocessor.processVariables(self.profile)
def test_missing_variables_field(self):
# Remove variables
del self.profile["variables"]
with self.assertRaises(UndefinedVariableException):
ProfilePreprocessor.processVariables(self.profile)
def test_incorrect_variable_key(self):
# Non-existent variable
self.profile["stages"][0]["dynamics"]["points"][0][0] = "$time_2"
with self.assertRaises(UndefinedVariableException):
ProfilePreprocessor.processVariables(self.profile)
def test_wrong_variable_type(self):
# Change expected type
self.profile["variables"][0]["type"] = "time"
with self.assertRaises(VariableTypeException):
ProfilePreprocessor.processVariables(self.profile)
def test_missing_stage_key(self):
# Remove required key
del self.profile["stages"][0]["type"]
with self.assertRaises(FormatException):
ProfilePreprocessor.processVariables(self.profile)
def test_invalid_stage_structure(self):
# Invalid structure of the profile (array expected)
self.profile["stages"][0]["dynamics"]["points"] = 1234
with self.assertRaises(FormatException):
ProfilePreprocessor.processVariables(self.profile)
if __name__ == "__main__":
unittest.main()