From d40a49e6e3f70ef13e792643c801accda4f9cb08 Mon Sep 17 00:00:00 2001 From: Per von Zweigbergk Date: Sat, 9 Sep 2023 14:16:17 +0200 Subject: [PATCH] Refactor of expand_alphanumeric_pattern and tests The new behaviour of expand_alphanumeric_pattern is to return the input unmodified if there are no expansion patterns in it. Unit tests changed to reflect this new behaviour. This is fine because the only place that calls expand_alphanumeric_pattern was doing this check before anyway (and that place has been changed in this commit), this aligns the unit tests better with actual application behaviour, without changing external behaviour at all. --- netbox/utilities/forms/fields/expandable.py | 4 +-- netbox/utilities/forms/utils.py | 6 +++- netbox/utilities/tests/test_forms.py | 40 ++++++++++++--------- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/netbox/utilities/forms/fields/expandable.py b/netbox/utilities/forms/fields/expandable.py index 959271a85..c6370bbd3 100644 --- a/netbox/utilities/forms/fields/expandable.py +++ b/netbox/utilities/forms/fields/expandable.py @@ -29,9 +29,7 @@ class ExpandableNameField(forms.CharField): def to_python(self, value): if not value: return '' - if re.search(ALPHANUMERIC_EXPANSION_PATTERN, value): - return list(expand_alphanumeric_pattern(value)) - return [value] + return list(expand_alphanumeric_pattern(value)) class ExpandableIPAddressField(forms.CharField): diff --git a/netbox/utilities/forms/utils.py b/netbox/utilities/forms/utils.py index 4d737f163..bb333bbcc 100644 --- a/netbox/utilities/forms/utils.py +++ b/netbox/utilities/forms/utils.py @@ -87,7 +87,11 @@ def expand_alphanumeric_pattern(string): """ Expand an alphabetic pattern into a list of strings. """ - lead, pattern, remnant = re.split(ALPHANUMERIC_EXPANSION_PATTERN, string, maxsplit=1) + try: + lead, pattern, remnant = re.split(ALPHANUMERIC_EXPANSION_PATTERN, string, maxsplit=1) + except ValueError: + yield string + return parsed_range = parse_alphanumeric_range(pattern) for i in parsed_range: if re.search(ALPHANUMERIC_EXPANSION_PATTERN, remnant): diff --git a/netbox/utilities/tests/test_forms.py b/netbox/utilities/tests/test_forms.py index 79ba3f4d8..ac58100c3 100644 --- a/netbox/utilities/tests/test_forms.py +++ b/netbox/utilities/tests/test_forms.py @@ -246,18 +246,22 @@ class ExpandAlphanumeric(TestCase): self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) def test_invalid_non_pattern(self): - with self.assertRaises(ValueError): - sorted(expand_alphanumeric_pattern('r9a')) + input = 'r9a' + output = sorted([input]) + self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) def test_invalid_range(self): - with self.assertRaises(ValueError): - sorted(expand_alphanumeric_pattern('r[8-]a')) + input = 'r[8-]a' + output = sorted([input]) + self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) - with self.assertRaises(ValueError): - sorted(expand_alphanumeric_pattern('r[-8]a')) + input = 'r[-8]a' + output = sorted([input]) + self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) - with self.assertRaises(ValueError): - sorted(expand_alphanumeric_pattern('r[8--9]a')) + input = 'r[8--9]a' + output = sorted([input]) + self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) def test_invalid_range_alphanumeric(self): self.assertEqual(sorted(expand_alphanumeric_pattern('r[9-a]a')), []) @@ -273,17 +277,21 @@ class ExpandAlphanumeric(TestCase): sorted(expand_alphanumeric_pattern('r[a-bb]a')) def test_invalid_set(self): - with self.assertRaises(ValueError): - sorted(expand_alphanumeric_pattern('r[a]a')) + input = 'r[a]a' + output = sorted([input]) + self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) - with self.assertRaises(ValueError): - sorted(expand_alphanumeric_pattern('r[a,]a')) + input = 'r[a,]a' + output = sorted([input]) + self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) - with self.assertRaises(ValueError): - sorted(expand_alphanumeric_pattern('r[,a]a')) + input = 'r[,a]a' + output = sorted([input]) + self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) - with self.assertRaises(ValueError): - sorted(expand_alphanumeric_pattern('r[a,,b]a')) + input = 'r[a,,b]a' + output = sorted([input]) + self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output) class ImportFormTest(TestCase):