File size: 4,353 Bytes
278a64b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
"""
Test script to verify thumbnail conversion works locally.
This script can be run to test the thumbnail conversion logic without affecting production data.
"""

import os
import sys
import logging
from typing import List

# Add the current directory to Python path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from app.database import SessionLocal
from app.models import Images
from app.services.thumbnail_service import ImageProcessingService

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def test_thumbnail_service():
    """Test the thumbnail service with sample data"""
    logger.info("Testing thumbnail service...")
    
    # Test with a sample image (you can replace this with actual test data)
    sample_image_data = b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00H\x00H\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.\' ",#\x1c\x1c(7),01444\x1f\'9=82<.342\xff\xc0\x00\x11\x08\x00\x10\x00\x10\x01\x01\x11\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x14\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00\x3f\x00\xaa\xff\xd9'
    
    try:
        # Test WebP thumbnail creation
        thumbnail_bytes, thumbnail_filename = ImageProcessingService.create_thumbnail(
            sample_image_data, 
            "test_image.jpg"
        )
        
        if thumbnail_bytes and thumbnail_filename:
            logger.info(f"βœ… WebP thumbnail created successfully: {thumbnail_filename}")
            logger.info(f"   Thumbnail size: {len(thumbnail_bytes)} bytes")
            
            # Check if it's actually WebP format
            if thumbnail_bytes.startswith(b'RIFF') and thumbnail_bytes[8:12] == b'WEBP':
                logger.info("βœ… Thumbnail is valid WebP format")
            else:
                logger.warning("⚠️ Thumbnail doesn't appear to be WebP format")
        else:
            logger.error("❌ Failed to create WebP thumbnail")
            return False
        
        # Test WebP detail image creation
        detail_bytes, detail_filename = ImageProcessingService.create_detail_image(
            sample_image_data, 
            "test_image.jpg"
        )
        
        if detail_bytes and detail_filename:
            logger.info(f"βœ… WebP detail image created successfully: {detail_filename}")
            logger.info(f"   Detail image size: {len(detail_bytes)} bytes")
            
            # Check if it's actually WebP format
            if detail_bytes.startswith(b'RIFF') and detail_bytes[8:12] == b'WEBP':
                logger.info("βœ… Detail image is valid WebP format")
            else:
                logger.warning("⚠️ Detail image doesn't appear to be WebP format")
        else:
            logger.error("❌ Failed to create WebP detail image")
            return False
        
        return True
        
    except Exception as e:
        logger.error(f"❌ Error testing thumbnail service: {e}")
        return False

def test_database_connection():
    """Test database connection and count images"""
    logger.info("Testing database connection...")
    
    try:
        db = SessionLocal()
        image_count = db.query(Images).count()
        logger.info(f"βœ… Database connection successful. Found {image_count} images.")
        db.close()
        return True
    except Exception as e:
        logger.error(f"❌ Database connection failed: {e}")
        return False

def main():
    """Main test function"""
    logger.info("Starting thumbnail conversion tests...")
    
    # Test 1: Thumbnail service
    if not test_thumbnail_service():
        logger.error("Thumbnail service test failed!")
        return 1
    
    # Test 2: Database connection
    if not test_database_connection():
        logger.error("Database connection test failed!")
        return 1
    
    logger.info("βœ… All tests passed! Thumbnail conversion should work properly.")
    return 0

if __name__ == "__main__":
    exit_code = main()
    sys.exit(exit_code)