<commit_before>import os
from django.core.urlresolvers import reverse
from odk_logger.models import Instance
from odk_viewer.views import kml_export
from main.tests.test_base import MainTestCase


class TestKMLExport(MainTestCase):
    def _publish_survey(self):
        self.this_directory = os.path.dirname(__file__)
        xls_path = self._fixture_path("gps", "gps.xls")
        MainTestCase._publish_xls_file(self, xls_path)

    def _make_submissions(self):
        surveys = ['gps_1980-01-23_20-52-08',
                   'gps_1980-01-23_21-21-33', ]
        for survey in surveys:
            path = self._fixture_path('gps', 'instances', survey + '.xml')
            self._make_submission(path)

    def test_kml_export(self):
        self._publish_survey()
        self._make_submissions()
        self.fixtures = os.path.join(
            self.this_directory, 'fixtures', 'kml_export')
        url = reverse(
            kml_export,
            kwargs={'username': self.user.username, 'id_string': 'gps'})
        response = self.client.get(url)
        instances = Instance.objects.filter(
            xform__id_string='gps').order_by('id')

        self.assertEqual(instances.count(), 2)

        first, second = [str(i.pk) for i in instances]

        with open(os.path.join(self.fixtures, 'export.kml')) as f:
            expected_content = f.read()
            expected_content = expected_content.replace('{{first}}', first)
            expected_content = expected_content.replace('{{second}}', second)

            self.assertMultiLineEqual(
                response.content, expected_content.strip())<commit_msg>PLD: fetch instances from ParsedInstance, variable for id_string<commit_after>import os
from django.core.urlresolvers import reverse
from odk_viewer.models import ParsedInstance
from odk_viewer.views import kml_export
from main.tests.test_base import MainTestCase


class TestKMLExport(MainTestCase):
    def _publish_survey(self):
        self.this_directory = os.path.dirname(__file__)
        xls_path = self._fixture_path("gps", "gps.xls")
        MainTestCase._publish_xls_file(self, xls_path)

    def _make_submissions(self):
        surveys = ['gps_1980-01-23_20-52-08',
                   'gps_1980-01-23_21-21-33', ]
        for survey in surveys:
            path = self._fixture_path('gps', 'instances', survey + '.xml')
            self._make_submission(path)

    def test_kml_export(self):
        id_string = 'gps'

        self._publish_survey()
        self._make_submissions()
        self.fixtures = os.path.join(
            self.this_directory, 'fixtures', 'kml_export')
        url = reverse(
            kml_export,
            kwargs={'username': self.user.username, 'id_string': id_string})
        response = self.client.get(url)
        pis = ParsedInstance.objects.filter(
            instance__user=self.user, instance__xform__id_string=id_string,
            lat__isnull=False, lng__isnull=False).order_by('id')

        self.assertEqual(pis.count(), 2)

        first, second = [str(i.pk) for i in pis]

        with open(os.path.join(self.fixtures, 'export.kml')) as f:
            expected_content = f.read()
            expected_content = expected_content.replace('{{first}}', first)
            expected_content = expected_content.replace('{{second}}', second)

            self.assertMultiLineEqual(
                response.content, expected_content.strip())
{'fraction_non_alphanumeric': 0.1138353765323993, 'fraction_numerical': 0.016929363689433742, 'mean_word_length': 2.74945295404814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5268293', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1045, 'n_words': 191}