Files
@ 529cb3e7db9b
Branch filter:
Location: conntrackt/conntrackt/tests/test_forms.py
529cb3e7db9b
3.5 KiB
text/x-python
CONNT-13: Moved the CSS styling and text input placeholder setting into auxiliary form mixin clases. Update existing forms and tests.
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 | # 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")
|