forked from ReactionMechanismGenerator/RMG-Py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
databaseTest.py
188 lines (167 loc) · 10.1 KB
/
databaseTest.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
This scripts runs tests on the database
"""
import os.path
import logging
from external.wip import work_in_progress
from rmgpy import settings
from rmgpy.data.rmg import RMGDatabase
from copy import copy, deepcopy
from rmgpy.data.base import LogicOr
from rmgpy.molecule import Group
import nose
import nose.tools
class TestDatabase(): # cannot inherit from unittest.TestCase if we want to use nose test generators
"""
Contains unit tests for the database for rigorous error checking.
"""
@classmethod
def setUpClass(cls):
"""
Load the database before running the tests.
"""
databaseDirectory = settings['database.directory']
cls.database = RMGDatabase()
cls.database.load(databaseDirectory, kineticsFamilies='all')
# These are generators, that call the methods below.
def test_kinetics(self):
for family_name, family in self.database.kinetics.families.iteritems():
test = lambda x: self.kinetics_checkCorrectNumberofNodesInRules(family_name)
test_name = "Kinetics family {0}: rules have correct number of nodes?".format(family_name)
test.description = test_name
self.compat_func_name = test_name
yield test, None
test = lambda x: self.kinetics_checkNodesInRulesFoundInGroups(family_name)
test_name = "Kinetics family {0}: rules' nodes exist in the groups?".format(family_name)
test.description = test_name
self.compat_func_name = test_name
yield test, None
test = lambda x: self.kinetics_checkGroupsFoundInTree(family_name)
test_name = "Kinetics family {0}: groups are in the tree with proper parents?".format(family_name)
test.description = test_name
self.compat_func_name = test_name
yield test, None
test = lambda x: self.kinetics_checkGroupsNonidentical(family_name)
test_name = "Kinetics family {0}: groups are not identical?".format(family_name)
test.description = test_name
self.compat_func_name = test_name
yield test, family_name
test = lambda x: self.kinetics_checkChildParentRelationships(family_name)
test_name = "Kinetics family {0}: parent-child relationships are correct?".format(family_name)
test.description = test_name
self.compat_func_name = test_name
yield test, family_name
def test_thermo(self):
for group_name, group in self.database.thermo.groups.iteritems():
test = lambda x: self.general_checkNodesFoundInTree(group_name, group)
test_name = "Thermo groups {0}: nodes are in the tree with proper parents?".format(group_name)
test.description = test_name
self.compat_func_name = test_name
yield test, group_name
def test_solvation(self):
for group_name, group in self.database.solvation.groups.iteritems():
test = lambda x: self.general_checkNodesFoundInTree(group_name, group)
test_name = "Solvation groups {0}: nodes are in the tree with proper parents?".format(group_name)
test.description = test_name
self.compat_func_name = test_name
yield test, group_name
def test_statmech(self):
for group_name, group in self.database.statmech.groups.iteritems():
test = lambda x: self.general_checkNodesFoundInTree(group_name, group)
test_name = "Statmech groups {0}: nodes are in the tree with proper parents?".format(group_name)
test.description = test_name
self.compat_func_name = test_name
yield work_in_progress(test), group_name
def test_transport(self):
for group_name, group in self.database.transport.groups.iteritems():
test = lambda x: self.general_checkNodesFoundInTree(group_name, group)
test_name = "Transport groups {0}: nodes are in the tree with proper parents?".format(group_name)
test.description = test_name
self.compat_func_name = test_name
yield test, group_name
# These are the actual tests, that don't start with a "test_" name:
def kinetics_checkCorrectNumberofNodesInRules(self, family_name):
"""
This test ensures that each rate rule contains the proper number of nodes according to the family it originates.
"""
family = self.database.kinetics.families[family_name]
expectedNumberNodes = len(family.getRootTemplate())
for label, entries in family.rules.entries.iteritems():
for entry in entries:
nodes = label.split(';')
nose.tools.assert_equal(len(nodes), expectedNumberNodes, "Wrong number of groups or semicolons in family {family} rule {entry}. Should be {num_nodes}".format(family=family_name, entry=entry, num_nodes=expectedNumberNodes))
def kinetics_checkNodesInRulesFoundInGroups(self, family_name):
"""
This test ensures that each rate rule contains nodes that exist in the groups
"""
family = self.database.kinetics.families[family_name]
for label, entries in family.rules.entries.iteritems():
for entry in entries:
nodes = label.split(';')
for node in nodes:
nose.tools.assert_true(node in family.groups.entries, "In {family} family, no group definition found for label {label} in rule {entry}".format(family=family_name, label=node, entry=entry))
def kinetics_checkGroupsFoundInTree(self, family_name):
"""
This test checks whether groups are found in the tree, with proper parents.
"""
family = self.database.kinetics.families[family_name]
for nodeName, nodeGroup in family.groups.entries.iteritems():
ascendParent = nodeGroup
# Check whether the node has proper parents unless it is the top reactant or product node
while ascendParent not in family.groups.top and ascendParent not in family.forwardTemplate.products:
child = ascendParent
ascendParent = ascendParent.parent
nose.tools.assert_true(ascendParent is not None, "Group {group} in {family} family was found in the tree without a proper parent.".format(group=child, family=family_name))
nose.tools.assert_true(child in ascendParent.children, "Group {group} in {family} family was found in the tree without a proper parent.".format(group=nodeName, family=family_name))
def kinetics_checkGroupsNonidentical(self, family_name):
"""
This test checks that the groups are non-identical.
"""
from rmgpy.data.base import Database
originalFamily = self.database.kinetics.families[family_name]
family = Database()
family.entries = originalFamily.groups.entries
entriesCopy = copy(family.entries)
for nodeName, nodeGroup in family.entries.iteritems():
del entriesCopy[nodeName]
for nodeNameOther, nodeGroupOther in entriesCopy.iteritems():
nose.tools.assert_false(family.matchNodeToNode(nodeGroup, nodeGroupOther), "Group {group} in {family} family was found to be identical to group {groupOther}".format(group=nodeName, family=family_name, groupOther=nodeNameOther))
def kinetics_checkChildParentRelationships(self, family_name):
"""
This test checks that groups' parent-child relationships are correct in the database.
"""
from rmgpy.data.base import Database
originalFamily = self.database.kinetics.families[family_name]
family = Database()
family.entries = originalFamily.groups.entries
for nodeName, childNode in family.entries.iteritems():
#top nodes and product nodes don't have parents by definition, so they get an automatic pass:
if childNode in originalFamily.groups.top or childNode in originalFamily.forwardTemplate.products: continue
parentNode = childNode.parent
# Check whether the node has proper parents unless it is the top reactant or product node
# The parent should be more general than the child
nose.tools.assert_true(family.matchNodeToChild(parentNode, childNode),
"In {family} family, group {parent} is not a proper parent of its child {child}.".format(family=family_name, parent=parentNode, child=nodeName))
#check that parentNodes which are LogicOr do not have an ancestor that is a Group
#If it does, then the childNode must also be a child of the ancestor
if isinstance(parentNode, LogicOr):
ancestorNode = childNode
while ancestorNode not in originalFamily.groups.top and isinstance(ancestorNode, LogicOr):
ancestorNode = ancestorNode.parent
if isinstance(ancestorNode, Group):
nose.tools.assert_true(family.matchNodeToChild(ancestorNode, childNode),
"In {family} family, group {ancestor} is not a proper ancestor of its child {child}.".format(family=family_name, ancestor=ancestorNode, child=nodeName))
def general_checkNodesFoundInTree(self, group_name, group):
"""
This test checks whether nodes are found in the tree, with proper parents.
"""
for nodeName, nodeGroup in group.entries.iteritems():
ascendParent = nodeGroup
# Check whether the node has proper parents unless it is the top reactant or product node
while ascendParent not in group.top:
child = ascendParent
ascendParent = ascendParent.parent
nose.tools.assert_true(ascendParent is not None, "Node {node} in {group} group was found in the tree without a proper parent.".format(node=child, group=group_name))
nose.tools.assert_true(child in ascendParent.children, "Node {node} in {group} group was found in the tree without a proper parent.".format(node=nodeName, group=group_name))
if __name__ == '__main__':
nose.run(argv=[__file__, '-v', '--nologcapture'], defaultTest=__name__)