Files
BDFR_Web/tests/test_extension_case_normalization.py
ModerateWinGuy 0157f462cc
formatting_check / formatting_check (push) Failing after 3s
Python Test / test (.ps1, windows-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, macos-latest, 3.9) (push) Has been cancelled
Python Test / test (.sh, ubuntu-latest, 3.9) (push) Has been cancelled
reformatting
2026-07-14 21:32:55 +12:00

87 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""
Test extension case normalization functionality
"""
from unittest.mock import MagicMock
import pytest
from bdfr.resource import Resource
class TestExtensionNormalization:
"""Test that extensions are properly normalized to lowercase"""
def test_url_extensions_normalized(self):
"""Test that extensions from URLs are normalized to lowercase"""
test_cases = [
("https://example.com/image.JPG", ".jpg"),
("https://example.com/image.jpeg", ".jpeg"),
("https://example.com/image.JPEG", ".jpeg"),
("https://example.com/image.jpg", ".jpg"),
("https://example.com/image.PNG", ".png"),
("https://example.com/image.GIF", ".gif"),
]
for url, expected in test_cases:
mock_submission = MagicMock()
mock_submission.id = "test123"
resource = Resource(mock_submission, url, lambda: None)
assert resource.extension == expected, f"URL {url} should normalize to {expected}, got {resource.extension}"
def test_reddit_media_urls_normalized(self):
"""Test that Reddit media URLs are properly normalized"""
test_cases = [
("https://www.reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fr2mv10i4vkfd1.JPG", ".jpg"),
("https://www.reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fr2mv10i4vkfd1.jpeg", ".jpeg"),
("https://www.reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fr2mv10i4vkfd1.JPEG", ".jpeg"),
("https://www.reddit.com/media?url=https%3A%2F%2Fi.redd.it%2Fr2mv10i4vkfd1.PNG", ".png"),
]
for url, expected in test_cases:
mock_submission = MagicMock()
mock_submission.id = "test123"
resource = Resource(mock_submission, url, lambda: None)
assert (
resource.extension == expected
), f"Reddit media URL {url} should normalize to {expected}, got {resource.extension}"
def test_constructor_extensions_normalized(self):
"""Test that extensions passed to constructor are normalized"""
test_cases = [
(".JPG", ".jpg"),
(".JPEG", ".jpeg"),
(".PNG", ".png"),
(".GIF", ".gif"),
# Test extensions without dots (common from yt-dlp)
("JPG", ".jpg"),
("JPEG", ".jpeg"),
("MP4", ".mp4"),
("WEBM", ".webm"),
("mp4", ".mp4"),
("gif", ".gif"),
]
for input_ext, expected in test_cases:
mock_submission = MagicMock()
mock_submission.id = "test123"
resource = Resource(mock_submission, "https://example.com/test", lambda: None, input_ext)
assert (
resource.extension == expected
), f"Constructor extension {input_ext} should normalize to {expected}, got {resource.extension}"
def test_magic_number_detection_normalized(self):
"""Test that magic number detection returns normalized extensions"""
mock_submission = MagicMock()
mock_submission.id = "test123"
# Test JPEG magic number detection
jpeg_content = b"\xff\xd8\xff" + b"0" * 100 # JPEG magic number
resource = Resource(mock_submission, "https://example.com/no-extension", lambda params: jpeg_content)
resource.download() # Trigger content-based detection
assert resource.extension == ".jpg", f"Magic number detection should return .jpg, got {resource.extension}"