"""This module contains the test cases for the v2 api views of the ``metaplans`` app"""

from django.core.urlresolvers import reverse

from rest_framework import status

from mtp.apps.api.tests.test_cases import MTPAPITestCase
from mtp.apps.metaplans.factories import MetaplanPropositionFactory
from mtp.apps.metaplans.tests.test_cases import BaseMetaplanTestCase


class MetaplanViewSetTestCase(BaseMetaplanTestCase, MTPAPITestCase):
    """Test cases for the ``MetaplanViewSet`` api view"""

    def test_retrieve_route_should_output_correct_data(self):
        """Test that the retreive route should output correct data"""

        url = reverse('apiv2:metaplan-detail', kwargs={
            'pk': self.metaplan.pk,
        })

        self.login_as_trainee()
        response = self.client.get(url)

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

        # No propositions yet
        self.assertEqual(
            response.data,
            {
                'id': self.metaplan.id,
                'name': self.metaplan.name,
                'propositions': [],
            }
        )

        # Add a new proposition
        MetaplanPropositionFactory(
            metaplan=self.metaplan,
            user=self.trainee
        )

        response = self.client.get(url)

        # We got our new proposition in the response
        self.assertEqual(
            len(response.data['propositions']),
            1
        )
