1import os 2from distutils.version import StrictVersion 3 4import django 5from django.test import TestCase 6from django.template import Template, Context 7from django import forms 8 9from .templatetags import bootstrap 10 11TEST_DIR = os.path.abspath(os.path.join(__file__, '..')) 12 13 14CHOICES = ( 15 (0, 'Zero'), 16 (1, 'One'), 17 (2, 'Two'), 18) 19 20try: 21 # required by Django 1.7 and later 22 django.setup() 23except: 24 pass 25 26class ExampleForm(forms.Form): 27 char_field = forms.CharField(required=False) 28 choice_field = forms.ChoiceField(choices=CHOICES, required=False) 29 radio_choice = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect, required=False) 30 multiple_choice = forms.MultipleChoiceField(choices=CHOICES, required=False) 31 multiple_checkbox = forms.MultipleChoiceField(choices=CHOICES, widget=forms.CheckboxSelectMultiple, required=False) 32 file_fied = forms.FileField(required=False) 33 password_field = forms.CharField(widget=forms.PasswordInput, required=False) 34 textarea = forms.CharField(widget=forms.Textarea, required=False) 35 boolean_field = forms.BooleanField(required=False) 36 37 38class BootstrapTemplateTagTests(TestCase): 39 maxDiff = None 40 41 def test_basic_form(self): 42 form = ExampleForm() 43 44 html = Template("{% load bootstrap %}{{ form|bootstrap }}").render(Context({'form': form})) 45 46 47 if StrictVersion(django.get_version()) >= StrictVersion('1.7'): 48 fixture = 'basic.html' 49 elif StrictVersion(django.get_version()) >= StrictVersion('1.6'): 50 fixture = 'basic_dj16.html' 51 else: 52 fixture = 'basic_old.html' 53 54 tpl = os.path.join('fixtures', fixture) 55 with open(os.path.join(TEST_DIR, tpl)) as f: 56 content = f.read() 57 58 self.assertHTMLEqual(html, content) 59 60 def test_horizontal_form(self): 61 form = ExampleForm() 62 63 html = Template("{% load bootstrap %}{{ form|bootstrap_horizontal }}").render(Context({'form': form})) 64 65 if StrictVersion(django.get_version()) >= StrictVersion('1.7'): 66 fixture = 'horizontal.html' 67 elif StrictVersion(django.get_version()) >= StrictVersion('1.6'): 68 fixture = 'horizontal_dj16.html' 69 else: 70 fixture = 'horizontal_old.html' 71 72 tpl = os.path.join('fixtures', fixture) 73 with open(os.path.join(TEST_DIR, tpl)) as f: 74 content = f.read() 75 76 self.assertHTMLEqual(html, content) 77 78 def test_bound_field(self): 79 form = ExampleForm(data={'char_field': 'asdf'}) 80 81 self.assertTrue(form.is_bound) 82 rendered_template = bootstrap.bootstrap(form['char_field']) 83