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:
Per von Zweigbergk 2023-09-09 14:16:17 +02:00
parent a8a36c0a8f
commit d40a49e6e3
3 changed files with 30 additions and 20 deletions

View File

@ -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]
class ExpandableIPAddressField(forms.CharField):

View File

@ -87,7 +87,11 @@ def expand_alphanumeric_pattern(string):
"""
Expand an alphabetic pattern into a list of strings.
"""
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):

View File

@ -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):