<commit_before>from __future__ import unicode_literals


from unittest import skipUnless
import django
from django.test import TestCase

from lbutils import fmt_month
from lbutils import get_year_choices
from lbutils import fmt_num
from lbutils import create_instance
from lbutils import get_or_none
from lbutils import get_pk_or_none
from lbutils import get_sum
from lbutils import get_max
from lbutils import do_filter
from lbutils import QuickSearchForm

from .models import Book
from .models import Category
from .forms import BookForm


class DateUtilsTests(TestCase):
    def test_fmt_month(self):
        self.assertEqual('01', fmt_month('1'))
        self.assertEqual('12', fmt_month('12'))

    def test_get_year_choices(self):
        choices = get_year_choices()
        self.assertTrue(choices[0][0])
        choices = get_year_choices(blank_label='------')
        self.assertEqual('', choices[0][0])


class UtilsTests(TestCase):
    def test_fmt_num(self):
        self.assertEqual('9,000,000', fmt_num(9000000))

    def test_create_instance(self):
        d = create_instance('datetime.datetime', year=2000, month=2, day=1)
        self.assertEqual('2000-02-01', d.strftime('%Y-%m-%d'))


class QSTests(TestCase):
    def setUp(self):
        category = Category.objects.create(name='category')
        Book.objects.create(
            category=category, name='book-01',
            price=100,
            descn='descn')
        Book.objects.create(
            category=category, name='book-02',
            price=200,
            descn='descn')
        Book.objects.create(
            name='book-03',
            is_active=False,
            descn='descn')

    def test_get_or_none(self):
        self.assertEqual(None, get_or_none(Book, name='not exist'))
        self.assertEqual(
            'book-01',
            get_or_none(Book, name='book-01').name)

    def test_get_pk_or_none(self):
        self.assertEqual(None, get_pk_or_none(Book, name='not exist'))
        self.assertEqual(
            Book.objects.get(name='book-01').pk,
            get_pk_or_none(Book, name='book-01'))

    def test_get_sum(self):
        qs = Book.objects.all()
        self.assertEqual(300, get_sum(qs, 'price'))
        qs = Book.objects.filter(name='book-01')
        self.assertEqual(100, get_sum(qs, 'price'))

    def test_get_max(self):
        qs = Book.objects.all()
        self.assertEqual(200, get_max(qs, 'price'))

    def test_do_filter_quick_query_fields(self):
        books = Book.objects.all()
        qdata = {'q_quick_search_kw': ''}
        qs = do_filter(books, qdata, ['name', 'category__name'])
        self.assertEqual(3, qs.count())
        qdata = {'q_quick_search_kw': '01'}
        qs = do_filter(books, qdata, ['name', 'category__name'])
        self.assertEqual(1, qs.count())
        qdata = {'q_quick_search_kw': 'xxxxx'}
        qs = do_filter(books, qdata, ['name', 'category__name'])
        self.assertEqual(0, qs.count())
        qdata = {'q_quick_search_kw': 'catego'}
        qs = do_filter(books, qdata, ['name', 'category__name'])
        self.assertEqual(2, qs.count())

    def test_do_filter_int_quick_query_fields(self):
        books = Book.objects.all()
        qdata = {'q_quick_search_kw': '100'}
        qs = do_filter(books, qdata, ['name', 'category__name'], int_quick_query_fields=['price'])
        self.assertEqual(1, qs.count())

    def test_do_filter(self):
        books = Book.objects.all()
        qdata = {'q__price': '100'}
        qs = do_filter(books, qdata)
        self.assertEqual(1, qs.count())
        qdata = {'q__name__icontains': 'book-03'}
        qs = do_filter(books, qdata)
        self.assertEqual(1, qs.count())
        qdata = {'q__category__name__icontains': 'catego'}
        qs = do_filter(books, qdata)
        self.assertEqual(2, qs.count())
        qdata = {'q__is_active': '__False'}
        qs = do_filter(books, qdata)
        self.assertEqual(1, qs.count())


