/
dracat
/
chat_assembler
Обзор
Документация
Войти
/
dracat
/
chat_assembler
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
tests/test_utils.py
321 строка
8 KB
Марченков Сергей Анатольевич
Иэрархический вывод сообщений в md
31 янв 2026, 04:36
31 янв 2026, 04:36
9c48888
Код
Авторство
О чём код?
import sys import os from pathlib import Path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import pytest from chat_assembler.utils import merge_lines_prefer_new, string_to_lines as s2l, human_readable_file_size_in_bytes, sanitize_markdown_text def test_no_changes(): lines1 = s2l(""" a b c """) lines2 = s2l(""" a b c """) assert merge_lines_prefer_new(lines1, lines2) == lines2 def test_addition_in_new(): lines1 = s2l(""" a c """) lines2 = s2l(""" a b c """) assert merge_lines_prefer_new(lines1, lines2) == lines2 def test_deletion_in_new(): lines1 = s2l(""" a b c """) lines2 = s2l(""" a c """) assert merge_lines_prefer_new(lines1, lines2) == lines1 def test_preserve_user_additions_simple(): lines1 = s2l(""" a user_comment b """) lines2 = s2l(""" a b """) expected = s2l(""" a user_comment b """) assert merge_lines_prefer_new(lines1, lines2) == expected def test_preserve_user_additions_multiple_lines(): lines1 = s2l(""" a comment1 comment2 b """) lines2 = s2l(""" a b c """) expected = s2l(""" a comment1 comment2 b c """) assert merge_lines_prefer_new(lines1, lines2) == expected def test_modification(): lines1 = s2l(""" a b_old c """) lines2 = s2l(""" a b_new c """) assert merge_lines_prefer_new(lines1, lines2) == lines2 def test_mixed_changes(): lines1 = s2l(""" line1 comment1 line11 line2_old line22 comment2 line3 """) lines2 = s2l(""" line1 line11 line2_new line22 line3 line4_new """) expected = s2l(""" line1 comment1 line11 line2_new line22 comment2 line3 line4_new """) assert merge_lines_prefer_new(lines1, lines2) == expected # @pytest.mark.skip(reason="Test requires local files not present in the repository") # def test_wtf1(): # lines1 = s2l(Path('/mnt/work6/telegram/telegram_data/channel/ИСП_РАН___Env/General/2021-01-chat.md').read_text()) # lines2 = s2l(Path('/mnt/work6/telegram/telegram_data/tmp-new.md').read_text()) # reslines = merge_lines_prefer_new(lines1, lines2) # assert reslines == lines1 def test_empty_files_both_empty(): lines1 = s2l("") lines2 = s2l("") assert merge_lines_prefer_new(lines1, lines2) == lines2 def test_empty_files_old_empty(): lines1 = s2l("") lines2 = s2l("b") assert merge_lines_prefer_new(lines1, lines2) == lines2 def test_empty_files_new_empty(): lines1 = s2l("a") lines2 = s2l("") assert merge_lines_prefer_new(lines1, lines2) == lines1 def test_user_content_at_ends(): lines1 = s2l(""" comment_start a b comment_end """) lines2 = s2l(""" a b """) expected = s2l(""" comment_start a b comment_end """) assert merge_lines_prefer_new(lines1, lines2) == expected @pytest.mark.parametrize("input_str, expected_bytes", [ ("1K", 1024), ("5M", 5 * 1024 * 1024), ("7G", 7 * 1024 * 1024 * 1024), ("2.5k", int(2.5 * 1024)), ("1024", 1024), (" 1G ", 1024**3), ("1GB", 1024**3), ("", 0), ("0", 0), ]) def test_human_readable_file_size_in_bytes_valid(input_str, expected_bytes): assert human_readable_file_size_in_bytes(input_str) == expected_bytes @pytest.mark.parametrize("invalid_input", [ "1X", "abc", "1MK", ]) def test_human_readable_file_size_in_bytes_invalid(invalid_input): with pytest.raises(ValueError): human_readable_file_size_in_bytes(invalid_input) @pytest.mark.parametrize("char_code", ['\u200e', '\u200f']) def test_sanitize_markdown_text_removes_bidi_marks(char_code): text_with_mark = f"Hello{char_code} World" expected_text = "Hello World" assert sanitize_markdown_text(text_with_mark) == expected_text # Find all test case directories in the examples directory EXAMPLE_DIR = Path(__file__).parent / "sanitize_examples" TEST_DIRS = sorted([d for d in EXAMPLE_DIR.iterdir() if d.is_dir()]) if not TEST_DIRS: raise RuntimeError(f"No test case directories found in {EXAMPLE_DIR}") @pytest.mark.parametrize("test_dir", TEST_DIRS, ids=[d.name for d in TEST_DIRS]) def test_sanitize_markdown_text(test_dir): """ Перебираем все каталоги в `test_dir` отсортированные алфавитно - Если есть в каталоге `data.json` и `data.md` - считываем `data.json`, берем message из поля "message" - сравниваем `sanitize_markdown(message)` с содержимым файла `data.md` - без учета конечного вайтспейса и переводов строк """ md_path = test_dir / "data.md" json_path = test_dir / "data.json" if not md_path.exists() or not json_path.exists(): pytest.skip(f"Missing data.md or data.json in {test_dir}") import json json_data = json.loads(json_path.read_text()) input_message = json_data.get("message") expected_markdown = md_path.read_text() actual_markdown = sanitize_markdown_text(input_message) assert actual_markdown.strip() == expected_markdown.strip() def test_sanitize_markdown_text_adds_newline_before_setext_separators(): # Test for '---' text_with_separator = "Some text\n---\nMore text" expected_text = "Some text\n\n---\nMore text" assert sanitize_markdown_text(text_with_separator) == expected_text text_with_separator_and_space = "Some text\n\n---\nMore text" assert sanitize_markdown_text(text_with_separator_and_space) == text_with_separator_and_space text_with_long_separator = "Some text\n-----\nMore text" expected_long_separator = "Some text\n\n-----\nMore text" assert sanitize_markdown_text(text_with_long_separator) == expected_long_separator # Test for '===' text_with_equals = "Some text\n===\nMore text" expected_equals = "Some text\n\n===\nMore text" assert sanitize_markdown_text(text_with_equals) == expected_equals text_with_equals_and_space = "Some text\n\n===\nMore text" assert sanitize_markdown_text(text_with_equals_and_space) == text_with_equals_and_space text_with_short_equals = "Some text\n==\nMore text" expected_short_equals = "Some text\n\n==\nMore text" assert sanitize_markdown_text(text_with_short_equals) == expected_short_equals # Reply formatting tests from chat_assembler.utils import format_reply_message def test_format_reply_message(): """Test reply message formatting""" # Mock messages all_messages = { 137: { "message": "Кстати, вы всем офисом уезжаете?", "sender": {"username": "Sakrat91", "id": 169164243} } } # Reply message reply_msg = { "id": 140, "message": "да не, леста-то никуда не денется", "sender": {"username": "lanvin_s", "id": 124114202}, "reply_to": {"reply_to_msg_id": 137} } result = format_reply_message(reply_msg, all_messages) expected = "да не, леста-то никуда не денется" assert result == expected def test_format_reply_message_no_original(): """Test reply when original message doesn't exist""" all_messages = {} reply_msg = { "id": 140, "message": "да не, леста-то никуда не денется", "sender": {"username": "lanvin_s", "id": 124114202}, "reply_to": {"reply_to_msg_id": 999} } result = format_reply_message(reply_msg, all_messages) expected = "да не, леста-то никуда не денется" assert result == expected def test_format_reply_message_multiline(): """Test reply with multiline original message""" all_messages = { 37: { "message": "Думаю, после первых встреч станет понятнее, куда эта история может привести. \n\nГлавное набросать небольшой пул правил, чтобы было проще.", "sender": {"username": "VErmakova", "id": 519704541} } } reply_msg = { "id": 39, "message": "Согласен на все 100%", "sender": {"username": "nvandreev", "id": 49236267}, "reply_to": {"reply_to_msg_id": 37} } result = format_reply_message(reply_msg, all_messages) expected = "Согласен на все 100%" assert result == expected