File: test_forms.py

package info (click to toggle)
python-jsonfield 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 180 kB
  • sloc: python: 739; makefile: 15
file content (154 lines) | stat: -rw-r--r-- 5,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from django import forms
from django.test import TestCase

from .models import JSONNotRequiredModel


class JSONModelFormTest(TestCase):
    def setUp(self):
        class JSONNotRequiredForm(forms.ModelForm):
            class Meta:
                model = JSONNotRequiredModel
                fields = '__all__'

        self.form_class = JSONNotRequiredForm

    def test_blank_form(self):
        form = self.form_class(data={'json': ''})
        self.assertFalse(form.has_changed())

    def test_form_with_data(self):
        form = self.form_class(data={'json': '{}'})
        self.assertTrue(form.has_changed())

    def test_form_save(self):
        form = self.form_class(data={'json': ''})
        form.save()

    def test_save_values(self):
        values = [
            # (type, form input, db value)
            ('object', '{"a": "b"}', {'a': 'b'}),
            ('array', '[1, 2]', [1, 2]),
            ('string', '"test"', 'test'),
            ('float', '1.2', 1.2),
            ('int', '1234', 1234),
            ('bool', 'true', True),
            ('null', 'null', None),
        ]

        for vtype, form_input, db_value in values:
            with self.subTest(type=vtype, input=form_input, db=db_value):
                form = self.form_class(data={'json': form_input})
                self.assertTrue(form.is_valid(), msg=form.errors)

                instance = form.save()
                self.assertEqual(instance.json, db_value)

    def test_render_initial_values(self):
        values = [
            # (type, db value, form output)
            ('object', {'a': 'b'}, '{\n    "a": "b"\n}'),
            ('array', [1, 2], "[\n    1,\n    2\n]"),
            ('string', 'test', '"test"'),
            ('float', 1.2, '1.2'),
            ('int', 1234, '1234'),
            ('bool', True, 'true'),
            ('null', None, 'null'),
        ]

        for vtype, db_value, form_output in values:
            with self.subTest(type=vtype, db=db_value, output=form_output):
                instance = JSONNotRequiredModel.objects.create(json=db_value)

                form = self.form_class(instance=instance)
                self.assertEqual(form['json'].value(), form_output)

    def test_render_bound_values(self):
        values = [
            # (type, db value, form input, form output)
            ('object', '{"a": "b"}', '{\n    "a": "b"\n}'),
            ('array', '[1, 2]', "[\n    1,\n    2\n]"),
            ('string', '"test"', '"test"'),
            ('float', '1.2', '1.2'),
            ('int', '1234', '1234'),
            ('bool', 'true', 'true'),
            ('null', 'null', 'null'),
        ]

        for vtype, form_input, form_output in values:
            with self.subTest(type=vtype, input=form_input, output=form_output):
                form = self.form_class(data={'json': form_input})
                self.assertEqual(form['json'].value(), form_output)

    def test_render_indent(self):
        form = self.form_class(initial={'json': {'a': 'b'}})
        self.assertEqual(form['json'].value(), '{\n    "a": "b"\n}')

    def test_render_unicode(self):
        form = self.form_class(initial={'json': '✨'})
        self.assertEqual(form['json'].value(), '"✨"')

    def test_invalid_value(self):
        form = self.form_class(data={'json': 'foo'})

        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors, {
            'json': ['"foo" value must be valid JSON.'],
        })
        self.assertEqual(form['json'].value(), 'foo')

    def test_disabled_field(self):
        instance = JSONNotRequiredModel.objects.create(json=100)

        form = self.form_class(data={'json': '{"foo": "bar"}'}, instance=instance)
        form.fields['json'].disabled = True

        self.assertTrue(form.is_valid())
        self.assertEqual(form.cleaned_data, {'json': 100})

        # rendered value
        self.assertEqual(form['json'].value(), '100')

    def test_initial_data_has_changed(self):
        instance = JSONNotRequiredModel.objects.create(json=[1, 2])

        form = self.form_class(data={'json': '[1, 2]'}, instance=instance)
        self.assertFalse(form.has_changed())

        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"}')