class FormTests(TestCase):
    def setUp(self):
        data = {'name': 'book name'}
        self.form = BookForm(data)

    def test_quicksearchform(self):
        from crispy_forms.templatetags.crispy_forms_filters import as_crispy_form
        data = {'q_quick_search_kw': 'keyword'}
        form = QuickSearchForm(data)
        form.is_valid()
        self.assertEqual('keyword', form.cleaned_data['q_quick_search_kw'])
        self.assertTrue('id_q_quick_search_kw' in as_crispy_form(form))

    def test_filter_fields(self):
        # filter_fields(self, fields=[], exclude=[], include_all_if_empty=True):
        field_names = ['name', 'descn']
        fields = self.form.filter_fields(field_names)
        self.assertEqual(field_names, [e.name for e in fields])
        fields = self.form.filter_fields(exclude=field_names)
        self.assertEqual(['price', 'is_active', 'category'], [e.name for e in fields])
        fields = self.form.filter_fields(include_all_if_empty=True)
        self.assertEqual(['name', 'descn', 'price', 'is_active', 'category'], [e.name for e in fields])
        fields = self.form.filter_fields(include_all_if_empty=False)
        self.assertEqual([], [e.name for e in fields])

    def test_init_crispy_helper(self):
        self.form.init_crispy_helper()
        self.assertTrue(self.form.helper is not None)

    @skipUnless(django.VERSION >= (1, 7, 0), "test only applies to Django 1.7+")
    def test_errors_as_text(self):
        data = {}
        form = BookForm(data)
        form.is_valid()
        self.assertTrue('Name' in form.errors_as_text())
<commit_msg>remove add skipUnless for test_quicksearchform<commit_after>from __future__ import unicode_literals


from unittest import skipUnless
import django
from django.test import TestCase

from lbutils import fmt_month
from lbutils import get_year_choices
from lbutils import fmt_num
from lbutils import create_instance
from lbutils import get_or_none
from lbutils import get_pk_or_none
from lbutils import get_sum
from lbutils import get_max
from lbutils import do_filter
from lbutils import QuickSearchForm

from .models import Book
from .models import Category
from .forms import BookForm


class DateUtilsTests(TestCase):
    def test_fmt_month(self):
        self.assertEqual('01', fmt_month('1'))
        self.assertEqual('12', fmt_month('12'))

    def test_get_year_choices(self):
        choices = get_year_choices()
        self.assertTrue(choices[0][0])
        choices = get_year_choices(blank_label='------')
        self.assertEqual('', choices[0][0])


class UtilsTests(TestCase):
    def test_fmt_num(self):
        self.assertEqual('9,000,000', fmt_num(9000000))

    def test_create_instance(self):
        d = create_instance('datetime.datetime', year=2000, month=2, day=1)
        self.assertEqual('2000-02-01', d.strftime('%Y-%m-%d'))


