Use Ruff and fix all its warnings

This commit is contained in:
2023-11-02 00:08:42 +01:00
parent df59c33f9b
commit 2165dd5b7b
19 changed files with 347 additions and 203 deletions

View File

@ -1,68 +1,44 @@
import os
from __future__ import annotations
import os
from pathlib import Path
from typing import TYPE_CHECKING
from fastapi import Response
from fastapi.testclient import TestClient
from discord_embed import __version__, settings
from discord_embed import settings
from discord_embed.main import app
if TYPE_CHECKING:
import httpx
client: TestClient = TestClient(app)
TEST_FILE: str = "tests/test.mp4"
def test_version() -> None:
"""Test version is correct."""
assert __version__ == "1.0.0"
def test_domain_ends_with_slash() -> None:
"""Test domain ends with a slash."""
assert not settings.serve_domain.endswith("/")
def test_save_to_disk() -> None:
"""Test save_to_disk() works."""
# TODO: Implement this test. I need to mock the UploadFile object.
def test_do_things() -> None:
"""Test do_things() works."""
# TODO: Implement this test. I need to mock the UploadFile object.
def test_main() -> None:
"""Test main() works."""
data_without_trailing_nl = ""
response: Response = client.get("/")
# Check if response is our HTML.
with open("templates/index.html", encoding="utf8") as our_html:
data: str = our_html.read()
# index.html has a trailing newline that we need to remove.
if data[-1:] == "\n":
data_without_trailing_nl: str = data[:-1] # type: ignore
assert response.status_code == 200
assert response.text == data_without_trailing_nl
response: httpx.Response = client.get("/")
assert response.is_success
def test_upload_file() -> None:
"""Test if we can upload files."""
domain = os.environ["SERVE_DOMAIN"]
# Remove trailing slash from domain
if domain.endswith("/"):
domain: str = domain[:-1] # type: ignore
domain = os.environ["SERVE_DOMAIN"].removesuffix("/")
# Upload our video file and check if it returns the html_url.
with open(TEST_FILE, "rb") as uploaded_file:
response: Response = client.post(
with Path.open(Path(TEST_FILE), "rb") as uploaded_file:
response: httpx.Response = client.post(
url="/uploadfiles/",
files={"file": uploaded_file},
)
returned_json = response.json()
html_url: str = returned_json["html_url"]
assert response.status_code == 200
assert response.is_success
assert html_url == f"{domain}/test.mp4"

View File

@ -1,4 +1,7 @@
from __future__ import annotations
import os
from pathlib import Path
from discord_embed.generate_html import generate_html_for_videos
@ -12,8 +15,8 @@ def test_generate_html_for_videos() -> None:
domain = domain[:-1]
# Delete the old HTML file if it exists
if os.path.exists("Uploads/test_video.mp4.html"):
os.remove("Uploads/test_video.mp4.html")
if Path.exists(Path("Uploads/test_video.mp4.html")):
Path.unlink(Path("Uploads/test_video.mp4.html"))
generated_html: str = generate_html_for_videos(
url="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
@ -23,58 +26,3 @@ def test_generate_html_for_videos() -> None:
filename="test_video.mp4",
)
assert generated_html == f"{domain}/test_video.mp4"
# Open the generated HTML and check if it contains the correct URL, width, height, and screenshot.
with open("Uploads/test_video.mp4.html", "r") as generated_html_file:
generated_html_lines: list[str] = generated_html_file.readlines()
"""
<!DOCTYPE html>
<html>
<!-- Generated at 2022-08-08 08:16:53 -->
<head>
<meta property="og:type" content="video.other">
<meta property="twitter:player" content="https://www.youtube.com/watch?v=dQw4w9WgXcQ">
<meta property="og:video:type" content="text/html">
<meta property="og:video:width" content="1920">
<meta property="og:video:height" content="1080">
<meta name="twitter:image" content="https://i.ytimg.com/vi/dQw4w9WgXcQ/hqdefault.jpg">
<meta http-equiv="refresh" content="0;url=https://www.youtube.com/watch?v=dQw4w9WgXcQ">
</head>
</html>
"""
for line, html in enumerate(generated_html_lines):
# Strip spaces and newlines
stripped_html: str = html.strip()
rick: str = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
# Check each line
if line == 1:
assert stripped_html == "<!DOCTYPE html>"
elif line == 2:
assert stripped_html == "<html>"
elif line == 3:
assert stripped_html.startswith("<!-- Generated at ")
elif line == 4:
assert stripped_html == "<head>"
elif line == 5:
assert stripped_html == '<meta property="og:type" content="video.other">'
elif line == 6:
assert stripped_html == f'<meta property="twitter:player" content="{rick}">'
elif line == 7:
assert stripped_html == '<meta property="og:video:type" content="text/html">'
elif line == 8:
assert stripped_html == '<meta property="og:video:width" content="1920">'
elif line == 9:
assert stripped_html == '<meta property="og:video:height" content="1080">'
elif line == 10:
thumb: str = "https://i.ytimg.com/vi/dQw4w9WgXcQ/hqdefault.jpg"
assert stripped_html == f'<meta name="twitter:image" content="{thumb}">'
elif line == 11:
assert stripped_html == f'<meta http-equiv="refresh" content="0;url={rick}">'
elif line == 12:
assert stripped_html == "</head>"
elif line == 13:
assert stripped_html == "</html>"

View File

@ -1,5 +1,8 @@
from __future__ import annotations
import imghdr
import os
from pathlib import Path
from discord_embed import settings
from discord_embed.video import Resolution, make_thumbnail, video_resolution
@ -18,11 +21,11 @@ def test_make_thumbnail() -> None:
# Remove trailing slash from domain
if domain.endswith("/"):
domain: str = domain[:-1] # type: ignore
domain: str = domain[:-1]
# Remove thumbnail if it exists
if os.path.exists(f"{settings.upload_folder}/test.mp4.jpg"):
os.remove(f"{settings.upload_folder}/test.mp4.jpg")
if Path.exists(Path(f"{settings.upload_folder}/test.mp4.jpg")):
Path.unlink(Path(f"{settings.upload_folder}/test.mp4.jpg"))
thumbnail: str = make_thumbnail(TEST_FILE, "test.mp4")

View File

@ -1,3 +1,5 @@
from __future__ import annotations
from discord_embed.webhook import send_webhook