"""Test the duplication for models with generic relations"""

from django.test import TestCase

from ..utils import duplicate

from .duplicable_testapp.models import *  # pylint: disable=wildcard-import


class DuplicableGenericTestCase(TestCase):
    """Test that the duplicate works with generic relations"""

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

        # One question
        cls.choice_question = ChoicesQuestion.objects.create()

        # One answer
        cls.choice_answer = Answer.objects.create(
            question=cls.choice_question
        )

    def test_duplicate(self):
        """Test the duplication feature with the generic relations"""

        new_obj, dup_objects = duplicate(self.choice_question)

        # Two objects should have been duplicated: the question and the answer
        self.assertEqual(len(dup_objects), 2)

        dup_objects = dict(dup_objects)

        # The question have a different pk
        self.assertEqual(dup_objects[self.choice_question].pk, new_obj.pk)
        self.assertNotEqual(new_obj.pk, self.choice_question.pk)

        # The answer have a different pk, and is linked to the newly duplicated question
        self.assertNotEqual(dup_objects[self.choice_answer].pk, self.choice_answer.pk)
        self.assertEqual(dup_objects[self.choice_answer].question, new_obj)
