From 51933bb331d5737c1ae5bc13cf4fd1b5dde3f1a2 Mon Sep 17 00:00:00 2001 From: Desiders Date: Tue, 13 Feb 2024 18:31:20 +0300 Subject: [PATCH] Apply `ruff` and `isort` for `tests` dir --- tests/__init__.py | 1 - tests/karma/common.py | 27 +++++++++-- tests/karma/fixtures/__init__.py | 10 +++- tests/karma/fixtures/fixtures_karma.py | 18 +++---- tests/karma/test_correct_karma_filter.py | 6 +-- tests/karma/test_not_one_word.py | 9 ++-- tests/target/common.py | 1 - tests/target/correct_targets.py | 15 ++++-- tests/target/fixtures/__init__.py | 17 ++++++- tests/target/fixtures/targets.py | 48 +++++++++---------- tests/target/test_auto_target.py | 35 +++++++++----- .../timedelta/fixtures/fixterus_timedelta.py | 4 +- tests/timedelta/test_timedelta_parser.py | 1 + 13 files changed, 124 insertions(+), 68 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 911265c3..deabe5ee 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,5 +2,4 @@ from app.config import load_config - load_config(Path(__file__).parent.parent / "config_dist") diff --git a/tests/karma/common.py b/tests/karma/common.py index e213fb1c..3993636b 100644 --- a/tests/karma/common.py +++ b/tests/karma/common.py @@ -3,11 +3,17 @@ from aiogram import types from app.filters.karma_change import ( - KarmaFilter, PUNCTUATIONS, PLUS_TRIGGERS, PLUS_EMOJI, MINUS_TRIGGERS, - MINUS_EMOJI, PLUS, INF, check, + INF, + MINUS_EMOJI, + MINUS_TRIGGERS, + PLUS, + PLUS_EMOJI, + PLUS_TRIGGERS, + PUNCTUATIONS, + KarmaFilter, + check, ) - plus_texts = (*PLUS_TRIGGERS, *PLUS_EMOJI, PLUS * 2, PLUS * 3, PLUS * 4) minus_texts = (*MINUS_TRIGGERS, *MINUS_EMOJI) punctuations = [*PUNCTUATIONS, ""] @@ -19,6 +25,17 @@ def filter_check(message: types.Message): __all__ = [ - KarmaFilter, PUNCTUATIONS, PLUS_TRIGGERS, PLUS_EMOJI, MINUS_TRIGGERS, MINUS_EMOJI, PLUS, INF, - plus_texts, minus_texts, punctuations, SPACES, filter_check + KarmaFilter, + PUNCTUATIONS, + PLUS_TRIGGERS, + PLUS_EMOJI, + MINUS_TRIGGERS, + MINUS_EMOJI, + PLUS, + INF, + plus_texts, + minus_texts, + punctuations, + SPACES, + filter_check, ] diff --git a/tests/karma/fixtures/__init__.py b/tests/karma/fixtures/__init__.py index 94da0bfe..75f43ba9 100644 --- a/tests/karma/fixtures/__init__.py +++ b/tests/karma/fixtures/__init__.py @@ -2,6 +2,14 @@ generate_phrases_next_word, get_message_with_text, get_next_word_parts, - wrong_generate_phrases_next_word, get_wrong_next_word_parts, + wrong_generate_phrases_next_word, ) + +__all__ = [ + "generate_phrases_next_word", + "get_message_with_text", + "get_next_word_parts", + "get_wrong_next_word_parts", + "wrong_generate_phrases_next_word", +] diff --git a/tests/karma/fixtures/fixtures_karma.py b/tests/karma/fixtures/fixtures_karma.py index 9f350aaf..c4f02d66 100644 --- a/tests/karma/fixtures/fixtures_karma.py +++ b/tests/karma/fixtures/fixtures_karma.py @@ -7,9 +7,7 @@ def get_next_word_parts( - first_word: str, - punctuations: typing.Iterable[str], - spaces: typing.Iterable[str] + first_word: str, punctuations: typing.Iterable[str], spaces: typing.Iterable[str] ) -> typing.List[typing.List[str]]: """ собрать список из списков [first_word, знак препинания, пробельный символ, сгенерированные следующее слово] @@ -23,9 +21,7 @@ def get_next_word_parts( def generate_phrases_next_word( - first_word: str, - punctuations: typing.Iterable[str], - spaces: typing.Iterable[str] + first_word: str, punctuations: typing.Iterable[str], spaces: typing.Iterable[str] ) -> typing.List[str]: """ get string like %karma_trigger%%punctuation%%space%%next_words% @@ -34,7 +30,9 @@ def generate_phrases_next_word( return ["".join(precursors_list) for precursors_list in precursors_lists] -def get_wrong_next_word_parts(first_word: str, punctuations: typing.Iterable[str]) -> typing.List[typing.List[str]]: +def get_wrong_next_word_parts( + first_word: str, punctuations: typing.Iterable[str] +) -> typing.List[typing.List[str]]: """ собрать список из списков [first_word, знак препинания, "", сгенерированные следующее слово] """ @@ -45,14 +43,16 @@ def get_wrong_next_word_parts(first_word: str, punctuations: typing.Iterable[str return rez -def wrong_generate_phrases_next_word(first_word: str, punctuations: typing.Iterable[str]) -> typing.List[str]: +def wrong_generate_phrases_next_word( + first_word: str, punctuations: typing.Iterable[str] +) -> typing.List[str]: """get string like %karma_trigger%%punctuation%%next_words% without spaces""" precursors_lists = get_wrong_next_word_parts(first_word, punctuations) return ["".join(precursors_list) for precursors_list in precursors_lists] def get_next_words(count_symbols: int = 10) -> str: - return ''.join(choice(ascii_letters) for _ in range(count_symbols)) + return "".join(choice(ascii_letters) for _ in range(count_symbols)) def get_message_with_text(text: str) -> types.Message: diff --git a/tests/karma/test_correct_karma_filter.py b/tests/karma/test_correct_karma_filter.py index 8410f3ef..db004f9c 100644 --- a/tests/karma/test_correct_karma_filter.py +++ b/tests/karma/test_correct_karma_filter.py @@ -1,6 +1,6 @@ import pytest -from .common import plus_texts, minus_texts, punctuations, filter_check, SPACES, INF +from .common import INF, SPACES, filter_check, minus_texts, plus_texts, punctuations from .fixtures import generate_phrases_next_word, get_message_with_text @@ -13,7 +13,7 @@ def test_correct_plus(text: str): def check_plus(text_with_plus_trigger: str): msg = get_message_with_text(text_with_plus_trigger) filter_rez = filter_check(msg) - assert filter_rez['karma']['karma_change'] == INF, str(msg) + assert filter_rez["karma"]["karma_change"] == INF, str(msg) @pytest.mark.parametrize("text", minus_texts) @@ -25,4 +25,4 @@ def test_correct_minus(text: str): def check_minus_reply(text_with_minus_trigger: str): msg = get_message_with_text(text_with_minus_trigger) filter_rez = filter_check(msg) - assert filter_rez['karma']['karma_change'] == -INF, str(msg) + assert filter_rez["karma"]["karma_change"] == -INF, str(msg) diff --git a/tests/karma/test_not_one_word.py b/tests/karma/test_not_one_word.py index d980db30..d2e52336 100644 --- a/tests/karma/test_not_one_word.py +++ b/tests/karma/test_not_one_word.py @@ -1,6 +1,9 @@ -from .common import PLUS_TRIGGERS, MINUS_TRIGGERS, punctuations, filter_check -from .fixtures import (wrong_generate_phrases_next_word, get_message_with_text, - generate_phrases_next_word) +from .common import MINUS_TRIGGERS, PLUS_TRIGGERS, filter_check, punctuations +from .fixtures import ( + generate_phrases_next_word, + get_message_with_text, + wrong_generate_phrases_next_word, +) def test_plus(): diff --git a/tests/target/common.py b/tests/target/common.py index b0c37f2e..356d097f 100644 --- a/tests/target/common.py +++ b/tests/target/common.py @@ -5,7 +5,6 @@ from app.filters.has_target import HasTargetFilter - CONF_CAN_BE_SAME = dict(can_be_same=True) CONF_CANT_BE_SAME = dict(can_be_same=False) diff --git a/tests/target/correct_targets.py b/tests/target/correct_targets.py index 269c3159..ad53d045 100644 --- a/tests/target/correct_targets.py +++ b/tests/target/correct_targets.py @@ -1,9 +1,14 @@ import pytest from aiogram import types -from .common import filter_check, CONF_CANT_BE_SAME -from .fixtures import (get_from_user, get_message_with_reply, - get_message_with_text_mention, get_message_with_mention, get_parts) +from .common import CONF_CANT_BE_SAME, filter_check +from .fixtures import ( + get_from_user, + get_message_with_mention, + get_message_with_reply, + get_message_with_text_mention, + get_parts, +) def test_reply_target(): @@ -35,6 +40,8 @@ def check_target(target_user: dict, msg: types.Message): target_user = types.User(**target_user) founded_user = filter_rez["target"] if founded_user.id is None: - assert founded_user.username == target_user.username, f"msg text {{{msg.text}}} user: {{{target_user}}}" + assert ( + founded_user.username == target_user.username + ), f"msg text {{{msg.text}}} user: {{{target_user}}}" else: assert founded_user == target_user, f"msg text {{{msg.text}}} user: {{{target_user}}}" diff --git a/tests/target/fixtures/__init__.py b/tests/target/fixtures/__init__.py index 4f7ec16c..50a31ec1 100644 --- a/tests/target/fixtures/__init__.py +++ b/tests/target/fixtures/__init__.py @@ -1,2 +1,15 @@ -from .targets import get_from_user, get_message_with_reply, \ - get_message_with_text_mention, get_message_with_mention, get_parts +from .targets import ( + get_from_user, + get_message_with_mention, + get_message_with_reply, + get_message_with_text_mention, + get_parts, +) + +__all__ = [ + "get_from_user", + "get_message_with_mention", + "get_message_with_reply", + "get_message_with_text_mention", + "get_parts", +] diff --git a/tests/target/fixtures/targets.py b/tests/target/fixtures/targets.py index c92130ac..2089301d 100644 --- a/tests/target/fixtures/targets.py +++ b/tests/target/fixtures/targets.py @@ -18,7 +18,7 @@ def get_parts() -> typing.List[typing.List[str]]: def get_words(count_symbols: int = 10) -> str: - return ''.join(choice(ascii_letters) for _ in range(count_symbols)) + return "".join(choice(ascii_letters) for _ in range(count_symbols)) def get_message_with_reply(author_user: dict, target_user: dict, text: str) -> types.Message: @@ -32,7 +32,9 @@ def get_message_with_reply(author_user: dict, target_user: dict, text: str) -> t ) -def get_message_with_mention(author_user: dict, target_user: dict, text_precursors: typing.List[str]) -> types.Message: +def get_message_with_mention( + author_user: dict, target_user: dict, text_precursors: typing.List[str] +) -> types.Message: username = get_user_username(target_user) text_precursors[1] = username msg_text = " ".join(text_precursors) @@ -45,46 +47,40 @@ def get_message_with_mention(author_user: dict, target_user: dict, text_precurso ], message_id=1, date=datetime.now(), - chat=types.Chat(id=1, type="group") + chat=types.Chat(id=1, type="group"), ) def get_entity_mention(offset, length) -> types.MessageEntity: - return types.MessageEntity(**{ - "offset": offset, - "length": length, - "type": "mention" - }) + return types.MessageEntity(**{"offset": offset, "length": length, "type": "mention"}) def get_message_with_text_mention( - author_user: dict, - target_user: dict, - text_precursors: typing.List[str] + author_user: dict, target_user: dict, text_precursors: typing.List[str] ) -> types.Message: - first_name = target_user['first_name'] + first_name = target_user["first_name"] text_precursors[1] = first_name msg_text = " ".join(text_precursors) start_entity_pos = len(text_precursors[0]) + 1 # добавляем длину пробела return types.Message( from_user=types.User(**author_user), text=msg_text, - entities=[ - get_entity_text_mention(start_entity_pos, target_user) - ], + entities=[get_entity_text_mention(start_entity_pos, target_user)], message_id=1, date=datetime.now(), - chat=types.Chat(id=1, type="group") + chat=types.Chat(id=1, type="group"), ) def get_entity_text_mention(offset, user: dict) -> types.MessageEntity: - return types.MessageEntity(**{ - "offset": offset, - "length": len(user["first_name"]), - "type": "text_mention", - "user": user - }) + return types.MessageEntity( + **{ + "offset": offset, + "length": len(user["first_name"]), + "type": "text_mention", + "user": user, + } + ) def get_user_username(user: dict) -> str: @@ -102,8 +98,8 @@ def get_reply_message(user_dict): def get_from_user(id_=777, username=None, first_name="Bob"): return { - 'id': id_, - 'username': username, - 'first_name': first_name, - 'is_bot': False, + "id": id_, + "username": username, + "first_name": first_name, + "is_bot": False, } diff --git a/tests/target/test_auto_target.py b/tests/target/test_auto_target.py index b92fd965..1bd5786c 100644 --- a/tests/target/test_auto_target.py +++ b/tests/target/test_auto_target.py @@ -1,9 +1,14 @@ import pytest from aiogram import types -from .common import filter_check, CONF_CAN_BE_SAME, CONF_CANT_BE_SAME -from .fixtures import (get_from_user, get_message_with_reply, - get_message_with_text_mention, get_message_with_mention, get_parts) +from .common import CONF_CAN_BE_SAME, CONF_CANT_BE_SAME, filter_check +from .fixtures import ( + get_from_user, + get_message_with_mention, + get_message_with_reply, + get_message_with_text_mention, + get_parts, +) @pytest.mark.parametrize("phrase", get_parts()) @@ -35,16 +40,22 @@ def check_msg_auto_target(user: dict, msg: types.Message): target_user = types.User(**user) founded_user = filter_rez["target"] if founded_user.id is None: - assert founded_user.username == target_user.username, f"msg text {{{msg.text}}} user: {{{user}}}" + assert ( + founded_user.username == target_user.username + ), f"msg text {{{msg.text}}} user: {{{user}}}" else: - assert are_users_equals(founded_user, target_user), f"msg text {{{msg.text}}} user: {{{user}}}" + assert are_users_equals( + founded_user, target_user + ), f"msg text {{{msg.text}}} user: {{{user}}}" def are_users_equals(expected: types.User, actual: types.User) -> bool: - return all([ - expected.id == actual.id, - expected.is_bot == actual.is_bot, - expected.username == actual.username, - expected.first_name == actual.first_name, - expected.last_name == actual.last_name, - ]) + return all( + [ + expected.id == actual.id, + expected.is_bot == actual.is_bot, + expected.username == actual.username, + expected.first_name == actual.first_name, + expected.last_name == actual.last_name, + ] + ) diff --git a/tests/timedelta/fixtures/fixterus_timedelta.py b/tests/timedelta/fixtures/fixterus_timedelta.py index 1e432008..521dc15e 100644 --- a/tests/timedelta/fixtures/fixterus_timedelta.py +++ b/tests/timedelta/fixtures/fixterus_timedelta.py @@ -13,7 +13,9 @@ CORRECT_TUPLE_TYPE = typing.Tuple[str, timedelta] -def get_difficult_correct(count: int = 10, count_in_one_up_to: int = 3) -> typing.List[CORRECT_TUPLE_TYPE]: +def get_difficult_correct( + count: int = 10, count_in_one_up_to: int = 3 +) -> typing.List[CORRECT_TUPLE_TYPE]: return [get_many_correct(i) for _ in range(count) for i in range(2, count_in_one_up_to)] diff --git a/tests/timedelta/test_timedelta_parser.py b/tests/timedelta/test_timedelta_parser.py index 370a53b0..5641a22f 100644 --- a/tests/timedelta/test_timedelta_parser.py +++ b/tests/timedelta/test_timedelta_parser.py @@ -1,4 +1,5 @@ from app.utils.timedelta_functions import parse_timedelta + from .fixtures import CORRECT_SIMPLE, get_difficult_correct