Repository: wagtail/wagtail
File: wagtail/images/tests/test_models.py (Python)

Code before:
            img.__html__(), f"<picture>{self.rendition_10.img_tag()}</picture>"
        )


@override_settings(
    CACHES={"default": {"BACKEND": "django.core.cache.backends.dummy.DummyCache"}}
)
class TestRenditions(TestCase):
    SPECS = ("height-66", "width-100", "width-400")

    def setUp(self):
        # Create an image for running tests on
        self.image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )

    def test_get_rendition_model(self):
        self.assertIs(Image.get_rendition_model(), Rendition)

    def test_stock_rendition_contains_unique_together_constraint(self):
        self.assertEqual([], Rendition.check())

    def test_custom_rendition_may_have_unique_constraint(self):
        self.assertEqual([], CustomRendition.check())

    def test_minification(self):
        rendition = self.image.get_rendition("width-400")

        # Check size
        self.assertEqual(rendition.width, 400)
        self.assertEqual(rendition.height, 300)

        # check that the rendition has been recorded under the correct filter,
        # via the Rendition.filter_spec attribute (in active use as of Wagtail 1.8)
        self.assertEqual(rendition.filter_spec, "width-400")

    def test_resize_to_max(self):
        rendition = self.image.get_rendition("max-100x100")

        # Check size
        self.assertEqual(rendition.width, 100)
        self.assertEqual(rendition.height, 75)

    def test_resize_to_min(self):
        rendition = self.image.get_rendition("min-120x120")

        # Check size
        self.assertEqual(rendition.width, 160)
        self.assertEqual(rendition.height, 120)


Review comment (suggestion):
Unfortunately these only test the "happy path" where either `unique_together` or `CustomRendition` is defined, but not when none of them are defined and the warning is triggered. I think we can follow the approach in #11112 where we dynamically modify the model's `._meta`, call `.check()`, and revert the modifications.

Code after:
        self.assertHTMLEqual(
            img.__html__(), f"<picture>{self.rendition_10.img_tag()}</picture>"
        )


@override_settings(
    CACHES={"default": {"BACKEND": "django.core.cache.backends.dummy.DummyCache"}}
)
class TestRenditions(TestCase):
    SPECS = ("height-66", "width-100", "width-400")

    def setUp(self):
        # Create an image for running tests on
        self.image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )

    def test_get_rendition_model(self):
        self.assertIs(Image.get_rendition_model(), Rendition)

    def test_stock_rendition_contains_unique_together_constraint(self):
        self.assertEqual([], Rendition.check())

    def test_custom_rendition_may_have_unique_constraint(self):
        self.assertEqual([], CustomRendition.check())

    def test_custom_rendition_without_unique_constraint_raises_error(self):
        custom_rendition_constraints = CustomRendition._meta.constraints
        try:
            CustomRendition._meta.constraints = []
            errors = CustomRendition.check()
        finally:
            CustomRendition._meta.constraints = custom_rendition_constraints

        self.assertEqual(CustomRendition._meta.unique_together, ())
        self.assertEqual(len(errors), 1)
        self.assertIsInstance(errors[0], checks.Error)
        self.assertEqual(errors[0].id, "wagtailimages.E001")
        self.assertEqual(errors[0].obj, CustomRendition)
        self.assertEqual(
            errors[0].msg,
            "Custom rendition model 'tests.CustomRendition' must include a unique "
            "constraint on the 'image', 'filter_spec', and 'focal_point_key' fields.",
        )
        self.assertEqual(
            errors[0].hint,
            "Add models.UniqueConstraint(fields={"
            '"image", "filter_spec", "focal_point_key"}, '
            'name="unique_rendition") to CustomRendition.Meta.constraints.',
        )