<commit_before>from django.test import TestCase
from django.contrib.auth.models import User
from odk_viewer.models import DataDictionary
from odk_logger.import_tools import import_instances_from_phone
from odk_logger.models import XForm
import os
from odk_viewer.views import csv_export
from django.core.urlresolvers import reverse
import csv
import json


def publish(username, password, path_to_xls):
    from django.test.client import Client
    client = Client()
    assert client.login(username=username, password=password)
    with open(path_to_xls) as xls_file:
        post_data = {'xls_file': xls_file}
        return client.post('/', post_data)


class TestSite(TestCase):

    def test_process(self):
        self._create_user()
        self._publish_xls_file()
        self._check_formList()
        self._download_xform()
        self._make_submissions()
        self._check_csv_export()

    def _create_user(self):
        self.user = User.objects.create(username="bob")
        self.user.set_password("<PASSWORD>")
        self.user.save()

    def _publish_xls_file(self):
        self.this_directory = os.path.join("main", "tests")
        xls_path = os.path.join(self.this_directory, "transportation.xls")
        response = publish("bob", "bob", xls_path)

        # make sure publishing the survey worked
        self.assertEqual(response.status_code, 200)
        self.assertEqual(XForm.objects.count(), 1)
        self.xform = XForm.objects.all()[0]
        self.assertEqual(self.xform.id_string, "transportation_2011_07_25")

    def _check_formList(self):
        pass

    def _download_xform(self):
        pass

    def _make_submissions(self):
        odk_path = os.path.join(self.this_directory, "odk")
        import_instances_from_phone(odk_path)
        self.assertEqual(self.xform.surveys.count(), 4)

    def _check_csv_export(self):
        self._check_data_dictionary()
        self._check_data_for_csv_export()
        self._check_group_xpaths_do_not_appear_in_dicts_for_export()
        self._check_csv_export_first_pass()
        self._check_csv_export_second_pass()

    def _check_data_dictionary(self):
        # test to make sure the data dictionary returns the expected headers<commit_msg>Moved publish function into testing class.<commit_after>from django.test import TestCase
from django.contrib.auth.models import User
from odk_viewer.models import DataDictionary
from odk_logger.import_tools import import_instances_from_phone
from odk_logger.models import XForm
import os
from odk_viewer.views import csv_export
from django.core.urlresolvers import reverse
import csv
import json
from django.test.client import Client


class TestSite(TestCase):

    def test_process(self):
        self._create_user_and_login()
        self._publish_xls_file()
        self._check_formList()
        self._download_xform()
        self._make_submissions()
        self._check_csv_export()

    def _create_user_and_login(self):
        self.user = User.objects.create(username="bob")
        self.user.set_password("<PASSWORD>")
        self.user.save()
        self.client = Client()
        assert self.client.login(username="bob", password="<PASSWORD>")

    def _publish_xls_file(self):
        self.this_directory = os.path.join("main", "tests")
        xls_path = os.path.join(self.this_directory, "transportation.xls")
        with open(xls_path) as xls_file:
            post_data = {'xls_file': xls_file}
            response = self.client.post('/', post_data)

        # make sure publishing the survey worked
        self.assertEqual(response.status_code, 200)
        self.assertEqual(XForm.objects.count(), 1)
        self.xform = XForm.objects.all()[0]
        self.assertEqual(self.xform.id_string, "transportation_2011_07_25")

    def _check_formList(self):
        pass

    def _download_xform(self):
        pass

    def _make_submissions(self):
        odk_path = os.path.join(self.this_directory, "odk")
        import_instances_from_phone(odk_path)
        self.assertEqual(self.xform.surveys.count(), 4)

    def _check_csv_export(self):
        self._check_data_dictionary()
        self._check_data_for_csv_export()
        self._check_group_xpaths_do_not_appear_in_dicts_for_export()
        self._check_csv_export_first_pass()
        self._check_csv_export_second_pass()

    def _check_data_dictionary(self):
        # test to make sure the data dictionary returns the expected headers
{'fraction_non_alphanumeric': 0.09233880026960234, 'fraction_numerical': 0.006290721186250281, 'mean_word_length': 3.407920792079208, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '774334', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1357, 'n_words': 262}