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


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

from ..constants import POTENTIAL_DUPLICATE_REASONS


class MTPUserTestCase(MTPTestCase):
    """Test case for the redis model of the ``MTPUser`` django model"""

    def setUp(self):
        """Clear the limpyd field for the test needs"""

        super().setUp()

        self.trainee.redis_instance.potential_duplicates_ids.delete()

    def test_flag_user_as_potential_duplicate(self):
        """Test the ``flag_user_as_potential_duplicate`` method"""

        # Flag the trainee 2 as a potential duplicate of the trainee 1
        self.trainee.redis_instance.flag_user_as_potential_duplicate(
            self.trainees[1],
            POTENTIAL_DUPLICATE_REASONS.SAME_LAST_AND_FIRST_NAME
        )

        # If we try to add again the trainee 2 as a duplicate, this should fail...
        with self.assertRaises(ValueError) as exception_context:
            self.trainee.redis_instance.flag_user_as_potential_duplicate(
                self.trainees[1],
                POTENTIAL_DUPLICATE_REASONS.SAME_LAST_AND_FIRST_NAME
            )

        self.assertEqual(
            str(exception_context.exception),
            'The user with id "%s" is already flagged as a potential duplicate' % (
                self.trainees[1].id
            )
        )

        # ...But we can a different user
        self.trainee.redis_instance.flag_user_as_potential_duplicate(
            self.trainees[2],
            POTENTIAL_DUPLICATE_REASONS.USERNAME_CONFLICT
        )

        # We got the user ids / reasons in the limpyd field
        self.assertEqual(
            self.trainee.redis_instance.potential_duplicates_ids.hgetall(),
            {
                self.trainees[1].id: POTENTIAL_DUPLICATE_REASONS.SAME_LAST_AND_FIRST_NAME,
                self.trainees[2].id: POTENTIAL_DUPLICATE_REASONS.USERNAME_CONFLICT
            }
        )

    def test_remove_user_as_potential_duplicate(self):
        """Test the ``remove_user_as_potential_duplicate`` method"""

        # Flag the trainee 2 as a potential duplicate of the trainee 1
        self.trainee.redis_instance.flag_user_as_potential_duplicate(
            self.trainees[1],
            POTENTIAL_DUPLICATE_REASONS.SAME_LAST_AND_FIRST_NAME
        )

        # We got the user id / reason in the limpyd field
        self.assertEqual(
            self.trainee.redis_instance.potential_duplicates_ids.hgetall(),
            {
                self.trainees[1].id: POTENTIAL_DUPLICATE_REASONS.SAME_LAST_AND_FIRST_NAME
            }
        )

        # Remove the trainee 2 as a potential duplicate of the trainee 1
        self.trainee.redis_instance.remove_user_as_potential_duplicate(
            self.trainees[1]
        )

        # Empty potential duplicate list
        self.assertEqual(
            self.trainee.redis_instance.potential_duplicates_ids.hgetall(),
            {}
        )

