- вынес token/chat/update логику в services - вынес диалог и текст инструкции в ui - добавил и обновил тесты для нового слоя
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
import unittest
|
|
import importlib.util
|
|
from types import SimpleNamespace
|
|
from pathlib import Path
|
|
|
|
_SPEC = importlib.util.spec_from_file_location(
|
|
"chat_actions",
|
|
Path("services/chat_actions.py"),
|
|
)
|
|
_MODULE = importlib.util.module_from_spec(_SPEC)
|
|
_SPEC.loader.exec_module(_MODULE)
|
|
load_chat_conversations = _MODULE.load_chat_conversations
|
|
resolve_user_ids = _MODULE.resolve_user_ids
|
|
|
|
|
|
class ChatActionsTests(unittest.TestCase):
|
|
def test_resolve_user_ids_mixed_results(self):
|
|
mapping = {
|
|
"id1": {"type": "user", "object_id": 1},
|
|
"id2": {"type": "group", "object_id": 2},
|
|
}
|
|
|
|
def call_with_retry(func, **kwargs):
|
|
return func(**kwargs)
|
|
|
|
def resolve_screen_name(screen_name):
|
|
if screen_name == "boom":
|
|
raise RuntimeError("boom")
|
|
return mapping.get(screen_name)
|
|
|
|
vk_api = SimpleNamespace(utils=SimpleNamespace(resolveScreenName=resolve_screen_name))
|
|
links = [
|
|
"https://vk.com/id1",
|
|
"https://vk.com/id2",
|
|
"https://vk.com/boom",
|
|
"https://vk.com/",
|
|
]
|
|
resolved, failed = resolve_user_ids(call_with_retry, vk_api, links)
|
|
|
|
self.assertEqual(resolved, [1])
|
|
self.assertEqual(len(failed), 3)
|
|
self.assertEqual(failed[0][0], "https://vk.com/id2")
|
|
self.assertIsNone(failed[0][1])
|
|
|
|
def test_load_chat_conversations_paginated(self):
|
|
pages = [
|
|
{"items": [{"id": 1}], "next_from": "page-2"},
|
|
{"items": [{"id": 2}]},
|
|
]
|
|
|
|
def get_conversations(**kwargs):
|
|
if kwargs.get("start_from") == "page-2":
|
|
return pages[1]
|
|
return pages[0]
|
|
|
|
def call_with_retry(func, **kwargs):
|
|
return func(**kwargs)
|
|
|
|
vk_api = SimpleNamespace(messages=SimpleNamespace(getConversations=get_conversations))
|
|
items = load_chat_conversations(call_with_retry, vk_api)
|
|
self.assertEqual(items, [{"id": 1}, {"id": 2}])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|