#!/usr/bin/env python3 """ Test extension case normalization functionality """ import pytest from unittest.mock import MagicMock 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}"