-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
132 lines (99 loc) · 3.81 KB
/
tests.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import copy
import shutil
import tempfile
import unittest
import wc_kb
import wc_lang
from wc_sim.simulation import Simulation
from wc_sim.run_results import RunResults
class KnowledgeBaseTestCase(unittest.TestCase):
""" Test case for a knowledge base
Attributes:
KB (:obj:`wc_kb.KnowledgeBase`): knowledge base
"""
pass
class ModelTestCase(unittest.TestCase):
""" Test case for a model
Class provides
* Convenience methods for testing models
* Basic tests applicable to all models
* Reactions are element and charged balance
Attributes:
KB (:obj:`wc_kb.KnowledgeBase`): knowledge base
MODEL (:obj:`wc_lang.Model`): model
"""
def test_reactions_balanced(self):
""" Check that each reaction is element and charge balanced
Raises:
:obj:`Exception`: if one or more reactions is unbalanced
"""
imbalances = []
for reaction in self.MODEL.reactions:
imbalance = self.is_reaction_balanced(reaction)
if imbalance:
imbalances.append(imbalance)
if imbalances:
msg = []
for imbalance in imbalances:
if imbalance.element:
msg.append('Reaction {} is element imbalanced: {}'.format())
if imbalance.charge:
msg.append('Reaction {} is charge imbalanced: {}'.format())
raise Exception('The following reactions are not balanced:\n {}'.format('\n '.join(msg)))
def is_reaction_balanced(self, reaction):
""" Check that a reaction is element and charge balanced
Returns:
imbalance
"""
pass
class SubmodelTestCase(unittest.TestCase):
""" Test case for a submodel
Attributes:
KB (:obj:`wc_kb.KnowledgeBase`): knowledge base
SUBMODEL (:obj:`wc_lang.Submodel`): submodel
"""
pass
class ModelSimulationTestCase(unittest.TestCase):
""" Test case for a simulation of a model
Provides basic functionality needed to test simulations of models
* Perturb model parameters
* Simulate model
* Log simulation results
* Read simulation results
* Reduce simulation results
Attributes:
KB (:obj:`wc_kb.KnowledgeBase`): knowledge base
MODEL (:obj:`wc_lang.Model`): model
_results_path (:obj:`str`): path to temporarily save simulation result
"""
def setUp(self):
self._results_path = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self._results_path)
def simulate(self, end_time, checkpoint_period, peturbation=None):
if perturbation:
model = copy.deepcopy(self.MODEL)
peturbation(model)
else:
model = self.MODEL
sim = Simulation(self.MODEL)
sim.run(time_max=end_time,
results_dir=self._results_path,
checkpoint_period=checkpoint_period)
return RunResults(self._results_path)
def simulate_edge_case(self, end_time, checkpoint_period, param_id, value=0):
def peturbation(model):
param = model.parameters.get(id=param_id)
param.value = value
return self.simulate(end_time, checkpoint_period, peturbation=peturbation)
class SubmodelSimulationTestCase(unittest.TestCase):
""" Test case for a simulation of a submodel
Attributes:
KB (:obj:`wc_kb.KnowledgeBase`): knowledge base
SUBMODEL (:obj:`wc_lang.Submodel`): submodel
_results_path (:obj:`str`): path to temporarily save simulation result
"""
def setUp(self):
self._results_path = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self._results_path)