def test_handleerror(self): @handleerror def testfunc(repo, doc): if doc.exception: raise doc.exception else: return True mockrepo = Mock() mockrepo.config = MagicMock() # must support __in__() mockdoc = Mock() # 1. should not raise an exception (but should call log.info # and util.robust_remove, and return false) with patch('ferenda.util.robust_remove') as robust_remove: mockdoc.exception = DocumentRemovedError self.assertFalse(testfunc(mockrepo, mockdoc)) self.assertTrue(mockrepo.log.info.called) self.assertTrue(robust_remove.called) # 2. should raise the same exception mockdoc.exception = KeyboardInterrupt with self.assertRaises(KeyboardInterrupt): testfunc(mockrepo, mockdoc) # 3.1 Should raise the same exeption mockdoc.exception = ParseError mockrepo.config.fatalexceptions = True with self.assertRaises(ParseError): testfunc(mockrepo, mockdoc) mockrepo.config.fatalexceptions = None # 3.2 Should not raise an exception (but should call log.error and return false) mockdoc.exception = ParseError self.assertFalse(testfunc(mockrepo, mockdoc)) self.assertTrue(mockrepo.log.error.called) # 4.1 Should raise the same exception mockdoc.exception = Exception mockrepo.config.fatalexceptions = True with self.assertRaises(Exception): testfunc(mockrepo, mockdoc) mockrepo.config.fatalexceptions = None # 4.2 Should not raise an exception mockdoc.exception = Exception self.assertFalse(testfunc(mockrepo, mockdoc)) self.assertTrue(mockrepo.log.error.called) # 5. No exceptions - everything should go fine mockdoc.exception = None self.assertTrue(testfunc(mockrepo, mockdoc))