Mark Lavin avatar Mark Lavin committed b24ca01

Updating functional tests to better handle HTML assertions. See #85

Comments (0)

Files changed (2)

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]
+        attributes = dict(thing_0.attributes.items())
+        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]
+        attributes = dict(thing_1.attributes.items())
+        self.assertEqual(thing_1.attributes['value'].value, str(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]
+        attributes = dict(thing_0.attributes.items())
+        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]
+        attributes = dict(thing_1.attributes.items())
+        self.assertEqual(thing_1.attributes['value'].value, str(self.test_thing.pk))
 
 
 class SelectWidgetForm(forms.ModelForm):
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.