-
Notifications
You must be signed in to change notification settings - Fork 7
add Nh3Char(models.CharField) #44
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
Closed
+231
−7
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
from django.db import models | ||
from django.forms import ModelForm | ||
from django.test import TestCase | ||
from django.utils.safestring import SafeString | ||
|
||
from django_nh3.models import Nh3Text | ||
|
||
|
||
class Nh3Content(models.Model): | ||
"""NH3 test model""" | ||
|
||
content = Nh3Text( | ||
strip_comments=True, | ||
) | ||
blank_field = Nh3Text(blank=True) | ||
null_field = Nh3Text(blank=True, null=True) | ||
|
||
|
||
class Nh3ContentModelForm(ModelForm): | ||
"""NH3 test model form""" | ||
|
||
class Meta: | ||
model = Nh3Content | ||
fields = ["content"] | ||
|
||
|
||
class Nh3NullableContent(models.Model): | ||
"""NH3 test model""" | ||
|
||
CHOICES = (("f", "first choice"), ("s", "second choice")) | ||
choice = Nh3Text(choices=CHOICES, blank=True) | ||
content = Nh3Text(blank=True, null=True) | ||
|
||
|
||
class Nh3NullableContentModelForm(ModelForm): | ||
"""NH3 test model form""" | ||
|
||
class Meta: | ||
model = Nh3NullableContent | ||
fields = ["choice"] | ||
|
||
|
||
class TestNh3ModelField(TestCase): | ||
"""Test model field""" | ||
|
||
def test_cleaning(self): | ||
"""Test values are sanitized""" | ||
test_data = { | ||
"html_data": "<h1>Heading</h1>", | ||
"no_html": "Heading", | ||
"html_comment": "<!-- this is a comment -->", | ||
} | ||
expected_values = { | ||
"html_data": "Heading", | ||
"no_html": "Heading", | ||
"html_comment": "", | ||
} | ||
|
||
for key, value in test_data.items(): | ||
obj = Nh3Content.objects.create(content=value) | ||
self.assertEqual(obj.content, expected_values[key]) | ||
|
||
def test_retrieved_values_are_template_safe(self): | ||
obj = Nh3Content.objects.create(content="some content") | ||
obj.refresh_from_db() | ||
self.assertIsInstance(obj.content, SafeString) | ||
obj = Nh3Content.objects.create(content="") | ||
obj.refresh_from_db() | ||
self.assertIsInstance(obj.content, SafeString) | ||
|
||
def test_saved_values_are_template_safe(self): | ||
obj = Nh3Content(content="some content") | ||
obj.save() | ||
self.assertIsInstance(obj.content, SafeString) | ||
obj = Nh3Content(content="") | ||
obj.save() | ||
self.assertIsInstance(obj.content, SafeString) | ||
|
||
def test_saved_none_values_are_none(self): | ||
obj = Nh3Content(null_field=None) | ||
obj.save() | ||
self.assertIsNone(obj.null_field) | ||
|
||
|
||
class TestNh3NullableModelField(TestCase): | ||
"""Test model field""" | ||
|
||
def test_cleaning(self): | ||
"""Test values are sanitized""" | ||
test_data = { | ||
"none": None, | ||
"empty": "", | ||
"whitespaces": " ", | ||
"linebreak": "\n", | ||
} | ||
expected_values = { | ||
"none": None, | ||
"empty": "", | ||
"whitespaces": " ", | ||
"linebreak": "\n", | ||
} | ||
|
||
for key, value in test_data.items(): | ||
obj = Nh3NullableContent.objects.create(content=value) | ||
self.assertEqual(obj.content, expected_values[key]) | ||
|
||
|
||
class TestNh3ModelFormField(TestCase): | ||
"""Test model form field""" | ||
|
||
def test_cleaning(self): | ||
"""Test values are sanitized""" | ||
test_data = { | ||
"html_data": "<h1>Heading</h1>", | ||
"no_html": "Heading", | ||
"spacing": " Heading ", | ||
} | ||
expected_values = { | ||
"html_data": "Heading", | ||
"no_html": "Heading", | ||
"spacing": "Heading", | ||
} | ||
|
||
for key, value in test_data.items(): | ||
form = Nh3ContentModelForm(data={"content": value}) | ||
self.assertTrue(form.is_valid()) | ||
obj = form.save() | ||
self.assertEqual(obj.content, expected_values[key]) | ||
|
||
def test_stripped_comments(self): | ||
"""Content field strips comments so ensure they aren't allowed""" | ||
|
||
self.assertFalse( | ||
Nh3ContentModelForm( | ||
data={"content": "<!-- this is a comment -->"} | ||
).is_valid() | ||
) | ||
|
||
def test_field_choices(self): | ||
"""Content field strips comments so ensure they aren't allowed""" | ||
test_data = dict(Nh3NullableContent.CHOICES) | ||
|
||
for key, value in test_data.items(): | ||
form = Nh3NullableContentModelForm(data={"choice": key}) | ||
self.assertTrue(form.is_valid()) | ||
obj = form.save() | ||
self.assertEqual(obj.get_choice_display(), value) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 done a little research on the deprecation of a class now. So I think keep
Nh3Field
as a class.Add a docstring note of the deprecation along with perhaps adding the typing decorator would be a good idea;
Then raise the deprecation in the
__init__
similar to;And to prevent this being a breaking change, it could inherit
Nh3Text
;