Add more tests

This commit is contained in:
2023-01-19 04:05:35 +01:00
parent 82bf97491d
commit f39c9056fd
5 changed files with 177 additions and 6 deletions

133
tests/test_custom_filter.py Normal file
View File

@ -0,0 +1,133 @@
import os
import pathlib
import tempfile
from reader import Reader
from discord_rss_bot.custom_filters import convert_to_md, encode_url, entry_is_blacklisted, entry_is_whitelisted
from discord_rss_bot.settings import get_reader
def test_encode_url() -> None:
# Test normal input
assert encode_url("https://www.example.com") == r"https%3A//www.example.com"
# Test input with spaces
assert encode_url("https://www.example.com/my path") == r"https%3A//www.example.com/my%20path"
# Test input with special characters
assert (
encode_url("https://www.example.com/my path?q=abc&b=1")
== r"https%3A//www.example.com/my%20path%3Fq%3Dabc%26b%3D1"
)
# Test empty input
assert encode_url("") == ""
# Test input as None
assert encode_url(None) == "" # type: ignore
def test_entry_is_whitelisted() -> None:
# Test with a custom reader.
with tempfile.TemporaryDirectory() as temp_dir:
# Create the temp directory
os.makedirs(temp_dir, exist_ok=True)
custom_loc: pathlib.Path = pathlib.Path(temp_dir, "custom_loc_db.sqlite")
custom_reader: Reader = get_reader(custom_location=str(custom_loc))
# Add a feed to the database.
custom_reader.add_feed("https://lovinator.space/rss_test.xml")
custom_reader.update_feed("https://lovinator.space/rss_test.xml")
# whitelist_title
custom_reader.set_tag("https://lovinator.space/rss_test.xml", "whitelist_title", "fvnnnfnfdnfdnfd") # type: ignore # noqa: E501
for entry in custom_reader.get_entries():
if entry_is_whitelisted(entry) is True:
assert entry.title == "fvnnnfnfdnfdnfd"
break
custom_reader.delete_tag("https://lovinator.space/rss_test.xml", "whitelist_title")
# whitelist_summary
custom_reader.set_tag("https://lovinator.space/rss_test.xml", "whitelist_summary", "fvnnnfnfdnfdnfd") # type: ignore # noqa: E501
for entry in custom_reader.get_entries():
if entry_is_whitelisted(entry) is True:
assert entry.summary == "fvnnnfnfdnfdnfd"
break
custom_reader.delete_tag("https://lovinator.space/rss_test.xml", "whitelist_summary")
# whitelist_content
custom_reader.set_tag("https://lovinator.space/rss_test.xml", "whitelist_content", "fvnnnfnfdnfdnfd") # type: ignore # noqa: E501
for entry in custom_reader.get_entries():
if entry_is_whitelisted(entry) is True:
assert entry.content[0].value == "<p>ffdnfdnfdnfdnfdndfn</p>"
break
custom_reader.delete_tag("https://lovinator.space/rss_test.xml", "whitelist_content")
# Close the reader, so we can delete the directory.
custom_reader.close()
def test_entry_is_blacklisted() -> None:
# Test with a custom reader.
with tempfile.TemporaryDirectory() as temp_dir:
# Create the temp directory
os.makedirs(temp_dir, exist_ok=True)
custom_loc: pathlib.Path = pathlib.Path(temp_dir, "custom_loc_db.sqlite")
custom_reader: Reader = get_reader(custom_location=str(custom_loc))
# Add a feed to the database.
custom_reader.add_feed("https://lovinator.space/rss_test.xml")
custom_reader.update_feed("https://lovinator.space/rss_test.xml")
# blacklist_title
custom_reader.set_tag("https://lovinator.space/rss_test.xml", "blacklist_title", "fvnnnfnfdnfdnfd") # type: ignore # noqa: E501
for entry in custom_reader.get_entries():
if entry_is_blacklisted(entry) is True:
assert entry.title == "fvnnnfnfdnfdnfd"
break
custom_reader.delete_tag("https://lovinator.space/rss_test.xml", "blacklist_title")
# blacklist_summary
custom_reader.set_tag("https://lovinator.space/rss_test.xml", "blacklist_summary", "fvnnnfnfdnfdnfd") # type: ignore # noqa: E501
for entry in custom_reader.get_entries():
if entry_is_blacklisted(entry) is True:
assert entry.summary == "fvnnnfnfdnfdnfd"
break
custom_reader.delete_tag("https://lovinator.space/rss_test.xml", "blacklist_summary")
# blacklist_content
custom_reader.set_tag("https://lovinator.space/rss_test.xml", "blacklist_content", "fvnnnfnfdnfdnfd") # type: ignore # noqa: E501
for entry in custom_reader.get_entries():
if entry_is_blacklisted(entry) is True:
assert entry.content[0].value == "<p>ffdnfdnfdnfdnfdndfn</p>"
break
custom_reader.delete_tag("https://lovinator.space/rss_test.xml", "blacklist_content")
# Close the reader, so we can delete the directory.
custom_reader.close()
def test_convert_to_md():
# Test normal input
assert (
convert_to_md("<h1>Headline</h1><h2>Subheadline</h2><h3>subsubheadline</h3>")
== """Headline
========
Subheadline
-----------
### subsubheadline
"""
)
# Test input with tables
assert (
convert_to_md(
"<table><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody><tr><td>Row 1, Column 1</td><td>Row 1, Column 2</td><td>Row 1, Column 3</td></tr><tr><td>Row 2, Column 1</td><td>Row 2, Column 2</td><td>Row 2, Column 3</td></tr></tbody></table>" # noqa: E501
)
== "Column 1Column 2Column 3Row 1, Column 1Row 1, Column 2Row 1, Column 3Row 2, Column 1Row 2, Column 2Row 2, Column 3" # noqa: E501
)
# Test empty input
assert convert_to_md("") == ""
# Test input as None
assert convert_to_md(None) == "" # type: ignore

