summaryrefslogtreecommitdiff
path: root/tests/regressiontests/model_formsets_regress/tests.py
blob: d058f1d8bcb97cce7833ede60f3aad76583223e5 (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from __future__ import absolute_import, with_statement

from django import forms
from django.forms.formsets import BaseFormSet, DELETION_FIELD_NAME
from django.forms.util import ErrorDict, ErrorList
from django.forms.models import modelform_factory, inlineformset_factory, modelformset_factory, BaseModelFormSet
from django.test import TestCase

from .models import User, UserSite, Restaurant, Manager, Network, Host


class InlineFormsetTests(TestCase):
    def test_formset_over_to_field(self):
        "A formset over a ForeignKey with a to_field can be saved. Regression for #10243"
        Form = modelform_factory(User)
        FormSet = inlineformset_factory(User, UserSite)

        # Instantiate the Form and FormSet to prove
        # you can create a form with no data
        form = Form()
        form_set = FormSet(instance=User())

        # Now create a new User and UserSite instance
        data = {
            'serial': u'1',
            'username': u'apollo13',
            'usersite_set-TOTAL_FORMS': u'1',
            'usersite_set-INITIAL_FORMS': u'0',
            'usersite_set-MAX_NUM_FORMS': u'0',
            'usersite_set-0-data': u'10',
            'usersite_set-0-user': u'apollo13'
        }
        user = User()
        form = Form(data)
        if form.is_valid():
            user = form.save()
        else:
            self.fail('Errors found on form:%s' % form_set)

        form_set = FormSet(data, instance=user)
        if form_set.is_valid():
            form_set.save()
            usersite = UserSite.objects.all().values()
            self.assertEqual(usersite[0]['data'], 10)
            self.assertEqual(usersite[0]['user_id'], u'apollo13')
        else:
            self.fail('Errors found on formset:%s' % form_set.errors)

        # Now update the UserSite instance
        data = {
            'usersite_set-TOTAL_FORMS': u'1',
            'usersite_set-INITIAL_FORMS': u'1',
            'usersite_set-MAX_NUM_FORMS': u'0',
            'usersite_set-0-id': unicode(usersite[0]['id']),
            'usersite_set-0-data': u'11',
            'usersite_set-0-user': u'apollo13'
        }
        form_set = FormSet(data, instance=user)
        if form_set.is_valid():
            form_set.save()
            usersite = UserSite.objects.all().values()
            self.assertEqual(usersite[0]['data'], 11)
            self.assertEqual(usersite[0]['user_id'], u'apollo13')
        else:
            self.fail('Errors found on formset:%s' % form_set.errors)

        # Now add a new UserSite instance
        data = {
            'usersite_set-TOTAL_FORMS': u'2',
            'usersite_set-INITIAL_FORMS': u'1',
            'usersite_set-MAX_NUM_FORMS': u'0',
            'usersite_set-0-id': unicode(usersite[0]['id']),
            'usersite_set-0-data': u'11',
            'usersite_set-0-user': u'apollo13',
            'usersite_set-1-data': u'42',
            'usersite_set-1-user': u'apollo13'
        }
        form_set = FormSet(data, instance=user)
        if form_set.is_valid():
            form_set.save()
            usersite = UserSite.objects.all().values().order_by('data')
            self.assertEqual(usersite[0]['data'], 11)
            self.assertEqual(usersite[0]['user_id'], u'apollo13')
            self.assertEqual(usersite[1]['data'], 42)
            self.assertEqual(usersite[1]['user_id'], u'apollo13')
        else:
            self.fail('Errors found on formset:%s' % form_set.errors)

    def test_formset_over_inherited_model(self):
        "A formset over a ForeignKey with a to_field can be saved. Regression for #11120"
        Form = modelform_factory(Restaurant)
        FormSet = inlineformset_factory(Restaurant, Manager)

        # Instantiate the Form and FormSet to prove
        # you can create a form with no data
        form = Form()
        form_set = FormSet(instance=Restaurant())

        # Now create a new Restaurant and Manager instance
        data = {
            'name': u"Guido's House of Pasta",
            'manager_set-TOTAL_FORMS': u'1',
            'manager_set-INITIAL_FORMS': u'0',
            'manager_set-MAX_NUM_FORMS': u'0',
            'manager_set-0-name': u'Guido Van Rossum'
        }
        restaurant = User()
        form = Form(data)
        if form.is_valid():
            restaurant = form.save()
        else:
            self.fail('Errors found on form:%s' % form_set)

        form_set = FormSet(data, instance=restaurant)
        if form_set.is_valid():
            form_set.save()
            manager = Manager.objects.all().values()
            self.assertEqual(manager[0]['name'], 'Guido Van Rossum')
        else:
            self.fail('Errors found on formset:%s' % form_set.errors)

        # Now update the Manager instance
        data = {
            'manager_set-TOTAL_FORMS': u'1',
            'manager_set-INITIAL_FORMS': u'1',
            'manager_set-MAX_NUM_FORMS': u'0',
            'manager_set-0-id': unicode(manager[0]['id']),
            'manager_set-0-name': u'Terry Gilliam'
        }
        form_set = FormSet(data, instance=restaurant)
        if form_set.is_valid():
            form_set.save()
            manager = Manager.objects.all().values()
            self.assertEqual(manager[0]['name'], 'Terry Gilliam')
        else:
            self.fail('Errors found on formset:%s' % form_set.errors)

        # Now add a new Manager instance
        data = {
            'manager_set-TOTAL_FORMS': u'2',
            'manager_set-INITIAL_FORMS': u'1',
            'manager_set-MAX_NUM_FORMS': u'0',
            'manager_set-0-id': unicode(manager[0]['id']),
            'manager_set-0-name': u'Terry Gilliam',
            'manager_set-1-name': u'John Cleese'
        }
        form_set = FormSet(data, instance=restaurant)
        if form_set.is_valid():
            form_set.save()
            manager = Manager.objects.all().values().order_by('name')
            self.assertEqual(manager[0]['name'], 'John Cleese')
            self.assertEqual(manager[1]['name'], 'Terry Gilliam')
        else:
            self.fail('Errors found on formset:%s' % form_set.errors)

    def test_formset_with_none_instance(self):
        "A formset with instance=None can be created. Regression for #11872"
        Form = modelform_factory(User)
        FormSet = inlineformset_factory(User, UserSite)

        # Instantiate the Form and FormSet to prove
        # you can create a formset with an instance of None
        form = Form(instance=None)
        formset = FormSet(instance=None)

    def test_empty_fields_on_modelformset(self):
        "No fields passed to modelformset_factory should result in no fields on returned forms except for the id. See #14119."
        UserFormSet = modelformset_factory(User, fields=())
        formset = UserFormSet()
        for form in formset.forms:
            self.assertTrue('id' in form.fields)
            self.assertEqual(len(form.fields), 1)

    def test_save_as_new_with_new_inlines(self):
        """
        Existing and new inlines are saved with save_as_new.

        Regression for #14938.

        """
        efnet = Network.objects.create(name="EFNet")
        host1 = Host.objects.create(hostname="irc.he.net", network=efnet)

        HostFormSet = inlineformset_factory(Network, Host)

        # Add a new host, modify previous host, and save-as-new
        data = {
            'host_set-TOTAL_FORMS': u'2',
            'host_set-INITIAL_FORMS': u'1',
            'host_set-MAX_NUM_FORMS': u'0',
            'host_set-0-id': unicode(host1.id),
            'host_set-0-hostname': u'tranquility.hub.dal.net',
            'host_set-1-hostname': u'matrix.de.eu.dal.net'
        }

        # To save a formset as new, it needs a new hub instance
        dalnet = Network.objects.create(name="DALnet")
        formset = HostFormSet(data, instance=dalnet, save_as_new=True)

        self.assertTrue(formset.is_valid())
        formset.save()
        self.assertQuerysetEqual(
            dalnet.host_set.order_by("hostname"),
            ["<Host: matrix.de.eu.dal.net>", "<Host: tranquility.hub.dal.net>"]
            )

    def test_initial_data(self):
        user = User.objects.create(username="bibi", serial=1)
        UserSite.objects.create(user=user, data=7)
        FormSet = inlineformset_factory(User, UserSite, extra=2)

        formset = FormSet(instance=user, initial=[{'data': 41}, {'data': 42}])
        self.assertEqual(formset.forms[0].initial['data'], 7)
        self.assertEqual(formset.extra_forms[0].initial['data'], 41)
        self.assertTrue(u'value="42"' in formset.extra_forms[1].as_p())


class FormsetTests(TestCase):
    def test_error_class(self):
        '''
        Test the type of Formset and Form error attributes
        '''
        Formset = modelformset_factory(User)
        data = {
            'form-TOTAL_FORMS': u'2',
            'form-INITIAL_FORMS': u'0',
            'form-MAX_NUM_FORMS': u'0',
            'form-0-id': '',
            'form-0-username': u'apollo13',
            'form-0-serial': u'1',
            'form-1-id': '',
            'form-1-username': u'apollo13',
            'form-1-serial': u'2',
        }
        formset = Formset(data)
        # check if the returned error classes are correct
        # note: formset.errors returns a list as documented
        self.assertTrue(isinstance(formset.errors, list))
        self.assertTrue(isinstance(formset.non_form_errors(), ErrorList))
        for form in formset.forms:
            self.assertTrue(isinstance(form.errors, ErrorDict))
            self.assertTrue(isinstance(form.non_field_errors(), ErrorList))

    def test_initial_data(self):
        User.objects.create(username="bibi", serial=1)
        Formset = modelformset_factory(User, extra=2)
        formset = Formset(initial=[{'username': u'apollo11'}, {'username': u'apollo12'}])
        self.assertEqual(formset.forms[0].initial['username'], "bibi")
        self.assertEqual(formset.extra_forms[0].initial['username'], "apollo11")
        self.assertTrue(u'value="apollo12"' in formset.extra_forms[1].as_p())

    def test_extraneous_query_is_not_run(self):
        Formset = modelformset_factory(Network)
        data = {u'test-TOTAL_FORMS': u'1',
                u'test-INITIAL_FORMS': u'0',
                u'test-MAX_NUM_FORMS': u'',
                u'test-0-name': u'Random Place', }
        with self.assertNumQueries(1):
            formset = Formset(data, prefix="test")
            formset.save()


class CustomWidget(forms.CharField):
    pass


class UserSiteForm(forms.ModelForm):
    class Meta:
        model = UserSite
        widgets = {'data': CustomWidget}


class Callback(object):

    def __init__(self):
        self.log = []

    def __call__(self, db_field, **kwargs):
        self.log.append((db_field, kwargs))
        return db_field.formfield(**kwargs)


class FormfieldCallbackTests(TestCase):
    """
    Regression for #13095: Using base forms with widgets
    defined in Meta should not raise errors.
    """

    def test_inlineformset_factory_default(self):
        Formset = inlineformset_factory(User, UserSite, form=UserSiteForm)
        form = Formset().forms[0]
        self.assertTrue(isinstance(form['data'].field.widget, CustomWidget))

    def test_modelformset_factory_default(self):
        Formset = modelformset_factory(UserSite, form=UserSiteForm)
        form = Formset().forms[0]
        self.assertTrue(isinstance(form['data'].field.widget, CustomWidget))

    def assertCallbackCalled(self, callback):
        id_field, user_field, data_field = UserSite._meta.fields
        expected_log = [
            (id_field, {}),
            (user_field, {}),
            (data_field, {'widget': CustomWidget}),
        ]
        self.assertEqual(callback.log, expected_log)

    def test_inlineformset_custom_callback(self):
        callback = Callback()
        inlineformset_factory(User, UserSite, form=UserSiteForm,
                              formfield_callback=callback)
        self.assertCallbackCalled(callback)

    def test_modelformset_custom_callback(self):
        callback = Callback()
        modelformset_factory(UserSite, form=UserSiteForm,
                             formfield_callback=callback)
        self.assertCallbackCalled(callback)


class BaseCustomDeleteFormSet(BaseFormSet):
    """
    A formset mix-in that lets a form decide if it's to be deleted.
    Works for BaseFormSets. Also works for ModelFormSets with #14099 fixed.

    form.should_delete() is called. The formset delete field is also suppressed.
    """
    def add_fields(self, form, index):
        super(BaseCustomDeleteFormSet, self).add_fields(form, index)
        self.can_delete = True
        if DELETION_FIELD_NAME in form.fields:
            del form.fields[DELETION_FIELD_NAME]

    def _should_delete_form(self, form):
        return hasattr(form, 'should_delete') and form.should_delete()


class FormfieldShouldDeleteFormTests(TestCase):
    """
    Regression for #14099: BaseModelFormSet should use ModelFormSet method _should_delete_form
    """

    class BaseCustomDeleteModelFormSet(BaseModelFormSet, BaseCustomDeleteFormSet):
        """ Model FormSet with CustomDelete MixIn """

    class CustomDeleteUserForm(forms.ModelForm):
        """ A model form with a 'should_delete' method """
        class Meta:
            model = User

        def should_delete(self):
            """ delete form if odd PK """
            return self.instance.id % 2 != 0

    NormalFormset = modelformset_factory(User, form=CustomDeleteUserForm, can_delete=True)
    DeleteFormset = modelformset_factory(User, form=CustomDeleteUserForm, formset=BaseCustomDeleteModelFormSet)

    data = {
            'form-TOTAL_FORMS': '4',
            'form-INITIAL_FORMS': '0',
            'form-MAX_NUM_FORMS': '4',
            'form-0-username': 'John',
            'form-0-serial': '1',
            'form-1-username': 'Paul',
            'form-1-serial': '2',
            'form-2-username': 'George',
            'form-2-serial': '3',
            'form-3-username': 'Ringo',
            'form-3-serial': '5',
            }

    delete_all_ids = {
            'form-0-DELETE': '1',
            'form-1-DELETE': '1',
            'form-2-DELETE': '1',
            'form-3-DELETE': '1',
            }

    def test_init_database(self):
        """ Add test data to database via formset """
        formset = self.NormalFormset(self.data)
        self.assertTrue(formset.is_valid())
        self.assertEqual(len(formset.save()), 4)

    def test_no_delete(self):
        """ Verify base formset doesn't modify database """
        # reload database
        self.test_init_database()

        # pass standard data dict & see none updated
        data = dict(self.data)
        data['form-INITIAL_FORMS'] = 4
        data.update(dict(
            ('form-%d-id' % i, user.id)
            for i,user in enumerate(User.objects.all())
        ))
        formset = self.NormalFormset(data, queryset=User.objects.all())
        self.assertTrue(formset.is_valid())
        self.assertEqual(len(formset.save()), 0)
        self.assertEqual(len(User.objects.all()), 4)

    def test_all_delete(self):
        """ Verify base formset honors DELETE field """
        # reload database
        self.test_init_database()

        # create data dict with all fields marked for deletion
        data = dict(self.data)
        data['form-INITIAL_FORMS'] = 4
        data.update(dict(
            ('form-%d-id' % i, user.id)
            for i,user in enumerate(User.objects.all())
        ))
        data.update(self.delete_all_ids)
        formset = self.NormalFormset(data, queryset=User.objects.all())
        self.assertTrue(formset.is_valid())
        self.assertEqual(len(formset.save()), 0)
        self.assertEqual(len(User.objects.all()), 0)

    def test_custom_delete(self):
        """ Verify DeleteFormset ignores DELETE field and uses form method """
        # reload database
        self.test_init_database()

        # Create formset with custom Delete function
        # create data dict with all fields marked for deletion
        data = dict(self.data)
        data['form-INITIAL_FORMS'] = 4
        data.update(dict(
            ('form-%d-id' % i, user.id)
            for i,user in enumerate(User.objects.all())
        ))
        data.update(self.delete_all_ids)
        formset = self.DeleteFormset(data, queryset=User.objects.all())

        # verify two were deleted
        self.assertTrue(formset.is_valid())
        self.assertEqual(len(formset.save()), 0)
        self.assertEqual(len(User.objects.all()), 2)

        # verify no "odd" PKs left
        odd_ids = [user.id for user in User.objects.all() if user.id % 2]
        self.assertEqual(len(odd_ids), 0)