Skip to content

Commit

Permalink
first tests for parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavi committed Mar 11, 2024
1 parent 11e7052 commit 26a5d23
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 7 deletions.
9 changes: 7 additions & 2 deletions mastofeed/lib/mentions_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mastofeed.lib.publisher import Publisher
from definitions import ROOT_DIR
from slugify import slugify
from bs4 import BeautifulSoup as bs4
from bs4 import BeautifulSoup
import re


Expand Down Expand Up @@ -103,7 +103,7 @@ def parse(self) -> bool:
raise RuntimeError("Load the mention successfully before trying to parse it")

# Before anything, remove the HTML stuff
content = bs4(self.mention.content, features="html.parser").get_text()
content = BeautifulSoup(self.mention.content, features="html.parser").get_text()

# Removing the self username from the mention, so we have a clean string to parse
username_position, content = self.remove_self_username_from_content(content=content)
Expand All @@ -127,6 +127,11 @@ def parse(self) -> bool:
# We work by words, so split the mentionby spaces:
words = content.split()

# If the mention was empty, behave polite like an organinc mention.
if len(words) == 0:
self.error = self.ERROR_NO_COMMAND
return False

# First word must be the verb/action, from the valid ones
self.action = MentionAction.valid_or_none(words.pop(0))
self._logger.debug(f"Got an action: {self.action}")
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ markers = ["slow"]
pythonpath = [
"."
]
filterwarnings = [
"ignore::bs4.MarkupResemblesLocatorWarning"
]

[tool.yapf]
column_limit = 96
Expand Down
118 changes: 113 additions & 5 deletions tests/lib/test_mentions_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,7 @@ def test_get_text_inside_quotes(content, expected_result):
"add https://xavier.arnaus.net/blog alias \"Xavi's blog\""
),
("Ola @feeder k ase", 4, "Ola k ase"),
(
"@[email protected] list",
0,
"list"
),
("@[email protected] list", 0, "list"),
("Ola @[email protected] k ase", 4, "Ola k ase"),
("Ola k ase", -1, "Ola k ase"),
],
Expand All @@ -186,3 +182,115 @@ def test_remove_self_username_from_content(content, expected_position, expected_
expected_position, expected_content
)


@pytest.mark.parametrize(
argnames=(
'content',
'action',
'complements',
'error',
'returned',
'is_a_valid_feed',
'findfeeds',
"username"
),
argvalues=[
# An example of a correct full normal one
(
"@feeder add https://xavier.arnaus.net/blog xavi \"Xavi's blog\"",
MentionAction.ADD,
{
"alias": "xavi",
"site_url": "https://xavier.arnaus.net/blog",
"feed_url": "https://xavier.arnaus.net/blog.rss",
"name": "Xavi's blog"
},
None, # No error
True, # Parse returns true
False, # The site_url is not a valid feed itself
["https://xavier.arnaus.net/blog.rss", "https://xavier.arnaus.net/blog.atom"],
"@[email protected]" # Who is actually mentioning
),
# Content comes with HTML, even invalid, once cleaned has an action
(
"<p><small>@feeder</small> <strong>list</strong>",
MentionAction.LIST,
{}, # No complements
None, # No error
True, # Parse returns true
False, # The site_url is not a valid feed itself
[], # No Feeds found
"@[email protected]" # Who is actually mentioning
),
# Content comes with HTML, once cleaned does not have an action
(
"<p><small>@feeder</small> <strong>unknown</strong>",
None, # No action
{}, # No complements
MentionParser.ERROR_INVALID_ACTION, # No action error
False, # Parse returns false
False, # The site_url is not a valid feed itself
[], # No Feeds found
"@[email protected]" # Who is actually mentioning
),
# It's an organic mention, does not mean an action
(
"I am working in my @feeder project",
None, # No action
{}, # No complements
MentionParser.ERROR_NO_COMMAND, # No command error
False, # Parse returns false
False, # The site_url is not a valid feed itself
[], # No Feeds found
"@[email protected]" # Who is actually mentioning
),
# The mention does not come with any other word
(
"@feeder",
None, # No action
{}, # No complements
MentionParser.ERROR_NO_COMMAND, # No command error
False, # Parse returns false
False, # The site_url is not a valid feed itself
[], # No Feeds found
"@[email protected]" # Who is actually mentioning
),
],
)
def test_parse(
content: str,
action: str,
complements: dict,
error: str,
returned: bool,
is_a_valid_feed: bool,
findfeeds: list,
username: str
):

# Set up the mentioning environment
instance = get_mention_parser()
mention = Mention.from_dict(
{
"status_id": 1234,
"content": content,
"username": username,
"visibility": StatusPostVisibility.PUBLIC
}
)
instance.mention = mention

# Mock the external calls and trigger the parse
mocked_url_findfeeds = Mock()
mocked_url_findfeeds.return_value = findfeeds
mocked_url_is_a_valid_feed = Mock()
mocked_url_is_a_valid_feed.return_value = is_a_valid_feed
with patch.object(Url, "findfeeds", new=mocked_url_findfeeds):
with patch.object(Url, "is_a_valid_feed", new=mocked_url_is_a_valid_feed):
parsed_result = instance.parse()

# Assert that the parse outcomed what we expect
assert instance.action == action
assert instance.complements == complements
assert instance.error == error
assert parsed_result == returned

0 comments on commit 26a5d23

Please sign in to comment.