Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
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)