Mikhail Korobov avatar Mikhail Korobov committed ee3b091

More tests; testing cleanup

Comments (0)

Files changed (2)

 [tox]
-envlist = py25,py26,py27,py32,pypy,py_django12,py_django14
+envlist = py25,py26,py27,py32,py33,pypy,dj12,dj13,dj14,dj15
 
 [testenv]
+; current django
 deps=
-    django == 1.3.1
+    django
+
+basepython = python2.7
 
 commands=
     python runtests.py
 
-[testenv:py_django12]
+[testenv:dj12]
 deps=
-    django == 1.2.7
+    django >= 1.2, <1.3
 
-[testenv:py_django14]
+[testenv:dj13]
 deps=
-    http://www.djangoproject.com/download/1.4-rc-2/tarball/#egg=django
+    django >= 1.3, <1.4
+
+[testenv:dj14]
+deps=
+    django >= 1.4, <1.5
+
+[testenv:dj15]
+deps=
+    django >= 1.5, <1.6
+
+[testenv:py25]
+deps=
+
+; use insecure pip
+commands=
+    easy_install pip==1.2.1
+    pip install 'django >= 1.4, < 1.5'
+    python runtests.py
 
 [testenv:py32]
 deps=
-    https://bitbucket.org/vinay.sajip/django/get/tip.zip#egg=django
+    django >= 1.5
+
+[testenv:py33]
+deps=
+    django >= 1.5

widget_tweaks/tests.py

 from django.template import Template, Context
 from django.forms.extras.widgets import SelectDateWidget
 
+# ==============================
+#       Testing helpers
+# ==============================
 
 class MyForm(Form):
+    """
+    Test form. If you want to test rendering of a field,
+    add it to this form and use one of 'render_...' functions
+    from this module.
+    """
     simple = CharField()
     with_attrs = CharField(widget=TextInput(attrs={
-                    'foo':'baz',
+                    'foo': 'baz',
                     'egg': 'spam'
                  }))
     with_cls = CharField(widget=TextInput(attrs={'class':'class0'}))
     date = forms.DateField(widget=SelectDateWidget(attrs={'egg': 'spam'}))
 
-def render_form(text):
+
+def render_form(text, form=None):
+    """
+    Renders template ``text`` with widget_tweaks library loaded
+    and MyForm instance available in context as ``form``.
+    """
     tpl = Template("{% load widget_tweaks %}" + text)
-    context = Context({'form': MyForm()})
+    context = Context({'form': MyForm() if form is None else form})
     return tpl.render(context)
 
 
-def render_field(field, filter, params, *args):
-    filters = [(filter, params)]
+def render_field(field, template_filter, params, *args, **kwargs):
+    """
+    Renders ``field`` of MyForm with filter ``template_filter`` applied.
+    ``params`` are filter arguments.
+
+    If you want to apply several filters (in a chain),
+    pass extra ``template_filter`` and ``params`` as positional arguments.
+
+    In order to use custom form, pass form instance as ``form``
+    keyword argument.
+    """
+    filters = [(template_filter, params)]
     filters.extend(zip(args[::2], args[1::2]))
     filter_strings = ['|%s:"%s"' % (f[0], f[1],) for f in filters]
     render_field_str = '{{ form.%s%s }}' % (field, ''.join(filter_strings))
-    return render_form(render_field_str)
+    return render_form(render_field_str, **kwargs)
+
 
 def render_field_from_tag(field, *attributes):
+    """
+    Renders MyForm's field ``field`` with attributes passed
+    as positional arguments.
+    """
     attr_strings = [' %s' % f for f in attributes]
     tpl = string.Template('{% render_field form.$field$attrs %}')
     render_field_str = tpl.substitute(field=field, attrs=''.join(attr_strings))
     return render_form(render_field_str)
 
+
 def assertIn(value, obj):
     assert value in obj, "%s not in %s" % (value, obj,)
 
+
 def assertNotIn(value, obj):
     assert value not in obj, "%s in %s" % (value, obj,)
 
+
+# ===============================
+#           Test cases
+# ===============================
+
 class SimpleAttrTest(TestCase):
     def test_attr(self):
         res = render_field('simple', 'attr', 'foo:bar')
         res = render_field('simple', 'add_class', 'foo', 'add_class', 'bar')
         assertIn('class="bar foo"', res)
 
+    def test_set_data(self):
+        res = render_field('simple', 'set_data', 'key:value')
+        assertIn('data-key="value"', res)
+
+
+class ErrorsTest(TestCase):
+
+    def _err_form(self):
+        form = MyForm({'foo': 'bar'})  # some random data
+        form.is_valid()  # trigger form validation
+        return form
+
+    def test_error_class_no_error(self):
+        res = render_field('simple', 'add_error_class', 'err')
+        assertNotIn('class="err"', res)
+
+    def test_error_class_error(self):
+        form = self._err_form()
+        res = render_field('simple', 'add_error_class', 'err', form=form)
+        assertIn('class="err"', res)
+
+    def test_error_attr_no_error(self):
+        res = render_field('simple', 'add_error_attr', 'aria-invalid:true')
+        assertNotIn('aria-invalid="true"', res)
+
+    def test_error_attr_error(self):
+        form = self._err_form()
+        res = render_field('simple', 'add_error_attr', 'aria-invalid:true', form=form)
+        assertIn('aria-invalid="true"', res)
+
+
 class SilenceTest(TestCase):
     def test_silence_without_field(self):
         res = render_field("nothing", 'attr', 'foo:bar')
-        self.assertEquals(res, "")
+        self.assertEqual(res, "")
         res = render_field("nothing", 'add_class', 'some')
-        self.assertEquals(res, "")
+        self.assertEqual(res, "")
+
 
 class CustomizedWidgetTest(TestCase):
     def test_attr(self):
 class RenderFieldTagSilenceTest(TestCase):
     def test_silence_without_field(self):
         res = render_field_from_tag("nothing", 'foo="bar"')
-        self.assertEquals(res, "")
+        self.assertEqual(res, "")
         res = render_field_from_tag("nothing", 'class+="some"')
-        self.assertEquals(res, "")
+        self.assertEqual(res, "")
 
 
 class RenderFieldTagCustomizedWidgetTest(TestCase):
         res = render_form('{% render_field form.with_cls %}{% render_field form.with_cls class+="bar" %}{% render_field form.with_cls %}')
         self.assertEqual(res.count("class0"), 3)
         self.assertEqual(res.count("bar"), 1)
-        
+
+
 class RenderFieldTagUseTemplateVariableTest(TestCase):
     def test_use_template_variable_in_parametrs(self):
         res = render_form('{% render_field form.with_attrs egg+="pahaz" placeholder=form.with_attrs.label %}')
         assertIn('egg="spam pahaz"', res)
         assertIn('placeholder="With attrs"', res)
-        
+
+
 class RenderFieldFilter_field_type_widget_type_Test(TestCase):
     def test_field_type_widget_type_rendering_simple(self):
         res = render_form('<div class="{{ form.simple|field_type }} {{ form.simple|widget_type }} {{ form.simple.html_name }}">{{ form.simple }}</div>')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.