def test_log(self): sink = ListSink() logger = BasicLogger(sink) logs = sink.items logger.log('a') logger.log('c') logger.log('d') self.assertEqual(logs[0], 'a') self.assertEqual(logs[1], 'c') self.assertEqual(logs[2], 'd')
def test_log_with_interrupt(self): sink = ListSink() logger = BasicLogger(sink) logs = sink.items with self.assertRaises(BaseException) as e: with logger.log('a'): logger.log('c') logger.log('d') raise KeyboardInterrupt() self.assertEqual(4, len(logs)) self.assertEqual(logs[0], 'a') self.assertEqual(logs[1], 'c') self.assertEqual(logs[2], 'd') self.assertEqual(logs[3], 'a (interrupt)')
def test_log_with_exception(self): sink = ListSink() logger = BasicLogger(sink) logs = sink.items with self.assertRaises(Exception): with logger.log('a'): logger.log('c') logger.log('d') raise Exception() self.assertEqual(4, len(logs)) self.assertEqual(logs[0], 'a') self.assertEqual(logs[1], 'c') self.assertEqual(logs[2], 'd') self.assertEqual(logs[3], 'a (exception)')
def test_log_with(self): sink = ListSink() logger = BasicLogger(sink) logs = sink.items with logger.log('a'): logger.log('c') logger.log('d') logger.log('e') self.assertEqual(5, len(logs)) self.assertEqual(logs[0], 'a') self.assertEqual(logs[1], 'c') self.assertEqual(logs[2], 'd') self.assertEqual(logs[3], 'a (completed)') self.assertEqual(logs[4], 'e')
def test_time_with_interrupt(self): sink = ListSink() logger = BasicLogger(sink) logs = sink.items with self.assertRaises(BaseException) as e: with logger.time('a'): logger.log('c') logger.log('d') raise KeyboardInterrupt() self.assertEqual(4, len(logs)) self.assertEqual(logs[0], 'a') self.assertEqual(logs[1], 'c') self.assertEqual(logs[2], 'd') self.assertRegex(logs[3], '^a \\(\\d+\\.\\d+ seconds\\) \\(interrupt\\)$')
def test_time_with_1(self): #This test is somewhat time dependent. #I don't think it should ever fail, but if it does #try running it again and see if it works that time. sink = ListSink() logger = BasicLogger(sink) logs = sink.items with logger.time('a'): logger.log('c') time.sleep(0.15) logger.log('d') logger.log('e') self.assertEqual(5, len(logs)) self.assertEqual(logs[0], 'a') self.assertEqual(logs[1], 'c') self.assertEqual(logs[2], 'd') self.assertRegex(logs[3], '^a \\(\\d+\\.\\d+ seconds\\) \\(completed\\)$') self.assertEqual(logs[4], 'e') self.assertAlmostEqual(float(logs[3][3:7]), 0.15, 1)
def test_time_two_separate(self): #This test is somewhat time dependent. #I don't think it should ever fail, but if it does #try running it again and see if it works that time. sink = ListSink() logger = BasicLogger(sink) logs = sink.items with logger.time('a'): time.sleep(0.05) with logger.time('d'): logger.log('e') time.sleep(0.05) logger.log('g') with logger.time('a'): time.sleep(0.05) with logger.time('d'): logger.log('e') time.sleep(0.05) self.assertEqual(11, len(logs)) self.assertEqual(logs[0], 'a') self.assertEqual(logs[1], 'd') self.assertEqual(logs[2], 'e') self.assertRegex(logs[3], 'd \\(\\d+\\.\\d+ seconds\\)') self.assertRegex(logs[4], 'a \\(\\d+\\.\\d+ seconds\\)') self.assertEqual(logs[5], 'g') self.assertEqual(logs[6], 'a') self.assertEqual(logs[7], 'd') self.assertEqual(logs[8], 'e') self.assertRegex(logs[9], 'd \\(\\d+\\.\\d+ seconds\\)') self.assertRegex(logs[10], 'a \\(\\d+\\.\\d+ seconds\\)') self.assertAlmostEqual(float(logs[3][3:7]), 0.05, 1) self.assertAlmostEqual(float(logs[4][3:7]), 0.10, 1) self.assertAlmostEqual(float(logs[9][3:7]), 0.05, 1) self.assertAlmostEqual(float(logs[10][3:7]), 0.10, 1)