|
33 | 33 | """ |
34 | 34 |
|
35 | 35 | import unittest |
| 36 | +import mock |
36 | 37 |
|
37 | | -from rmgpy.constraints import * |
| 38 | +from rmgpy.rmg.main import RMG |
| 39 | +from rmgpy.constraints import failsSpeciesConstraints |
| 40 | +from rmgpy.species import Species |
| 41 | +from rmgpy.molecule import Molecule |
| 42 | +import rmgpy.rmg.input |
38 | 43 |
|
39 | 44 | ################################################################################ |
40 | 45 |
|
41 | 46 | class TestFailsSpeciesConstraints(unittest.TestCase): |
42 | 47 | """ |
43 | 48 | Contains unit tests of the failsSpeciesConstraints function. |
44 | 49 | """ |
45 | | - |
46 | | - def testConstraintsNotLoaded(self): |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def setUpClass(cls): |
| 53 | + """ |
| 54 | + A function run ONCE before all unit tests in this class. |
| 55 | + """ |
| 56 | + cls.rmg = RMG() |
| 57 | + rmgpy.rmg.input.rmg = cls.rmg |
| 58 | + rmgpy.rmg.input.generatedSpeciesConstraints( |
| 59 | + maximumCarbonAtoms=2, |
| 60 | + maximumOxygenAtoms=1, |
| 61 | + maximumNitrogenAtoms=1, |
| 62 | + maximumSiliconAtoms=1, |
| 63 | + maximumSulfurAtoms=1, |
| 64 | + maximumHeavyAtoms=3, |
| 65 | + maximumRadicalElectrons=2, |
| 66 | + maximumSingletCarbenes=1, |
| 67 | + maximumCarbeneRadicals=0, |
| 68 | + maximumIsotopicAtoms=2, |
| 69 | + ) |
| 70 | + |
| 71 | + @mock.patch('rmgpy.constraints.logging') |
| 72 | + def testConstraintsNotLoaded(self, mock_logging): |
47 | 73 | """ |
48 | 74 | Test what happens when constraints are not loaded. |
49 | 75 | """ |
50 | | - from rmgpy.species import Species |
| 76 | + # Reset module level rmg variable in rmgpy.rmg.input |
| 77 | + rmgpy.rmg.input.rmg = None |
| 78 | + |
| 79 | + mol = Molecule(SMILES='C') |
| 80 | + |
| 81 | + self.assertFalse(failsSpeciesConstraints(mol)) |
| 82 | + |
| 83 | + mock_logging.debug.assert_called_with('Species constraints could not be found.') |
51 | 84 |
|
| 85 | + # Restore module level rmg variable in rmgpy.rmg.input |
| 86 | + rmgpy.rmg.input.rmg = self.rmg |
| 87 | + |
| 88 | + def testSpeciesInput(self): |
| 89 | + """ |
| 90 | + Test that failsSpeciesConstraints can handle a Species object. |
| 91 | + """ |
52 | 92 | spc = Species().fromSMILES('C') |
53 | 93 |
|
54 | | - fails = failsSpeciesConstraints(spc) |
55 | | - self.assertFalse(fails) |
| 94 | + self.assertFalse(failsSpeciesConstraints(spc)) |
| 95 | + |
| 96 | + def testExplicitlyAllowedMolecules(self): |
| 97 | + """ |
| 98 | + Test that we can explicitly allow molecules in species constraints. |
| 99 | + """ |
| 100 | + mol = Molecule(SMILES='CCCC') |
| 101 | + self.assertTrue(failsSpeciesConstraints(mol)) |
| 102 | + |
| 103 | + self.rmg.speciesConstraints['explicitlyAllowedMolecules'] = [Molecule(SMILES='CCCC')] |
| 104 | + self.assertFalse(failsSpeciesConstraints(mol)) |
| 105 | + |
| 106 | + def testCarbonConstraint(self): |
| 107 | + """ |
| 108 | + Test that we can constrain the max number of carbon atoms. |
| 109 | + """ |
| 110 | + mol1 = Molecule(SMILES='CC') |
| 111 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 112 | + |
| 113 | + mol2 = Molecule(SMILES='CCC') |
| 114 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 115 | + |
| 116 | + def testOxygenConstraint(self): |
| 117 | + """ |
| 118 | + Test that we can constrain the max number of oxygen atoms. |
| 119 | + """ |
| 120 | + mol1 = Molecule(SMILES='C=O') |
| 121 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 122 | + |
| 123 | + mol2 = Molecule(SMILES='OC=O') |
| 124 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 125 | + |
| 126 | + def testNitrogenConstraint(self): |
| 127 | + """ |
| 128 | + Test that we can constrain the max number of nitrogen atoms. |
| 129 | + """ |
| 130 | + mol1 = Molecule(SMILES='CN') |
| 131 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 132 | + |
| 133 | + mol2 = Molecule(SMILES='NCN') |
| 134 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 135 | + |
| 136 | + def testSiliconConstraint(self): |
| 137 | + """ |
| 138 | + Test that we can constrain the max number of silicon atoms. |
| 139 | + """ |
| 140 | + mol1 = Molecule(SMILES='[SiH4]') |
| 141 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 142 | + |
| 143 | + mol2 = Molecule(SMILES='[SiH3][SiH3]') |
| 144 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 145 | + |
| 146 | + def testSulfurConstraint(self): |
| 147 | + """ |
| 148 | + Test that we can constrain the max number of sulfur atoms. |
| 149 | + """ |
| 150 | + mol1 = Molecule(SMILES='CS') |
| 151 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 152 | + |
| 153 | + mol2 = Molecule(SMILES='SCS') |
| 154 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 155 | + |
| 156 | + def testHeavyConstraint(self): |
| 157 | + """ |
| 158 | + Test that we can constrain the max number of heavy atoms. |
| 159 | + """ |
| 160 | + mol1 = Molecule(SMILES='CCO') |
| 161 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 162 | + |
| 163 | + mol2 = Molecule(SMILES='CCN=O') |
| 164 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 165 | + |
| 166 | + def testRadicalConstraint(self): |
| 167 | + """ |
| 168 | + Test that we can constrain the max number of radical electrons. |
| 169 | + """ |
| 170 | + mol1 = Molecule(SMILES='[CH2][CH2]') |
| 171 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 172 | + |
| 173 | + mol2 = Molecule(SMILES='[CH2][CH][CH2]') |
| 174 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 175 | + |
| 176 | + def testCarbeneConstraint(self): |
| 177 | + """ |
| 178 | + Test that we can constrain the max number of singlet carbenes. |
| 179 | + """ |
| 180 | + mol1 = Molecule().fromAdjacencyList(""" |
| 181 | +1 C u0 p1 c0 {2,S} {3,S} |
| 182 | +2 H u0 p0 c0 {1,S} |
| 183 | +3 H u0 p0 c0 {1,S} |
| 184 | +""") |
| 185 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 186 | + |
| 187 | + mol2 = Molecule().fromAdjacencyList(""" |
| 188 | +1 C u0 p1 c0 {2,S} {3,S} |
| 189 | +2 H u0 p0 c0 {1,S} |
| 190 | +3 C u0 p1 c0 {1,S} {4,S} |
| 191 | +4 H u0 p0 c0 {3,S} |
| 192 | +""") |
| 193 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 194 | + |
| 195 | + def testCarbeneRadicalConstraint(self): |
| 196 | + """ |
| 197 | + Test that we can constrain the max number of radical electrons with a carbene. |
| 198 | + """ |
| 199 | + mol1 = Molecule().fromAdjacencyList(""" |
| 200 | +1 C u0 p1 c0 {2,S} {3,S} |
| 201 | +2 H u0 p0 c0 {1,S} |
| 202 | +3 H u0 p0 c0 {1,S} |
| 203 | +""") |
| 204 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 205 | + |
| 206 | + mol2 = Molecule().fromAdjacencyList(""" |
| 207 | +1 C u0 p1 c0 {2,S} {3,S} |
| 208 | +2 H u0 p0 c0 {1,S} |
| 209 | +3 C u1 p0 c0 {1,S} {4,S} {5,S} |
| 210 | +4 H u0 p0 c0 {3,S} |
| 211 | +5 H u0 p0 c0 {3,S} |
| 212 | +""") |
| 213 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
| 214 | + |
| 215 | + def testIsotopeConstraint(self): |
| 216 | + """ |
| 217 | + Test that we can constrain the max number of isotopic atoms. |
| 218 | + """ |
| 219 | + mol1 = Molecule().fromAdjacencyList(""" |
| 220 | +1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S} |
| 221 | +2 D u0 p0 c0 {1,S} |
| 222 | +3 D u0 p0 c0 {1,S} |
| 223 | +4 H u0 p0 c0 {1,S} |
| 224 | +5 H u0 p0 c0 {1,S} |
| 225 | +""") |
| 226 | + self.assertFalse(failsSpeciesConstraints(mol1)) |
| 227 | + |
| 228 | + mol2 = Molecule().fromAdjacencyList(""" |
| 229 | +1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S} |
| 230 | +2 D u0 p0 c0 {1,S} |
| 231 | +3 D u0 p0 c0 {1,S} |
| 232 | +4 D u0 p0 c0 {1,S} |
| 233 | +5 H u0 p0 c0 {1,S} |
| 234 | +""") |
| 235 | + self.assertTrue(failsSpeciesConstraints(mol2)) |
0 commit comments