<commit_before>#-*- coding: utf-8 -*-

import datetime
import json
import os

from markdown import markdown
from markdown import Markdown

from django.core.cache import cache
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
from django.template import Template, Context, TemplateSyntaxError
from django import forms
from django.utils import translation
from django.utils import timezone
from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpResponseRedirect
from django.http import Http404, HttpResponse
from django.conf import settings
from django.core import mail
from django.template.loader import render_to_string
from django.core.exceptions import PermissionDenied
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.utils.timezone import utc

from spirit.models.category import Category
from spirit.utils.forms import NestedModelChoiceField
from spirit.utils.timezone import TIMEZONE_CHOICES
from spirit.utils.decorators import moderator_required, administrator_required
from spirit.utils.user.tokens import UserActivationTokenGenerator, UserEmailChangeTokenGenerator
from spirit.utils.user.email import send_activation_email, send_email_change_email, sender
from spirit.utils.user import email

from spirit import utils as spirit_utils
from spirit.templatetags.tags.utils import time as ttags_utils
import utils as test_utils
from spirit.utils.markdown import quotify
from spirit.templatetags.tags.utils.messages import render_messages


class UtilsTests(TestCase):

    def setUp(self):
        cache.clear()

    def test_render_form_errors(self):
        """
        return form errors string
        """
        class MockForm:
            non_field_errors = "error1"
            errors = "error2"

        res = spirit_utils.render_form_errors(MockForm())
        self.assertEqual(res.splitlines(), "error1\r\nerror2".splitlines())

    def test_json_response(self):
        """
        return json_response
        """
        res = spirit_utils.json_response()
        self.assertIsInstance(res, HttpResponse)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(res['Content-Type'], 'application/json')
        self.assertDictEqual(json.loads(res.content), {})
<commit_msg>test render_form_errors<commit_after>#-*- coding: utf-8 -*-

import datetime
import json
import os

from markdown import markdown
from markdown import Markdown

from django.core.cache import cache
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
from django.template import Template, Context, TemplateSyntaxError
from django import forms
from django.utils import translation
from django.utils import timezone
from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpResponseRedirect
from django.http import Http404, HttpResponse
from django.conf import settings
from django.core import mail
from django.template.loader import render_to_string
from django.core.exceptions import PermissionDenied
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.utils.timezone import utc

from spirit.models.category import Category
from spirit.utils.forms import NestedModelChoiceField
from spirit.utils.timezone import TIMEZONE_CHOICES
from spirit.utils.decorators import moderator_required, administrator_required
from spirit.utils.user.tokens import UserActivationTokenGenerator, UserEmailChangeTokenGenerator
from spirit.utils.user.email import send_activation_email, send_email_change_email, sender
from spirit.utils.user import email

from spirit import utils as spirit_utils
from spirit.templatetags.tags.utils import time as ttags_utils
import utils as test_utils
from spirit.utils.markdown import quotify
from spirit.templatetags.tags.utils.messages import render_messages


class UtilsTests(TestCase):

    def setUp(self):
        cache.clear()

    def test_render_form_errors(self):
        """
        return form errors string
        """
        class MockForm:
            non_field_errors = ["error1", ]
            hidden_fields = [{'errors': "error2", }, ]
            visible_fields = [{'errors': "error3", }, ]

        res = spirit_utils.render_form_errors(MockForm())
        lines = [line.strip() for line in res.splitlines()]
        self.assertEqual("".join(lines), '<ul class="errorlist"><li>error1</li><li>error2</li><li>error3</li></ul>')

    def test_json_response(self):
        """
        return json_response
        """
        res = spirit_utils.json_response()
        self.assertIsInstance(res, HttpResponse)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(res['Content-Type'], 'application/json')
        self.assertDictEqual(json.loads(res.content), {})

{'fraction_non_alphanumeric': 0.0762433210028771, 'fraction_numerical': 0.004932182490752158, 'mean_word_length': 4.389811738648948, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4483414', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1270, 'n_words': 374}