<commit_before>from test_base import MainTestCase
from odk_viewer.models import DataDictionary
from odk_logger.models import XForm, Instance
import os
import fnmatch
from odk_viewer.views import xls_export, csv_export
from django.core.urlresolvers import reverse
import csv
import json
import urllib2

def internet_on():
    try:
        response=urllib2.urlopen('http://74.125.113.99',timeout=1)
        return True
    except urllib2.URLError as err: pass
    return False

class TestSite(MainTestCase):

    def test_process(self, username=None, password=None):
        if username is not None:
            self._create_user_and_login(username, password)
        self._publish_xls_file()
        self._check_formList()
        self._download_xform()
        self._make_submissions()
        self._check_csv_export()
        self._check_delete()

    def test_uuid_submit(self):
        self._create_user_and_login()
        self._publish_xls_file()
        survey = 'transport_2011-07-25_19-05-49'
        path = os.path.join(self.this_directory, 'fixtures', 'transportation',
            'instances', survey, survey + '.xml')
        with open(path) as f:
            post_data = { 'xml_submission_file': f, 'uuid': self.xform.uuid }
            url = '/submission'
            self.response = self.anon.post(url, post_data)

    def test_url_upload(self):
        if internet_on():
            self._create_user_and_login()
            xls_url = 'http://formhub.org/pld/forms/transportation_2011_07_25/form.xls'
            pre_count = XForm.objects.count()
            response = self.client.post('/%s/' % self.user.username, {'xls_url': xls_url})
            # make sure publishing the survey worked
            self.assertEqual(response.status_code, 200)
            self.assertEqual(XForm.objects.count(), pre_count+1)

    # This method tests a large number of xls files.
    # create a directory /main/test/fixtures/online_xls
    # containing the files you would like to test.
    # DO NOT CHECK IN PRIVATE XLS FILES!!
    def test_upload_all_xls(self):
        root_dir = os.path.join(self.this_directory, "fixtures", "online_xls")
        if os.path.exists(root_dir):
            success = True
            for root, sub_folders, filenames in os.walk(root_dir):
                # ignore files that don't end in '.xls'
                for filename in fnmatch.filter(filenames, '*.xls'):
                    success = self._publish_file(os.path.join(root, filename), False)
                    if success:
                        # delete it so we don't have id_string conflicts
                        if self.xform:
                            self.xform.delete()
                            self.xform = None
                print 'finished sub-folder %s' % root
        self.assertEqual(success, True)

    def test_url_upload_non_dot_xls_path(self):
        if internet_on():
            self._create_user_and_login()
            xls_url = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AgpC5gsTSm_4dFZQdzZZVGxlcEQ3aktBbFlyRXE3cFE&output=xls'<commit_msg>test all xls files at once<commit_after>from test_base import MainTestCase
from odk_viewer.models import DataDictionary
from odk_logger.models import XForm, Instance
import os
import fnmatch
from odk_viewer.views import xls_export, csv_export
from django.core.urlresolvers import reverse
import csv
import json
import urllib2

def internet_on():
    try:
        response=urllib2.urlopen('http://74.125.113.99',timeout=1)
        return True
    except urllib2.URLError as err: pass
    return False

class TestSite(MainTestCase):

    def test_process(self, username=None, password=<PASSWORD>):
        if username is not None:
            self._create_user_and_login(username, password)
        self._publish_xls_file()
        self._check_formList()
        self._download_xform()
        self._make_submissions()
        self._check_csv_export()
        self._check_delete()

    def test_uuid_submit(self):
        self._create_user_and_login()
        self._publish_xls_file()
        survey = 'transport_2011-07-25_19-05-49'
        path = os.path.join(self.this_directory, 'fixtures', 'transportation',
            'instances', survey, survey + '.xml')
        with open(path) as f:
            post_data = { 'xml_submission_file': f, 'uuid': self.xform.uuid }
            url = '/submission'
            self.response = self.anon.post(url, post_data)

    def test_url_upload(self):
        if internet_on():
            self._create_user_and_login()
            xls_url = 'http://formhub.org/pld/forms/transportation_2011_07_25/form.xls'
            pre_count = XForm.objects.count()
            response = self.client.post('/%s/' % self.user.username, {'xls_url': xls_url})
            # make sure publishing the survey worked
            self.assertEqual(response.status_code, 200)
            self.assertEqual(XForm.objects.count(), pre_count+1)

    # This method tests a large number of xls files.
    # create a directory /main/test/fixtures/online_xls
    # containing the files you would like to test.
    # DO NOT CHECK IN PRIVATE XLS FILES!!
    def test_upload_all_xls(self):
        root_dir = os.path.join(self.this_directory, "fixtures", "online_xls")
        if os.path.exists(root_dir):
            success = True
            for root, sub_folders, filenames in os.walk(root_dir):
                # ignore files that don't end in '.xls'
                for filename in fnmatch.filter(filenames, '*.xls'):
                    success = self._publish_file(os.path.join(root, filename), False)
                    if success:
                        # delete it so we don't have id_string conflicts
                        if self.xform:
                            self.xform.delete()
                            self.xform = None
                print 'finished sub-folder %s' % root
            self.assertEqual(success, True)

    def test_url_upload_non_dot_xls_path(self):
        if internet_on():
            self._create_user_and_login()
            xls_url = 'https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0AgpC5gsTSm_4dFZQdzZZVGxlcEQ3aktBbFlyRXE3cFE&output=xls'
{'fraction_non_alphanumeric': 0.0958219067591547, 'fraction_numerical': 0.014518470721084045, 'mean_word_length': 2.3953997809419496, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6784367', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1879, 'n_words': 414}