Add type hints and other small stuff

This commit is contained in:
2022-12-14 02:16:33 +01:00
parent 1fcb9ffbf8
commit 3dbe6ba79d
8 changed files with 65 additions and 55 deletions

View File

@ -2,7 +2,7 @@ import os
import tempfile
from pathlib import Path
from reader import make_reader
from reader import Feed, Reader, make_reader
from discord_rss_bot.feeds import send_to_discord
@ -15,7 +15,7 @@ def test_send_to_discord() -> None:
assert os.path.exists(temp_dir)
# Create a temporary reader.
reader = make_reader(url=str(Path(temp_dir, "test_db.sqlite")))
reader: Reader = make_reader(url=str(Path(temp_dir, "test_db.sqlite")))
assert reader is not None
# Add a feed to the reader.
@ -25,19 +25,19 @@ def test_send_to_discord() -> None:
reader.update_feeds()
# Get the feed.
feed = reader.get_feed("https://www.reddit.com/r/Python/.rss")
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")
webhook_url: str | None = 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)
reader.set_tag(feed, "webhook", webhook_url) # type: ignore
assert reader.get_tag(feed, "webhook") == webhook_url
# Send the feed to Discord.
send_to_discord(reader=reader, feed=feed, do_once=True)
send_to_discord(custom_reader=reader, feed=feed, do_once=True)
# Close the reader, so we can delete the directory.
reader.close()

View File

@ -1,26 +1,33 @@
from typing import Literal
from fastapi.testclient import TestClient
from httpx import Response
from discord_rss_bot.main import app, create_list_of_webhooks, encode_url
from discord_rss_bot.main import app, encode_url
client = TestClient(app)
client: TestClient = TestClient(app)
def test_read_main():
response = client.get("/")
def test_read_main() -> None:
"""Test the main page."""
response: Response = client.get("/")
assert response.status_code == 200
def test_add():
response = client.get("/add")
def test_add() -> None:
"""Test the /add page."""
response: Response = client.get("/add")
assert response.status_code == 200
def test_search():
response = client.get("/search/?query=a")
def test_search() -> None:
"""Test the /search page."""
response: 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/"
def test_encode_url() -> None:
"""Test the encode_url function."""
before: Literal["https://www.google.com/"] = "https://www.google.com/"
after: Literal["https%3A//www.google.com/"] = "https%3A//www.google.com/"
assert encode_url(url_to_quote=before) == after

View File

@ -1,10 +1,11 @@
import os
import tempfile
from pathlib import Path
from typing import Iterable
from reader import make_reader
from reader import EntrySearchResult, Feed, Reader, make_reader
from discord_rss_bot.search import add_span_with_slice, create_html_for_search_results
from discord_rss_bot.search import create_html_for_search_results
def test_create_html_for_search_results() -> None:
@ -16,7 +17,7 @@ def test_create_html_for_search_results() -> None:
assert os.path.exists(temp_dir)
# Create a temporary reader.
reader = make_reader(url=str(Path(temp_dir, "test_db.sqlite")))
reader: Reader = make_reader(url=str(Path(temp_dir, "test_db.sqlite")))
assert reader is not None
# Add a feed to the reader.
@ -26,7 +27,7 @@ def test_create_html_for_search_results() -> None:
reader.update_feeds()
# Get the feed.
feed = reader.get_feed("https://www.reddit.com/r/Python/.rss")
feed: Feed = reader.get_feed("https://www.reddit.com/r/Python/.rss")
assert feed is not None
# Update the search index.
@ -34,7 +35,7 @@ def test_create_html_for_search_results() -> None:
reader.update_search()
# Get the HTML for the search results.
search_results = reader.search_entries("a", feed=feed)
search_results: Iterable[EntrySearchResult] = 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)

View File

@ -6,13 +6,19 @@ 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
from discord_rss_bot.settings import (
create_settings_file,
data_dir,
get_db_location,
get_reader,
read_settings_file,
)
def test_read_settings_file():
def test_read_settings_file() -> None:
"""Test reading the settings file."""
with tempfile.TemporaryDirectory() as temp_dir:
custom_loc = os.path.join(temp_dir, "test_settings.toml")
custom_loc: str = os.path.join(temp_dir, "test_settings.toml")
# File should not exist yet should this should fail.
assert not os.path.exists(custom_loc)
@ -31,10 +37,10 @@ def test_read_settings_file():
assert settings["database"] == {}
def test_get_db_location():
def test_get_db_location() -> None:
"""Test getting the database location."""
with tempfile.TemporaryDirectory() as temp_dir:
custom_loc = os.path.join(temp_dir, "test_db.sqlite")
custom_loc: str = os.path.join(temp_dir, "test_db.sqlite")
# File should not exist yet.
assert not os.path.exists(custom_loc)
@ -43,13 +49,13 @@ def test_get_db_location():
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)
loc: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True)
assert get_db_location() == os.path.join(loc, "db.sqlite")
def test_reader():
def test_reader() -> None:
"""Test the reader."""
reader = get_reader()
reader: Reader = get_reader()
assert isinstance(reader, Reader)
# Test the reader with a custom location.
@ -57,15 +63,15 @@ def test_reader():
# 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))
custom_loc: pathlib.Path = pathlib.Path(temp_dir, "custom_loc_db.sqlite")
custom_reader: 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():
def test_create_settings_file() -> None:
"""Test creating the settings file."""
with tempfile.TemporaryDirectory() as temp_dir:
settings_file_location: str = os.path.join(temp_dir, "settings.toml")
@ -78,6 +84,6 @@ def test_create_settings_file():
assert os.path.exists(settings_file_location)
def test_data_dir():
def test_data_dir() -> None:
"""Test the data directory."""
assert os.path.exists(data_dir)