View File

@ -2,6 +2,7 @@ import os
import tempfile
from pathlib import Path
import pytest
from reader import Feed, Reader, make_reader # type: ignore
from discord_rss_bot.feeds import send_to_discord
@ -30,6 +31,10 @@ def test_send_to_discord() -> None:
# Get the webhook.
webhook_url: str | None = os.environ.get("TEST_WEBHOOK_URL")
if webhook_url is None:
pytest.skip("No webhook URL provided.")
assert webhook_url is not None
# Add tag to the feed and check if it is there.

View File

@ -5,7 +5,13 @@ import tempfile
from platformdirs import user_data_dir
from reader import Reader
from discord_rss_bot.settings import data_dir, get_db_location, get_reader
from discord_rss_bot.settings import (
data_dir,
default_custom_message,
get_db_location,
get_reader,
get_webhook_for_entry,
)
def test_get_db_location() -> None:
@ -45,3 +51,34 @@ def test_reader() -> None:
def test_data_dir() -> None:
"""Test the data directory."""
assert os.path.exists(data_dir)
def test_default_custom_message() -> None:
"""Test the default custom message."""
assert "{{entry_title}}\n{{entry_link}}" == default_custom_message
def test_get_webhook_for_entry() -> None:
"""Test getting the webhook for an entry."""
# Test with a custom reader.
with tempfile.TemporaryDirectory() as temp_dir:
# Create the temp directory
os.makedirs(temp_dir, exist_ok=True)
custom_loc: pathlib.Path = pathlib.Path(temp_dir, "custom_loc_db.sqlite")
custom_reader: Reader = get_reader(custom_location=str(custom_loc))
# Add a feed to the database.
custom_reader.add_feed("https://www.reddit.com/r/movies.rss")
custom_reader.update_feed("https://www.reddit.com/r/movies.rss")
for entry in custom_reader.get_entries():
assert get_webhook_for_entry(custom_reader=custom_reader, entry=entry) == ""
# Add a webhook to the database.
custom_reader.set_tag("https://www.reddit.com/r/movies.rss", "webhook", "https://example.com") # type: ignore
our_tag: str = custom_reader.get_tag("https://www.reddit.com/r/movies.rss", "webhook") # type: ignore
assert our_tag == "https://example.com"
# Close the reader, so we can delete the directory.
custom_reader.close()