From 822a4a4bc81b2ec339ae841590989643e7104d49 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Tue, 24 Sep 2024 12:16:14 -0700 Subject: [PATCH] 17558 add tests --- netbox/extras/models/customfields.py | 6 +- netbox/extras/tests/test_customfields.py | 72 ++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 73b541a9b..270de3488 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -825,9 +825,9 @@ class CustomFieldChoiceSet(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel # check if removing any used choices if self._original_extra_choices: original_choices = new_choices = [] - original_choices = [obj[1] for obj in self._original_extra_choices] + original_choices = [obj[0] for obj in self._original_extra_choices] if self.extra_choices: - new_choices = [obj[1] for obj in self.extra_choices] + new_choices = [obj[0] for obj in self.extra_choices] # only care about what fields are being deleted. if diff_choices := list(set(original_choices) - set(new_choices)): @@ -845,7 +845,7 @@ class CustomFieldChoiceSet(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel } if object_type.model_class().objects.filter(models.Q(**query_args, _connector=models.Q.OR)).exists(): raise ValidationError( - _("Cannot remove choice {choice} as it is used in model {model}").format( + _("Cannot remove choice {choice} as it is used in records of model {model}").format( choice=choice, model=object_type ) ) diff --git a/netbox/extras/tests/test_customfields.py b/netbox/extras/tests/test_customfields.py index 697b756ec..f440ebd2f 100644 --- a/netbox/extras/tests/test_customfields.py +++ b/netbox/extras/tests/test_customfields.py @@ -343,6 +343,78 @@ class CustomFieldTest(TestCase): instance.refresh_from_db() self.assertIsNone(instance.custom_field_data.get(cf.name)) + def test_select_validation(self): + CHOICES = ( + ('a', 'Option A'), + ('b', 'Option B'), + ('c', 'Option C'), + ('d', 'Option D'), + ) + + # Create a set of custom field choices + choice_set = CustomFieldChoiceSet.objects.create( + name='Custom Field Choice Set 1', + extra_choices=CHOICES + ) + + # Create a custom field & check that initial value is null + cf = CustomField.objects.create( + name='select_field', + type=CustomFieldTypeChoices.TYPE_SELECT, + required=False, + choice_set=choice_set + ) + cf.object_types.set([self.object_type]) + + # Create a custom field & check that initial value is null + cf_multiselect = CustomField.objects.create( + name='multiselect_field', + type=CustomFieldTypeChoices.TYPE_MULTISELECT, + required=False, + choice_set=choice_set + ) + cf_multiselect.object_types.set([self.object_type]) + + instance = Site.objects.first() + + # Assign a value and check that it is saved + instance.custom_field_data[cf.name] = 'a' + instance.custom_field_data[cf_multiselect.name] = ['b', 'c'] + instance.save() + instance.refresh_from_db() + + # check can't delete single choice custom field option + with self.assertRaises(ValidationError): + CHOICES = ( + ('b', 'Option B'), + ('c', 'Option C'), + ('d', 'Option D'), + ) + choice_set.extra_choices = CHOICES + choice_set.full_clean() + choice_set.save() + + # check can't delete multi choice custom field option + with self.assertRaises(ValidationError): + CHOICES = ( + ('a', 'Option A'), + ('b', 'Option B'), + ('d', 'Option D'), + ) + choice_set.extra_choices = CHOICES + choice_set.full_clean() + choice_set.save() + + # delete non selected option should work fine + CHOICES = ( + ('a', 'Option A'), + ('b', 'Option B'), + ('c', 'Option C'), + ) + choice_set.extra_choices = CHOICES + choice_set.full_clean() + choice_set.save() + def test_object_field(self): value = VLAN.objects.create(name='VLAN 1', vid=1).pk