<commit_before>import sys, os
sys.path.append(os.path.abspath("."))

import asyncio, unittest, logging, time


# Optimize testing
import utils

async def parse_user_name(user_id, entity):
    return str(user_id)

async def parse_user_id(msg, can_be_argument=True, argument_ind=-1, custom_text=None):
    text = msg.text.split(" ")[argument_ind]

    if text.isdigit():
        return int(text)

    if text.startswith("https://vk.com/"):
        text = text[15:]

    if text[:3] == "[id":
        puid = text[3:].split("|")[0]

        if puid.isdigit() and "]" in text[3:]:
            return int(puid)

utils.parse_user_name = parse_user_name
utils.parse_user_id = parse_user_id
# ----------------


from bot import Bot
from plugins import *
from utils import *

from settings_base import BaseSettings

try:
    from settings_prod import BotSettings
except ImportError:
    from settings import BotSettings
    BotSettings.USERS = ()

# BotSettings.DEBUG = True

# Install tokens for tests if provided
test_group_token = os.environ.get('SKETAL_GROUP_TOKEN', '')
if test_group_token and not int(os.environ.get('SKETAL_GROUP_IGNORE', 0)):
    BotSettings.USERS += (
        ("group", test_group_token,),
    )

test_user_token = os.environ.get('SKETAL_USER_TOKEN', '')
if test_user_token and not int(os.environ.get('SKETAL_USER_IGNORE', 0)):
    BotSettings.USERS += (
        ("user", test_user_token,),
    )

class TestSketal(unittest.TestCase):
    # def setUp(self):
    #     self.startTime = time.time()
    #
    # def tearDown(self):
    #     t = time.time() - self.startTime
    #     print("%s: %.3f" % (self.id(), t))

    @classmethod
    def setUpClass(cls):
        required = [AntifloodPlugin, TimePlugin]

        for p in BotSettings.PLUGINS[::]:
            for r in required[::]:
                if isinstance(p, r):
                    required.remove(r)

        for r in required:
            BotSettings.PLUGINS = BotSettings.PLUGINS + r()

        cls.bot = Bot(BotSettings)
        cls.user_id = cls.bot.api.get_current_id()

    def test_loading(self):
        logger = logging.getLogger()

        with self.assertLogs(logger, level='INFO') as cm:
            self.bot = Bot(BotSettings, logger=logger)

        self.assertIn(f'INFO:{self.bot.logger.name}:Initializing bot', cm.output)
        self.assertIn(f'INFO:{self.bot.logger.name}:Initializing vk clients', cm.output)
        self.assertIn(f'INFO:{self.bot.logger.name}:Loading plugins', cm.output)

    def test_process(self):
        m = Message(self.bot.api, MessageEventData.from_message_body({
            "id": 1, "date": 0,
            "user_id": self.user_id,
            "body": "Привет! Просто сообщение.",
            "random_id": 0, "read_state": 1, "title": "", "out": 0
        }))

        self.bot.coroutine_exec(self.bot.handler.process(m))

    def test_plugins(self):
        """Requires TimePlugin and AntifloodPlugin."""

        messages = []

        _answer = Message.answer
        async def answer(self, message="", **kwargs):
            messages.append(message)
        Message.answer = answer

        m = Message(self.bot.api, MessageEventData.from_message_body({
            "id": 1,
            "date": 0,
            "user_id": self.user_id,
            "body": "/время",
            "random_id": 0, "read_state": 1, "title": "", "out": 0
        }))

        self.bot.coroutine_exec(self.bot.handler.process(m))

        self.assertIn("Текущие дата и время по МСК", messages[0])

        self.bot.coroutine_exec(self.bot.handler.process(m))

        self.assertEqual(len(messages), 1)

        Message.answer = _answer

    def test_methods(self):
        """Requires at least 1 message in dialogs on account."""

        result = self.bot.coroutine_exec(self.bot.api.groups.getById(group_id=1))
        self.assertNotIn(result, (False, None))

        count = 2
        result = self.bot.coroutine_exec(self.bot.api.messages.get(count=count))
        self.assertNotIn(result, (False, None))
        self.assertIn("items", result)
        self.assertEqual(len(result['items']), count)

        result = self.bot.coroutine_exec(self.bot.api(wait="custom").messages.get(count=1))
        self.assertEqual(asyncio.isfuture(result), True)
        self.bot.loop.run_until_complete(result)
        self.assertEqual(result.done(), True)
        self.assertNotIn(result.result(), (False, None))

    def test_longpoll(self):
        task = self.bot.longpoll_run(True)

        async def bot_stopper():
            await asyncio.sleep(1.5)
            await self.bot.stop_tasks()

        asyncio.ensure_future(bot_stopper(), loop=self.bot.loop)

        with self.assertLogs(self.bot.logger, level='INFO') as cm:<commit_msg>removed test<commit_after>import sys, os
