mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-04 06:38:16 -06:00
17558 add tests
This commit is contained in:
parent
52b8bd5523
commit
822a4a4bc8
@ -825,9 +825,9 @@ class CustomFieldChoiceSet(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel
|
|||||||
# check if removing any used choices
|
# check if removing any used choices
|
||||||
if self._original_extra_choices:
|
if self._original_extra_choices:
|
||||||
original_choices = new_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:
|
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.
|
# only care about what fields are being deleted.
|
||||||
if diff_choices := list(set(original_choices) - set(new_choices)):
|
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():
|
if object_type.model_class().objects.filter(models.Q(**query_args, _connector=models.Q.OR)).exists():
|
||||||
raise ValidationError(
|
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
|
choice=choice, model=object_type
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -343,6 +343,78 @@ class CustomFieldTest(TestCase):
|
|||||||
instance.refresh_from_db()
|
instance.refresh_from_db()
|
||||||
self.assertIsNone(instance.custom_field_data.get(cf.name))
|
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):
|
def test_object_field(self):
|
||||||
value = VLAN.objects.create(name='VLAN 1', vid=1).pk
|
value = VLAN.objects.create(name='VLAN 1', vid=1).pk
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user