|
|
@@ -7,6 +7,7 @@ from utilities.forms.bulk_import import BulkImportForm
|
|
|
from utilities.forms.fields.csv import CSVSelectWidget
|
|
|
from utilities.forms.forms import BulkRenameForm
|
|
|
from utilities.forms.utils import get_field_value, expand_alphanumeric_pattern, expand_ipaddress_pattern
|
|
|
+from utilities.forms.widgets.select import AvailableOptions, SelectedOptions
|
|
|
|
|
|
|
|
|
class ExpandIPAddress(TestCase):
|
|
|
@@ -481,3 +482,71 @@ class CSVSelectWidgetTest(TestCase):
|
|
|
widget = CSVSelectWidget()
|
|
|
data = {'test_field': 'valid_value'}
|
|
|
self.assertFalse(widget.value_omitted_from_data(data, {}, 'test_field'))
|
|
|
+
|
|
|
+
|
|
|
+class SelectMultipleWidgetTest(TestCase):
|
|
|
+ """
|
|
|
+ Validate filtering behavior of AvailableOptions and SelectedOptions widgets.
|
|
|
+ """
|
|
|
+
|
|
|
+ def test_available_options_flat_choices(self):
|
|
|
+ """AvailableOptions should exclude selected values from flat choices"""
|
|
|
+ widget = AvailableOptions(choices=[
|
|
|
+ (1, 'Option 1'),
|
|
|
+ (2, 'Option 2'),
|
|
|
+ (3, 'Option 3'),
|
|
|
+ ])
|
|
|
+ widget.optgroups('test', ['2'], None)
|
|
|
+
|
|
|
+ self.assertEqual(len(widget.choices), 2)
|
|
|
+ self.assertEqual(widget.choices[0], (1, 'Option 1'))
|
|
|
+ self.assertEqual(widget.choices[1], (3, 'Option 3'))
|
|
|
+
|
|
|
+ def test_available_options_optgroups(self):
|
|
|
+ """AvailableOptions should exclude selected values from optgroups"""
|
|
|
+ widget = AvailableOptions(choices=[
|
|
|
+ ('Group A', [(1, 'Option 1'), (2, 'Option 2')]),
|
|
|
+ ('Group B', [(3, 'Option 3'), (4, 'Option 4')]),
|
|
|
+ ])
|
|
|
+
|
|
|
+ # Select options 2 and 3
|
|
|
+ widget.optgroups('test', ['2', '3'], None)
|
|
|
+
|
|
|
+ # Should have 2 groups with filtered choices
|
|
|
+ self.assertEqual(len(widget.choices), 2)
|
|
|
+ self.assertEqual(widget.choices[0][0], 'Group A')
|
|
|
+ self.assertEqual(widget.choices[0][1], [(1, 'Option 1')])
|
|
|
+ self.assertEqual(widget.choices[1][0], 'Group B')
|
|
|
+ self.assertEqual(widget.choices[1][1], [(4, 'Option 4')])
|
|
|
+
|
|
|
+ def test_selected_options_flat_choices(self):
|
|
|
+ """SelectedOptions should include only selected values from flat choices"""
|
|
|
+ widget = SelectedOptions(choices=[
|
|
|
+ (1, 'Option 1'),
|
|
|
+ (2, 'Option 2'),
|
|
|
+ (3, 'Option 3'),
|
|
|
+ ])
|
|
|
+
|
|
|
+ # Select option 2
|
|
|
+ widget.optgroups('test', ['2'], None)
|
|
|
+
|
|
|
+ # Should only have option 2
|
|
|
+ self.assertEqual(len(widget.choices), 1)
|
|
|
+ self.assertEqual(widget.choices[0], (2, 'Option 2'))
|
|
|
+
|
|
|
+ def test_selected_options_optgroups(self):
|
|
|
+ """SelectedOptions should include only selected values from optgroups"""
|
|
|
+ widget = SelectedOptions(choices=[
|
|
|
+ ('Group A', [(1, 'Option 1'), (2, 'Option 2')]),
|
|
|
+ ('Group B', [(3, 'Option 3'), (4, 'Option 4')]),
|
|
|
+ ])
|
|
|
+
|
|
|
+ # Select options 2 and 3
|
|
|
+ widget.optgroups('test', ['2', '3'], None)
|
|
|
+
|
|
|
+ # Should have 2 groups with only selected choices
|
|
|
+ self.assertEqual(len(widget.choices), 2)
|
|
|
+ self.assertEqual(widget.choices[0][0], 'Group A')
|
|
|
+ self.assertEqual(widget.choices[0][1], [(2, 'Option 2')])
|
|
|
+ self.assertEqual(widget.choices[1][0], 'Group B')
|
|
|
+ self.assertEqual(widget.choices[1][1], [(3, 'Option 3')])
|