def history(update: Update, context: CallbackContext): """Send a message when the command /logs is issued.""" logs = Logs() logslist = logs.getLastFiveLogs() # print(logslist) for log in logslist: response = "" for key, value in log.items(): response = response + f'{key}: {value}\n' update.message.reply_text(response)
def wrapper(*args, **kwargs): message = 'button' if args[0].message is None else args[0].message.text new_log = { "user": args[0].effective_user.username, "function": function.__name__, "message": message, "time": args[0].effective_message.date } logs = Logs() logs.addLog(new_log) return function(*args, **kwargs)
def test(update: Update, context: CallbackContext): new_log = { "user": update.effective_user.first_name, "function": "anonym", "message": "test", "time": update.message.date } logs = Logs() loglist = [] for _ in range(100000): loglist.append(new_log) logs.addLogs(loglist)
def setUp(self): self.logs = Logs('tests.db')
class TestLogs(unittest.TestCase): def setUp(self): self.logs = Logs('tests.db') def tearDown(self): self.logs.clean() def test_addLog(self): new_log = { "user": "******", "function": "corona_stats", "message": "button", "time": "31-Mar-2020 (11:02:57.000000)" } self.logs.addLog(new_log) last_five_logs = self.logs.getLastFiveLogs() self.assertEqual(last_five_logs[0], new_log) def test_addLogs(self): new_logs = [{ "user": "******", "function": "corona_stats", "message": "button", "time": "31-Mar-2020 (11:02:57.000000)" }, { "user": "******", "function": "corona_stats", "message": "button", "time": "31-Mar-2020 (11:02:57.000000)" }] self.logs.addLogs(new_logs) self.assertEqual(self.logs.getLastFiveLogs()[0:2], new_logs) def test_getLastFiveLogs(self): new_log = { "user": "******", "function": "corona_stats", "message": "button", "time": "31-Mar-2020 (11:02:57.000000)" } last_log = { "user": "******", "function": "corona_stats", "message": "button", "time": "01-Apr-2020 (12:20:00.000000)" } ans = [] for _i in range(5): self.logs.addLog(new_log) ans.append(new_log) self.logs.addLog(last_log) ans.append(last_log) self.assertEqual(self.logs.getLastFiveLogs(), ans[1::]) def test_getLastFiveLogs_len(self): self.assertTrue(len(self.logs.getLastFiveLogs()) <= 5) def test_magic_mock_getLastFiveLogs(self): self.logs.getLastFiveLogs = mock.MagicMock(return_value=1) self.assertEqual(self.logs.getLastFiveLogs(), 1)