From 1d111f003f275fce48763d0b533abd1a8bf52329 Mon Sep 17 00:00:00 2001 From: Michal Rudowicz Date: Thu, 13 Jun 2024 18:24:19 +0200 Subject: [PATCH] Initial test --- tests.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/tests.py b/tests.py index 0dc2b94..92b0682 100644 --- a/tests.py +++ b/tests.py @@ -1,10 +1,11 @@ #!/usr/bin/env python import unittest -from unittest.mock import Mock +from unittest.mock import MagicMock import os import re import sys +from dataclasses import dataclass fakes_dir = os.path.join(os.path.dirname(__file__), 'fakes') assert(os.path.exists(fakes_dir)) sys.path.insert(0, fakes_dir) @@ -16,12 +17,49 @@ MESSAGE_ID = "Message ID" BOT_SPEC = ["kick_chat_member", "delete_message"] +@dataclass +class Chat: + ban_member = MagicMock() + effective_name = "effective_chat_name" + + +@dataclass +class User: + id = 0 + name = "User name" + + +@dataclass +class Message: + text = "Message text" + delete = MagicMock() + chat = Chat() + from_user = User() + + +@dataclass +class Update: + message = Message() + + +def make_update(msg: str) -> Update: + update = Update() + update.message.text = msg + return update + + class TestAutoBanBot(unittest.IsolatedAsyncioTestCase): def setUp(self): - self.regexes = map(lambda r: re.compile(r, re.I), ["test123", "(t\.me\/|\@)[a-z]+bot"]) + self.regexes = list(map(lambda r: re.compile(r, re.I), ["test123", "(t\\.me\\/|\\@)[a-z]+bot"])) async def test_not_bannable(self): - await autobanbot.new_msg(None, None, self.regexes) + messages = ["not bannable message", "siematest_123elo", "i am not a bot", "t.me, i'm not a bot"] + for msg in messages: + with self.subTest(msg=msg): + update = make_update(msg) + await autobanbot.new_msg(update, None, self.regexes) + update.message.delete.assert_not_called() + update.message.chat.ban_member.assert_not_called() if __name__ == '__main__':