-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LUN-2235 Clean snippet variable names #62
Conversation
Valid characters for django template variables are [a-zA-Z0-9_]. Snippets that have invalid variables cannot use those variables, but creating a migration that would change them can break the snippet templates in subtle ways.
The fake admin site is registered because the smartsnippets.admin module requires it when loaded.
@@ -155,6 +160,29 @@ def test_vars_generated(self): | |||
self.assertEqual(Variable.objects.count(), 3) | |||
self.assertEqual(plugin3.variables.count(), 1) | |||
|
|||
def test_validation_passes_for_correct_variables(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some tests with valid variable names? Just to make sure that the base functionality is not broken.
@@ -12,6 +13,10 @@ | |||
SmartSnippetPointer, | |||
Variable, | |||
) | |||
class FakeSiteAdmin(admin.ModelAdmin): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed?
Maybe add a short comment explaining why, as it's not very intuitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the explanation in the commit, but maybe it would be better to describe it in the code.
def validate_unique_variable_names(self): | ||
""" Validates name uniqueness over all variable inlines. """ | ||
unique_variable_names = set() | ||
duplicate_variable_names = set() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Optional) Fancier way of doing the same thing:
duplicate_variable_names = [
var_name
for var_name, count in collections.Counter(['foo', 'bar', 'foo']).iteritems()
if count > 1
]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. It's a little bit more clear this way.
LUN-2235 Clean snippet variable names
No description provided.