quicktools / backend /test_main.py
github-actions[bot]
deploy: sync to hugging face
4cb7ab8
import pytest
from fastapi.testclient import TestClient
from main import app
import io
from PIL import Image
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert "running" in response.json()["message"]
@pytest.mark.parametrize("width, height", [
(600, 600), # US
(413, 531), # EU
(354, 472), # JP
(591, 827), # CA
])
def test_process_image_various_sizes(width, height):
# Create a dummy RGBA image (red square)
file = io.BytesIO()
# Use a larger source image to test cropping/resizing
image = Image.new('RGBA', size=(1000, 1500), color=(255, 0, 0, 255))
image.save(file, 'png')
file.seek(0)
response = client.post(
"/process-image",
files={"file": ("test.png", file, "image/png")},
data={"width": str(width), "height": str(height)}
)
assert response.status_code == 200
assert response.headers["content-type"] == "image/jpeg"
output_image = Image.open(io.BytesIO(response.content))
assert output_image.size == (width, height)
def test_manual_crop():
# Create a 500x500 source image
file = io.BytesIO()
image = Image.new('RGBA', size=(500, 500), color=(0, 0, 255, 255))
image.save(file, 'png')
file.seek(0)
# Define a crop in the middle (100, 100 to 300, 300)
response = client.post(
"/process-image",
files={"file": ("manual.png", file, "image/png")},
data={
"width": "600",
"height": "600",
"crop_x": "100",
"crop_y": "100",
"crop_w": "200",
"crop_h": "200"
}
)
assert response.status_code == 200
output_image = Image.open(io.BytesIO(response.content))
# Output should still be normalized to the requested 600x600
assert output_image.size == (600, 600)
import xml.etree.ElementTree as ET
import os
def test_sitemap_validity():
# Path to the sitemap file in frontend/public
sitemap_path = os.path.join(os.path.dirname(__file__), "..", "frontend", "public", "sitemap.xml")
# 1. Check if file exists
assert os.path.exists(sitemap_path), "sitemap.xml does not exist in frontend/public"
# 2. Try to parse XML
try:
tree = ET.parse(sitemap_path)
root = tree.getroot()
except ET.ParseError as e:
pytest.fail(f"sitemap.xml is not valid XML: {e}")
# 3. Check Namespace
assert "sitemaps.org" in root.tag, "Sitemap namespace missing or incorrect"
# 4. Check URLs
urls = root.findall(".//{http://www.sitemaps.org/schemas/sitemap/0.9}loc")
assert len(urls) >= 16, f"Expected at least 16 URLs (8 portals + 8 tools), found {len(urls)}"
for loc in urls:
url_text = loc.text.strip()
# Verify no whitespace inside the URL
assert " " not in url_text, f"Sitemap URL contains spaces: '{url_text}'"
assert url_text.startswith("https://quicktools.dpdns.org"), f"Invalid URL domain in sitemap: {url_text}"
assert not url_text.endswith("/undefined"), f"Sitemap contains undefined path: {url_text}"
# Check for typical malformed patterns
assert "\n" not in url_text, f"Sitemap URL contains newline: '{url_text}'"
def test_processing_lock():
# This is hard to test with a simple TestClient because it's synchronous,
# but we can verify it doesn't crash.
file = io.BytesIO()
image = Image.new('RGBA', size=(10, 10), color=(0, 255, 0, 255))
image.save(file, 'png')
file.seek(0)
response = client.post(
"/process-image",
files={"file": ("test.png", file, "image/png")},
data={"width": "100", "height": "100"}
)
assert response.status_code == 200