mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-24 08:25:17 -06:00
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.
This commit is contained in:
parent
a8a36c0a8f
commit
d40a49e6e3
@ -29,9 +29,7 @@ class ExpandableNameField(forms.CharField):
|
|||||||
def to_python(self, value):
|
def to_python(self, value):
|
||||||
if not value:
|
if not value:
|
||||||
return ''
|
return ''
|
||||||
if re.search(ALPHANUMERIC_EXPANSION_PATTERN, value):
|
return list(expand_alphanumeric_pattern(value))
|
||||||
return list(expand_alphanumeric_pattern(value))
|
|
||||||
return [value]
|
|
||||||
|
|
||||||
|
|
||||||
class ExpandableIPAddressField(forms.CharField):
|
class ExpandableIPAddressField(forms.CharField):
|
||||||
|
@ -87,7 +87,11 @@ def expand_alphanumeric_pattern(string):
|
|||||||
"""
|
"""
|
||||||
Expand an alphabetic pattern into a list of strings.
|
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)
|
parsed_range = parse_alphanumeric_range(pattern)
|
||||||
for i in parsed_range:
|
for i in parsed_range:
|
||||||
if re.search(ALPHANUMERIC_EXPANSION_PATTERN, remnant):
|
if re.search(ALPHANUMERIC_EXPANSION_PATTERN, remnant):
|
||||||
|
@ -246,18 +246,22 @@ class ExpandAlphanumeric(TestCase):
|
|||||||
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
def test_invalid_non_pattern(self):
|
def test_invalid_non_pattern(self):
|
||||||
with self.assertRaises(ValueError):
|
input = 'r9a'
|
||||||
sorted(expand_alphanumeric_pattern('r9a'))
|
output = sorted([input])
|
||||||
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
def test_invalid_range(self):
|
def test_invalid_range(self):
|
||||||
with self.assertRaises(ValueError):
|
input = 'r[8-]a'
|
||||||
sorted(expand_alphanumeric_pattern('r[8-]a'))
|
output = sorted([input])
|
||||||
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
input = 'r[-8]a'
|
||||||
sorted(expand_alphanumeric_pattern('r[-8]a'))
|
output = sorted([input])
|
||||||
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
input = 'r[8--9]a'
|
||||||
sorted(expand_alphanumeric_pattern('r[8--9]a'))
|
output = sorted([input])
|
||||||
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
def test_invalid_range_alphanumeric(self):
|
def test_invalid_range_alphanumeric(self):
|
||||||
self.assertEqual(sorted(expand_alphanumeric_pattern('r[9-a]a')), [])
|
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'))
|
sorted(expand_alphanumeric_pattern('r[a-bb]a'))
|
||||||
|
|
||||||
def test_invalid_set(self):
|
def test_invalid_set(self):
|
||||||
with self.assertRaises(ValueError):
|
input = 'r[a]a'
|
||||||
sorted(expand_alphanumeric_pattern('r[a]a'))
|
output = sorted([input])
|
||||||
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
input = 'r[a,]a'
|
||||||
sorted(expand_alphanumeric_pattern('r[a,]a'))
|
output = sorted([input])
|
||||||
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
input = 'r[,a]a'
|
||||||
sorted(expand_alphanumeric_pattern('r[,a]a'))
|
output = sorted([input])
|
||||||
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
input = 'r[a,,b]a'
|
||||||
sorted(expand_alphanumeric_pattern('r[a,,b]a'))
|
output = sorted([input])
|
||||||
|
self.assertEqual(sorted(expand_alphanumeric_pattern(input)), output)
|
||||||
|
|
||||||
|
|
||||||
class ImportFormTest(TestCase):
|
class ImportFormTest(TestCase):
|
||||||
|
Loading…
Reference in New Issue
Block a user