Mark Lavin avatar Mark Lavin committed d69afbd

Clean up widget tests for HTML assertions. Fixes #85

Comments (0)

Files changed (2)

selectable/tests/functests.py

         inputs = parsed_inputs(rendered_form)
         # Selected text should be populated
         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 = inputs['thing_1'][0]
-        attributes = dict(thing_1.attributes.items())
-        self.assertEqual(thing_1.attributes['value'].value, str(self.test_thing.pk))
+        self.assertEqual(int(thing_1.attributes['value'].value), self.test_thing.pk)
 
     def test_populate_from_model(self):
         "Populate from existing model."
         inputs = parsed_inputs(rendered_form)
         # Selected text should be populated
         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 = inputs['thing_1'][0]
-        attributes = dict(thing_1.attributes.items())
-        self.assertEqual(thing_1.attributes['value'].value, str(self.test_thing.pk))
+        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} 
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.