Add tests

This commit is contained in:
2022-12-13 22:09:51 +01:00
parent 32f474a0ed
commit a704a63582
10 changed files with 325 additions and 22 deletions

43
tests/test_feeds.py Normal file
View File

@ -0,0 +1,43 @@
import os
import tempfile
from pathlib import Path
from reader import make_reader
from discord_rss_bot.feeds import send_to_discord
def test_send_to_discord() -> None:
"""Test sending to Discord."""
with tempfile.TemporaryDirectory() as temp_dir:
# Create the temp directory.
os.makedirs(temp_dir, exist_ok=True)
assert os.path.exists(temp_dir)
# Create a temporary reader.
reader = make_reader(url=str(Path(temp_dir, "test_db.sqlite")))
assert reader is not None
# Add a feed to the reader.
reader.add_feed("https://www.reddit.com/r/Python/.rss")
# Update the feed to get the entries.
reader.update_feeds()
# Get the feed.
feed = reader.get_feed("https://www.reddit.com/r/Python/.rss")
assert feed is not None
# Get the webhook.
webhook_url = os.environ.get("TEST_WEBHOOK_URL")
assert webhook_url is not None
# Add tag to the feed and check if it's there.
reader.set_tag(feed, "webhook", webhook_url)
assert reader.get_tag(feed, "webhook") == webhook_url
# Send the feed to Discord.
send_to_discord(reader=reader, feed=feed, do_once=True)
# Close the reader, so we can delete the directory.
reader.close()

26
tests/test_main.py Normal file
View File

@ -0,0 +1,26 @@
from fastapi.testclient import TestClient
from discord_rss_bot.main import app, create_list_of_webhooks, encode_url
client = TestClient(app)
def test_read_main():
response = client.get("/")
assert response.status_code == 200
def test_add():
response = client.get("/add")
assert response.status_code == 200
def test_search():
response = client.get("/search/?query=a")
assert response.status_code == 200
def test_encode_url():
before = "https://www.google.com/"
after = "https%3A//www.google.com/"
assert encode_url(url_to_quote=before) == after

45
tests/test_search.py Normal file
View File

@ -0,0 +1,45 @@
import os
import tempfile
from pathlib import Path
from reader import make_reader
from discord_rss_bot.search import add_span_with_slice, create_html_for_search_results
def test_create_html_for_search_results() -> None:
"""Test create_html_for_search_results."""
# Create a reader.
with tempfile.TemporaryDirectory() as temp_dir:
# Create the temp directory.
os.makedirs(temp_dir, exist_ok=True)
assert os.path.exists(temp_dir)
# Create a temporary reader.
reader = make_reader(url=str(Path(temp_dir, "test_db.sqlite")))
assert reader is not None
# Add a feed to the reader.
reader.add_feed("https://www.reddit.com/r/Python/.rss")
# Update the feed to get the entries.
reader.update_feeds()
# Get the feed.
feed = reader.get_feed("https://www.reddit.com/r/Python/.rss")
assert feed is not None
# Update the search index.
reader.enable_search()
reader.update_search()
# Get the HTML for the search results.
search_results = reader.search_entries("a", feed=feed)
# Create the HTML and check if it is not empty.
search_html: str = create_html_for_search_results(search_results, reader)
assert search_html is not None
assert len(search_html) > 10
# Close the reader, so we can delete the directory.
reader.close()

83
tests/test_settings.py Normal file
View File

@ -0,0 +1,83 @@
import os
import pathlib
import tempfile
from platformdirs import user_data_dir
from reader import Reader
from tomlkit import TOMLDocument
from discord_rss_bot.settings import create_settings_file, data_dir, get_db_location, get_reader, read_settings_file
def test_read_settings_file():
"""Test reading the settings file."""
with tempfile.TemporaryDirectory() as temp_dir:
custom_loc = os.path.join(temp_dir, "test_settings.toml")
# File should not exist yet should this should fail.
assert not os.path.exists(custom_loc)
# Create the file.
settings: TOMLDocument = read_settings_file(custom_location=custom_loc)
# Check if the settings file is a toml document.
assert isinstance(settings, TOMLDocument)
# Check if file exists
assert os.path.exists(os.path.join(temp_dir, "test_settings.toml"))
# Check if the file has the correct contents
assert settings["webhooks"] == {}
assert settings["database"] == {}
def test_get_db_location():
"""Test getting the database location."""
with tempfile.TemporaryDirectory() as temp_dir:
custom_loc = os.path.join(temp_dir, "test_db.sqlite")
# File should not exist yet.
assert not os.path.exists(custom_loc)
# Create the file and check if it exists.
assert get_db_location(custom_location=custom_loc) == os.path.join(temp_dir, "test_db.sqlite")
# Test with the default location
loc = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True)
assert get_db_location() == os.path.join(loc, "db.sqlite")
def test_reader():
"""Test the reader."""
reader = get_reader()
assert isinstance(reader, Reader)
# Test the reader with a custom location.
with tempfile.TemporaryDirectory() as temp_dir:
# Create the temp directory
os.makedirs(temp_dir, exist_ok=True)
custom_loc = pathlib.Path(temp_dir, "custom_loc_db.sqlite")
custom_reader = get_reader(custom_location=str(custom_loc))
assert isinstance(custom_reader, Reader)
# Close the reader, so we can delete the directory.
custom_reader.close()
def test_create_settings_file():
"""Test creating the settings file."""
with tempfile.TemporaryDirectory() as temp_dir:
settings_file_location: str = os.path.join(temp_dir, "settings.toml")
# File should not exist yet.
assert not os.path.exists(settings_file_location)
# Create the file and check if it exists.
create_settings_file(settings_file_location)
assert os.path.exists(settings_file_location)
def test_data_dir():
"""Test the data directory."""
assert os.path.exists(data_dir)