from django.test import RequestFactory
from main.tests.test_base import MainTestCase

from api.views import DataViewSet


class TestDataAPI(MainTestCase):

    def setUp(self):
        MainTestCase.setUp(self)
        self._create_user_and_login()
        self._publish_transportation_form()
        self._make_submissions()
        self.factory = RequestFactory()
        self.extra = {
            'HTTP_AUTHORIZATION': 'Token %s' % self.user.auth_token}

    def test_form_list(self):
        view = DataViewSet.as_view({'get': 'list'})
        request = self.factory.get('/', **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 200)
        data = {
            u'transportation_2011_07_25': 'http://testserver/api/v1/data/bob/1'
        }
        self.assertDictEqual(response.data, data)
        response = view(request, owner='bob', formid=1)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, list)
        data = {
            u'_bamboo_dataset_id': u'',
            u'_deleted_at': None,
            u'_attachments': [],
            u'_geolocation': [None, None],
            u'_xform_id_string': u'transportation_2011_07_25',
            u'transport/available_transportation_types_to_referral_facility':
            u'none',
            u'_status': u'submitted_via_web',
            u'_id': 1
        }
        self.assertDictContainsSubset(data, response.data[0])
        response = view(request, owner='bob', formid=1, dataid=1)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, dict)
        self.assertDictContainsSubset(data, response.data)

@@ -77,16 +77,55 @@
 TestCase
+%0Afrom odk_logger.models import Instance
 %0A%0Afrom a
@@ -151,16 +151,30 @@
 aViewSet
+, XFormViewSet
 %0A%0A%0Aclass
@@ -1734,12 +1734,840 @@
 ponse.data)%0A
+%0A    def test_add_form_tag_propagates_to_data_tags(self):%0A        %22%22%22Test that when a tag is applied on an xform,%0A        it propagates to the instance submissions%0A        %22%22%22%0A        view = XFormViewSet.as_view(%7B%0A            'get': 'labels',%0A            'post': 'labels',%0A        %7D)%0A        # no tags%0A        request = self.factory.get('/', **self.extra)%0A        response = view(request, owner='bob', pk=1, formid=1)%0A        self.assertEqual(response.data, %5B%5D)%0A        # add tag %22hello%22%0A        request = self.factory.post('/', data=%7B%22tags%22: %22hello%22%7D, **self.extra)%0A        response = view(request, owner='bob', pk=1, formid=1)%0A        self.assertEqual(response.status_code, 201)%0A        self.assertEqual(response.data, %5Bu'hello'%5D)%0A        for i in self.xform.surveys.all():%0A            self.assertIn(u'hello', i.tags.names())%0A

test adding tags to an xform should also update the instances - submissions - attached