"""This module contains the test cases for the V2 api views of the ``assessments`` app"""

from django.core.urlresolvers import reverse

from rest_framework import status

from mtp.apps.api.tests.test_cases import MTPAPITestCase

from mtp.apps.assessments.models import LessonReference


class LessonReferenceCreateViewTestCase(MTPAPITestCase):
    """Test case for the ``LessonReferenceCreateView`` view"""

    @classmethod
    def setUpTestData(cls):
        """Store the url of the view"""

        super().setUpTestData()

        cls.url = reverse('apiv2:create-lesson-references')

    def test_view_should_create_lesson_references(self):
        """Test that the view should create lesson references"""

        self.login_as_admin()
        response = self.client.post(
            self.url,
            data=[
                {
                    'external_id': 'test_id',
                    'name': 'test_name'
                },
                {
                    'external_id': 'test_id_2',
                    'name': 'test_name_2'
                }
            ],
            format='json'
        )

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        # This should fail if the lesson references weren't created
        LessonReference.objects.get(external_id='test_id', external_name='test_name')
        LessonReference.objects.get(external_id='test_id_2', external_name='test_name_2')

    def test_view_should_update_a_previously_created_lesson_reference(self):
        """Test that the view should update a previsously created reference"""

        # Create manually a lesson reference
        lesson_reference = LessonReference.objects.create(
            external_id='test_id',
            external_name='test_name'
        )

        self.login_as_admin()
        self.client.post(
            self.url,
            data=[
                {
                    'external_id': 'test_id',
                    'name': 'new_name'
                }
            ],
            format='json'
        )

        lesson_reference.refresh_from_db()
        self.assertEqual(lesson_reference.external_name, 'new_name')
