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

from django.contrib.contenttypes.models import ContentType

from mtp.apps.assessments.factories.activities import AssessableActivityPartUserScoreFactory
from mtp.apps.assessments.models import AssessableActivityPartUserScore
from mtp.apps.assessments.tests.test_cases import MTPComplexAssessmentTestCase
from mtp.apps.workshops.models import WorkshopStepQuestion

from ..models import CompetencyTree


class CompetencyTreeManagerTestCase(MTPComplexAssessmentTestCase):
    """Test the ``CompetencyTreeManager``."""

    def test_get_for_session(self):
        """Test the return value of ``get_for_session`` method."""

        self.assertEqual(
            CompetencyTree.objects.get_for_session(self.training_session),
            self.competency_tree
        )

    def test_get_for_job_profile(self):
        """Test the return value of ``get_for_job_profile`` method."""

        self.assertEqual(
            CompetencyTree.objects.get_for_job_profile(self.job_profile),
            self.competency_tree
        )


class AssessableActivityPartUserScoreManagerTestCase(MTPComplexAssessmentTestCase):
    """Test case for the ``AssessableActivityPartUserScoreManager`` manager."""

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

        super().setUpTestData()

        content_type = ContentType.objects.get_for_model(WorkshopStepQuestion)
        AssessableActivityPartUserScoreFactory(
            user=cls.trainee,
            session=cls.training_session,
            activity_part_id=cls.quiz_questions[0].id,
            activity_part_type=content_type,
        )
        AssessableActivityPartUserScoreFactory(
            user=cls.trainee,
            session=cls.training_session,
            activity_part_id=cls.quiz_questions[1].id,
            activity_part_type=content_type,
        )
        AssessableActivityPartUserScoreFactory(
            user=cls.trainees[1],
            session=cls.training_session,
            activity_part_id=cls.quiz_questions[1].id,
            activity_part_type=content_type,
        )

    def assertScoresCount(self, count):
        """Check the number of ``AssessableActivityPartUserScore`` instances."""

        scores_count = AssessableActivityPartUserScore.objects.count()
        self.assertEqual(scores_count, count)

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

        This test has been edited when migration which apply the new ``unique_together``
        has been merged. Now, we simply check ``clean_duplicates`` delete no instances
        because there are no duplicated instances.

        """

        self.assertScoresCount(3)

        AssessableActivityPartUserScore.objects.clean_duplicates(
            [('user', 'session', 'activity_part_id', 'activity_part_type')]
        )

        self.assertScoresCount(3)
