Skip to content

Commit

Permalink
Test non-JSONField form field
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkilby committed Feb 22, 2020
1 parent 1ef0fc8 commit d69898a
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.forms import ModelForm
from django import forms
from django.test import TestCase

from .models import JSONNotRequiredModel


class JSONModelFormTest(TestCase):
def setUp(self):
class JSONNotRequiredForm(ModelForm):
class JSONNotRequiredForm(forms.ModelForm):
class Meta:
model = JSONNotRequiredModel
fields = '__all__'
Expand Down Expand Up @@ -110,3 +110,37 @@ def test_initial_data_has_changed(self):

form = self.form_class(data={'json': '[3, 4]'}, instance=instance)
self.assertTrue(form.has_changed())


class NonJSONFieldModelFormTest(TestCase):
"""Test model form behavior when field class has been overridden."""

def setUp(self):
class JSONNotRequiredForm(forms.ModelForm):
class Meta:
model = JSONNotRequiredModel
fields = ['json']
field_classes = {
'json': forms.CharField,
}

self.form_class = JSONNotRequiredForm

def test_field_type(self):
form = self.form_class()
field = form.fields['json']

self.assertIsInstance(field, forms.CharField)

def test_field_kwargs(self):
form = self.form_class()
field = form.fields['json']

self.assertFalse(hasattr(field, 'dump_kwargs'))
self.assertFalse(hasattr(field, 'load_kwargs'))

def test_no_indent(self):
# Because we're using a regular CharField, the value is not parsed and
# rerendered with indentation. Compare with `test_render_bound_values`.
form = self.form_class(data={'json': '{"a": "b"}'})
self.assertEqual(form['json'].value(), '{"a": "b"}')

0 comments on commit d69898a

Please sign in to comment.