tests.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2015 The Distro Tracker Developers
  2. # See the COPYRIGHT file at the top-level directory of this distribution and
  3. # at http://deb.li/DTAuthors
  4. #
  5. # This file is part of Distro Tracker. It is subject to the license terms
  6. # in the LICENSE file found in the top-level directory of this
  7. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  8. # including this file, may be copied, modified, propagated, or distributed
  9. # except according to the terms contained in the LICENSE file.
  10. # This file has been forked from django-bootstrap-form which was
  11. # BSD licensed:
  12. #
  13. # Copyright (c) Ming Hsien Tzang and individual contributors.
  14. # All rights reserved.
  15. #
  16. # Redistribution and use in source and binary forms, with or without
  17. # modification, are permitted provided that the following conditions are met:
  18. #
  19. # 1. Redistributions of source code must retain the above copyright notice,
  20. # this list of conditions and the following disclaimer.
  21. #
  22. # 2. Redistributions in binary form must reproduce the above copyright
  23. # notice, this list of conditions and the following disclaimer in the
  24. # documentation and/or other materials provided with the distribution.
  25. #
  26. # 3. Neither the name of django-bootstrap-form nor the names of its
  27. # contributors may be used to endorse or promote products derived from
  28. # this software without specific prior written permission.
  29. #
  30. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  34. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  38. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. from distro_tracker.test import TestCase
  41. from django.template import Template, Context
  42. from django import forms
  43. CHOICES = (
  44. (0, 'Zero'),
  45. (1, 'One'),
  46. (2, 'Two'),
  47. )
  48. class ExampleForm(forms.Form):
  49. char_field = forms.CharField()
  50. choice_field = forms.ChoiceField(choices=CHOICES)
  51. radio_choice = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect)
  52. multiple_choice = forms.MultipleChoiceField(choices=CHOICES)
  53. multiple_checkbox = forms.MultipleChoiceField(
  54. choices=CHOICES, widget=forms.CheckboxSelectMultiple)
  55. file_fied = forms.FileField()
  56. password_field = forms.CharField(widget=forms.PasswordInput)
  57. textarea = forms.CharField(widget=forms.Textarea)
  58. boolean_field = forms.BooleanField()
  59. class BootstrapTemplateTagTests(TestCase):
  60. def setUp(self):
  61. self.maxDiff = None
  62. self.form = ExampleForm()
  63. self.form.use_required_attribute = False
  64. def render_template(self, content):
  65. return Template(content).render(Context({'form': self.form}))
  66. def load_test_data(self, filename):
  67. path = self.get_test_data_path(filename)
  68. with open(path) as f:
  69. return f.read()
  70. def test_basic_form(self):
  71. expected = self.load_test_data('bootstrap-form-basic.html')
  72. template_content = "{% load bootstrap %}{{ form|bootstrap }}"
  73. html = self.render_template(template_content)
  74. self.assertHTMLEqual(expected, html)
  75. def test_inline_form(self):
  76. expected = self.load_test_data('bootstrap-form-inline.html')
  77. template_content = "{% load bootstrap %}{{ form|bootstrap_inline }}"
  78. html = self.render_template(template_content)
  79. self.assertHTMLEqual(expected, html)
  80. def test_horizontal_form(self):
  81. expected = self.load_test_data('bootstrap-form-horizontal.html')
  82. template_content = "{% load bootstrap %}{{ form|bootstrap_horizontal }}"
  83. html = self.render_template(template_content)
  84. self.assertHTMLEqual(expected, html)