Mark Lavin avatar Mark Lavin committed 1d5eedd

Tests and fixes for multi-select has changed detection.

Comments (0)

Files changed (2)

selectable/forms/widgets.py

         value = ['', value]
         return super(AutoCompleteSelectMultipleWidget, self).render(name, value, attrs)
 
+    def _has_changed(self, initial, data):
+        """"
+        Decects if the widget was changed. This is removed in 1.6.
+
+        For the multi-select case we only care if the hidden inputs changed.
+        """
+        initial = ['', initial]
+        data = ['', data]
+        return super(AutoCompleteSelectMultipleWidget, self)._has_changed(initial, data)
+
+
 
 class AutoComboboxSelectMultipleWidget(SelectableMultiWidget, SelectableMediaMixin):
 
         value = ['', value]
         return super(AutoComboboxSelectMultipleWidget, self).render(name, value, attrs)
 
+    def _has_changed(self, initial, data):
+        """"
+        Decects if the widget was changed. This is removed in 1.6.
+
+        For the multi-select case we only care if the hidden inputs changed.
+        """
+        initial = ['', initial]
+        data = ['', data]
+        return super(AutoComboboxSelectMultipleWidget, self)._has_changed(initial, data)

selectable/tests/functests.py

         things = manything.things.all()
         self.assertEqual(things.count(), 0)
 
+    def test_has_changed(self):
+        "Populate intial data from a model."
+        manything = ManyThing.objects.create(name='Foo')
+        thing_1 = self.create_thing()
+        manything.things.add(thing_1)
+        data = {
+            'name': manything.name,
+            'things_0': '', # Text input
+            'things_1': [thing_1.pk], # Hidden inputs
+        }
+        form = ManyThingForm(data=data, instance=manything)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+
 
 class SimpleForm(forms.Form):
     "Non-model form usage."
         """
         form = SimpleForm(data={}, empty_permitted=True)
         self.assertFalse(form.has_changed(), str(form.changed_data))
-        self.assertTrue(form.is_valid(), str(form.errors))
+        self.assertTrue(form.is_valid(), str(form.errors))
+
+    def test_select_multiple_changed(self):
+        """
+        Detect changes for a multiple select input with and without
+        initial data.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': '',
+            'new_thing_0': '',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': [self.test_thing.pk, ]
+        }
+        form = SimpleForm(data=data)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('things' in form.changed_data)
+
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': [self.test_thing.pk, ],
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': [],
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('things' in form.changed_data)
+
+    def test_single_select_changed(self):
+        """
+        Detect changes for a single select input with and without
+        initial data.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': self.test_thing.pk,
+            'new_thing_0': '',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': ''
+        }
+        form = SimpleForm(data=data)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('thing' in form.changed_data)
+
+        initial = {
+            'thing': self.test_thing.pk,
+            'new_thing': '',
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('thing' in form.changed_data)
+
+    def test_new_select_changed(self):
+        """
+        Detect changes for a single select input which allows new items
+        with and without initial data.
+        """
+        data = {
+            'thing_0': '',
+            'thing_1': '',
+            'new_thing_0': 'Foo',
+            'new_thing_1': '',
+            'things_0': '',
+            'things_1': ''
+        }
+        form = SimpleForm(data=data)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('new_thing' in form.changed_data)
+
+        initial = {
+            'thing': '',
+            'new_thing': ['Foo', None],
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertFalse(form.has_changed(), str(form.changed_data))
+
+        initial = {
+            'thing': '',
+            'new_thing': '',
+            'things': '',
+        }
+        form = SimpleForm(data=data, initial=initial)
+        self.assertTrue(form.has_changed())
+        self.assertTrue('new_thing' in form.changed_data)
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.