<commit_before>from itertools import takewhile
from tempfile import TemporaryFile

from django.test import TestCase
from django.test.utils import override_settings
from django.contrib.auth.models import User, Group, Permission
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import connection as dj_connection
from django.core.urlresolvers import reverse

from enhydris.conf import settings
from enhydris.hcore.models import StationType, Organization, Variable, \
    UnitOfMeasurement, TimeZone, Station, Timeseries, InstrumentType, \
    Instrument, GentityFile
from enhydris.hcore.forms import TimeseriesDataForm
from enhydris.hcore.views import ALLOWED_TO_EDIT
from enhydris.hcore.views import get_search_query


class SearchTestCase(TestCase):
    fixtures = ['testdata.json']

    def test_search_in_timeseries_remarks(self):
        # Search for something that exists
        query = get_search_query(['extremely important time series'])
        result = Station.objects.filter(query).distinct()
        self.assertEquals(result.count(), 1)
        self.assertEquals(result[0].id, 3)

        # Search for something that doesn't exist
        query = get_search_query(['this should not exist anywhere'])
        result = Station.objects.filter(query).distinct()
        self.assertEquals(result.count(), 0)


class GentityFileTestCase(TestCase):
    fixtures = ['testdata.json']

    def test_upload_gentity_file(self):
        r = self.client.login(username='admin', password='<PASSWORD>')
        self.assertEquals(r, True)
        self.assertEquals(GentityFile.objects.filter(gentity__id=2).count(), 0)
        with TemporaryFile(suffix='.jpg') as tmpfile:
            tmpfile.write('Irrelevant data\n')
            tmpfile.seek(0)
            response = self.client.post(reverse('gentityfile_add'),
                                        {'gentity': 2,
                                         'date': '',
                                         'file_type': 1,
                                         'descr': 'A description',
                                         'remarks': '',
                                         'descr_alt': 'An alt description',
                                         'remarks_alt': '',
                                         'content': tmpfile,
                                         })
        self.assertEquals(response.status_code, 302)
        self.assertEquals(GentityFile.objects.filter(gentity__id=2).count(), 1)


class SmokeTestCase(TestCase):
    """Test that all project URLs return correct status code."""
    #TODO: Run this for all applications, somehow
    #TODO: More customization: 404s, etc
    #TODO: Make this run automatic for all

    pages = {
        200: ['/',
                '/accounts/login/',
                '/accounts/logout/',
                '/accounts/register/',
                '/accounts/password/reset/',
                '/accounts/password/reset/done/',
                '/admin/',
                '/map/',
                ],
        404: ['/nonexistent/',
                '/stations/l/',
                '/stations/d/',
                '/stations/d/nonexistent/',
                '/timeseries/d/',
                '/timeseries/d/nonexistent/',
                '/instruments/d/',
                '/instruments/d/nonexistent/',
                '/account/foob4r/',
                ],
    }

    @override_settings(REGISTRATION_OPEN=True)
    def testStatusCode(self):
        """Test that the response status code is correct"""

        for expected_code in self.pages:
            for page_url in self.pages[expected_code]:
                page = self.client.get(page_url)
                self.assertEquals(
                    page.status_code, expected_code,
                    "Status code for page '%s' was %s instead of %s" %
                    (page_url, page.status_code, expected_code))


class TsTestCase(TestCase):
    """Test timeseries data upload/download code."""
<commit_msg>Code beautification<commit_after>from itertools import takewhile
from tempfile import TemporaryFile

from django.test import TestCase
from django.test.utils import override_settings
from django.contrib.auth.models import User, Group, Permission
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import connection as dj_connection
from django.core.urlresolvers import reverse

from enhydris.conf import settings
from enhydris.hcore.models import StationType, Organization, Variable, \
    UnitOfMeasurement, TimeZone, Station, Timeseries, InstrumentType, \
    Instrument, GentityFile
from enhydris.hcore.forms import TimeseriesDataForm
from enhydris.hcore.views import ALLOWED_TO_EDIT
from enhydris.hcore.views import get_search_query


class SearchTestCase(TestCase):
    fixtures = ['testdata.json']

    def test_search_in_timeseries_remarks(self):
        # Search for something that exists
        query = get_search_query(['extremely important time series'])
        result = Station.objects.filter(query).distinct()
        self.assertEquals(result.count(), 1)
        self.assertEquals(result[0].id, 3)

        # Search for something that doesn't exist
        query = get_search_query(['this should not exist anywhere'])
        result = Station.objects.filter(query).distinct()
        self.assertEquals(result.count(), 0)


class GentityFileTestCase(TestCase):
    fixtures = ['testdata.json']

    def test_upload_gentity_file(self):
        r = self.client.login(username='admin', password='<PASSWORD>')
        self.assertEquals(r, True)
        self.assertEquals(GentityFile.objects.filter(gentity__id=2).count(), 0)
        with TemporaryFile(suffix='.jpg') as tmpfile:
            tmpfile.write('Irrelevant data\n')
            tmpfile.seek(0)
            response = self.client.post(reverse('gentityfile_add'),
                                        {'gentity': 2,
                                         'date': '',
                                         'file_type': 1,
                                         'descr': 'A description',
                                         'remarks': '',
                                         'descr_alt': 'An alt description',
                                         'remarks_alt': '',
                                         'content': tmpfile,
                                         })
        self.assertEquals(response.status_code, 302)
        self.assertEquals(GentityFile.objects.filter(gentity__id=2).count(), 1)


class SmokeTestCase(TestCase):
    """Test that all project URLs return correct status code."""
    #TODO: Run this for all applications, somehow
    #TODO: More customization: 404s, etc
    #TODO: Make this run automatic for all

    pages = {
        200: ['/',
              '/accounts/login/',
              '/accounts/logout/',
              '/accounts/register/',
              '/accounts/password/reset/',
              '/accounts/password/reset/done/',
              '/admin/',
              '/map/',
              ],
        404: ['/nonexistent/',
              '/stations/l/',
              '/stations/d/',
              '/stations/d/nonexistent/',
              '/timeseries/d/',
              '/timeseries/d/nonexistent/',
              '/instruments/d/',
              '/instruments/d/nonexistent/',
              '/account/foob4r/',
              ],
    }

    @override_settings(REGISTRATION_OPEN=True)
    def testStatusCode(self):
        """Test that the response status code is correct"""

        for expected_code in self.pages:
            for page_url in self.pages[expected_code]:
                page = self.client.get(page_url)
                self.assertEquals(
                    page.status_code, expected_code,
                    "Status code for page '%s' was %s instead of %s" %
                    (page_url, page.status_code, expected_code))


class TsTestCase(TestCase):
    """Test timeseries data upload/download code."""

{'fraction_non_alphanumeric': 0.10485485485485485, 'fraction_numerical': 0.006006006006006006, 'mean_word_length': 1.9129008746355685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3723583', 'n_tokens_mistral': 2104, 'n_tokens_neox': 2006, 'n_words': 477}