class QSTests(TestCase):
    def setUp(self):
        category = Category.objects.create(name='category')
        Book.objects.create(
            category=category, name='book-01',
            price=100,
            descn='descn')
        Book.objects.create(
            category=category, name='book-02',
            price=200,
            descn='descn')
        Book.objects.create(
            name='book-03',
            is_active=False,
            descn='descn')

    def test_get_or_none(self):
        self.assertEqual(None, get_or_none(Book, name='not exist'))
        self.assertEqual(
            'book-01',
            get_or_none(Book, name='book-01').name)

    def test_get_pk_or_none(self):
        self.assertEqual(None, get_pk_or_none(Book, name='not exist'))
        self.assertEqual(
            Book.objects.get(name='book-01').pk,
            get_pk_or_none(Book, name='book-01'))

    def test_get_sum(self):
        qs = Book.objects.all()
        self.assertEqual(300, get_sum(qs, 'price'))
        qs = Book.objects.filter(name='book-01')
        self.assertEqual(100, get_sum(qs, 'price'))

    def test_get_max(self):
        qs = Book.objects.all()
        self.assertEqual(200, get_max(qs, 'price'))

    def test_do_filter_quick_query_fields(self):
        books = Book.objects.all()
        qdata = {'q_quick_search_kw': ''}
        qs = do_filter(books, qdata, ['name', 'category__name'])
        self.assertEqual(3, qs.count())
        qdata = {'q_quick_search_kw': '01'}
        qs = do_filter(books, qdata, ['name', 'category__name'])
        self.assertEqual(1, qs.count())
        qdata = {'q_quick_search_kw': 'xxxxx'}
        qs = do_filter(books, qdata, ['name', 'category__name'])
        self.assertEqual(0, qs.count())
        qdata = {'q_quick_search_kw': 'catego'}
        qs = do_filter(books, qdata, ['name', 'category__name'])
        self.assertEqual(2, qs.count())

    def test_do_filter_int_quick_query_fields(self):
        books = Book.objects.all()
        qdata = {'q_quick_search_kw': '100'}
        qs = do_filter(books, qdata, ['name', 'category__name'], int_quick_query_fields=['price'])
        self.assertEqual(1, qs.count())

    def test_do_filter(self):
        books = Book.objects.all()
        qdata = {'q__price': '100'}
        qs = do_filter(books, qdata)
        self.assertEqual(1, qs.count())
        qdata = {'q__name__icontains': 'book-03'}
        qs = do_filter(books, qdata)
        self.assertEqual(1, qs.count())
        qdata = {'q__category__name__icontains': 'catego'}
        qs = do_filter(books, qdata)
        self.assertEqual(2, qs.count())
        qdata = {'q__is_active': '__False'}
        qs = do_filter(books, qdata)
        self.assertEqual(1, qs.count())


class FormTests(TestCase):
    def setUp(self):
        data = {'name': 'book name'}
        self.form = BookForm(data)

    @skipUnless(django.VERSION < (1, 10, 0), "crispy_forms not support Django 1.10")
    def test_quicksearchform(self):
        from crispy_forms.templatetags.crispy_forms_filters import as_crispy_form
        data = {'q_quick_search_kw': 'keyword'}
        form = QuickSearchForm(data)
        form.is_valid()
        self.assertEqual('keyword', form.cleaned_data['q_quick_search_kw'])
        self.assertTrue('id_q_quick_search_kw' in as_crispy_form(form))

    def test_filter_fields(self):
        # filter_fields(self, fields=[], exclude=[], include_all_if_empty=True):
        field_names = ['name', 'descn']
        fields = self.form.filter_fields(field_names)
        self.assertEqual(field_names, [e.name for e in fields])
        fields = self.form.filter_fields(exclude=field_names)
        self.assertEqual(['price', 'is_active', 'category'], [e.name for e in fields])
        fields = self.form.filter_fields(include_all_if_empty=True)
        self.assertEqual(['name', 'descn', 'price', 'is_active', 'category'], [e.name for e in fields])
        fields = self.form.filter_fields(include_all_if_empty=False)
        self.assertEqual([], [e.name for e in fields])

    def test_init_crispy_helper(self):
        self.form.init_crispy_helper()
        self.assertTrue(self.form.helper is not None)

    @skipUnless(django.VERSION >= (1, 7, 0), "test only applies to Django 1.7+")
    def test_errors_as_text(self):
        data = {}
        form = BookForm(data)
        form.is_valid()
        self.assertTrue('Name' in form.errors_as_text())

{'fraction_non_alphanumeric': 0.13230851252887862, 'fraction_numerical': 0.017327172560867247, 'mean_word_length': 3.0587811034980166, 'pattern_counts': {'":': 0, '<': 4, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6474173', 'n_tokens_mistral': 3748, 'n_tokens_neox': 3603, 'n_words': 715}