Skip to content

Commit

Permalink
Apply ruff and isort for tests dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Desiders committed Feb 13, 2024
1 parent f85ddd9 commit 51933bb
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 68 deletions.
1 change: 0 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

from app.config import load_config


load_config(Path(__file__).parent.parent / "config_dist")
27 changes: 22 additions & 5 deletions tests/karma/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""]
Expand All @@ -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,
]
10 changes: 9 additions & 1 deletion tests/karma/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
18 changes: 9 additions & 9 deletions tests/karma/fixtures/fixtures_karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, знак препинания, пробельный символ, сгенерированные следующее слово]
Expand All @@ -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%
Expand All @@ -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, знак препинания, "", сгенерированные следующее слово]
"""
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions tests/karma/test_correct_karma_filter.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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)
Expand All @@ -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)
9 changes: 6 additions & 3 deletions tests/karma/test_not_one_word.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
1 change: 0 additions & 1 deletion tests/target/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 11 additions & 4 deletions tests/target/correct_targets.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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}}}"
17 changes: 15 additions & 2 deletions tests/target/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
]
48 changes: 22 additions & 26 deletions tests/target/fixtures/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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,
}
35 changes: 23 additions & 12 deletions tests/target/test_auto_target.py
Original file line number Diff line number Diff line change
@@ -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())
Expand Down Expand Up @@ -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,
]
)
4 changes: 3 additions & 1 deletion tests/timedelta/fixtures/fixterus_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]


Expand Down
1 change: 1 addition & 0 deletions tests/timedelta/test_timedelta_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from app.utils.timedelta_functions import parse_timedelta

from .fixtures import CORRECT_SIMPLE, get_difficult_correct


Expand Down

0 comments on commit 51933bb

Please sign in to comment.