96 lines
3.6 KiB
Python
96 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test script to verify that the duplicate folder creation fix works correctly.
|
|
This script simulates the scenario where duplicate posts would previously create empty folders.
|
|
"""
|
|
|
|
import tempfile
|
|
import shutil
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock
|
|
|
|
# Add the bdfr module to the path
|
|
import sys
|
|
sys.path.insert(0, '.')
|
|
|
|
from bdfr.configuration import Configuration
|
|
from bdfr.connector import RedditConnector
|
|
from bdfr.downloader import RedditDownloader
|
|
|
|
|
|
def test_duplicate_folder_creation_fix():
|
|
"""Test that folders are not created for duplicate posts when no_dupes is enabled."""
|
|
|
|
# Create a temporary directory for testing
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
temp_path = Path(temp_dir)
|
|
|
|
# Create test configuration
|
|
args = Configuration()
|
|
args.no_dupes = True
|
|
args.folder_scheme = ""
|
|
args.file_scheme = "{POSTID}"
|
|
|
|
# Create downloader instance
|
|
downloader = RedditDownloader(args)
|
|
downloader.download_directory = temp_path
|
|
downloader.file_name_formatter = RedditConnector.create_file_name_formatter(downloader)
|
|
|
|
# Mock a submission
|
|
submission = MagicMock()
|
|
submission.id = "test123"
|
|
submission.subreddit.display_name = "testsubreddit"
|
|
submission.author.name = "testuser"
|
|
submission.score = 100
|
|
submission.upvote_ratio = 0.8
|
|
submission.created_utc = 1640995200 # Jan 1, 2022
|
|
submission.url = "https://example.com/image.jpg"
|
|
submission.title = "Test Post"
|
|
|
|
# Mock the downloader chain
|
|
mock_downloader_class = MagicMock()
|
|
mock_downloader_class.__name__ = "MockDownloader"
|
|
|
|
mock_downloader = MagicMock()
|
|
mock_resource = MagicMock()
|
|
mock_resource.url = "https://example.com/image.jpg"
|
|
mock_resource.extension = "jpg"
|
|
mock_resource.hash.hexdigest.return_value = "duplicate_hash_12345"
|
|
mock_resource.content = b"fake image content"
|
|
|
|
mock_downloader.find_resources.return_value = [mock_resource]
|
|
|
|
# Set up the master hash list to contain our "duplicate" hash
|
|
test_hash = "duplicate_hash_12345"
|
|
existing_file = temp_path / "existing_file.jpg"
|
|
existing_file.parent.mkdir(parents=True, exist_ok=True)
|
|
existing_file.touch()
|
|
downloader.master_hash_list = {test_hash: existing_file}
|
|
|
|
# Mock the download factory
|
|
import bdfr.site_downloaders.download_factory as df
|
|
original_pull_lever = df.DownloadFactory.pull_lever
|
|
df.DownloadFactory.pull_lever = MagicMock(return_value=mock_downloader_class)
|
|
|
|
try:
|
|
# Call the download submission method
|
|
downloader._download_submission(submission)
|
|
|
|
# Check that no new directories were created (the fix)
|
|
subdirs = [d for d in temp_path.rglob("*") if d.is_dir() and d != temp_path]
|
|
print(f"Number of subdirectories created: {len(subdirs)}")
|
|
|
|
# With the fix, no new directories should be created for duplicates
|
|
# The only directory that might exist is the one we created for the existing file
|
|
assert len(subdirs) <= 1, f"Expected 0 or 1 subdirectories, but found {len(subdirs)}"
|
|
|
|
print("Test passed: No empty folders created for duplicate posts!")
|
|
|
|
finally:
|
|
# Restore original function
|
|
df.DownloadFactory.pull_lever = original_pull_lever
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_duplicate_folder_creation_fix()
|
|
print("All tests passed! The duplicate folder creation fix is working correctly.") |