def test_log_entries_details_after(self): # noinspection PyTypeChecker log_manager = ExecuteLogManager() message1 = u'Message 1' message2 = u'Downloaded 1' message3 = u'Failed 1' finish_time_1 = datetime.now(pytz.utc) log_manager.started(finish_time_1) log_manager.log_entry(message1, 'info') entries = log_manager.get_execute_log_details(1) self.assertEqual(len(entries), 1) self.assertEqual(entries[0]['level'], 'info') self.assertEqual(entries[0]['message'], message1) log_manager.log_entry(message2, 'downloaded') log_manager.log_entry(message3, 'failed') log_manager.finished(finish_time_1, None) entries = log_manager.get_execute_log_details(1, after=entries[0]['id']) self.assertEqual(len(entries), 2) self.assertEqual(entries[0]['level'], 'downloaded') self.assertEqual(entries[0]['message'], message2) self.assertEqual(entries[1]['level'], 'failed') self.assertEqual(entries[1]['message'], message3)
def test_get_current_execute_log_details(self): # noinspection PyTypeChecker log_manager = ExecuteLogManager() message11 = u'Message 1' message12 = u'Downloaded 1' message13 = u'Failed 1' finish_time_1 = datetime.now(pytz.utc) self.assertIsNone(log_manager.get_current_execute_log_details()) log_manager.started(finish_time_1) log_manager.log_entry(message11, 'info') result = log_manager.get_current_execute_log_details() self.assertEqual(len(result), 1) self.assertEqual(result[0]['message'], message11) self.assertEqual(result[0]['level'], 'info') log_manager.log_entry(message12, 'downloaded') log_manager.log_entry(message13, 'failed') result = log_manager.get_current_execute_log_details(result[0]['id']) self.assertEqual(len(result), 2) self.assertEqual(result[0]['message'], message12) self.assertEqual(result[0]['level'], 'downloaded') self.assertEqual(result[1]['message'], message13) self.assertEqual(result[1]['level'], 'failed') log_manager.finished(finish_time_1, None) self.assertIsNone(log_manager.get_current_execute_log_details())
def test_log_entries_details_after(self): log_manager = ExecuteLogManager() message1 = u'Message 1' message2 = u'Downloaded 1' message3 = u'Failed 1' finish_time_1 = datetime.now(pytz.utc) log_manager.started(finish_time_1) log_manager.log_entry(message1, 'info') entries = log_manager.get_execute_log_details(1) self.assertEqual(len(entries), 1) self.assertEqual(entries[0]['level'], 'info') self.assertEqual(entries[0]['message'], message1) log_manager.log_entry(message2, 'downloaded') log_manager.log_entry(message3, 'failed') log_manager.finished(finish_time_1, None) entries = log_manager.get_execute_log_details(1, after=entries[0]['id']) self.assertEqual(len(entries), 2) self.assertEqual(entries[0]['level'], 'downloaded') self.assertEqual(entries[0]['message'], message2) self.assertEqual(entries[1]['level'], 'failed') self.assertEqual(entries[1]['message'], message3)
def test_get_current_execute_log_details(self): log_manager = ExecuteLogManager() message11 = u'Message 1' message12 = u'Downloaded 1' message13 = u'Failed 1' finish_time_1 = datetime.now(pytz.utc) self.assertIsNone(log_manager.get_current_execute_log_details()) log_manager.started(finish_time_1) log_manager.log_entry(message11, 'info') result = log_manager.get_current_execute_log_details() self.assertEqual(len(result), 1) self.assertEqual(result[0]['message'], message11) self.assertEqual(result[0]['level'], 'info') log_manager.log_entry(message12, 'downloaded') log_manager.log_entry(message13, 'failed') result = log_manager.get_current_execute_log_details(result[0]['id']) self.assertEqual(len(result), 2) self.assertEqual(result[0]['message'], message12) self.assertEqual(result[0]['level'], 'downloaded') self.assertEqual(result[1]['message'], message13) self.assertEqual(result[1]['level'], 'failed') log_manager.finished(finish_time_1, None) self.assertIsNone(log_manager.get_current_execute_log_details())
def test_remove_old_entries(self): # noinspection PyTypeChecker log_manager = ExecuteLogManager() now = datetime.now(pytz.utc) message11 = u'Message 1' start1 = now - timedelta(days=13) log_manager.started(start1) log_manager.log_entry(message11 + ' 1', 'info') log_manager.finished(start1, None) start2 = now - timedelta(days=12) log_manager.started(start2) log_manager.log_entry(message11 + ' 2', 'info') log_manager.finished(start2, None) start3 = now - timedelta(days=11) log_manager.started(start3) log_manager.log_entry(message11 + ' 3', 'info') log_manager.finished(start3, None) # This should be deleted as well it was exactly 10 days before start4 = now - timedelta(days=10) log_manager.started(start4) log_manager.log_entry(message11 + ' 4', 'info') log_manager.finished(start4, None) start5 = now - timedelta(days=5) log_manager.started(start5) log_manager.log_entry(message11 + ' 5', 'info') log_manager.log_entry(message11 + ' 6', 'info') log_manager.finished(start5, None) log_manager.remove_old_entries(10) entries, count = log_manager.get_log_entries(0, 10) self.assertEqual(count, 1) self.assertEqual(len(entries), 1) self.assertEqual(entries[0]['start_time'], start5) self.assertEqual(entries[0]['finish_time'], start5) execute_id = entries[0]['id'] details = log_manager.get_execute_log_details(execute_id) self.assertEqual(len(details), 2) self.assertEqual(details[0]['level'], 'info') self.assertEqual(details[0]['message'], message11 + ' 5') self.assertEqual(details[1]['level'], 'info') self.assertEqual(details[1]['message'], message11 + ' 6')
def test_remove_old_entries(self): log_manager = ExecuteLogManager() now = datetime.now(pytz.utc) message11 = u'Message 1' start1 = now - timedelta(days=13) log_manager.started(start1) log_manager.log_entry(message11 + ' 1', 'info') log_manager.finished(start1, None) start2 = now - timedelta(days=12) log_manager.started(start2) log_manager.log_entry(message11 + ' 2', 'info') log_manager.finished(start2, None) start3 = now - timedelta(days=11) log_manager.started(start3) log_manager.log_entry(message11 + ' 3', 'info') log_manager.finished(start3, None) # This should be deleted as well it was exactly 10 days before start4 = now - timedelta(days=10) log_manager.started(start4) log_manager.log_entry(message11 + ' 4', 'info') log_manager.finished(start4, None) start5 = now - timedelta(days=5) log_manager.started(start5) log_manager.log_entry(message11 + ' 5', 'info') log_manager.log_entry(message11 + ' 6', 'info') log_manager.finished(start5, None) log_manager.remove_old_entries(10) entries, count = log_manager.get_log_entries(0, 10) self.assertEqual(count, 1) self.assertEqual(len(entries), 1) self.assertEqual(entries[0]['start_time'], start5) self.assertEqual(entries[0]['finish_time'], start5) execute_id = entries[0]['id'] details = log_manager.get_execute_log_details(execute_id) self.assertEqual(len(details), 2) self.assertEqual(details[0]['level'], 'info') self.assertEqual(details[0]['message'], message11 + ' 5') self.assertEqual(details[1]['level'], 'info') self.assertEqual(details[1]['message'], message11 + ' 6')
def test_remove_old_entries_keep_all(self): # noinspection PyTypeChecker log_manager = ExecuteLogManager() now = datetime.now(pytz.utc) message11 = u'Message 1' start1 = now - timedelta(days=9) log_manager.started(start1) log_manager.log_entry(message11 + ' 1', 'info') log_manager.finished(start1, None) start2 = now - timedelta(days=8) log_manager.started(start2) log_manager.log_entry(message11 + ' 2', 'info') log_manager.finished(start2, None) log_manager.remove_old_entries(10) entries, count = log_manager.get_log_entries(0, 10) self.assertEqual(count, 2) self.assertEqual(len(entries), 2) self.assertEqual(entries[0]['start_time'], start2) self.assertEqual(entries[0]['finish_time'], start2) self.assertEqual(entries[1]['start_time'], start1) self.assertEqual(entries[1]['finish_time'], start1) execute_id = entries[0]['id'] details = log_manager.get_execute_log_details(execute_id) self.assertEqual(len(details), 1) self.assertEqual(details[0]['level'], 'info') self.assertEqual(details[0]['message'], message11 + ' 2') execute_id = entries[1]['id'] details = log_manager.get_execute_log_details(execute_id) self.assertEqual(len(details), 1) self.assertEqual(details[0]['level'], 'info') self.assertEqual(details[0]['message'], message11 + ' 1')
def test_remove_old_entries_keep_all(self): log_manager = ExecuteLogManager() now = datetime.now(pytz.utc) message11 = u'Message 1' start1 = now - timedelta(days=9) log_manager.started(start1) log_manager.log_entry(message11 + ' 1', 'info') log_manager.finished(start1, None) start2 = now - timedelta(days=8) log_manager.started(start2) log_manager.log_entry(message11 + ' 2', 'info') log_manager.finished(start2, None) log_manager.remove_old_entries(10) entries, count = log_manager.get_log_entries(0, 10) self.assertEqual(count, 2) self.assertEqual(len(entries), 2) self.assertEqual(entries[0]['start_time'], start2) self.assertEqual(entries[0]['finish_time'], start2) self.assertEqual(entries[1]['start_time'], start1) self.assertEqual(entries[1]['finish_time'], start1) execute_id = entries[0]['id'] details = log_manager.get_execute_log_details(execute_id) self.assertEqual(len(details), 1) self.assertEqual(details[0]['level'], 'info') self.assertEqual(details[0]['message'], message11 + ' 2') execute_id = entries[1]['id'] details = log_manager.get_execute_log_details(execute_id) self.assertEqual(len(details), 1) self.assertEqual(details[0]['level'], 'info') self.assertEqual(details[0]['message'], message11 + ' 1')
def test_log_entries_details_multiple_execute(self): # noinspection PyTypeChecker log_manager = ExecuteLogManager() message11 = u'Message 1' message12 = u'Downloaded 1' message13 = u'Failed 1' message21 = u'Failed 2' message22 = u'Downloaded 2' message23 = u'Message 2' finish_time_1 = datetime.now(pytz.utc) finish_time_2 = datetime.now(pytz.utc) + timedelta(minutes=60) log_manager.started(finish_time_1) log_manager.log_entry(message11, 'info') log_manager.log_entry(message12, 'downloaded') log_manager.log_entry(message13, 'failed') log_manager.finished(finish_time_1, None) log_manager.started(finish_time_2) log_manager.log_entry(message21, 'failed') log_manager.log_entry(message22, 'downloaded') log_manager.log_entry(message23, 'info') log_manager.finished(finish_time_2, None) entries = log_manager.get_execute_log_details(1) self.assertEqual(len(entries), 3) self.assertEqual(entries[0]['level'], 'info') self.assertEqual(entries[1]['level'], 'downloaded') self.assertEqual(entries[2]['level'], 'failed') self.assertEqual(entries[0]['message'], message11) self.assertEqual(entries[1]['message'], message12) self.assertEqual(entries[2]['message'], message13) entries = log_manager.get_execute_log_details(2) self.assertEqual(len(entries), 3) self.assertEqual(entries[0]['level'], 'failed') self.assertEqual(entries[1]['level'], 'downloaded') self.assertEqual(entries[2]['level'], 'info') self.assertEqual(entries[0]['message'], message21) self.assertEqual(entries[1]['message'], message22) self.assertEqual(entries[2]['message'], message23)
def test_log_entries_details_multiple_execute(self): log_manager = ExecuteLogManager() message11 = u'Message 1' message12 = u'Downloaded 1' message13 = u'Failed 1' message21 = u'Failed 2' message22 = u'Downloaded 2' message23 = u'Message 2' finish_time_1 = datetime.now(pytz.utc) finish_time_2 = datetime.now(pytz.utc) + timedelta(minutes=60) log_manager.started(finish_time_1) log_manager.log_entry(message11, 'info') log_manager.log_entry(message12, 'downloaded') log_manager.log_entry(message13, 'failed') log_manager.finished(finish_time_1, None) log_manager.started(finish_time_2) log_manager.log_entry(message21, 'failed') log_manager.log_entry(message22, 'downloaded') log_manager.log_entry(message23, 'info') log_manager.finished(finish_time_2, None) entries = log_manager.get_execute_log_details(1) self.assertEqual(len(entries), 3) self.assertEqual(entries[0]['level'], 'info') self.assertEqual(entries[1]['level'], 'downloaded') self.assertEqual(entries[2]['level'], 'failed') self.assertEqual(entries[0]['message'], message11) self.assertEqual(entries[1]['message'], message12) self.assertEqual(entries[2]['message'], message13) entries = log_manager.get_execute_log_details(2) self.assertEqual(len(entries), 3) self.assertEqual(entries[0]['level'], 'failed') self.assertEqual(entries[1]['level'], 'downloaded') self.assertEqual(entries[2]['level'], 'info') self.assertEqual(entries[0]['message'], message21) self.assertEqual(entries[1]['message'], message22) self.assertEqual(entries[2]['message'], message23)
def test_log_entries(self): log_manager = ExecuteLogManager() log_manager.started(datetime.now(pytz.utc)) log_manager.log_entry(u'Message 1', 'info') log_manager.log_entry(u'Message 2', 'downloaded') log_manager.log_entry(u'Message 3', 'downloaded') log_manager.log_entry(u'Message 4', 'failed') log_manager.log_entry(u'Message 5', 'failed') log_manager.log_entry(u'Message 6', 'failed') log_manager.finished(datetime.now(pytz.utc), None) entries, count = log_manager.get_log_entries(0, 5) self.assertEqual(len(entries), 1) self.assertEqual(count, 1) self.assertEqual(entries[0]['downloaded'], 2) self.assertEqual(entries[0]['failed'], 3) execute = entries[0] self.assertEqual(execute['status'], 'finished')
def test_log_entries(self): log_manager = ExecuteLogManager() log_manager.started(datetime.now(pytz.utc)) log_manager.log_entry(u'Message 1', 'info') log_manager.log_entry(u'Message 2', 'downloaded') log_manager.log_entry(u'Message 3', 'downloaded') log_manager.log_entry(u'Message 4', 'failed') log_manager.log_entry(u'Message 5', 'failed') log_manager.log_entry(u'Message 6', 'failed') log_manager.finished(datetime.now(pytz.utc), None) entries, count = log_manager.get_log_entries(0, 5) self.assertEqual(len(entries), 1) self.assertEqual(count, 1) self.assertEqual(entries[0]['downloaded'], 2) self.assertEqual(entries[0]['failed'], 3) execute = entries[0] self.assertEqual(execute['status'], 'finished')
def test_is_running(self): # noinspection PyTypeChecker log_manager = ExecuteLogManager() message11 = u'Message 1' message12 = u'Downloaded 1' message13 = u'Failed 1' finish_time_1 = datetime.now(pytz.utc) self.assertFalse(log_manager.is_running()) log_manager.started(finish_time_1) log_manager.log_entry(message11, 'info') log_manager.log_entry(message12, 'downloaded') log_manager.log_entry(message13, 'failed') self.assertTrue(log_manager.is_running()) self.assertTrue(log_manager.is_running(1)) self.assertFalse(log_manager.is_running(2)) log_manager.finished(finish_time_1, None) self.assertFalse(log_manager.is_running()) self.assertFalse(log_manager.is_running(1)) self.assertFalse(log_manager.is_running(2)) finish_time_2 = datetime.now(pytz.utc) + timedelta(minutes=60) log_manager.started(finish_time_2) log_manager.log_entry(message11, 'info') log_manager.log_entry(message12, 'downloaded') log_manager.log_entry(message13, 'failed') self.assertTrue(log_manager.is_running()) self.assertFalse(log_manager.is_running(1)) self.assertTrue(log_manager.is_running(2)) log_manager.finished(finish_time_2, None)
def test_is_running(self): log_manager = ExecuteLogManager() message11 = u'Message 1' message12 = u'Downloaded 1' message13 = u'Failed 1' finish_time_1 = datetime.now(pytz.utc) self.assertFalse(log_manager.is_running()) log_manager.started(finish_time_1) log_manager.log_entry(message11, 'info') log_manager.log_entry(message12, 'downloaded') log_manager.log_entry(message13, 'failed') self.assertTrue(log_manager.is_running()) self.assertTrue(log_manager.is_running(1)) self.assertFalse(log_manager.is_running(2)) log_manager.finished(finish_time_1, None) self.assertFalse(log_manager.is_running()) self.assertFalse(log_manager.is_running(1)) self.assertFalse(log_manager.is_running(2)) finish_time_2 = datetime.now(pytz.utc) + timedelta(minutes=60) log_manager.started(finish_time_2) log_manager.log_entry(message11, 'info') log_manager.log_entry(message12, 'downloaded') log_manager.log_entry(message13, 'failed') self.assertTrue(log_manager.is_running()) self.assertFalse(log_manager.is_running(1)) self.assertTrue(log_manager.is_running(2)) log_manager.finished(finish_time_2, None)
def test_log_entries_paging(self): # noinspection PyTypeChecker log_manager = ExecuteLogManager() finish_time_1 = datetime.now(pytz.utc) finish_time_2 = finish_time_1 + timedelta(seconds=10) finish_time_3 = finish_time_2 + timedelta(seconds=10) log_manager.started(finish_time_1) log_manager.log_entry(u'Message 1', 'info') log_manager.finished(finish_time_1, None) log_manager.started(finish_time_2) log_manager.log_entry(u'Download 2', 'downloaded') log_manager.finished(finish_time_2, None) log_manager.started(finish_time_3) log_manager.log_entry(u'Failed 3', 'failed') log_manager.finished(finish_time_3, None) entries, count = log_manager.get_log_entries(0, 1) self.assertEqual(len(entries), 1) self.assertEqual(count, 3) execute = entries[0] self.assertEqual(execute['downloaded'], 0) self.assertEqual(execute['failed'], 1) self.assertEqual(execute['status'], 'finished') entries, count = log_manager.get_log_entries(1, 1) self.assertEqual(len(entries), 1) self.assertEqual(count, 3) execute = entries[0] self.assertEqual(execute['downloaded'], 1) self.assertEqual(execute['failed'], 0) self.assertEqual(execute['status'], 'finished') entries, count = log_manager.get_log_entries(2, 1) self.assertEqual(len(entries), 1) self.assertEqual(count, 3) execute = entries[0] self.assertEqual(execute['downloaded'], 0) self.assertEqual(execute['failed'], 0) self.assertEqual(execute['status'], 'finished')
def test_log_entries_paging(self): log_manager = ExecuteLogManager() finish_time_1 = datetime.now(pytz.utc) finish_time_2 = finish_time_1 + timedelta(seconds=10) finish_time_3 = finish_time_2 + timedelta(seconds=10) log_manager.started(finish_time_1) log_manager.log_entry(u'Message 1', 'info') log_manager.finished(finish_time_1, None) log_manager.started(finish_time_2) log_manager.log_entry(u'Download 2', 'downloaded') log_manager.finished(finish_time_2, None) log_manager.started(finish_time_3) log_manager.log_entry(u'Failed 3', 'failed') log_manager.finished(finish_time_3, None) entries, count = log_manager.get_log_entries(0, 1) self.assertEqual(len(entries), 1) self.assertEqual(count, 3) execute = entries[0] self.assertEqual(execute['downloaded'], 0) self.assertEqual(execute['failed'], 1) self.assertEqual(execute['status'], 'finished') entries, count = log_manager.get_log_entries(1, 1) self.assertEqual(len(entries), 1) self.assertEqual(count, 3) execute = entries[0] self.assertEqual(execute['downloaded'], 1) self.assertEqual(execute['failed'], 0) self.assertEqual(execute['status'], 'finished') entries, count = log_manager.get_log_entries(2, 1) self.assertEqual(len(entries), 1) self.assertEqual(count, 3) execute = entries[0] self.assertEqual(execute['downloaded'], 0) self.assertEqual(execute['failed'], 0) self.assertEqual(execute['status'], 'finished')
def test_finished_fail(self): log_manager = ExecuteLogManager() with self.assertRaises(Exception): log_manager.finished(datetime.now(pytz.utc), None)
def test_finished_fail(self): # noinspection PyTypeChecker log_manager = ExecuteLogManager() with self.assertRaises(Exception): log_manager.finished(datetime.now(pytz.utc), None)