"""This module holds managers tests for ``workshops`` app."""

from mtp.apps.workshops.factories import WorkshopStepQuestionUserAnswerFactory
from mtp.apps.workshops.models import WorkshopStepQuestionUserAnswer
from mtp.apps.workshops.tests.test_cases import WorkshopTestCase


class WorkshopStepQuestionUserAnswerManagerTestCase(WorkshopTestCase):
    """Test case for the ``WorkshopStepQuestionUserAnswerManager`` manager."""

    @classmethod
    def setUpTestData(cls):
        """Create several ``WorkshopStepQuestionUserAnswer`` instance."""

        super().setUpTestData()

        WorkshopStepQuestionUserAnswerFactory(
            user=cls.trainee,
            training_session=cls.training_session,
            question=cls.workshop_question_mc,
            answer=cls.answers[0]
        )
        WorkshopStepQuestionUserAnswerFactory(
            user=cls.trainee,
            training_session=cls.training_session,
            question=cls.workshop_question_mc,
            answer=cls.answers[1]
        )
        WorkshopStepQuestionUserAnswerFactory(
            user=cls.trainees[1],
            training_session=cls.training_session,
            question=cls.workshop_question_mc,
            answer=cls.answers[0]
        )

    def assertUserAnswersCount(self, count):
        """Check the number of ``WorkshopStepQuestionUserAnswer`` instances."""

        user_answers_count = WorkshopStepQuestionUserAnswer.objects.count()
        self.assertEqual(user_answers_count, count)

    def test_clean_duplicates(self):
        """Test the ``clean_duplicates`` manager method."""

        self.assertUserAnswersCount(3)

        WorkshopStepQuestionUserAnswer.objects.clean_duplicates(
            (('user', 'training_session', 'answer', 'question'),)
        )

        self.assertUserAnswersCount(3)
