def testApplyActionLosePair(self): """ Test the GroupAtom.applyAction() method for a LOSE_PAIR action when lonePairs is either specified or not. """ action = ['LOSE_PAIR', '*1', 1] # lonePairs specified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[1]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue( a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0) #lonePairs unspecified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue( a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual([1, 2, 3, 4], [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0)
def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue( a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0)
def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0) #test when radicals unspecified group = Group().fromAdjacencyList(""" 1 R ux """) #ux causes a wildcare for radicals atom1 = group.atoms[0] atom1.applyAction(action) self.assertListEqual(atom1.radicalElectrons, [0,1,2,3])
def testApplyActionLosePair(self): """ Test the GroupAtom.applyAction() method for a LOSE_PAIR action when lonePairs is either specified or not. """ action = ['LOSE_PAIR', '*1', 1] # lonePairs specified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[1]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue(a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0) #lonePairs unspecified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue(a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual([1,2,3,4], [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0)
def testApplyActionDecrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', -1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementBond)) for a in atomType.decrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementBond), 0)
def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0)
def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ["FORM_BOND", "*1", "S", "*2"] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label="*1") atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.formBond), 0)
def testApplyActionBreakBond(self): """ Test the GroupAtom.applyAction() method for a BREAK_BOND action. """ action = ['BREAK_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.breakBond)) for a in atomType.breakBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.breakBond), 0)
def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ['FORM_BOND', '*1', 'S', '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.formBond), 0)
def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue( a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0) #test when radicals unspecified group = Group().fromAdjacencyList(""" 1 R ux """) #ux causes a wildcare for radicals atom1 = group.atoms[0] atom1.applyAction(action) self.assertListEqual(atom1.radicalElectrons, [0, 1, 2, 3])
def testApplyActionIncrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementBond)) for a in atomType.incrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.incrementBond), 0)
class TestGroupAtom(unittest.TestCase): """ Contains unit tests of the GroupAtom class. """ def setUp(self): """ A method called before each unit test in this class. """ self.atom = GroupAtom(atomType=[atomTypes['Cd']], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) def testApplyActionBreakBond(self): """ Test the GroupAtom.applyAction() method for a BREAK_BOND action. """ action = ['BREAK_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.breakBond)) for a in atomType.breakBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.breakBond), 0) def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ['FORM_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.formBond), 0) def testApplyActionIncrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementBond)) for a in atomType.incrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.incrementBond), 0) def testApplyActionDecrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', -1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementBond)) for a in atomType.decrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.decrementBond), 0) def testApplyActionGainRadical(self): """ Test the GroupAtom.applyAction() method for a GAIN_RADICAL action. """ action = ['GAIN_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementRadical)) for a in atomType.incrementRadical: self.assertTrue( a in atom.atomType, "GAIN_RADICAL on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.incrementRadical)) self.assertEqual(atom0.radicalElectrons, [r - 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.incrementRadical), 0) #test when radicals unspecified group = Group().fromAdjacencyList(""" 1 R ux """) #ux causes a wildcare for radicals atom1 = group.atoms[0] atom1.applyAction(action) self.assertListEqual(atom1.radicalElectrons, [1, 2, 3, 4]) def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue( a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, atom.lonePairs) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0) #test when radicals unspecified group = Group().fromAdjacencyList(""" 1 R ux """) #ux causes a wildcare for radicals atom1 = group.atoms[0] atom1.applyAction(action) self.assertListEqual(atom1.radicalElectrons, [0, 1, 2, 3]) def testApplyActionGainPair(self): """ Test the GroupAtom.applyAction() method for a GAIN_PAIR action when lonePairs is either specified or not. """ action = ['GAIN_PAIR', '*1', 1] #lonePairs specified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementLonePair)) for a in atomType.incrementLonePair: self.assertTrue( a in atom.atomType, "GAIN_PAIR on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.incrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, [r - 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.incrementLonePair), 0) #lonePairs unspecified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementLonePair)) for a in atomType.incrementLonePair: self.assertTrue( a in atom.atomType, "GAIN_PAIR on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.incrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual([0, 1, 2, 3], [r - 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.incrementLonePair), 0) def testApplyActionLosePair(self): """ Test the GroupAtom.applyAction() method for a LOSE_PAIR action when lonePairs is either specified or not. """ action = ['LOSE_PAIR', '*1', 1] # lonePairs specified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[1]) atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue( a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual(atom0.lonePairs, [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0) #lonePairs unspecified: for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementLonePair)) for a in atomType.decrementLonePair: self.assertTrue( a in atom.atomType, "LOSE_PAIR on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementLonePair)) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) self.assertEqual([1, 2, 3, 4], [r + 1 for r in atom.lonePairs]) except ActionError: self.assertEqual(len(atomType.decrementLonePair), 0) def testEquivalent(self): """ Test the GroupAtom.equivalent() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) if label1 == label2 or atomType2 in atomType1.generic or atomType1 in atomType2.generic: self.assertTrue( atom1.equivalent(atom2), '{0!s} is not equivalent to {1!s}'.format( atom1, atom2)) self.assertTrue( atom2.equivalent(atom1), '{0!s} is not equivalent to {1!s}'.format( atom2, atom1)) else: self.assertFalse( atom1.equivalent(atom2), '{0!s} is equivalent to {1!s}'.format(atom1, atom2)) self.assertFalse( atom2.equivalent(atom1), '{0!s} is equivalent to {1!s}'.format(atom2, atom1)) # Now see if charge and radical count are checked properly for charge in range(3): for radicals in range(2): for lonePair in range(2): atom3 = GroupAtom(atomType=[atomType1], radicalElectrons=[radicals], charge=[charge], label='*1', lonePairs=[lonePair]) if radicals == 1 and charge == 0 and lonePair == 0: self.assertTrue( atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format( atom1, atom3)) self.assertTrue( atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format( atom3, atom1)) else: self.assertFalse( atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format( atom1, atom3)) self.assertFalse( atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format( atom3, atom1)) def testIsSpecificCaseOf(self): """ Test the GroupAtom.isSpecificCaseOf() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1', lonePairs=[0]) # And make more generic types of these two atoms atom1gen = GroupAtom(atomType=[atomType1], radicalElectrons=[0, 1], charge=[0, 1], label='*1', lonePairs=[0, 1]) atom2gen = GroupAtom(atomType=[atomType2], radicalElectrons=[0, 1], charge=[0, 1], label='*1', lonePairs=[0, 1]) if label1 == label2 or atomType2 in atomType1.generic: self.assertTrue( atom1.isSpecificCaseOf(atom2), '{0!s} is not a specific case of {1!s}'.format( atom1, atom2)) self.assertTrue( atom1.isSpecificCaseOf(atom2gen), '{0!s} is not a specific case of {1!s}'.format( atom1, atom2gen)) self.assertFalse( atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1gen, atom2)) else: self.assertFalse( atom1.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1, atom2)) self.assertFalse( atom1.isSpecificCaseOf(atom2gen), '{0!s} is a specific case of {1!s}'.format( atom1, atom2gen)) self.assertFalse( atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1gen, atom2)) def testCopy(self): """ Test the GroupAtom.copy() method. """ atom = self.atom.copy() self.assertEqual(len(self.atom.atomType), len(atom.atomType)) self.assertEqual(self.atom.atomType[0].label, atom.atomType[0].label) self.assertEqual(self.atom.radicalElectrons, atom.radicalElectrons) self.assertEqual(self.atom.charge, atom.charge) self.assertEqual(self.atom.label, atom.label) self.assertEqual(self.atom.lonePairs, atom.lonePairs) def testPickle(self): """ Test that a GroupAtom object can be successfully pickled and unpickled with no loss of information. """ import cPickle atom = cPickle.loads(cPickle.dumps(self.atom)) self.assertEqual(len(self.atom.atomType), len(atom.atomType)) self.assertEqual(self.atom.atomType[0].label, atom.atomType[0].label) self.assertEqual(self.atom.radicalElectrons, atom.radicalElectrons) self.assertEqual(self.atom.charge, atom.charge) self.assertEqual(self.atom.label, atom.label) self.assertEqual(self.atom.lonePairs, atom.lonePairs) def testHasWildcards(self): """ Tests the GroupAtom.hasWildcards() method """ self.assertFalse(self.atom.hasWildcards()) adjlist = """ 1 *2 C u0 {2,[D,T]} {3,S} 2 *3 C u0 {1,[D,T]} {4,S} 3 C ux {1,S} {5,S} 4 C u[0,1] {2,S} 5 [C,O] u0 {3,S} """ group = Group().fromAdjacencyList(adjlist) for index, atom in enumerate(group.atoms): self.assertTrue( atom.hasWildcards(), 'GroupAtom with index {0} should have wildcards, but does not'. format(index)) def testMakeSampleAtom(self): """ Tests the GroupAtom.makeSampleAtom() method """ newAtom = self.atom.makeSampleAtom() self.assertEquals(newAtom.element, elements.__dict__['C']) self.assertEquals(newAtom.radicalElectrons, 1) self.assertEquals(newAtom.charge, 0) self.assertEquals(newAtom.lonePairs, 0)
class TestGroupAtom(unittest.TestCase): """ Contains unit tests of the GroupAtom class. """ def setUp(self): """ A method called before each unit test in this class. """ self.atom = GroupAtom(atomType=[atomTypes['Cd']], radicalElectrons=[1], charge=[0], label='*1') def testApplyActionBreakBond(self): """ Test the GroupAtom.applyAction() method for a BREAK_BOND action. """ action = ['BREAK_BOND', '*1', 'S', '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.breakBond)) for a in atomType.breakBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.breakBond), 0) def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ['FORM_BOND', '*1', 'S', '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.formBond), 0) def testApplyActionIncrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementBond)) for a in atomType.incrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.incrementBond), 0) def testApplyActionDecrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', -1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementBond)) for a in atomType.decrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementBond), 0) def testApplyActionGainRadical(self): """ Test the GroupAtom.applyAction() method for a GAIN_RADICAL action. """ action = ['GAIN_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementRadical)) for a in atomType.incrementRadical: self.assertTrue( a in atom.atomType, "GAIN_RADICAL on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.incrementRadical)) self.assertEqual(atom0.radicalElectrons, [r - 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.incrementRadical), 0) def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue( a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format( atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0) def testEquivalent(self): """ Test the GroupAtom.equivalent() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1') atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1') if label1 == label2 or atomType2 in atomType1.generic or atomType1 in atomType2.generic: self.assertTrue( atom1.equivalent(atom2), '{0!s} is not equivalent to {1!s}'.format( atom1, atom2)) self.assertTrue( atom2.equivalent(atom1), '{0!s} is not equivalent to {1!s}'.format( atom2, atom1)) else: self.assertFalse( atom1.equivalent(atom2), '{0!s} is equivalent to {1!s}'.format(atom1, atom2)) self.assertFalse( atom2.equivalent(atom1), '{0!s} is equivalent to {1!s}'.format(atom2, atom1)) # Now see if charge and radical count are checked properly for charge in range(3): for radicals in range(2): atom3 = GroupAtom(atomType=[atomType1], radicalElectrons=[radicals], charge=[charge], label='*1') if radicals == 1 and charge == 0: self.assertTrue( atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format( atom1, atom3)) self.assertTrue( atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format( atom3, atom1)) else: self.assertFalse( atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format( atom1, atom3)) self.assertFalse( atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format( atom3, atom1)) def testIsSpecificCaseOf(self): """ Test the GroupAtom.isSpecificCaseOf() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1') atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1') # And make more generic types of these two atoms atom1gen = GroupAtom(atomType=[atomType1], radicalElectrons=[0, 1], charge=[0, 1], label='*1') atom2gen = GroupAtom(atomType=[atomType2], radicalElectrons=[0, 1], charge=[0, 1], label='*1') if label1 == label2 or atomType2 in atomType1.generic: self.assertTrue( atom1.isSpecificCaseOf(atom2), '{0!s} is not a specific case of {1!s}'.format( atom1, atom2)) self.assertTrue( atom1.isSpecificCaseOf(atom2gen), '{0!s} is not a specific case of {1!s}'.format( atom1, atom2gen)) self.assertFalse( atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1gen, atom2)) else: self.assertFalse( atom1.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1, atom2)) self.assertFalse( atom1.isSpecificCaseOf(atom2gen), '{0!s} is a specific case of {1!s}'.format( atom1, atom2gen)) self.assertFalse( atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format( atom1gen, atom2)) def testCopy(self): """ Test the GroupAtom.copy() method. """ atom = self.atom.copy() self.assertEqual(len(self.atom.atomType), len(atom.atomType)) self.assertEqual(self.atom.atomType[0].label, atom.atomType[0].label) self.assertEqual(self.atom.radicalElectrons, atom.radicalElectrons) self.assertEqual(self.atom.charge, atom.charge) self.assertEqual(self.atom.label, atom.label) def testPickle(self): """ Test that a GroupAtom object can be successfully pickled and unpickled with no loss of information. """ import cPickle atom = cPickle.loads(cPickle.dumps(self.atom)) self.assertEqual(len(self.atom.atomType), len(atom.atomType)) self.assertEqual(self.atom.atomType[0].label, atom.atomType[0].label) self.assertEqual(self.atom.radicalElectrons, atom.radicalElectrons) self.assertEqual(self.atom.charge, atom.charge) self.assertEqual(self.atom.label, atom.label)
class TestGroupAtom(unittest.TestCase): """ Contains unit tests of the GroupAtom class. """ def setUp(self): """ A method called before each unit test in this class. """ self.atom = GroupAtom(atomType=[atomTypes['Cd']], radicalElectrons=[1], charge=[0], label='*1') def testApplyActionBreakBond(self): """ Test the GroupAtom.applyAction() method for a BREAK_BOND action. """ action = ['BREAK_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.breakBond)) for a in atomType.breakBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.breakBond), 0) def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ['FORM_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.formBond), 0) def testApplyActionIncrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementBond)) for a in atomType.incrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.incrementBond), 0) def testApplyActionDecrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', -1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementBond)) for a in atomType.decrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementBond), 0) def testApplyActionGainRadical(self): """ Test the GroupAtom.applyAction() method for a GAIN_RADICAL action. """ action = ['GAIN_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType, "GAIN_RADICAL on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.incrementRadical)) self.assertEqual(atom0.radicalElectrons, [r - 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.incrementRadical), 0) def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0) def testEquivalent(self): """ Test the GroupAtom.equivalent() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1') atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1') if label1 == label2 or atomType2 in atomType1.generic or atomType1 in atomType2.generic: self.assertTrue(atom1.equivalent(atom2), '{0!s} is not equivalent to {1!s}'.format(atom1, atom2)) self.assertTrue(atom2.equivalent(atom1), '{0!s} is not equivalent to {1!s}'.format(atom2, atom1)) else: self.assertFalse(atom1.equivalent(atom2), '{0!s} is equivalent to {1!s}'.format(atom1, atom2)) self.assertFalse(atom2.equivalent(atom1), '{0!s} is equivalent to {1!s}'.format(atom2, atom1)) # Now see if charge and radical count are checked properly for charge in range(3): for radicals in range(2): atom3 = GroupAtom(atomType=[atomType1], radicalElectrons=[radicals], charge=[charge], label='*1') if radicals == 1 and charge == 0: self.assertTrue(atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format(atom1, atom3)) self.assertTrue(atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format(atom3, atom1)) else: self.assertFalse(atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format(atom1, atom3)) self.assertFalse(atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format(atom3, atom1)) def testIsSpecificCaseOf(self): """ Test the GroupAtom.isSpecificCaseOf() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1') atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1') # And make more generic types of these two atoms atom1gen = GroupAtom(atomType=[atomType1], radicalElectrons=[0, 1], charge=[0, 1], label='*1') atom2gen = GroupAtom(atomType=[atomType2], radicalElectrons=[0, 1], charge=[0, 1], label='*1') if label1 == label2 or atomType2 in atomType1.generic: self.assertTrue(atom1.isSpecificCaseOf(atom2), '{0!s} is not a specific case of {1!s}'.format(atom1, atom2)) self.assertTrue(atom1.isSpecificCaseOf(atom2gen), '{0!s} is not a specific case of {1!s}'.format(atom1, atom2gen)) self.assertFalse(atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format(atom1gen, atom2)) else: self.assertFalse(atom1.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format(atom1, atom2)) self.assertFalse(atom1.isSpecificCaseOf(atom2gen), '{0!s} is a specific case of {1!s}'.format(atom1, atom2gen)) self.assertFalse(atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format(atom1gen, atom2)) def testCopy(self): """ Test the GroupAtom.copy() method. """ atom = self.atom.copy() self.assertEqual(len(self.atom.atomType), len(atom.atomType)) self.assertEqual(self.atom.atomType[0].label, atom.atomType[0].label) self.assertEqual(self.atom.radicalElectrons, atom.radicalElectrons) self.assertEqual(self.atom.charge, atom.charge) self.assertEqual(self.atom.label, atom.label) def testPickle(self): """ Test that a GroupAtom object can be successfully pickled and unpickled with no loss of information. """ import cPickle atom = cPickle.loads(cPickle.dumps(self.atom)) self.assertEqual(len(self.atom.atomType), len(atom.atomType)) self.assertEqual(self.atom.atomType[0].label, atom.atomType[0].label) self.assertEqual(self.atom.radicalElectrons, atom.radicalElectrons) self.assertEqual(self.atom.charge, atom.charge) self.assertEqual(self.atom.label, atom.label) def testHasWildcards(self): """ Tests the GroupAtom.hasWildcards() method """ self.assertFalse(self.atom.hasWildcards()) adjlist = """ 1 *2 C u0 {2,[D,T]} {3,S} 2 *3 C u0 {1,[D,T]} {4,S} 3 C ux {1,S} {5,S} 4 C u[0,1] {2,S} 5 [C,O] u0 {3,S} """ group = Group().fromAdjacencyList(adjlist) for index, atom in enumerate(group.atoms): self.assertTrue(atom.hasWildcards(), 'GroupAtom with index {0} should have wildcards, but does not'.format(index)) def testMakeSampleAtom(self): """ Tests the GroupAtom.makeSampleAtom() method """ newAtom = self.atom.makeSampleAtom() self.assertEquals(newAtom.element, elements.__dict__['C']) self.assertEquals(newAtom.radicalElectrons, 1) self.assertEquals(newAtom.charge, 0)
class TestGroupAtom(unittest.TestCase): """ Contains unit tests of the GroupAtom class. """ def setUp(self): """ A method called before each unit test in this class. """ self.atom = GroupAtom(atomType=[atomTypes['Cd']], radicalElectrons=[1], charge=[0], label='*1') def testApplyActionBreakBond(self): """ Test the GroupAtom.applyAction() method for a BREAK_BOND action. """ action = ['BREAK_BOND', '*1', 'S', '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.breakBond)) for a in atomType.breakBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.breakBond), 0) def testApplyActionFormBond(self): """ Test the GroupAtom.applyAction() method for a FORM_BOND action. """ action = ['FORM_BOND', '*1', 'S', '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.formBond)) for a in atomType.formBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.formBond), 0) def testApplyActionIncrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', 1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementBond)) for a in atomType.incrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.incrementBond), 0) def testApplyActionDecrementBond(self): """ Test the GroupAtom.applyAction() method for a CHANGE_BOND action. """ action = ['CHANGE_BOND', '*1', -1, '*2'] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementBond)) for a in atomType.decrementBond: self.assertTrue(a in atom.atomType) self.assertEqual(atom0.radicalElectrons, atom.radicalElectrons) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementBond), 0) def testApplyActionGainRadical(self): """ Test the GroupAtom.applyAction() method for a GAIN_RADICAL action. """ action = ['GAIN_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.incrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType, "GAIN_RADICAL on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.incrementRadical)) self.assertEqual(atom0.radicalElectrons, [r - 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.incrementRadical), 0) def testApplyActionLoseRadical(self): """ Test the GroupAtom.applyAction() method for a LOSE_RADICAL action. """ action = ['LOSE_RADICAL', '*1', 1] for label, atomType in atomTypes.iteritems(): atom0 = GroupAtom(atomType=[atomType], radicalElectrons=[1], charge=[0], label='*1') atom = atom0.copy() try: atom.applyAction(action) self.assertEqual(len(atom.atomType), len(atomType.decrementRadical)) for a in atomType.incrementRadical: self.assertTrue(a in atom.atomType, "LOSE_RADICAL on {0} gave {1} not {2}".format(atomType, atom.atomType, atomType.decrementRadical)) self.assertEqual(atom0.radicalElectrons, [r + 1 for r in atom.radicalElectrons]) self.assertEqual(atom0.charge, atom.charge) self.assertEqual(atom0.label, atom.label) except ActionError: self.assertEqual(len(atomType.decrementRadical), 0) def testEquivalent(self): """ Test the GroupAtom.equivalent() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1') atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1') if label1 == label2 or atomType2 in atomType1.generic or atomType1 in atomType2.generic: self.assertTrue(atom1.equivalent(atom2), '{0!s} is not equivalent to {1!s}'.format(atom1, atom2)) self.assertTrue(atom2.equivalent(atom1), '{0!s} is not equivalent to {1!s}'.format(atom2, atom1)) else: self.assertFalse(atom1.equivalent(atom2), '{0!s} is equivalent to {1!s}'.format(atom1, atom2)) self.assertFalse(atom2.equivalent(atom1), '{0!s} is equivalent to {1!s}'.format(atom2, atom1)) # Now see if charge and radical count are checked properly for charge in range(3): for radicals in range(2): atom3 = GroupAtom(atomType=[atomType1], radicalElectrons=[radicals], charge=[charge], label='*1') if radicals == 1 and charge == 0: self.assertTrue(atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format(atom1, atom3)) self.assertTrue(atom1.equivalent(atom3), '{0!s} is not equivalent to {1!s}'.format(atom3, atom1)) else: self.assertFalse(atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format(atom1, atom3)) self.assertFalse(atom1.equivalent(atom3), '{0!s} is equivalent to {1!s}'.format(atom3, atom1)) def testIsSpecificCaseOf(self): """ Test the GroupAtom.isSpecificCaseOf() method. """ for label1, atomType1 in atomTypes.iteritems(): for label2, atomType2 in atomTypes.iteritems(): atom1 = GroupAtom(atomType=[atomType1], radicalElectrons=[1], charge=[0], label='*1') atom2 = GroupAtom(atomType=[atomType2], radicalElectrons=[1], charge=[0], label='*1') # And make more generic types of these two atoms atom1gen = GroupAtom(atomType=[atomType1], radicalElectrons=[0, 1], charge=[0, 1], label='*1') atom2gen = GroupAtom(atomType=[atomType2], radicalElectrons=[0, 1], charge=[0, 1], label='*1') if label1 == label2 or atomType2 in atomType1.generic: self.assertTrue(atom1.isSpecificCaseOf(atom2), '{0!s} is not a specific case of {1!s}'.format(atom1, atom2)) self.assertTrue(atom1.isSpecificCaseOf(atom2gen), '{0!s} is not a specific case of {1!s}'.format(atom1, atom2gen)) self.assertFalse(atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format(atom1gen, atom2)) else: self.assertFalse(atom1.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format(atom1, atom2)) self.assertFalse(atom1.isSpecificCaseOf(atom2gen), '{0!s} is a specific case of {1!s}'.format(atom1, atom2gen)) self.assertFalse(atom1gen.isSpecificCaseOf(atom2), '{0!s} is a specific case of {1!s}'.format(atom1gen, atom2)) def testCopy(self): """ Test the GroupAtom.copy() method. """ atom = self.atom.copy() self.assertEqual(len(self.atom.atomType), len(atom.atomType)) self.assertEqual(self.atom.atomType[0].label, atom.atomType[0].label) self.assertEqual(self.atom.radicalElectrons, atom.radicalElectrons) self.assertEqual(self.atom.charge, atom.charge) self.assertEqual(self.atom.label, atom.label) def testPickle(self): """ Test that a GroupAtom object can be successfully pickled and unpickled with no loss of information. """ import cPickle atom = cPickle.loads(cPickle.dumps(self.atom)) self.assertEqual(len(self.atom.atomType), len(atom.atomType)) self.assertEqual(self.atom.atomType[0].label, atom.atomType[0].label) self.assertEqual(self.atom.radicalElectrons, atom.radicalElectrons) self.assertEqual(self.atom.charge, atom.charge) self.assertEqual(self.atom.label, atom.label)