<commit_before>from django.core.urlresolvers import reverse
from django.test import TestCase

from feder.main.mixins import PermissionStatusMixin
from feder.tasks.factories import (CharAnswerFactory, JSTAnswerFactory,
                                   SurveyFactory)
from feder.teryt.factories import JSTFactory

from ..factories import CharQuestionFactory, JSTQuestionFactory
from ..models import Questionary
from .test_general import ObjectMixin


class QuestionaryCreateTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = ['monitorings.add_questionary', ]

    def get_url(self):
        return reverse('questionaries:create',
                       kwargs={'monitoring': str(self.questionary.monitoring.pk)})


class QuestionaryListViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = []
    status_anonymous = 200
    status_no_permission = 200

    def get_url(self):
        return reverse('questionaries:list')

    def test_content(self):
        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'questionaries/questionary_filter.html')
        self.assertContains(response, self.questionary.title)


class QuestionaryDetailsViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = []
    status_anonymous = 200
    status_no_permission = 200

    def get_url(self):
        return self.questionary.get_absolute_url()

    def test_content(self):
        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'questionaries/questionary_detail.html')
        self.assertContains(response, self.questionary.title)


class QuestionaryUpdateTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = ['monitorings.change_questionary', ]

    def get_url(self):
        return reverse('questionaries:update',
                       kwargs={'pk': self.questionary.pk})


class QuestionaryDeleteTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = ['monitorings.delete_questionary', ]

    def get_url(self):
        return reverse('questionaries:delete',
                       kwargs={'pk': self.questionary.pk})

    def test_get_success_url(self):
        self.grant_permission()
        self.client.login(username='john', password='<PASSWORD>')
        response = self.client.post(self.get_url())
        self.assertFalse(Questionary.objects.filter(pk=self.questionary.pk).exists())
        self.assertRedirects(response,
                             '/monitorings/%s' %
                             (self.questionary.monitoring.slug))


class SitemapTestCase(ObjectMixin, TestCase):
    def test_institutions(self):
        url = reverse('sitemaps', kwargs={'section': 'questionaries'})
        response = self.client.get(url)
        self.assertContains(response, self.questionary.get_absolute_url())


class SurveyCSVView(ObjectMixin, TestCase):
    def get_url(self):
        return reverse('questionaries:export',
                       kwargs={'pk': self.questionary.pk})

    def test_no_question(self):
        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)

    def test_no_answer_but_has_questions(self):
        char_q = CharQuestionFactory(questionary=self.questionary)
        jst_q = JSTQuestionFactory(questionary=self.questionary)
        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, char_q.definition['name'])
        self.assertContains(response, jst_q.definition['name'])<commit_msg>Add PermissionStatusMixin to SurveyCSVViewTestCase<commit_after>from django.core.urlresolvers import reverse
from django.test import TestCase

from feder.main.mixins import PermissionStatusMixin
from feder.tasks.factories import (CharAnswerFactory, JSTAnswerFactory,
                                   SurveyFactory)
from feder.teryt.factories import JSTFactory

from ..factories import CharQuestionFactory, JSTQuestionFactory
from ..models import Questionary
from .test_general import ObjectMixin


class QuestionaryCreateTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = ['monitorings.add_questionary', ]

    def get_url(self):
        return reverse('questionaries:create',
                       kwargs={'monitoring': str(self.questionary.monitoring.pk)})


class QuestionaryListViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = []
    status_anonymous = 200
    status_no_permission = 200

    def get_url(self):
        return reverse('questionaries:list')

    def test_content(self):
        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'questionaries/questionary_filter.html')
        self.assertContains(response, self.questionary.title)


class QuestionaryDetailsViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = []
    status_anonymous = 200
    status_no_permission = 200

    def get_url(self):
        return self.questionary.get_absolute_url()

    def test_content(self):
        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'questionaries/questionary_detail.html')
        self.assertContains(response, self.questionary.title)


class QuestionaryUpdateTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = ['monitorings.change_questionary', ]

    def get_url(self):
        return reverse('questionaries:update',
                       kwargs={'pk': self.questionary.pk})


class QuestionaryDeleteTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = ['monitorings.delete_questionary', ]

    def get_url(self):
        return reverse('questionaries:delete',
                       kwargs={'pk': self.questionary.pk})

    def test_get_success_url(self):
        self.grant_permission()
        self.client.login(username='john', password='<PASSWORD>')
        response = self.client.post(self.get_url())
        self.assertFalse(Questionary.objects.filter(pk=self.questionary.pk).exists())
        self.assertRedirects(response,
                             '/monitorings/%s' %
                             (self.questionary.monitoring.slug))


class SitemapTestCase(ObjectMixin, TestCase):
    def test_institutions(self):
        url = reverse('sitemaps', kwargs={'section': 'questionaries'})
        response = self.client.get(url)
        self.assertContains(response, self.questionary.get_absolute_url())


class SurveyCSVViewTestCase(ObjectMixin, PermissionStatusMixin, TestCase):
    permission = []
    status_anonymous = 200
    status_no_permission = 200

    def get_url(self):
        return reverse('questionaries:export',
                       kwargs={'pk': self.questionary.pk})

    def test_no_question(self):
        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)

    def test_no_answer_but_has_questions(self):
        char_q = CharQuestionFactory(questionary=self.questionary)
        jst_q = JSTQuestionFactory(questionary=self.questionary)
        response = self.client.get(self.get_url())
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, char_q.definition['name'])
        self.assertContains(response, jst_q.definition['name'])
{'fraction_non_alphanumeric': 0.10027708140915688, 'fraction_numerical': 0.007124950521176936, 'mean_word_length': 3.5635159542444312, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7312756', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2042, 'n_words': 348}