|
| 1 | +import io |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.core.exceptions import ValidationError |
| 5 | +from django.core.files.uploadedfile import SimpleUploadedFile |
| 6 | +from PIL import Image |
| 7 | + |
| 8 | +from pictures import validators |
| 9 | +from tests.testapp.models import ValidatorModel |
| 10 | + |
| 11 | + |
| 12 | +class TestBaseSizeValidator: |
| 13 | + def test_init__none(self): |
| 14 | + assert validators.MinSizeValidator(None, None).limit_value == ( |
| 15 | + float("inf"), |
| 16 | + float("inf"), |
| 17 | + ) |
| 18 | + |
| 19 | + def test_compare(self): |
| 20 | + assert validators.BaseSizeValidator(None, None).compare(None) == NotImplemented |
| 21 | + |
| 22 | + |
| 23 | +class TestMaxSizeValidator: |
| 24 | + def test_compare__inf(self): |
| 25 | + limit_value = float("inf"), float("inf") |
| 26 | + instance = validators.MaxSizeValidator(*limit_value) |
| 27 | + assert not instance.compare((300, 200), limit_value) |
| 28 | + |
| 29 | + def test_compare__eq(self): |
| 30 | + assert not validators.MaxSizeValidator(300, 200).compare((300, 200), (300, 200)) |
| 31 | + |
| 32 | + def test_compare__gt(self): |
| 33 | + limit_value = 300, 200 |
| 34 | + instance = validators.MaxSizeValidator(*limit_value) |
| 35 | + assert instance.compare((600, 400), limit_value) |
| 36 | + assert instance.compare((600, 200), limit_value) |
| 37 | + assert instance.compare((300, 400), limit_value) |
| 38 | + assert instance.compare((600, 100), limit_value) |
| 39 | + assert instance.compare((150, 400), limit_value) |
| 40 | + |
| 41 | + def test_compare__lt(self): |
| 42 | + limit_value = 300, 200 |
| 43 | + instance = validators.MaxSizeValidator(*limit_value) |
| 44 | + assert not instance.compare((150, 100), (300, 200)) |
| 45 | + assert not instance.compare((300, 100), (300, 200)) |
| 46 | + assert not instance.compare((150, 200), (300, 200)) |
| 47 | + |
| 48 | + @pytest.mark.django_db |
| 49 | + def test_integration(self): |
| 50 | + img = Image.new("RGB", (800, 800), (255, 55, 255)) |
| 51 | + |
| 52 | + with io.BytesIO() as output: |
| 53 | + img.save(output, format="JPEG") |
| 54 | + file = SimpleUploadedFile("image.jpg", output.getvalue()) |
| 55 | + |
| 56 | + obj = ValidatorModel(picture=file) |
| 57 | + with pytest.raises(ValidationError) as e: |
| 58 | + obj.full_clean() |
| 59 | + |
| 60 | + assert "The required maximum resolution is: 800x600 px." in str( |
| 61 | + e.value.error_dict["picture"][0] |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +class TestMinSizeValidator: |
| 66 | + def test_compare__inf(self): |
| 67 | + limit_value = float("inf"), float("inf") |
| 68 | + instance = validators.MinSizeValidator(*limit_value) |
| 69 | + assert instance.compare((300, 200), limit_value) |
| 70 | + |
| 71 | + def test_compare__eq(self): |
| 72 | + assert not validators.MinSizeValidator(300, 200).compare((300, 200), (300, 200)) |
| 73 | + |
| 74 | + def test_compare__gt(self): |
| 75 | + limit_value = 300, 200 |
| 76 | + instance = validators.MinSizeValidator(*limit_value) |
| 77 | + assert not instance.compare((600, 400), limit_value) |
| 78 | + assert not instance.compare((600, 200), limit_value) |
| 79 | + assert not instance.compare((300, 400), limit_value) |
| 80 | + assert instance.compare((600, 100), limit_value) |
| 81 | + assert instance.compare((150, 400), limit_value) |
| 82 | + |
| 83 | + def test_compare__lt(self): |
| 84 | + limit_value = 300, 200 |
| 85 | + instance = validators.MinSizeValidator(*limit_value) |
| 86 | + assert instance.compare((150, 100), (300, 200)) |
| 87 | + assert instance.compare((300, 100), (300, 200)) |
| 88 | + assert instance.compare((150, 200), (300, 200)) |
| 89 | + |
| 90 | + @pytest.mark.django_db |
| 91 | + def test_integration(self): |
| 92 | + img = Image.new("RGB", (300, 200), (255, 55, 255)) |
| 93 | + |
| 94 | + with io.BytesIO() as output: |
| 95 | + img.save(output, format="JPEG") |
| 96 | + file = SimpleUploadedFile("image.jpg", output.getvalue()) |
| 97 | + |
| 98 | + obj = ValidatorModel(picture=file) |
| 99 | + with pytest.raises(ValidationError) as e: |
| 100 | + obj.full_clean() |
| 101 | + |
| 102 | + assert "The required minimum resolution is: 400x300 px." in str( |
| 103 | + e.value.error_dict["picture"][0] |
| 104 | + ) |
0 commit comments