"""This module holds serializers tests for ``reveal`` api."""

from django.core.urlresolvers import reverse

from easy_thumbnails.files import get_thumbnailer

from ..constants import REVEAL_CONFIGURATION_OPTIONS
from ..serializers import RevealBaseConfigSerializer, RevealPhotosSerializer
from .test_cases import RevealTestCase


class RevealBaseConfigSerializerTestCase(RevealTestCase):
    """Test the ``RevealBaseConfigSerializer``."""

    def test_to_representation_with_default_theme(self):
        """Test the ``to_representation`` method."""

        # Base config serializer should extend config
        serializer = RevealBaseConfigSerializer()

        # Using the default theme
        result = serializer.to_representation(self.session_with_default_theme)
        logo = self.session_with_default_theme.training.get_theme().reveal_logo

        # Test the thumbnail url field
        self.assertEqual(
            result['datas']['assets']['images']['final_logo_converge'],
            get_thumbnailer(logo)['reveal'].url
        )

        # Test the logo url field
        self.assertEqual(
            result['datas']['assets']['images']['final_logo'],
            logo.url
        )

        # Test the photo list url field
        photo_list_url = reverse(
            'apiv2:reveal-photos',
            kwargs={'session_uuid': self.session_with_default_theme.pk}
        )
        self.assertEqual(
            result['datas']['images']['url'],
            photo_list_url
        )

        # Test default fields
        self.check_dict(result, REVEAL_CONFIGURATION_OPTIONS)

    def test_to_representation_with_custom_theme(self):
        """Test the ``to_representation`` method."""

        # Base config serializer should extend config
        serializer = RevealBaseConfigSerializer()

        # Using the custom theme
        result = serializer.to_representation(self.session_with_custom_theme)
        logo = self.custom_theme.reveal_logo

        # Test the thumbnail url field
        self.assertEqual(
            result['datas']['assets']['images']['final_logo_converge'],
            get_thumbnailer(logo)['reveal'].url
        )

        # Test the logo url field
        self.assertEqual(
            result['datas']['assets']['images']['final_logo'],
            logo.url
        )

        # Test the photo list url field
        photo_list_url = reverse(
            'apiv2:reveal-photos',
            kwargs={'session_uuid': self.session_with_custom_theme.pk}
        )
        self.assertEqual(
            result['datas']['images']['url'],
            photo_list_url
        )

        # Test one default field
        self.assertIsNotNone(result['datas']['scene']['config_url'])

class RevealPhotosSerializerTestCase(RevealTestCase):
    """Test the ``RevealPhotosSerializer``."""

    def test_to_representation(self):
        """Test the ``to_representation`` method."""

        serializer = RevealPhotosSerializer()

        result = serializer.to_representation(self.trainee)

        # Test url field
        self.assertEqual(
            result['url'],
            get_thumbnailer(self.trainee.photo.img)['reveal_large'].url
        )

        # Test url_128x128 field
        self.assertEqual(
            result['url_128x128'],
            get_thumbnailer(self.trainee.photo.img)['reveal_small'].url
        )

        # Test url_text field
        self.assertEqual(
            result['url_text'],
            ''
        )
