"""This module holds the tests for the mixins of the ``apiv2`` module of ``assessments`` app"""

from rest_framework.serializers import BaseSerializer

from mtp.apps.common.tests.test_cases import MTPTestCase

from ..mixins import UserContextSerializerMixin, SessionContextSerializerMixin


class FakeUserContextSerializer(UserContextSerializerMixin, BaseSerializer):
    """A dummy class that inherits from our user mixin and a base serializer"""

    pass


class FakeSessionContextSerializer(SessionContextSerializerMixin, BaseSerializer):
    """A dummy class that inherits from our session mixin and a base serializer"""

    pass


class UserContextSerializerMixinTestCase(MTPTestCase):
    """Test the ``UserContextSerializerMixin`` mixin"""

    def test_mixin_instantiates_correctly(self):
        """Test the mixin instantiates correctly"""

        serializer = FakeUserContextSerializer(context={'user': self.trainee})
        self.assertEqual(serializer.user, self.trainee)

    def test_mixin_fails_when_instanciated_with_wrong_context(self):
        """Test the mixin can not be instantiated when context is wrong"""

        with self.assertRaises(KeyError):
            FakeUserContextSerializer()


class SessionContextSerializerMixinTestCase(MTPTestCase):
    """Test the ``SessionContextSerializerMixin`` mixin"""

    def test_mixin_instantiates_correctly(self):
        """Test the mixin instantiates correctly"""

        serializer = FakeSessionContextSerializer(context={'session': self.training_session})
        self.assertEqual(serializer.session, self.training_session)

    def test_mixin_fails_when_instanciated_with_wrong_context(self):
        """Test the mixin can not be instantiated when context is wrong"""

        with self.assertRaises(KeyError):
            FakeSessionContextSerializer()
