mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Test key fix (#6685)
* Fix for generateTestKey - Check for zero-length key * Ensure test template name would generate valid key * Add unit test * Improve generateTestKey method - Allow for non-latin chars * Update generateTestKey - Improve check for valid char
This commit is contained in:
parent
6033e87359
commit
6389493c33
@ -87,11 +87,24 @@ def generateTestKey(test_name: str) -> str:
|
|||||||
key = test_name.strip().lower()
|
key = test_name.strip().lower()
|
||||||
key = key.replace(' ', '')
|
key = key.replace(' ', '')
|
||||||
|
|
||||||
# Remove any characters that cannot be used to represent a variable
|
def valid_char(char: str):
|
||||||
key = re.sub(r'[^a-zA-Z0-9_]', '', key)
|
"""Determine if a particular character is valid for use in a test key."""
|
||||||
|
if not char.isprintable():
|
||||||
|
return False
|
||||||
|
|
||||||
# If the key starts with a digit, prefix with an underscore
|
if char.isidentifier():
|
||||||
if key[0].isdigit():
|
return True
|
||||||
|
|
||||||
|
if char.isalnum():
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Remove any characters that cannot be used to represent a variable
|
||||||
|
key = ''.join([c for c in key if valid_char(c)])
|
||||||
|
|
||||||
|
# If the key starts with a non-identifier character, prefix with an underscore
|
||||||
|
if len(key) > 0 and not key[0].isidentifier():
|
||||||
key = '_' + key
|
key = '_' + key
|
||||||
|
|
||||||
return key
|
return key
|
||||||
|
@ -3428,6 +3428,13 @@ class PartTestTemplate(InvenTree.models.InvenTreeMetadataModel):
|
|||||||
|
|
||||||
self.key = helpers.generateTestKey(self.test_name)
|
self.key = helpers.generateTestKey(self.test_name)
|
||||||
|
|
||||||
|
if len(self.key) == 0:
|
||||||
|
raise ValidationError({
|
||||||
|
'test_name': _(
|
||||||
|
'Invalid template name - must include at least one alphanumeric character'
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
self.validate_unique()
|
self.validate_unique()
|
||||||
super().clean()
|
super().clean()
|
||||||
|
|
||||||
@ -3445,7 +3452,9 @@ class PartTestTemplate(InvenTree.models.InvenTreeMetadataModel):
|
|||||||
|
|
||||||
if tests.exists():
|
if tests.exists():
|
||||||
raise ValidationError({
|
raise ValidationError({
|
||||||
'test_name': _('Test with this name already exists for this part')
|
'test_name': _(
|
||||||
|
'Test template with the same key already exists for part'
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
super().validate_unique(exclude)
|
super().validate_unique(exclude)
|
||||||
|
@ -431,6 +431,29 @@ class TestTemplateTest(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(variant.getTestTemplates().count(), n + 1)
|
self.assertEqual(variant.getTestTemplates().count(), n + 1)
|
||||||
|
|
||||||
|
def test_key_generation(self):
|
||||||
|
"""Test the key generation method."""
|
||||||
|
variant = Part.objects.get(pk=10004)
|
||||||
|
|
||||||
|
invalid_names = ['', '+', '+++++++', ' ', '<>$&&&']
|
||||||
|
|
||||||
|
for name in invalid_names:
|
||||||
|
template = PartTestTemplate(part=variant, test_name=name)
|
||||||
|
with self.assertRaises(ValidationError):
|
||||||
|
template.clean()
|
||||||
|
|
||||||
|
valid_names = [
|
||||||
|
'Собранный щит',
|
||||||
|
'!! 123 Собранный щит <><><> $$$$$ !!!',
|
||||||
|
'----hello world----',
|
||||||
|
'Olá Mundo',
|
||||||
|
'我不懂中文',
|
||||||
|
]
|
||||||
|
|
||||||
|
for name in valid_names:
|
||||||
|
template = PartTestTemplate(part=variant, test_name=name)
|
||||||
|
template.clean()
|
||||||
|
|
||||||
|
|
||||||
class PartSettingsTest(InvenTreeTestCase):
|
class PartSettingsTest(InvenTreeTestCase):
|
||||||
"""Tests to ensure that the user-configurable default values work as expected.
|
"""Tests to ensure that the user-configurable default values work as expected.
|
||||||
|
Loading…
Reference in New Issue
Block a user