119 lines
3.4 KiB
Python
119 lines
3.4 KiB
Python
import io
|
|
from django.test import TestCase
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
|
from PIL import Image
|
|
from rest_framework.test import APIClient
|
|
|
|
|
|
class ImageCompressViewTest(TestCase):
|
|
def setUp(self):
|
|
self.client = APIClient()
|
|
self.image = self.create_test_image()
|
|
|
|
def create_test_image(self):
|
|
img = Image.new('RGB', (100, 100), color='red')
|
|
output = io.BytesIO()
|
|
img.save(output, format='JPEG')
|
|
output.seek(0)
|
|
return SimpleUploadedFile(
|
|
'test.jpg',
|
|
output.getvalue(),
|
|
content_type='image/jpeg'
|
|
)
|
|
|
|
def test_compress_lossy(self):
|
|
response = self.client.post(
|
|
'/tool/image-compress/',
|
|
{
|
|
'file': self.image,
|
|
'mode': 'lossy',
|
|
'quality': 80,
|
|
'format': 'jpeg'
|
|
},
|
|
format='multipart'
|
|
)
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn('X-Original-Size', response)
|
|
self.assertIn('X-Compressed-Size', response)
|
|
self.assertIn('X-Compression-Ratio', response)
|
|
|
|
def test_compress_lossless(self):
|
|
response = self.client.post(
|
|
'/tool/image-compress/',
|
|
{
|
|
'file': self.image,
|
|
'mode': 'lossless',
|
|
'format': 'jpeg'
|
|
},
|
|
format='multipart'
|
|
)
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_invalid_file(self):
|
|
invalid_file = SimpleUploadedFile(
|
|
'test.txt',
|
|
b'not an image',
|
|
content_type='text/plain'
|
|
)
|
|
response = self.client.post(
|
|
'/tool/image-compress/',
|
|
{'file': invalid_file, 'mode': 'lossy'},
|
|
format='multipart'
|
|
)
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
|
class EXIFExtractViewTest(TestCase):
|
|
def setUp(self):
|
|
self.client = APIClient()
|
|
|
|
def test_extract_exif(self):
|
|
img = Image.new('RGB', (100, 100), color='red')
|
|
output = io.BytesIO()
|
|
img.save(output, format='JPEG')
|
|
output.seek(0)
|
|
|
|
image_file = SimpleUploadedFile(
|
|
'test.jpg',
|
|
output.getvalue(),
|
|
content_type='image/jpeg'
|
|
)
|
|
|
|
response = self.client.post(
|
|
'/tool/image-compress/exif/',
|
|
{'file': image_file},
|
|
format='multipart'
|
|
)
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertIn('exif', response.json())
|
|
|
|
|
|
class CompressionHistoryViewTest(TestCase):
|
|
def setUp(self):
|
|
self.client = APIClient()
|
|
|
|
def test_list_history(self):
|
|
response = self.client.get('/tool/image-compress/history/')
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(response.json()['data'], [])
|
|
|
|
def test_create_history(self):
|
|
data = {
|
|
'session_id': 'test_session',
|
|
'original_filename': 'test.jpg',
|
|
'original_size': 1000,
|
|
'compressed_size': 500,
|
|
'compression_ratio': 50.0,
|
|
'original_format': 'jpeg',
|
|
'output_format': 'jpeg',
|
|
'compression_mode': 'lossy',
|
|
'quality': 80,
|
|
'file_path': '/tmp/test.jpg',
|
|
}
|
|
response = self.client.post(
|
|
'/tool/image-compress/history/',
|
|
data,
|
|
format='json'
|
|
)
|
|
self.assertEqual(response.status_code, 201)
|