Commits

Mark Lavin committed d83fbfc Merge

Merge in stable testing work.

Comments (0)

Files changed (4)

selectable/tests/base.py

 import random
 import string
+from xml.dom.minidom import parseString
 
 from django.conf import settings
 from django.core.urlresolvers import reverse
 )
 
 
+def as_xml(html):
+    "Convert HTML portion to minidom node."
+    return parseString('<root>%s</root>' % html)
+
+
+def parsed_inputs(html):
+    "Returns a dictionary mapping name --> node of inputs found in the HTML."
+    node = as_xml(html)
+    inputs = {}
+    for field in node.getElementsByTagName('input'):
+        name = dict(field.attributes.items())['name']
+        current = inputs.get(name, [])
+        current.append(field)
+        inputs[name] = current
+    return inputs
+
+
 class PatchSettingsMixin(object):
     def setUp(self):
         super(PatchSettingsMixin, self).setUp()

selectable/tests/functests.py

 from selectable.forms import AutoCompleteSelectField, AutoCompleteSelectMultipleField
 from selectable.forms import AutoCompleteSelectWidget, AutoComboboxSelectWidget
 from selectable.tests import ManyThing, OtherThing, ThingLookup
-from selectable.tests.base import BaseSelectableTestCase
+from selectable.tests.base import BaseSelectableTestCase, parsed_inputs
 
 
 __all__ = (
         form = OtherThingForm(data=data)
         self.assertFalse(form.is_valid(), u'Form should not be valid')
         rendered_form = form.as_p()
+        inputs = parsed_inputs(rendered_form)
         # Selected text should be populated
-        thing_0 = 'name="thing_0" value="%s"' % self.test_thing.name
-        self.assertTrue(thing_0 in rendered_form, u"Didn't render selected text.")
+        thing_0 = inputs['thing_0'][0]
+        self.assertEqual(thing_0.attributes['value'].value, self.test_thing.name)
         # Selected pk should be populated
-        thing_1 = 'name="thing_1" value="%s"' % self.test_thing.pk
-        self.assertTrue(thing_1 in rendered_form, u"Didn't render selected pk.")
+        thing_1 = inputs['thing_1'][0]
+        self.assertEqual(int(thing_1.attributes['value'].value), self.test_thing.pk)
 
     def test_populate_from_model(self):
         "Populate from existing model."
         other_thing = OtherThing.objects.create(thing=self.test_thing, name='a')
         form = OtherThingForm(instance=other_thing)
         rendered_form = form.as_p()
+        inputs = parsed_inputs(rendered_form)
         # Selected text should be populated
-        thing_0 = 'name="thing_0" value="%s"' % self.test_thing.name
-        self.assertTrue(thing_0 in rendered_form, u"Didn't render selected text.")
+        thing_0 = inputs['thing_0'][0]
+        self.assertEqual(thing_0.attributes['value'].value, self.test_thing.name)
         # Selected pk should be populated
-        thing_1 = 'name="thing_1" value="%s"' % self.test_thing.pk
-        self.assertTrue(thing_1 in rendered_form, u"Didn't render selected pk.")
+        thing_1 = inputs['thing_1'][0]
+        self.assertEqual(int(thing_1.attributes['value'].value), self.test_thing.pk)
 
 
 class SelectWidgetForm(forms.ModelForm):

selectable/tests/widgets.py

 
 from selectable.forms import widgets
 from selectable.tests import Thing, ThingLookup
-from selectable.tests.base import BaseSelectableTestCase
+from selectable.tests.base import BaseSelectableTestCase, parsed_inputs
 
 
 __all__ = (
         widget = self.get_widget_instance()
         val = 4
         rendered_value = widget.render('field_name', val)
-        ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % val
-        self.assertTrue(ev in rendered_value,
-            "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+        inputs = parsed_inputs(rendered_value)
+        field = inputs['field_name_1'][0]
+        self.assertEqual(field.attributes['data-selectable-type'].value, 'hidden-multiple')
+        self.assertEqual(field.attributes['type'].value, 'hidden')
+        self.assertEqual(int(field.attributes['value'].value), val)
 
     def test_render_list(self):
         widget = self.get_widget_instance()
         list_val = [8, 5]
         rendered_value = widget.render('field_name', list_val)
-        for v in list_val:
-            ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % v
-            self.assertTrue(ev in rendered_value,
-                "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+        inputs = parsed_inputs(rendered_value)
+        found_values = []
+        for field in inputs['field_name_1']:
+            self.assertEqual(field.attributes['data-selectable-type'].value, 'hidden-multiple')
+            self.assertEqual(field.attributes['type'].value, 'hidden')
+            found_values.append(int(field.attributes['value'].value))
+        self.assertListEqual(found_values, list_val)
 
     def test_render_qs(self):
         widget = self.get_widget_instance()
         t2 = self.create_thing()
         qs_val = Thing.objects.filter(pk__in=[t1.pk, t2.pk]).values_list('pk', flat=True)
         rendered_value = widget.render('field_name', qs_val)
-        for t in qs_val:
-            ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % t
-            self.assertTrue(ev in rendered_value,
-                "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+        inputs = parsed_inputs(rendered_value)
+        found_values = []
+        for field in inputs['field_name_1']:
+            self.assertEqual(field.attributes['data-selectable-type'].value, 'hidden-multiple')
+            self.assertEqual(field.attributes['type'].value, 'hidden')
+            found_values.append(int(field.attributes['value'].value))
+        self.assertListEqual(found_values, [t1.pk, t2.pk])
 
     def test_update_query_parameters(self):
         params = {'active': 1} 
         widget = self.get_widget_instance()
         val = 4
         rendered_value = widget.render('field_name', val)
-        ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % val
-        self.assertTrue(ev in rendered_value,
-            "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+        inputs = parsed_inputs(rendered_value)
+        field = inputs['field_name_1'][0]
+        self.assertEqual(field.attributes['data-selectable-type'].value, 'hidden-multiple')
+        self.assertEqual(field.attributes['type'].value, 'hidden')
+        self.assertEqual(field.attributes['value'].value, str(val))
 
     def test_render_list(self):
         widget = self.get_widget_instance()
         list_val = [8, 5]
         rendered_value = widget.render('field_name', list_val)
-        for v in list_val:
-            ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % v
-            self.assertTrue(ev in rendered_value,
-                "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+        inputs = parsed_inputs(rendered_value)
+        found_values = []
+        for field in inputs['field_name_1']:
+            self.assertEqual(field.attributes['data-selectable-type'].value, 'hidden-multiple')
+            self.assertEqual(field.attributes['type'].value, 'hidden')
+            found_values.append(int(field.attributes['value'].value))
+        self.assertListEqual(found_values, list_val)
 
     def test_render_qs(self):
         widget = self.get_widget_instance()
         t2 = self.create_thing()
         qs_val = Thing.objects.filter(pk__in=[t1.pk, t2.pk]).values_list('pk', flat=True)
         rendered_value = widget.render('field_name', qs_val)
-        for t in qs_val:
-            ev = 'data-selectable-type="hidden-multiple" type="hidden" name="field_name_1" value="%d"' % t
-            self.assertTrue(ev in rendered_value,
-                "Did not find:\n\t%s\nin rendered value:\n\t%s" % (ev, rendered_value))
+        inputs = parsed_inputs(rendered_value)
+        found_values = []
+        for field in inputs['field_name_1']:
+            self.assertEqual(field.attributes['data-selectable-type'].value, 'hidden-multiple')
+            self.assertEqual(field.attributes['type'].value, 'hidden')
+            found_values.append(int(field.attributes['value'].value))
+        self.assertListEqual(found_values, [t1.pk, t2.pk])
 
     def test_update_query_parameters(self):
         params = {'active': 1} 
 
 [testenv:py26-1.5.X]
 basepython = python2.6
-deps = https://www.djangoproject.com/download/1.5b2/tarball/
+deps = https://www.djangoproject.com/download/1.5c1/tarball/
     mock
 
 [testenv:py26-1.4.X]
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.