| 1 |
from django.forms.models import inlineformset_factory |
| 2 |
from django.test import TestCase |
| 3 |
|
| 4 |
from .models import ( |
| 5 |
AutoPKChildOfUUIDPKParent, |
| 6 |
AutoPKParent, |
| 7 |
ChildRelatedViaAK, |
| 8 |
ChildWithEditablePK, |
| 9 |
ParentWithUUIDAlternateKey, |
| 10 |
UUIDPKChild, |
| 11 |
UUIDPKChildOfAutoPKParent, |
| 12 |
UUIDPKParent, |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
class InlineFormsetTests(TestCase): |
| 17 |
def test_inlineformset_factory_nulls_default_pks(self): |
| 18 |
""" |
| 19 |
#24377 - If we're adding a new object, a parent's auto-generated pk |
| 20 |
from the model field default should be ignored as it's regenerated on |
| 21 |
the save request. |
| 22 |
|
| 23 |
Tests the case where both the parent and child have a UUID primary key. |
| 24 |
""" |
| 25 |
FormSet = inlineformset_factory(UUIDPKParent, UUIDPKChild, fields="__all__") |
| 26 |
formset = FormSet() |
| 27 |
self.assertIsNone(formset.forms[0].fields["parent"].initial) |
| 28 |
|
| 29 |
def test_inlineformset_factory_ignores_default_pks_on_submit(self): |
| 30 |
""" |
| 31 |
#24377 - Inlines with a model field default should ignore that default |
| 32 |
value to avoid triggering validation on empty forms. |
| 33 |
""" |
| 34 |
FormSet = inlineformset_factory(UUIDPKParent, UUIDPKChild, fields="__all__") |
| 35 |
formset = FormSet( |
| 36 |
{ |
| 37 |
"uuidpkchild_set-TOTAL_FORMS": 3, |
| 38 |
"uuidpkchild_set-INITIAL_FORMS": 0, |
| 39 |
"uuidpkchild_set-MAX_NUM_FORMS": "", |
| 40 |
"uuidpkchild_set-0-name": "Foo", |
| 41 |
"uuidpkchild_set-1-name": "", |
| 42 |
"uuidpkchild_set-2-name": "", |
| 43 |
} |
| 44 |
) |
| 45 |
self.assertTrue(formset.is_valid()) |
| 46 |
self.assertIsNone(formset.instance.uuid) |
| 47 |
self.assertIsNone(formset.forms[0].instance.parent_id) |
| 48 |
|
| 49 |
def test_inlineformset_factory_nulls_default_pks_uuid_parent_auto_child(self): |
| 50 |
""" |
| 51 |
#24958 - Variant of test_inlineformset_factory_nulls_default_pks for |
| 52 |
the case of a parent object with a UUID primary key and a child object |
| 53 |
with an AutoField primary key. |
| 54 |
""" |
| 55 |
FormSet = inlineformset_factory( |
| 56 |
UUIDPKParent, AutoPKChildOfUUIDPKParent, fields="__all__" |
| 57 |
) |
| 58 |
formset = FormSet() |
| 59 |
self.assertIsNone(formset.forms[0].fields["parent"].initial) |
| 60 |
|
| 61 |
def test_inlineformset_factory_nulls_default_pks_auto_parent_uuid_child(self): |
| 62 |
""" |
| 63 |
#24958 - Variant of test_inlineformset_factory_nulls_default_pks for |
| 64 |
the case of a parent object with an AutoField primary key and a child |
| 65 |
object with a UUID primary key. |
| 66 |
""" |
| 67 |
FormSet = inlineformset_factory( |
| 68 |
AutoPKParent, UUIDPKChildOfAutoPKParent, fields="__all__" |
| 69 |
) |
| 70 |
formset = FormSet() |
| 71 |
self.assertIsNone(formset.forms[0].fields["parent"].initial) |
| 72 |
|
| 73 |
def test_inlineformset_factory_nulls_default_pks_child_editable_pk(self): |
| 74 |
""" |
| 75 |
#24958 - Variant of test_inlineformset_factory_nulls_default_pks for |
| 76 |
the case of a parent object with a UUID primary key and a child |
| 77 |
object with an editable natural key for a primary key. |
| 78 |
""" |
| 79 |
FormSet = inlineformset_factory( |
| 80 |
UUIDPKParent, ChildWithEditablePK, fields="__all__" |
| 81 |
) |
| 82 |
formset = FormSet() |
| 83 |
self.assertIsNone(formset.forms[0].fields["parent"].initial) |
| 84 |
|
| 85 |
def test_inlineformset_factory_nulls_default_pks_alternate_key_relation(self): |
| 86 |
""" |
| 87 |
#24958 - Variant of test_inlineformset_factory_nulls_default_pks for |
| 88 |
the case of a parent object with a UUID alternate key and a child |
| 89 |
object that relates to that alternate key. |
| 90 |
""" |
| 91 |
FormSet = inlineformset_factory( |
| 92 |
ParentWithUUIDAlternateKey, ChildRelatedViaAK, fields="__all__" |
| 93 |
) |
| 94 |
formset = FormSet() |
| 95 |
self.assertIsNone(formset.forms[0].fields["parent"].initial) |
| 96 |
|
| 97 |
def test_inlineformset_factory_nulls_default_pks_alternate_key_relation_data(self): |
| 98 |
""" |
| 99 |
If form data is provided, a parent's auto-generated alternate key is |
| 100 |
set. |
| 101 |
""" |
| 102 |
FormSet = inlineformset_factory( |
| 103 |
ParentWithUUIDAlternateKey, ChildRelatedViaAK, fields="__all__" |
| 104 |
) |
| 105 |
formset = FormSet( |
| 106 |
{ |
| 107 |
"childrelatedviaak_set-TOTAL_FORMS": 3, |
| 108 |
"childrelatedviaak_set-INITIAL_FORMS": 0, |
| 109 |
"childrelatedviaak_set-MAX_NUM_FORMS": "", |
| 110 |
"childrelatedviaak_set-0-name": "Test", |
| 111 |
"childrelatedviaak_set-1-name": "", |
| 112 |
"childrelatedviaak_set-2-name": "", |
| 113 |
} |
| 114 |
) |
| 115 |
self.assertIs(formset.is_valid(), True) |
| 116 |
self.assertIsNotNone(formset.instance.uuid) |
| 117 |
self.assertEqual(formset.forms[0].instance.parent_id, formset.instance.uuid) |