Spaces:
Sleeping
Sleeping
Download tests/test_processor.py from Rahul4112/BackDropStudio: direct link, hf CLI and curl.
- Browser
- Download file 6.99 kB
-
https://huggingface.co/spaces/Rahul4112/BackDropStudio/resolve/main/tests/test_processor.py
- Command line
-
hf download hf://spaces/Rahul4112/BackDropStudio/tests/test_processor.py
-
curl -L -o test_processor.py https://huggingface.co/spaces/Rahul4112/BackDropStudio/resolve/main/tests/test_processor.py
6.99 kB
| import tempfile | |
| import unittest | |
| import zipfile | |
| from pathlib import Path | |
| try: | |
| from PIL import Image | |
| except ImportError: # pragma: no cover | |
| Image = None | |
| from app.processor import ( | |
| ZipSlipError, | |
| _save_kwargs_for, | |
| parse_bg_color, | |
| process_zip, | |
| safe_extract_zip, | |
| sanitize_folder_name, | |
| validate_output_mode, | |
| validate_quality_profile, | |
| ) | |
| class ProcessorValidationTests(unittest.TestCase): | |
| def test_parse_bg_color_accepts_rgb_and_hex(self): | |
| self.assertEqual(parse_bg_color("241, 242, 242"), (241, 242, 242)) | |
| self.assertEqual(parse_bg_color("#F1F2F2"), (241, 242, 242)) | |
| def test_parse_bg_color_rejects_invalid_values(self): | |
| with self.assertRaises(ValueError): | |
| parse_bg_color("300,0,0") | |
| with self.assertRaises(ValueError): | |
| parse_bg_color("#XYZ123") | |
| def test_sanitize_folder_name(self): | |
| self.assertEqual(sanitize_folder_name(" Grey "), "Grey") | |
| for unsafe in ("", "../Grey", "Grey/Blue", "Grey\\Blue", "C:Grey"): | |
| with self.subTest(unsafe=unsafe): | |
| with self.assertRaises(ValueError): | |
| sanitize_folder_name(unsafe) | |
| def test_validates_quality_profile_and_output_mode(self): | |
| self.assertEqual(validate_quality_profile("Pro"), "pro") | |
| self.assertEqual(validate_output_mode("lossless_png"), "lossless_png") | |
| with self.assertRaises(ValueError): | |
| validate_quality_profile("ultra") | |
| with self.assertRaises(ValueError): | |
| validate_output_mode("gif") | |
| class ZipSafetyTests(unittest.TestCase): | |
| def test_safe_extract_blocks_zip_slip(self): | |
| with tempfile.TemporaryDirectory() as temp_dir_name: | |
| temp_dir = Path(temp_dir_name) | |
| zip_path = temp_dir / "bad.zip" | |
| extract_dir = temp_dir / "extract" | |
| with zipfile.ZipFile(zip_path, "w") as archive: | |
| archive.writestr("../escape.txt", "nope") | |
| with self.assertRaises(ZipSlipError): | |
| safe_extract_zip(zip_path, extract_dir) | |
| self.assertFalse((temp_dir / "escape.txt").exists()) | |
| class ProcessZipTests(unittest.TestCase): | |
| def test_process_zip_preserves_filename_dimensions_and_reports_failures(self): | |
| with tempfile.TemporaryDirectory() as temp_dir_name: | |
| temp_dir = Path(temp_dir_name) | |
| zip_path = temp_dir / "input.zip" | |
| output_zip = temp_dir / "result.zip" | |
| image_path = temp_dir / "SKU-100.jpg" | |
| Image.new("RGB", (12, 8), (255, 255, 255)).save(image_path, "JPEG") | |
| with zipfile.ZipFile(zip_path, "w") as archive: | |
| archive.write(image_path, "SKU-100.jpg") | |
| archive.writestr("broken.png", b"not an image") | |
| archive.writestr("notes.txt", "ignore me") | |
| progress_events = [] | |
| def fake_remove(image): | |
| return image.convert("RGBA") | |
| result = process_zip( | |
| zip_path, | |
| output_zip, | |
| "Grey", | |
| (241, 242, 242), | |
| remover=fake_remove, | |
| progress_callback=lambda total, completed, failed: progress_events.append( | |
| (total, completed, failed) | |
| ), | |
| ) | |
| self.assertEqual(result.total, 2) | |
| self.assertEqual(result.completed, 2) | |
| self.assertEqual(result.failed_files, ["broken.png"]) | |
| self.assertTrue(output_zip.exists()) | |
| self.assertEqual(progress_events[0], (2, 0, [])) | |
| self.assertEqual(progress_events[-1], (2, 2, ["broken.png"])) | |
| with zipfile.ZipFile(output_zip) as archive: | |
| names = set(archive.namelist()) | |
| self.assertIn("Grey/SKU-100.jpg", names) | |
| self.assertNotIn("Grey/notes.txt", names) | |
| extracted = temp_dir / "out" | |
| archive.extract("Grey/SKU-100.jpg", extracted) | |
| with Image.open(extracted / "Grey" / "SKU-100.jpg") as output_image: | |
| self.assertEqual(output_image.size, (12, 8)) | |
| def test_lossless_png_output_has_exact_background_pixel_and_png_name(self): | |
| with tempfile.TemporaryDirectory() as temp_dir_name: | |
| temp_dir = Path(temp_dir_name) | |
| zip_path = temp_dir / "input.zip" | |
| output_zip = temp_dir / "result.zip" | |
| image_path = temp_dir / "SKU-200.jpg" | |
| Image.new("RGB", (4, 4), (255, 255, 255)).save(image_path, "JPEG") | |
| with zipfile.ZipFile(zip_path, "w") as archive: | |
| archive.write(image_path, "nested/SKU-200.jpg") | |
| def fake_remove(_image): | |
| cutout = Image.new("RGBA", (4, 4), (0, 0, 0, 0)) | |
| cutout.putpixel((1, 1), (20, 40, 60, 255)) | |
| cutout.putpixel((2, 1), (20, 40, 60, 255)) | |
| return cutout | |
| result = process_zip( | |
| zip_path, | |
| output_zip, | |
| "Grey", | |
| (241, 242, 242), | |
| quality_profile="pro", | |
| output_mode="lossless_png", | |
| remover=fake_remove, | |
| ) | |
| self.assertEqual(result.failed_files, []) | |
| with zipfile.ZipFile(output_zip) as archive: | |
| names = set(archive.namelist()) | |
| self.assertIn("Grey/SKU-200.png", names) | |
| extracted = temp_dir / "png-out" | |
| archive.extract("Grey/SKU-200.png", extracted) | |
| with Image.open(extracted / "Grey" / "SKU-200.png") as output_image: | |
| self.assertEqual(output_image.size, (4, 4)) | |
| self.assertEqual(output_image.getpixel((0, 0)), (241, 242, 242)) | |
| def test_all_failed_images_raise_clear_error(self): | |
| with tempfile.TemporaryDirectory() as temp_dir_name: | |
| temp_dir = Path(temp_dir_name) | |
| zip_path = temp_dir / "input.zip" | |
| output_zip = temp_dir / "result.zip" | |
| with zipfile.ZipFile(zip_path, "w") as archive: | |
| archive.writestr("broken.png", b"not an image") | |
| with self.assertRaisesRegex(RuntimeError, "All 1 image files failed"): | |
| process_zip( | |
| zip_path, | |
| output_zip, | |
| "Grey", | |
| (241, 242, 242), | |
| remover=lambda image: image.convert("RGBA"), | |
| ) | |
| def test_jpeg_save_kwargs_use_maximum_practical_quality(self): | |
| kwargs = _save_kwargs_for(Path("SKU-100.jpg"), {"icc_profile": b"icc", "exif": b"exif"}) | |
| self.assertEqual(kwargs["quality"], 100) | |
| self.assertEqual(kwargs["subsampling"], 0) | |
| self.assertTrue(kwargs["optimize"]) | |
| self.assertEqual(kwargs["icc_profile"], b"icc") | |
| self.assertEqual(kwargs["exif"], b"exif") | |
| if __name__ == "__main__": | |
| unittest.main() | |