<commit_before>import shutil
import os

from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.test import TestCase

from osmaxx.excerptexport.models import ExtractionOrder, Excerpt
from osmaxx.excerptexport.models import BBoxBoundingGeometry, OsmosisPolygonFilterBoundingGeometry
from osmaxx.excerptexport.tests.permission_test_helper import PermissionHelperMixin

from excerptconverter import converter_registry
from excerptconverter.dummyexcerptconverter import dummy_excerpt_converter


class ExcerptExportViewTests(TestCase, PermissionHelperMixin):
    def setUp(self):
        self.user = User.objects.create_user('user', '<EMAIL>', 'pw')
        other_user = User.objects.create_user('other_user', '<EMAIL>', 'o_pw')
        self.new_excerpt_post_data = {
            'form-mode': 'new-excerpt',
            'new_excerpt_name': 'A very interesting region',
            'new_excerpt_is_public': 'True',
            'new_excerpt_bounding_box_north': '1.0',
            'new_excerpt_bounding_box_east': '2.0',
            'new_excerpt_bounding_box_south': '3.0',
            'new_excerpt_bounding_box_west': '4.0',
            'export_options.excerptconverter.dummyexcerptconverter.dummy_excerpt_converter.formats': ['txt'],
            'export_options.excerptconverter.dummyexcerptconverter.dummy_excerpt_converter.options.detail_level':
            'verbatim',
        }
        self.existing_own_excerpt = Excerpt.objects.create(
            name='Some old Excerpt',
            is_active=True,
            is_public=False,
            owner=self.user,
            bounding_geometry=BBoxBoundingGeometry.create_from_bounding_box_coordinates(0, 0, 0, 0)
        )
        self.existing_public_foreign_excerpt = Excerpt.objects.create(
            name='Public Excerpt by someone else',
            is_active=True,
            is_public=True,
            owner=other_user,
            bounding_geometry=BBoxBoundingGeometry.create_from_bounding_box_coordinates(0, 0, 0, 0)
        )
        self.existing_private_foreign_excerpt = Excerpt.objects.create(
            name='Private Excerpt by someone else',
            is_active=True,
            is_public=False,
            owner=other_user,
            bounding_geometry=BBoxBoundingGeometry.create_from_bounding_box_coordinates(0, 0, 0, 0)
        )
        self.country = Excerpt.objects.create(
            name='Neverland',
            is_active=True,
            is_public=False,
            owner=other_user,
            bounding_geometry=OsmosisPolygonFilterBoundingGeometry.objects.create(
                polygon_file=SimpleUploadedFile('in_memory_file.poly', b'the file content (not a real .poly file)')
            )
        )
        self.existing_excerpt_post_data = {
            'form-mode': 'existing-excerpt',
            'existing_excerpt.id': self.existing_own_excerpt.id,
            'export_options.excerptconverter.dummyexcerptconverter.dummy_excerpt_converter.formats': ['txt'],
            'export_options.excerptconverter.dummyexcerptconverter.dummy_excerpt_converter.options.detail_level':
            'verbatim',
        }
        self.existing_excerpt_extraction_options = {
            'excerptconverter.dummyexcerptconverter.dummy_excerpt_converter': {
                'formats': ['txt'],
                'options': {
                    'detail_level': 'verbatim'
                }
            },
            'excerptconverter.gisexcerptconverter.gis_excerpt_converter': {
                'formats': [],
                'options': {}
            }
        }

        if dummy_excerpt_converter not in converter_registry.available_converters:
            converter_registry.available_converters.append(dummy_excerpt_converter)

    def tearDown(self):
        if os.path.isdir(settings.PRIVATE_MEDIA_ROOT):
            shutil.rmtree(settings.PRIVATE_MEDIA_ROOT)

        if dummy_excerpt_converter in converter_registry.available_converters:
            converter_registry.available_converters.remove(dummy_excerpt_converter)

    def test_new_when_not_logged_in(self):
        """
        When not logged in, we get redirected.
        """
        response = self.client.get(reverse('excerptexport:new'))
        self.assertEqual(response.status_code, 302)

    def test_new(self):
        """
        When logged in, we get the excerpt choice form.
        """
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Create new excerpt')

    def test_new_offers_existing_own_excerpt(self):
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertEqual(response.context['excerpts']['own_private'].count(), 1)
        self.assertIn(self.existing_own_excerpt, response.context['excerpts']['own_private'])

    def test_new_offers_existing_public_foreign_excerpt(self):
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertEqual(response.context['excerpts']['other_public'].count(), 1)
        self.assertIn(self.existing_public_foreign_excerpt, response.context['excerpts']['other_public'])

    def test_new_doesnt_offer_existing_private_foreign_excerpt(self):
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertNotContains(response, self.existing_private_foreign_excerpt.name)

    def test_new_offers_country(self):
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertEqual(response.context['excerpts']['countries'].count(), 1)
        self.assertIn(self.country, response.context['excerpts']['countries'])

    def test_create_when_not_logged_in(self):
        """
        When not logged in, we get redirected.
        """
        response = self.client.post(reverse('excerptexport:new'), self.new_excerpt_post_data)
        self.assertEqual(response.status_code, 302)

    def test_create_with_new_excerpt(self):
        """
        When logged in, POSTing an export request with a new excerpt is successful.
        """
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.post(
            reverse('excerptexport:new'),
            self.new_excerpt_post_data,
            HTTP_HOST='thehost.example.com'
        )
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            Excerpt.objects.filter(name='A very interesting region', is_active=True, is_public=True).count(),
            1
        )

    def test_create_with_existing_excerpt(self):
        """
        When logged in, POSTing an export request using an existing excerpt is successful.
        """
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.post(
            reverse('excerptexport:new'),
            self.existing_excerpt_post_data,
            HTTP_HOST='thehost.example.com'
        )
        self.assertEqual(response.status_code, 302)
        self.assertEqual(ExtractionOrder.objects.filter(
            excerpt_id=self.existing_excerpt_post_data['existing_excerpt.id']
        ).count(), 1)  # only reproducible because there is only 1

    def test_create_with_new_excerpt_persists_a_new_order(self):
        """
        When logged in, POSTing an export request with a new excerpt persists a new ExtractionOrder.
        """
        self.add_permissions_to_user()
        self.assertEqual(ExtractionOrder.objects.count(), 0)
        self.client.login(username='user', password='pw')
        self.client.post(reverse('excerptexport:new'), self.new_excerpt_post_data, HTTP_HOST='thehost.example.com')
        self.assertEqual(ExtractionOrder.objects.count(), 1)

        newly_created_order = ExtractionOrder.objects.first()  # only reproducible because there is only 1
        from osmaxx.excerptexport.models.extraction_order import ExtractionOrderState
        self.assertEqual(newly_created_order.state, ExtractionOrderState.INITIALIZED)
        self.assertIsNotNone(newly_created_order.process_start_date)
        self.assertEqual(newly_created_order.extraction_configuration, self.existing_excerpt_extraction_options)
        self.assertEqual(newly_created_order.orderer, self.user)
        self.assertEqual(newly_created_order.excerpt.name, 'A very interesting region')

    def test_create_with_existing_excerpt_persists_a_new_order(self):
        """
        When logged in, POSTing an export request using an existing excerpt persists a new ExtractionOrder.
        """
        self.add_permissions_to_user()<commit_msg>tests changed to reflect new structures<commit_after>import shutil
