# -*- coding: utf-8 -*- from __future__ import unicode_literals import re from django.test import TestCase from django import forms from django.template import Template, Context from .text import text_value, text_concat from .exceptions import BootstrapError from .utils import add_css_class RADIO_CHOICES = ( ('1', 'Radio 1'), ('2', 'Radio 2'), ) MEDIA_CHOICES = ( ('Audio', ( ('vinyl', 'Vinyl'), ('cd', 'CD'), ) ), ('Video', ( ('vhs', 'VHS Tape'), ('dvd', 'DVD'), ) ), ('unknown', 'Unknown'), ) class TestForm(forms.Form): """ Form with a variety of widgets to test bootstrap3 rendering. """ date = forms.DateField(required=False) subject = forms.CharField( max_length=100, help_text='my_help_text', required=True, widget=forms.TextInput(attrs={'placeholder': 'placeholdertest'}), ) message = forms.CharField(required=False, help_text='my_help_text') sender = forms.EmailField(label='Sender © unicode') secret = forms.CharField(initial=42, widget=forms.HiddenInput) cc_myself = forms.BooleanField( required=False, help_text='You will get a copy in your mailbox.') select1 = forms.ChoiceField(choices=RADIO_CHOICES) select2 = forms.MultipleChoiceField( choices=RADIO_CHOICES, help_text='Check as many as you like.', ) select3 = forms.ChoiceField(choices=MEDIA_CHOICES) select4 = forms.MultipleChoiceField( choices=MEDIA_CHOICES, help_text='Check as many as you like.', ) category1 = forms.ChoiceField( choices=RADIO_CHOICES, widget=forms.RadioSelect) category2 = forms.MultipleChoiceField( choices=RADIO_CHOICES, widget=forms.CheckboxSelectMultiple, help_text='Check as many as you like.', ) category3 = forms.ChoiceField( widget=forms.RadioSelect, choices=MEDIA_CHOICES) category4 = forms.MultipleChoiceField( choices=MEDIA_CHOICES, widget=forms.CheckboxSelectMultiple, help_text='Check as many as you like.', ) addon = forms.CharField( widget=forms.TextInput(attrs={'addon_before': 'before', 'addon_after': 'after'}), ) required_css_class = 'bootstrap3-req' def clean(self): cleaned_data = super(TestForm, self).clean() raise forms.ValidationError( "This error was added to show the non field errors styling.") return cleaned_data class TestFormWithoutRequiredClass(TestForm): required_css_class = '' def render_template(text, **context_args): """ Create a template ``text`` that first loads bootstrap3. """ template = Template("{% load bootstrap3 %}" + text) if 'form' not in context_args: context_args['form'] = TestForm() return template.render(Context(context_args)) def render_formset(formset=None, **context_args): """ Create a template that renders a formset """ context_args['formset'] = formset return render_template('{% bootstrap_formset formset %}', **context_args) def render_form(form=None, **context_args): """ Create a template that renders a form """ if form: context_args['form'] = form return render_template('{% bootstrap_form form %}', **context_args) def render_form_field(field, **context_args): """ Create a template that renders a field """ form_field = 'form.%s' % field return render_template( '{% bootstrap_field ' + form_field + ' %}', **context_args) def render_field(field, **context_args): """ Create a template that renders a field """ context_args['field'] = field return render_template('{% bootstrap_field field %}', **context_args) class SettingsTest(TestCase): def test_settings(self): from .bootstrap import BOOTSTRAP3 self.assertTrue(BOOTSTRAP3) def test_settings_filter(self): res = render_template( '{% load bootstrap3 %}' + '{{ "required_css_class"|bootstrap_setting }}') self.assertEqual(res.strip(), 'bootstrap3-req') res = render_template( '{% load bootstrap3 %}' + '{% if "javascript_in_head"|bootstrap_setting %}' + 'head{% else %}body{% endif %}' ) self.assertEqual(res.strip(), 'head') def test_required_class(self): form = TestForm() res = render_template('{% bootstrap_form form %}', form=form) self.assertIn('bootstrap3-req', res) def test_error_class(self): form = TestForm({}) res = render_template('{% bootstrap_form form %}', form=form) self.assertIn('bootstrap3-err', res) def test_bound_class(self): form = TestForm({'sender': 'sender'}) res = render_template('{% bootstrap_form form %}', form=form) self.assertIn('bootstrap3-bound', res) class TemplateTest(TestCase): def test_empty_template(self): res = render_template('') self.assertEqual(res.strip(), '') def test_text_template(self): res = render_template('some text') self.assertEqual(res.strip(), 'some text') def test_bootstrap_template(self): template = Template(( '{% extends "bootstrap3/bootstrap3.html" %}' + '{% block bootstrap3_content %}' + 'test_bootstrap3_content' + '{% endblock %}' )) res = template.render(Context({})) self.assertIn('test_bootstrap3_content', res) def test_javascript_without_jquery(self): res = render_template('{% bootstrap_javascript jquery=0 %}') self.assertIn('bootstrap', res) self.assertNotIn('jquery', res) def test_javascript_with_jquery(self): res = render_template('{% bootstrap_javascript jquery=1 %}') self.assertIn('bootstrap', res) self.assertIn('jquery', res) class FormSetTest(TestCase): def test_illegal_formset(self): with self.assertRaises(BootstrapError): render_formset(formset='illegal') class FormTest(TestCase): def test_illegal_form(self): with self.assertRaises(BootstrapError): render_form(form='illegal') def test_field_names(self): form = TestForm() res = render_form(form) for field in form: self.assertIn('name="%s"' % field.name, res) def test_field_addons(self): form = TestForm() res = render_form(form) self.assertIn('