from django.test import TestCase

from wagtail.images.models import Filter
from wagtail.images.shortcuts import (
    get_rendition_or_not_found,
    get_renditions_or_not_found,
)

from .utils import Image, get_test_image_file


class TestShortcuts(TestCase):
    fixtures = ["test.json"]

    def test_fallback_to_not_found(self):
        bad_image = Image.objects.get(id=1)
        good_image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )

        rendition = get_rendition_or_not_found(good_image, "width-400")
        self.assertEqual(rendition.width, 400)

        rendition = get_rendition_or_not_found(bad_image, "width-400")
        self.assertEqual(rendition.file.name, "not-found")

    def test_multiple_fallback_to_not_found(self):
        bad_image = Image.objects.get(id=1)
        good_image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )

        renditions = get_renditions_or_not_found(good_image, ("width-200", "width-400"))
        self.assertEqual(tuple(renditions.keys()), ("width-200", "width-400"))
        self.assertEqual(renditions["width-200"].width, 200)
        self.assertEqual(renditions["width-400"].width, 400)

        renditions = get_renditions_or_not_found(bad_image, ("width-200", "width-400"))
        self.assertEqual(tuple(renditions.keys()), ("width-200", "width-400"))
        self.assertEqual(renditions["width-200"].file.name, "not-found")
        self.assertEqual(renditions["width-400"].file.name, "not-found")

    def test_multiple_fallback_to_not_found_with_filters(self):
        bad_image = Image.objects.get(id=1)
        good_image = Image.objects.create(
            title="Test image",
            file=get_test_image_file(),
        )

        renditions = get_renditions_or_not_found(
            good_image, (Filter("width-200"), Filter("width-400"))
        )
        self.assertEqual(tuple(renditions.keys()), ("width-200", "width-400"))
        self.assertEqual(renditions["width-200"].width, 200)
        self.assertEqual(renditions["width-400"].width, 400)

        renditions = get_renditions_or_not_found(
            bad_image, (Filter("width-200"), Filter("width-400"))
        )
        self.assertEqual(tuple(renditions.keys()), ("width-200", "width-400"))
        self.assertEqual(renditions["width-200"].file.name, "not-found")
        self.assertEqual(renditions["width-400"].file.name, "not-found")
