"""Base test cases for the ``Duplicable`` app."""

from django.test import override_settings, TestCase
from django.utils import timezone

from .duplicable_testapp.models import *


# Use a storage backend that stores file in memory to gain speed and avoid polluting the disk.
@override_settings(DEFAULT_STORAGE='inmemorystorage.storage.InMemoryStorage')
class DuplicableBaseTestCase(TestCase):
    """Base test case for the Duplicable app, creating lot of test data."""

    @classmethod
    def setUpTestData(cls):
        """Create instances of models defined in ``duplicable_testapp.models``"""

        super().setUpTestData()

        # Create a training
        cls.author = Person.objects.create(name='author')
        cls.training = Training.objects.create(name='training1', author=cls.author)

        # Some rewards
        cls.reward1 = Reward.objects.create(name='reward1')
        cls.reward2 = Reward.objects.create(name='reward2')

        # Some exercises and their solutions.
        cls.exercise1 = Exercise.objects.create(name='exercise1', training=cls.training)
        cls.exercise1.rewards = [cls.reward1]
        cls.solution1 = Solution.objects.create(name='solution1', exercise=cls.exercise1)
        cls.exercise2 = Exercise.objects.create(name='exercise2', training=cls.training)
        cls.exercise2.rewards = [cls.reward1, cls.reward2]
        cls.solution2 = Solution.objects.create(name='solution2', exercise=cls.exercise2)
        cls.exercise3 = Exercise.objects.create(name='exercise3', training=cls.training)
        cls.solution3 = Solution.objects.create(name='solution3', exercise=cls.exercise3)

        # Some hints
        cls.hint1 = Hint.objects.create(name='hint1', exercise=cls.exercise1)
        cls.hint2 = Hint.objects.create(name='hint2', exercise=cls.exercise2)

        # Create some plans
        cls.plan1 = Plan.objects.create(name='plan1', training=cls.training)
        cls.plannedexercise11 = PlannedExercise.objects.create(
            plan=cls.plan1, exercise=cls.exercise1, order=1)
        cls.plannedexercise12 = PlannedExercise.objects.create(
            plan=cls.plan1, exercise=cls.exercise2, order=2)
        cls.certificate1 = Certificate.objects.create(name='certificate1', plan=cls.plan1)

        cls.plan2 = Plan.objects.create(name='plan2', training=cls.training)
        cls.plannedexercise22 = PlannedExercise.objects.create(
            plan=cls.plan2, exercise=cls.exercise2, order=1)
        cls.plannedexercise23 = PlannedExercise.objects.create(
            plan=cls.plan2, exercise=cls.exercise3, order=2)
        cls.certificate2 = Certificate.objects.create(name='certificate2', plan=cls.plan2)

        # And some attendees
        cls.attendee1 = Person.objects.create(name='attendee1')
        cls.participated1 = ParticipatedTraining.objects.create(
            attendee=cls.attendee1,
            training=cls.training,
            date=timezone.now(),
            plan=cls.plan1
        )
        cls.attendee1.favorite_exercises = [cls.exercise1, cls.exercise3]

        cls.attendee2 = Person.objects.create(name='attendee2')
        cls.participated2 = ParticipatedTraining.objects.create(
            attendee=cls.attendee2,
            training=cls.training,
            date=timezone.now(),
            plan=cls.plan2
        )

        # Create a survey that should not be duplicated
        cls.survey = Survey.objects.create(name='survey1', training=cls.training)
