-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5662ff8
commit d286e4a
Showing
17 changed files
with
460 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pytest | ||
|
||
from models import create_tables, db, drop_tables | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def test_db(): | ||
create_tables() | ||
|
||
yield db | ||
|
||
drop_tables() | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=True) | ||
def _rollback_transactions(test_db): | ||
test_db.begin() | ||
|
||
yield | ||
|
||
test_db.rollback() | ||
|
||
@pytest.fixture | ||
def mock_message(mocker): | ||
return mocker.patch("telegram.Message", autospec=True).return_value |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from typing import Callable | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def do_filter(filter_obj) -> Callable[[], bool]: | ||
return lambda message: filter_obj.filter(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pytest | ||
|
||
from filters import ChatMessageOnly | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def filter_obj(): | ||
return ChatMessageOnly() | ||
|
||
|
||
def test_false_if_forwarded(do_filter, mock_message): | ||
mock_message.forward_from_message_id = "ordinary-id-yep" | ||
|
||
assert do_filter(mock_message) is False | ||
|
||
|
||
def test_true_if_not_forwarded(do_filter, mock_message): | ||
mock_message.forward_from_message_id = None | ||
|
||
assert do_filter(mock_message) is True |
48 changes: 48 additions & 0 deletions
48
tests/tests_filters/test_cointains_three_or_more_emojis.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import pytest | ||
|
||
from filters import ContainsThreeOrMoreEmojies | ||
|
||
|
||
@pytest.fixture | ||
def message(mock_message): | ||
mock_message.text = None | ||
return mock_message | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def filter_obj(): | ||
return ContainsThreeOrMoreEmojies() | ||
|
||
|
||
def test_false_if_empty_message(do_filter, message): | ||
assert do_filter(message) is False | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text", | ||
[ | ||
"Shalom 👋🏾", | ||
"Ou ui 👀🙃", | ||
"No emojis actually", | ||
"🐍", | ||
" ", | ||
] | ||
) | ||
def test_false_if_less_than_3_emojis(do_filter, message, text): | ||
message.text = text | ||
|
||
assert do_filter(message) is False | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text", | ||
[ | ||
"Shalom 👋🏾👀🙃", | ||
"😅😎🧑🏿🦱👨👨👧👧", | ||
"😅😎🧑🏿🦱👨👨👧👧🐍 some text 👋🏾👀🙃", | ||
] | ||
) | ||
def test_true_if_more_than_2_emojis(do_filter, message, text): | ||
message.text = text | ||
|
||
assert do_filter(message) is True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import pytest | ||
|
||
from filters import ContainsLink | ||
|
||
|
||
class FakeMessageEntity: | ||
def __init__(self, type: str): | ||
self.type = type | ||
|
||
|
||
@pytest.fixture | ||
def mock_message_entity(mocker): | ||
return lambda type_str: FakeMessageEntity(type_str) | ||
|
||
|
||
@pytest.fixture | ||
def message(mock_message, mock_message_entity): | ||
# To see all possible types look at telegram.MessageEntity Attributes | ||
message.text = "I'm not empty inside" | ||
code = mock_message_entity("code") | ||
phone_number = mock_message_entity("phone_number") | ||
mock_message.entities = [code, phone_number] | ||
return mock_message | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def filter_obj(): | ||
return ContainsLink() | ||
|
||
|
||
def test_false_if_no_links_message(do_filter, message): | ||
assert do_filter(message) is False | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"link_type", | ||
[ | ||
"url", | ||
"text_link", | ||
] | ||
) | ||
def test_true_if_has_link(do_filter, message, mock_message_entity, link_type): | ||
message_entity = mock_message_entity(link_type) | ||
message.entities.append(message_entity) | ||
|
||
assert do_filter(message) is True | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"link_types", | ||
[ | ||
["text_link", "url"], | ||
["text_link", "text_link"], | ||
["url", "url"], | ||
] | ||
) | ||
def test_true_if_has_many_links(do_filter, message, mock_message_entity, link_types): | ||
for link_type in link_types: | ||
message_entity = mock_message_entity(link_type) | ||
message.entities.append(message_entity) | ||
|
||
assert do_filter(message) is True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest | ||
|
||
from filters import ContainsTelegramContact | ||
|
||
|
||
@pytest.fixture | ||
def message(mock_message): | ||
message.text = "Ordinary text" | ||
return mock_message | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def filter_obj(): | ||
return ContainsTelegramContact() | ||
|
||
|
||
def test_false_if_no_text_message(do_filter, message): | ||
message.text = None | ||
assert do_filter(message) is False | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text", | ||
[ | ||
"Hello there!", | ||
"OMG look at my email [email protected]", | ||
"sobaka@sobaka", | ||
] | ||
) | ||
def test_false_if_no_contact(do_filter, message, text): | ||
message.text = text | ||
|
||
assert do_filter(message) is False | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"text", | ||
[ | ||
"write me a message @bbqomg", | ||
"@contact_me", | ||
] | ||
) | ||
def test_true_if_contact(do_filter, message, text): | ||
message.text = text | ||
|
||
assert do_filter(message) is True |
Oops, something went wrong.