<commit_before>import os

from django.core.files.base import ContentFile
from django.test import RequestFactory
from mock import patch

from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.api.viewsets.stats_viewset import StatsViewSet
from onadata.apps.api.viewsets.submissionstats_viewset import SubmissionStatsViewSet
from onadata.apps.odk_logger.models import XForm
from onadata.libs.utils.logger_tools import publish_xml_form, create_instance


class TestStatsViewSet(TestBase):

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

    @patch('onadata.apps.odk_logger.models.instance.submission_time')
    def test_form_list(self, mock_time):
        self._set_mock_time(mock_time)
        self._publish_transportation_form()
        self._make_submissions()
        view = SubmissionStatsViewSet.as_view({'get': 'list'})
        request = self.factory.get('/', **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 200)
        formid = self.xform.pk
        data = {
            u'transportation_2011_07_25':
            'http://testserver/api/v1/stats/submissions/bob/%s' % formid
        }
        self.assertDictEqual(response.data, data)
        response = view(request, owner='bob', formid=formid)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, list)
        data = {
            u'count': 4
        }
        self.assertDictContainsSubset(data, response.data[0])

    def test_anon_form_list(self):
        self._publish_transportation_form()
        self._make_submissions()
        view = SubmissionStatsViewSet.as_view({'get': 'list'})
        request = self.factory.get('/')
        response = view(request)
        self.assertEqual(response.status_code, 401)

    def _contributions_form_submissions(self):
        count = XForm.objects.count()
        path = os.path.join(os.path.dirname(__file__),
                            '..', 'fixtures', 'forms', 'contributions')
        form_path = os.path.join(path, 'contributions.xml')
        f = open(form_path)<commit_msg>UK: use _xform_id_string for consistent results during test<commit_after>import os

from django.core.files.base import ContentFile
from django.test import RequestFactory
from mock import patch

from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.api.viewsets.stats_viewset import StatsViewSet
from onadata.apps.api.viewsets.submissionstats_viewset import SubmissionStatsViewSet
from onadata.apps.odk_logger.models import XForm
from onadata.libs.utils.logger_tools import publish_xml_form, create_instance


class TestStatsViewSet(TestBase):

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

    @patch('onadata.apps.odk_logger.models.instance.submission_time')
    def test_form_list(self, mock_time):
        self._set_mock_time(mock_time)
        self._publish_transportation_form()
        self._make_submissions()
        view = SubmissionStatsViewSet.as_view({'get': 'list'})
        request = self.factory.get('/', **self.extra)
        response = view(request)
        self.assertEqual(response.status_code, 200)
        formid = self.xform.pk
        data = {
            u'transportation_2011_07_25':
            'http://testserver/api/v1/stats/submissions/bob/%s' % formid
        }
        self.assertDictEqual(response.data, data)
        request = self.factory.get('/?group=_xform_id_string', **self.extra)
        response = view(request)
        response = view(request, owner='bob', formid=formid)
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.data, list)
        data = {
            u'count': 4
        }
        self.assertDictContainsSubset(data, response.data[0])

    def test_anon_form_list(self):
        self._publish_transportation_form()
        self._make_submissions()
        view = SubmissionStatsViewSet.as_view({'get': 'list'})
        request = self.factory.get('/')
        response = view(request)
        self.assertEqual(response.status_code, 401)

    def _contributions_form_submissions(self):
        count = XForm.objects.count()
        path = os.path.join(os.path.dirname(__file__),
                            '..', 'fixtures', 'forms', 'contributions')
        form_path = os.path.join(path, 'contributions.xml')
        f = open(form_path)
{'fraction_non_alphanumeric': 0.10912190963341858, 'fraction_numerical': 0.008525149190110827, 'mean_word_length': 3.0076857386848848, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '975241', 'n_tokens_mistral': 1464, 'n_tokens_neox': 1401, 'n_words': 237}