|
1 | 1 | import tempfile |
2 | 2 | from pathlib import Path |
3 | 3 |
|
4 | | -from django.core.files.uploadedfile import ( |
5 | | - SimpleUploadedFile, |
6 | | -) |
| 4 | +from django.core.files.uploadedfile import SimpleUploadedFile |
7 | 5 |
|
8 | | -from easy_images.engine import efficient_load, scale_image |
| 6 | +from easy_images.engine import _new_image, efficient_load, scale_image |
9 | 7 | from easy_images.options import ParsedOptions |
10 | 8 | from pyvips import Image |
11 | 9 |
|
@@ -68,3 +66,35 @@ def test_scale(): |
68 | 66 |
|
69 | 67 | scaled_not_upscale = scale_image(small_src, (400, 500), contain=True) |
70 | 68 | assert (scaled_not_upscale.width, scaled_not_upscale.height) == (100, 100) |
| 69 | + |
| 70 | + |
| 71 | +def test_new_image_file_handling(): |
| 72 | + """Test the file read for different File backends.""" |
| 73 | + |
| 74 | + # Create a simple test image to work with |
| 75 | + image = Image.black(100, 100) |
| 76 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 77 | + image_path = Path(tmpdir) / "test.jpg" |
| 78 | + image.write_to_file(str(image_path)) |
| 79 | + |
| 80 | + # Test with a path string - should work |
| 81 | + result = _new_image(str(image_path), "sequential") |
| 82 | + assert result is not None |
| 83 | + assert result.width == 100 |
| 84 | + assert result.height == 100 |
| 85 | + |
| 86 | + # Test with a Path object - should also work |
| 87 | + result2 = _new_image(image_path, "sequential") |
| 88 | + assert result2 is not None |
| 89 | + assert result2.width == 100 |
| 90 | + assert result2.height == 100 |
| 91 | + |
| 92 | + # Test with SimpleUploadedFile (uses read/buffer path) |
| 93 | + with open(image_path, "rb") as f: |
| 94 | + content = f.read() |
| 95 | + simple_file = SimpleUploadedFile("test.jpg", content, content_type="image/jpeg") |
| 96 | + |
| 97 | + result3 = _new_image(simple_file, "sequential") |
| 98 | + assert result3 is not None |
| 99 | + assert result3.width == 100 |
| 100 | + assert result3.height == 100 |
0 commit comments