"""This module holds the tests for the serializers of the ``brainstormers`` app"""

from mtp.apps.brainstormers.tests.test_cases import BrainstormerTestCase
from mtp.apps.brainstormers.factories import BrainstormerFactory, BrainstormerPropositionFactory

from ..serializers import (
    BrainstormerPropositionSerializer,
    BrainstormerPropositionGroupSerializer,
)


class BrainstormerPropositionGroupSerializerTestCase(BrainstormerTestCase):
    """Test cases for the ``BrainstormerPropositionGroupSerializer`` serializer"""

    @classmethod
    def setUpTestData(cls):
        """Set up the test data"""

        super().setUpTestData()

        # We will need sample data to serialize
        cls.brainstormer_proposition_groups = {
            'brainstormer': cls.brainstormer.pk,
            'propositions': [
                {
                    'group': group.pk
                }
                for group in BrainstormerPropositionFactory.create_batch(3)
            ],
            'order': 0,
            'x': 10.10,
            'y': 11.10
        }

    def test_serializer_with_valid_data(self):
        """test the serializer with valid data"""

        serialized = BrainstormerPropositionGroupSerializer(
            data=self.brainstormer_proposition_groups)

        self.assertTrue(serialized.is_valid())


class BrainstomerPropositionSerializerTestCase(BrainstormerTestCase):
    """Test case for the ``BrainstomerPropositionSerializer`` serializer"""

    @classmethod
    def setUpTestData(cls):
        """Create one braindtormer and one proposition"""

        super().setUpTestData()

        cls.brainstormer = BrainstormerFactory()
        cls.proposition = BrainstormerPropositionFactory(
            group=cls.brainstormer.groups.first()
        )

    def test_serializer_output_should_not_include_the_deleted_likes(self):
        """Test that the serializer should not include the deleted likes"""

        # At first, no likes on this proposition
        self.assertEqual(
            len(BrainstormerPropositionSerializer(self.proposition).data['likes']),
            0
        )

        # One like
        self.trainee.like(self.proposition)
        self.assertEqual(
            len(BrainstormerPropositionSerializer(self.proposition).data['likes']),
            1
        )

        # One "un-like"
        self.trainee.unlike(self.proposition)
        self.assertEqual(
            len(BrainstormerPropositionSerializer(self.proposition).data['likes']),
            0
        )