sys.path.append(os.path.abspath("."))

import asyncio, unittest, logging, time


# Optimize testing
import utils

async def parse_user_name(user_id, entity):
    return str(user_id)

async def parse_user_id(msg, can_be_argument=True, argument_ind=-1, custom_text=None):
    text = msg.text.split(" ")[argument_ind]

    if text.isdigit():
        return int(text)

    if text.startswith("https://vk.com/"):
        text = text[15:]

    if text[:3] == "[id":
        puid = text[3:].split("|")[0]

        if puid.isdigit() and "]" in text[3:]:
            return int(puid)

utils.parse_user_name = parse_user_name
utils.parse_user_id = parse_user_id
# ----------------


from bot import Bot
from plugins import *
from utils import *

from settings_base import BaseSettings

try:
    from settings_prod import BotSettings
except ImportError:
    from settings import BotSettings
    BotSettings.USERS = ()

# BotSettings.DEBUG = True

# Install tokens for tests if provided
test_group_token = os.environ.get('SKETAL_GROUP_TOKEN', '')
if test_group_token and not int(os.environ.get('SKETAL_GROUP_IGNORE', 0)):
    BotSettings.USERS += (
        ("group", test_group_token,),
    )

test_user_token = os.environ.get('SKETAL_USER_TOKEN', '')
if test_user_token and not int(os.environ.get('SKETAL_USER_IGNORE', 0)):
    BotSettings.USERS += (
        ("user", test_user_token,),
    )

class TestSketal(unittest.TestCase):
    # def setUp(self):
    #     self.startTime = time.time()
    #
    # def tearDown(self):
    #     t = time.time() - self.startTime
    #     print("%s: %.3f" % (self.id(), t))

    @classmethod
    def setUpClass(cls):
        required = [AntifloodPlugin, TimePlugin]

        for p in BotSettings.PLUGINS[::]:
            for r in required[::]:
                if isinstance(p, r):
                    required.remove(r)

        for r in required:
            BotSettings.PLUGINS = BotSettings.PLUGINS + r()

        cls.bot = Bot(BotSettings)
        cls.user_id = cls.bot.api.get_current_id()

    def test_loading(self):
        logger = logging.getLogger()

        with self.assertLogs(logger, level='INFO') as cm:
            self.bot = Bot(BotSettings, logger=logger)

        self.assertIn(f'INFO:{self.bot.logger.name}:Initializing bot', cm.output)
        self.assertIn(f'INFO:{self.bot.logger.name}:Initializing vk clients', cm.output)
        self.assertIn(f'INFO:{self.bot.logger.name}:Loading plugins', cm.output)

    def test_process(self):
        m = Message(self.bot.api, MessageEventData.from_message_body({
            "id": 1, "date": 0,
            "user_id": self.user_id,
            "body": "Привет! Просто сообщение.",
            "random_id": 0, "read_state": 1, "title": "", "out": 0
        }))

        self.bot.coroutine_exec(self.bot.handler.process(m))

    def test_plugins(self):
        """Requires TimePlugin and AntifloodPlugin."""

        messages = []

        _answer = Message.answer
        async def answer(self, message="", **kwargs):
            messages.append(message)
        Message.answer = answer

        m = Message(self.bot.api, MessageEventData.from_message_body({
            "id": 1,
            "date": 0,
            "user_id": self.user_id,
            "body": "/время",
            "random_id": 0, "read_state": 1, "title": "", "out": 0
        }))

        self.bot.coroutine_exec(self.bot.handler.process(m))

        self.assertIn("Текущие дата и время по МСК", messages[0])

        self.bot.coroutine_exec(self.bot.handler.process(m))

        self.assertEqual(len(messages), 1)

        Message.answer = _answer

    def test_longpoll(self):
        task = self.bot.longpoll_run(True)

        async def bot_stopper():
            await asyncio.sleep(1.5)
            await self.bot.stop_tasks()

        asyncio.ensure_future(bot_stopper(), loop=self.bot.loop)

        with self.assertLogs(self.bot.logger, level='INFO') as cm:
{'fraction_non_alphanumeric': 0.1402642159678346, 'fraction_numerical': 0.005973578403216542, 'mean_word_length': 2.650314465408805, 'pattern_counts': {'":': 34, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2524426', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2827, 'n_words': 563}