161 lines
5.5 KiB
Python
161 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
Test script to verify user folder structure changes.
|
|
|
|
This script tests that:
|
|
1. Subreddit downloads go to: downloads/subreddit_name/files
|
|
2. User downloads go to: downloads/username/subreddit_name/files
|
|
"""
|
|
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
# Set UTF-8 encoding for Windows console
|
|
if sys.platform == 'win32':
|
|
import codecs
|
|
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
|
|
sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')
|
|
|
|
from bdfr.api import BDFRManager, DownloadType
|
|
from bdfr.configuration import Configuration
|
|
|
|
|
|
def test_subreddit_directory_structure():
|
|
"""Test that subreddit downloads use correct directory structure"""
|
|
print("\n=== Testing Subreddit Directory Structure ===")
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
manager = BDFRManager(download_directory=tmpdir)
|
|
|
|
# Create a download for a subreddit
|
|
download_id = manager.create_download(
|
|
DownloadType.SUBREDDIT,
|
|
"test_subreddit"
|
|
)
|
|
|
|
download_info = manager.get_download_status(download_id)
|
|
config = download_info["config"]
|
|
|
|
expected_dir = str(Path(tmpdir))
|
|
actual_dir = config.directory
|
|
|
|
print(f"Expected directory: {expected_dir}")
|
|
print(f"Actual directory: {actual_dir}")
|
|
print(f"Subreddit config: {config.subreddit}")
|
|
print(f"Folder scheme: {config.folder_scheme}")
|
|
|
|
assert actual_dir == expected_dir, f"Subreddit directory mismatch!"
|
|
print("[OK] Subreddit directory structure is correct")
|
|
print(f" Files will be saved to: {actual_dir}/{{SUBREDDIT}}/{{files}}")
|
|
|
|
|
|
def test_user_directory_structure():
|
|
"""Test that user downloads use correct directory structure"""
|
|
print("\n=== Testing User Directory Structure ===")
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
manager = BDFRManager(download_directory=tmpdir)
|
|
|
|
# Create a download for a user
|
|
username = "test_user"
|
|
download_id = manager.create_download(
|
|
DownloadType.USER,
|
|
username
|
|
)
|
|
|
|
download_info = manager.get_download_status(download_id)
|
|
config = download_info["config"]
|
|
|
|
expected_dir = str(Path(tmpdir) / username)
|
|
actual_dir = config.directory
|
|
|
|
print(f"Expected directory: {expected_dir}")
|
|
print(f"Actual directory: {actual_dir}")
|
|
print(f"User config: {config.user}")
|
|
print(f"Folder scheme: {config.folder_scheme}")
|
|
|
|
assert actual_dir == expected_dir, f"User directory mismatch!"
|
|
print("[OK] User directory structure is correct")
|
|
print(f" Files will be saved to: {actual_dir}/{{SUBREDDIT}}/{{files}}")
|
|
|
|
|
|
def test_convenience_method():
|
|
"""Test the convenience method download_user()"""
|
|
print("\n=== Testing download_user() Convenience Method ===")
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
manager = BDFRManager(download_directory=tmpdir)
|
|
|
|
# Don't actually start the download, just check the config
|
|
username = "convenience_test_user"
|
|
|
|
# Create config manually like the convenience method does
|
|
config = Configuration()
|
|
user_directory = Path(tmpdir) / username
|
|
config.directory = str(user_directory)
|
|
config.user = [username]
|
|
|
|
expected_dir = str(Path(tmpdir) / username)
|
|
actual_dir = config.directory
|
|
|
|
print(f"Expected directory: {expected_dir}")
|
|
print(f"Actual directory: {actual_dir}")
|
|
print(f"User config: {config.user}")
|
|
|
|
assert actual_dir == expected_dir, f"Convenience method directory mismatch!"
|
|
print("[OK] Convenience method directory structure is correct")
|
|
|
|
|
|
def demonstrate_folder_structure():
|
|
"""Demonstrate the folder structure for both download types"""
|
|
print("\n=== Folder Structure Demonstration ===")
|
|
print("\nWhen downloading from a SUBREDDIT 'python':")
|
|
print(" downloads/")
|
|
print(" └── python/")
|
|
print(" ├── file1.jpg")
|
|
print(" ├── file2.png")
|
|
print(" └── file3.mp4")
|
|
|
|
print("\nWhen downloading from a USER 'spez' who posts to multiple subreddits:")
|
|
print(" downloads/")
|
|
print(" └── spez/")
|
|
print(" ├── python/")
|
|
print(" │ ├── file1.jpg")
|
|
print(" │ └── file2.png")
|
|
print(" ├── announcements/")
|
|
print(" │ └── file3.jpg")
|
|
print(" └── pics/")
|
|
print(" └── file4.png")
|
|
|
|
print("\n[OK] This structure allows:")
|
|
print(" 1. Easy identification of user-specific downloads")
|
|
print(" 2. Organization by subreddit within each user folder")
|
|
print(" 3. No conflicts between subreddit and user downloads")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("=" * 60)
|
|
print("Testing User Folder Structure Changes")
|
|
print("=" * 60)
|
|
|
|
try:
|
|
test_subreddit_directory_structure()
|
|
test_user_directory_structure()
|
|
test_convenience_method()
|
|
demonstrate_folder_structure()
|
|
|
|
print("\n" + "=" * 60)
|
|
print("[SUCCESS] All tests passed!")
|
|
print("=" * 60)
|
|
|
|
except AssertionError as e:
|
|
print(f"\n[FAIL] Test failed: {e}")
|
|
exit(1)
|
|
except Exception as e:
|
|
print(f"\n[ERROR] Unexpected error: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
exit(1) |