Use Ruff and fix all its warnings and errors

This commit is contained in:
2023-03-18 01:50:45 +01:00
parent 15284c5646
commit 948a5a2af9
27 changed files with 504 additions and 313 deletions

0
tests/__init__.py Normal file
View File

View File

@ -1,11 +1,14 @@
import tempfile
from pathlib import Path
from typing import Iterable
from typing import TYPE_CHECKING
from reader import Entry, Feed, Reader, make_reader
from discord_rss_bot.filter.blacklist import has_black_tags, should_be_skipped
if TYPE_CHECKING:
from collections.abc import Iterable
feed_url: str = "https://lovinator.space/rss_test.xml"

View File

@ -1,12 +1,14 @@
import os
import pathlib
import tempfile
from reader import Reader
from pathlib import Path
from typing import TYPE_CHECKING
from discord_rss_bot.custom_filters import encode_url, entry_is_blacklisted, entry_is_whitelisted
from discord_rss_bot.settings import get_reader
if TYPE_CHECKING:
from reader import Reader
def test_encode_url() -> None:
# Test normal input
@ -19,16 +21,16 @@ def test_encode_url() -> None:
== r"https%3A//www.example.com/my%20path%3Fq%3Dabc%26b%3D1"
)
# Test empty input
assert encode_url("") == ""
assert not encode_url("")
# Test input as None
assert encode_url(None) == "" # type: ignore
assert not 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)
Path.mkdir(Path(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))
@ -69,7 +71,7 @@ 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)
Path.mkdir(Path(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))

View File

@ -13,11 +13,11 @@ 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)
Path.mkdir(Path(temp_dir), exist_ok=True)
assert Path.exists(Path(temp_dir))
# Create a temporary reader.
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.
@ -35,7 +35,8 @@ def test_send_to_discord() -> None:
# Get the webhook.
webhook_url: str | None = os.environ.get("TEST_WEBHOOK_URL")
if webhook_url is None:
if not webhook_url:
reader.close()
pytest.skip("No webhook URL provided.")
assert webhook_url is not None

View File

@ -1,10 +1,12 @@
from typing import Literal
from typing import TYPE_CHECKING, Literal
from fastapi.testclient import TestClient
from httpx import Response
from discord_rss_bot.main import app, encode_url
if TYPE_CHECKING:
from httpx import Response
client: TestClient = TestClient(app)
webhook_name: str = "Hello, I am a webhook!"
webhook_url: str = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"
@ -180,7 +182,7 @@ def test_unpause_feed() -> None:
assert feed_url in response.text
def test_remove_feed():
def test_remove_feed() -> None:
"""Test the /remove page."""
# Remove the feed if it already exists before we run the test.
feeds: Response = client.get("/")
@ -201,7 +203,7 @@ def test_remove_feed():
assert feed_url not in response.text
def test_delete_webhook():
def test_delete_webhook() -> None:
"""Test the /delete_webhook page."""
# Remove the feed if it already exists before we run the test.
feeds: Response = client.get("/webhooks")

View File

@ -28,7 +28,9 @@ def test_convert_to_md() -> None:
# Test multiple tags
assert (
convert_html_to_md('<b>bold</b> <i>italic</i> <a href="https://example.com">link</a> <code>code</code> <s>strikethrough</s>') # noqa: E501
convert_html_to_md(
'<b>bold</b> <i>italic</i> <a href="https://example.com">link</a> <code>code</code> <s>strikethrough</s>',
)
== "**bold** *italic* [link](https://example.com) `code` ~~strikethrough~~"
)

View File

@ -1,20 +1,22 @@
import os
import tempfile
from pathlib import Path
from typing import Iterable
from typing import TYPE_CHECKING
from reader import Feed, Reader, make_reader
from discord_rss_bot.search import create_html_for_search_results
if TYPE_CHECKING:
from collections.abc import Iterable
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)
Path.mkdir(Path(temp_dir), exist_ok=True)
assert Path.exists(Path(temp_dir))
# Create a temporary reader.
reader: Reader = make_reader(url=str(Path(temp_dir, "test_db.sqlite")))

View File

@ -1,6 +1,6 @@
import os
import pathlib
import tempfile
from pathlib import Path
from reader import Reader
@ -15,7 +15,7 @@ def test_reader() -> None:
# Test the reader with a custom location.
with tempfile.TemporaryDirectory() as temp_dir:
# Create the temp directory
os.makedirs(temp_dir, exist_ok=True)
Path.mkdir(Path(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))
@ -27,12 +27,12 @@ def test_reader() -> None:
def test_data_dir() -> None:
"""Test the data directory."""
assert os.path.exists(data_dir)
assert Path.exists(Path(data_dir))
def test_default_custom_message() -> None:
"""Test the default custom message."""
assert "{{entry_title}}\n{{entry_link}}" == default_custom_message
assert default_custom_message == "{{entry_title}}\n{{entry_link}}"
def test_get_webhook_for_entry() -> None:
@ -40,7 +40,7 @@ def test_get_webhook_for_entry() -> None:
# Test with a custom reader.
with tempfile.TemporaryDirectory() as temp_dir:
# Create the temp directory
os.makedirs(temp_dir, exist_ok=True)
Path.mkdir(Path(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))

View File

@ -1,11 +1,14 @@
import tempfile
from pathlib import Path
from typing import Iterable
from typing import TYPE_CHECKING
from reader import Entry, Feed, Reader, make_reader
from discord_rss_bot.filter.whitelist import has_white_tags, should_be_sent
if TYPE_CHECKING:
from collections.abc import Iterable
feed_url: str = "https://lovinator.space/rss_test.xml"