"""This module holds the tests of the models for the ``roleplays`` app"""

from ..factories import RatingFactory

from .test_cases import RoleplayTestCase


class RatingResultTestCases(RoleplayTestCase):
    """Test cases for the ``Rating`` ``result`` model"""

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

        super().setUpTestData()

        with cls.training.override_version_state(cls.training.VERSION_STATES.DRAFT):
            cls.roleplay_number = cls.roleplay.duplicate()
            cls.roleplay_number.criteria_rating_type = cls.roleplay.RATING_TYPES.NUMBERS
            cls.roleplay_number.save()

    def new_rating(self, roleplay, score):
        """Shortcut method to save a new ``Rating`` for the given roleplay"""

        criterion = roleplay.steps.first().criteria.first()

        return RatingFactory(
            training_session=self.training_session,
            trainee=self.trainee,
            criterion=criterion,
            score=score
        )

    def test_result_without_scale_should_work(self):
        """Test the result method without a rating_scale"""

        # Ensure we don't get any rating scale set
        with self.training.override_version_state(self.training.VERSION_STATES.DRAFT):
            self.roleplay.rating_scale = None
            self.roleplay.save()
            self.roleplay_number.rating_scale = None
            self.roleplay_number.save()

        self.assertEqual(
            self.new_rating(
                roleplay=self.roleplay,
                score=10).result,
            10
        )

        self.assertEqual(
            self.new_rating(
                roleplay=self.roleplay_number,
                score=10).result,
            10
        )

    def test_result_with_a_scale_should_work(self):
        """Test the result method with a rating_scale"""

        # First, we need to set the rating_scale on our two roleplays
        with self.training.override_version_state(self.training.VERSION_STATES.DRAFT):
            self.roleplay.rating_scale = {
                "A": 100, "B": 50
            }
            self.roleplay.save()
            self.roleplay_number.rating_scale = {
                "1": 0, "2": 100
            }
            self.roleplay_number.save()

        self.assertEqual(
            self.new_rating(
                roleplay=self.roleplay,
                score=1).result,
            50
        )

        self.assertEqual(
            self.new_rating(
                roleplay=self.roleplay_number,
                score=2).result,
            100
        )
