Skip to content

Commit d40a49e

Browse files
committed
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.
1 parent a8a36c0 commit d40a49e

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

netbox/utilities/forms/fields/expandable.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ def __init__(self, *args, **kwargs):
2929
def to_python(self, value):
3030
if not value:
3131
return ''
32-
if re.search(ALPHANUMERIC_EXPANSION_PATTERN, value):
33-
return list(expand_alphanumeric_pattern(value))
34-
return [value]
32+
return list(expand_alphanumeric_pattern(value))
3533

3634

3735
class ExpandableIPAddressField(forms.CharField):

netbox/utilities/forms/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ def expand_alphanumeric_pattern(string):
8787
"""
8888
Expand an alphabetic pattern into a list of strings.
8989
"""
90-
lead, pattern, remnant = re.split(ALPHANUMERIC_EXPANSION_PATTERN, string, maxsplit=1)
90+
try:
91+
lead, pattern, remnant = re.split(ALPHANUMERIC_EXPANSION_PATTERN, string, maxsplit=1)
92+
except ValueError:
93+
yield string
94+
return
9195
parsed_range = parse_alphanumeric_range(pattern)
9296
for i in parsed_range:
9397
if re.search(ALPHANUMERIC_EXPANSION_PATTERN, remnant):

netbox/utilities/tests/test_forms.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,22 @@ def test_set_and_range(self):
246246
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
247247

248248
def test_invalid_non_pattern(self):
249-
with self.assertRaises(ValueError):
250-
sorted(expand_alphanumeric_pattern('r9a'))
249+
input = 'r9a'
250+
output = sorted([input])
251+
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
251252

252253
def test_invalid_range(self):
253-
with self.assertRaises(ValueError):
254-
sorted(expand_alphanumeric_pattern('r[8-]a'))
254+
input = 'r[8-]a'
255+
output = sorted([input])
256+
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
255257

256-
with self.assertRaises(ValueError):
257-
sorted(expand_alphanumeric_pattern('r[-8]a'))
258+
input = 'r[-8]a'
259+
output = sorted([input])
260+
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
258261

259-
with self.assertRaises(ValueError):
260-
sorted(expand_alphanumeric_pattern('r[8--9]a'))
262+
input = 'r[8--9]a'
263+
output = sorted([input])
264+
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
261265

262266
def test_invalid_range_alphanumeric(self):
263267
self.assertEqual(sorted(expand_alphanumeric_pattern('r[9-a]a')), [])
@@ -273,17 +277,21 @@ def test_invalid_range_len(self):
273277
sorted(expand_alphanumeric_pattern('r[a-bb]a'))
274278

275279
def test_invalid_set(self):
276-
with self.assertRaises(ValueError):
277-
sorted(expand_alphanumeric_pattern('r[a]a'))
280+
input = 'r[a]a'
281+
output = sorted([input])
282+
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
278283

279-
with self.assertRaises(ValueError):
280-
sorted(expand_alphanumeric_pattern('r[a,]a'))
284+
input = 'r[a,]a'
285+
output = sorted([input])
286+
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
281287

282-
with self.assertRaises(ValueError):
283-
sorted(expand_alphanumeric_pattern('r[,a]a'))
288+
input = 'r[,a]a'
289+
output = sorted([input])
290+
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
284291

285-
with self.assertRaises(ValueError):
286-
sorted(expand_alphanumeric_pattern('r[a,,b]a'))
292+
input = 'r[a,,b]a'
293+
output = sorted([input])
294+
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
287295

288296

289297
class ImportFormTest(TestCase):

0 commit comments

Comments
 (0)