Files
@ 265b2706e075
Branch filter:
Location: conntrackt/conntrackt/tests/test_forms.py
265b2706e075
4.3 KiB
text/x-python
CONNT-22: Added licensing information to Python files. Added licensing information to documentation files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2013 Branko Majic
#
# This file is part of Django Conntrackt.
#
# Django Conntrackt is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Django Conntrackt is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Django Conntrackt. If not, see <http://www.gnu.org/licenses/>.
#
# Django imports.
from django.test import TestCase
# Python third-party library imports.
import mock
# Application imports.
from conntrackt.forms import WidgetCSSClassFormMixin, PlaceholderFormMixin
from conntrackt.forms import EntityForm, InterfaceForm, CommunicationForm
# Test imports.
from .forms import FormWithWidgetCSSClassFormMixin, FormWithPlaceholderFormMixin
class WidgetCSSClassFormMixinTest(TestCase):
"""
Tests the form mixin.
"""
def test_apply_to_all(self):
"""
Test if CSS class is appled to all form field widgets.
"""
# Set-up the form.
form_class = FormWithWidgetCSSClassFormMixin
form_class.widget_css_classes = {"ALL": "test"}
form = form_class()
self.assertEqual(form.fields["field1"].widget.attrs["class"], "test")
self.assertEqual(form.fields["field2"].widget.attrs["class"], "test")
def test_apply_to_single(self):
"""
Test if CSS class is appled to a single field widget.
"""
# Set-up the form.
form_class = FormWithWidgetCSSClassFormMixin
form_class.widget_css_classes = {"field2": "test"}
form = form_class()
self.assertEqual(form.fields["field1"].widget.attrs.get("class", None), None)
self.assertEqual(form.fields["field2"].widget.attrs["class"], "test")
def test_apply_multiple(self):
"""
Tests if different class is applied to multiple form field widgets.
"""
# Set-up the form.
form_class = FormWithWidgetCSSClassFormMixin
form_class.widget_css_classes = {"field1": "f1",
"field2": "f2"}
form = form_class()
self.assertEqual(form.fields["field1"].widget.attrs["class"], "f1")
self.assertEqual(form.fields["field2"].widget.attrs["class"], "f2")
def test_apply_to_all_additional(self):
"""
Tests if all widgets get the same CSS class in addition to induvidual
ones.
"""
# Set-up the form.
form_class = FormWithWidgetCSSClassFormMixin
form_class.widget_css_classes = {"field1": "f1",
"field2": "f2",
"ALL": "all"}
form = form_class()
self.assertEqual(sorted(["f1", "all"]), sorted(form.fields["field1"].widget.attrs["class"].split(" ")))
self.assertEqual(sorted(["f2", "all"]), sorted(form.fields["field2"].widget.attrs["class"].split(" ")))
class PlaceholderFormMixinTest(TestCase):
"""
Tests the form mixin.
"""
def test_apply_one(self):
"""
Test if a single placeholder is applied to a form field widget.
"""
# Set-up the form.
form_class = FormWithPlaceholderFormMixin
form_class.widget_placeholders = {"field1": "place1"}
form = form_class()
self.assertEqual(form.fields["field1"].widget.attrs["placeholder"], "place1")
self.assertEqual(form.fields["field2"].widget.attrs.get("placeholder", None), None)
def test_apply_multiple(self):
"""
Test if multiple placeholders are applied to form field widgets.
"""
# Set-up the form.
form_class = FormWithPlaceholderFormMixin
form_class.widget_placeholders = {"field1": "place1",
"field2": "place2"}
form = form_class()
self.assertEqual(form.fields["field1"].widget.attrs["placeholder"], "place1")
self.assertEqual(form.fields["field2"].widget.attrs["placeholder"], "place2")
|