Coverage for tests/test_spell.py: 100%
33 statements
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-21 23:45 +0000
« prev ^ index » next coverage.py v7.10.4, created at 2025-08-21 23:45 +0000
1import typing
3import pytest
5from tests._fixtures import COMMON_TEXT_MESSAGE
6from tests.test_spell_views import RU_LANG
7from whole_app import models
8from whole_app.settings import SETTINGS
9from whole_app.spell import SpellCheckService
12@pytest.mark.parametrize(
13 (
14 "text_input",
15 "expected_corrections",
16 ),
17 [
18 (
19 "Превед медвет",
20 [
21 ("Превед", 0, 6, None),
22 ("медвет", 7, 13, "медведь"),
23 ],
24 ),
25 (
26 "превет как дила",
27 [
28 ("превет", 0, 6, "привет"),
29 ("дила", 11, 15, "дела"),
30 ],
31 ),
32 ],
33)
34def test_correct_spell(
35 text_input: str,
36 expected_corrections: list[tuple[str, int, int, str | None]],
37) -> None:
38 fake_engine: SpellCheckService = SpellCheckService()
39 corrections = fake_engine.prepare(
40 models.SpellCheckRequest(text=text_input, language=RU_LANG),
41 ).run_check()
42 assert len(corrections) == len(expected_corrections)
43 for one_correction, (word, first_position, last_position, suggestion) in zip(
44 corrections,
45 expected_corrections, strict=False,
46 ):
47 assert one_correction.first_position == first_position
48 assert one_correction.last_position == last_position
49 assert one_correction.word == word
50 assert text_input[first_position:last_position] == word
51 if suggestion is None:
52 assert one_correction.suggestions
53 else:
54 assert suggestion in one_correction.suggestions
57@pytest.mark.parametrize(
58 "url",
59 [
60 "www.rzb.ru",
61 "https://rzb.ru",
62 "https://www.rzb.ru",
63 "rzb.ru/taCWpO",
64 "www.rzb.ru/taCWpO",
65 "https://rzb.ru/taCWpO",
66 "https://www.rzb.ru/taCWpO",
67 "https://www.asd.google.com/search?q=some+text¶m=3#dfsdf",
68 "https://www.google.com",
69 "http://google.com/?q=some+text¶m=3#dfsdf",
70 "https://www.google.com/api/?",
71 "https://www.google.com/api/login.php",
72 "https://r-chat.raiffeisen.ru/admin/operator/",
73 "https://r-chat.raiffeisen.ru/admin/operator/taCWpO",
74 ],
75)
76def test_urls_ignored(
77 url: str,
78) -> None:
79 fake_engine: SpellCheckService = SpellCheckService()
80 corrections = fake_engine.prepare(
81 models.SpellCheckRequest(text=COMMON_TEXT_MESSAGE.format(url), language="ru_RU", exclude_urls=True),
82 ).run_check()
83 assert not corrections
86@pytest.mark.parametrize(
87 ("wannabe_user_input", "excluded_words"),
88 [("ШЯЧЛО ПОПЯЧТСА ПОПЯЧТСА", {"шячло", "попячтса"})],
89)
90def test_default_excluded_words(
91 wannabe_user_input: str,
92 excluded_words: str,
93 monkeypatch: typing.Any,
94) -> None:
95 with monkeypatch.context() as patcher:
96 patcher.setattr(SETTINGS, "_exclusion_words_set", excluded_words)
97 fake_engine: SpellCheckService = SpellCheckService()
98 prepared = fake_engine.prepare(
99 models.SpellCheckRequest(text=wannabe_user_input, language=RU_LANG, exclude_urls=False),
100 )
102 corrections = prepared.run_check()
103 assert corrections == [], f"{corrections=} --- {prepared._exclusion_words=}" # noqa: SLF001