"""This module contains the test cases for the redis models of the ``workshops`` app"""

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

from .test_cases import WorkshopTestCase


class WorkshopRedisModelTestCase(WorkshopTestCase, MTPTestCase):
    """Test case for the ``Workshop`` redis model"""

    def test_save_and_retrieve_session_trainee_question_score_should_work(self):
        """Test that saving and retrieving the score for a question should work"""

        # Save the score for the first question...
        self.workshop.redis_instance.save_trainee_score(
            self.training_session.pk,
            self.trainee.pk,
            self.workshop_question.pk,
            10
        )

        # ...And retrieve it
        self.assertEqual(
            self.workshop.redis_instance.get_trainee_score(
                self.training_session.pk,
                self.trainee.pk,
                self.workshop_question.pk,
            ),
            10
        )

        # We can override the previous score
        self.workshop.redis_instance.save_trainee_score(
            self.training_session.pk,
            self.trainee.pk,
            self.workshop_question.pk,
            20
        )

        self.assertEqual(
            self.workshop.redis_instance.get_trainee_score(
                self.training_session.pk,
                self.trainee.pk,
                self.workshop_question.pk,
            ),
            20
        )

    def test_retrieve_session_average_score_should_work(self):
        """Test that retrieving the session average score should work"""

        # If we have no scores, we should get 0
        self.assertEqual(
            self.workshop.redis_instance.get_average_session_score(
                self.training_session.pk
            ),
            0
        )

        # Add one score
        self.workshop.redis_instance.save_trainee_score(
            self.training_session.pk,
            self.trainee.pk,
            self.workshop_question.pk,
            10
        )

        # The average is 10
        self.assertEqual(
            self.workshop.redis_instance.get_average_session_score(
                self.training_session.pk
            ),
            10
        )

        # Add another score
        self.workshop.redis_instance.save_trainee_score(
            self.training_session.pk,
            self.trainees[1].pk,
            self.workshop_question.pk,
            20
        )

        # Now, the average is 15
        self.assertEqual(
            self.workshop.redis_instance.get_average_session_score(
                self.training_session.pk
            ),
            15
        )