import os

from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.test import TestCase

from osmaxx.excerptexport.models import ExtractionOrder, Excerpt
from osmaxx.excerptexport.models import BBoxBoundingGeometry, OsmosisPolygonFilterBoundingGeometry
from osmaxx.excerptexport.tests.permission_test_helper import PermissionHelperMixin

from excerptconverter import converter_registry
from excerptconverter.dummyexcerptconverter import dummy_excerpt_converter


class ExcerptExportViewTests(TestCase, PermissionHelperMixin):
    def setUp(self):
        self.user = User.objects.create_user('user', '<EMAIL>', 'pw')
        other_user = User.objects.create_user('other_user', '<EMAIL>', 'o_pw')
        self.new_excerpt_post_data = {
            'form_mode': 'new-excerpt',
            'name': 'A very interesting region',
            'is_public': 'True',
            'north': '1.0',
            'east': '2.0',
            'south': '3.0',
            'west': '4.0',
            'formats': ['excerptconverter.gisexcerptconverter.gis_excerpt_converter.fgdb'],
            'detail_level': 'verbatim',
        }
        self.existing_own_excerpt = Excerpt.objects.create(
            name='Some old Excerpt',
            is_active=True,
            is_public=False,
            owner=self.user,
            bounding_geometry=BBoxBoundingGeometry.create_from_bounding_box_coordinates(0, 0, 0, 0)
        )
        self.existing_public_foreign_excerpt = Excerpt.objects.create(
            name='Public Excerpt by someone else',
            is_active=True,
            is_public=True,
            owner=other_user,
            bounding_geometry=BBoxBoundingGeometry.create_from_bounding_box_coordinates(0, 0, 0, 0)
        )
        self.existing_private_foreign_excerpt = Excerpt.objects.create(
            name='Private Excerpt by someone else',
            is_active=True,
            is_public=False,
            owner=other_user,
            bounding_geometry=BBoxBoundingGeometry.create_from_bounding_box_coordinates(0, 0, 0, 0)
        )
        self.country = Excerpt.objects.create(
            name='Neverland',
            is_active=True,
            is_public=False,
            owner=other_user,
            bounding_geometry=OsmosisPolygonFilterBoundingGeometry.objects.create(
                polygon_file=SimpleUploadedFile('in_memory_file.poly', b'the file content (not a real .poly file)')
            )
        )
        self.existing_excerpt_post_data = {
            'form_mode': 'existing-excerpt',
            'existing_excerpts': self.existing_own_excerpt.id,
            'formats': ['excerptconverter.gisexcerptconverter.gis_excerpt_converter.fgdb'],
        }
        self.existing_excerpt_extraction_options = {'excerptconverter.gisexcerptconverter.gis_excerpt_converter': {
            'formats': ['fgdb'], 'options': {}}
        }

        if dummy_excerpt_converter not in converter_registry.available_converters:
            converter_registry.available_converters.append(dummy_excerpt_converter)

    def tearDown(self):
        if os.path.isdir(settings.PRIVATE_MEDIA_ROOT):
            shutil.rmtree(settings.PRIVATE_MEDIA_ROOT)

        if dummy_excerpt_converter in converter_registry.available_converters:
            converter_registry.available_converters.remove(dummy_excerpt_converter)

    def test_new_when_not_logged_in(self):
        """
        When not logged in, we get redirected.
        """
        response = self.client.get(reverse('excerptexport:new'))
        self.assertEqual(response.status_code, 302)

    def test_new(self):
        """
        When logged in, we get the excerpt choice form.
        """
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Create new excerpt')

    def test_new_offers_existing_own_excerpt(self):
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertTrue(
            ('Personal excerpts (user)', ((1, 'Some old Excerpt'),)) in
            response.context['form'].fields['existing_excerpts'].choices
        )
        self.assertIn(self.existing_own_excerpt.name, response.context['form'].form_html)

    def test_new_offers_existing_public_foreign_excerpt(self):
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))

        self.assertTrue(
            ('Other excerpts', ((2, 'Public Excerpt by someone else'),)) in
            response.context['form'].fields['existing_excerpts'].choices
        )
        self.assertIn(self.existing_public_foreign_excerpt.name, response.context['form'].form_html)

    def test_new_doesnt_offer_existing_private_foreign_excerpt(self):
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertNotContains(response, self.existing_private_foreign_excerpt.name)

    def test_new_offers_country(self):
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.get(reverse('excerptexport:new'))
        self.assertTrue(
            ('Countries', ((4, 'Neverland'),)) in
            response.context['form'].fields['existing_excerpts'].choices
        )
        self.assertIn(self.country.name, response.context['form'].form_html)

    def test_create_when_not_logged_in(self):
        """
        When not logged in, we get redirected.
        """
        response = self.client.post(reverse('excerptexport:new'), self.new_excerpt_post_data)
        self.assertEqual(response.status_code, 302)

    def test_create_with_new_excerpt(self):
        """
        When logged in, POSTing an export request with a new excerpt is successful.
        """
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.post(
            reverse('excerptexport:new'),
            self.new_excerpt_post_data,
            HTTP_HOST='thehost.example.com'
        )
        self.assertEqual(response.status_code, 302)
        self.assertEqual(
            Excerpt.objects.filter(name='A very interesting region', is_active=True, is_public=True).count(),
            1
        )

    def test_create_with_existing_excerpt(self):
        """
        When logged in, POSTing an export request using an existing excerpt is successful.
        """
        self.add_permissions_to_user()
        self.client.login(username='user', password='pw')
        response = self.client.post(
            reverse('excerptexport:new'),
            self.existing_excerpt_post_data,
            HTTP_HOST='thehost.example.com'
        )
        self.assertEqual(response.status_code, 302)
        self.assertEqual(ExtractionOrder.objects.filter(
            excerpt_id=self.existing_excerpt_post_data['existing_excerpts']
        ).count(), 1)  # only reproducible because there is only 1

    def test_create_with_new_excerpt_persists_a_new_order(self):
        """
        When logged in, POSTing an export request with a new excerpt persists a new ExtractionOrder.
        """
        self.add_permissions_to_user()
        self.assertEqual(ExtractionOrder.objects.count(), 0)
        self.client.login(username='user', password='pw')
        self.client.post(reverse('excerptexport:new'), self.new_excerpt_post_data, HTTP_HOST='thehost.example.com')
        self.assertEqual(ExtractionOrder.objects.count(), 1)

        newly_created_order = ExtractionOrder.objects.first()  # only reproducible because there is only 1
        from osmaxx.excerptexport.models.extraction_order import ExtractionOrderState
        self.assertEqual(newly_created_order.state, ExtractionOrderState.INITIALIZED)
        self.assertIsNotNone(newly_created_order.process_start_date)
        self.assertEqual(newly_created_order.extraction_configuration, self.existing_excerpt_extraction_options)
        self.assertEqual(newly_created_order.orderer, self.user)
        self.assertEqual(newly_created_order.excerpt.name, 'A very interesting region')

    def test_create_with_existing_excerpt_persists_a_new_order(self):
        """
        When logged in, POSTing an export request using an existing excerpt persists a new ExtractionOrder.
        """
        self.add_permissions_to_user()
{'fraction_non_alphanumeric': 0.09872382741285012, 'fraction_numerical': 0.004861609855809071, 'mean_word_length': 3.0825439783491206, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '129482', 'n_tokens_mistral': 5540, 'n_tokens_neox': 5158, 'n_words': 822}