Skip to content

Commit 373a60b

Browse files
committed
add tests for nested embedded model form fields
1 parent d822b17 commit 373a60b

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

tests/model_forms_/forms.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
from django import forms
22

3-
from .models import Author
3+
from .models import Author, Book
44

55

66
class AuthorForm(forms.ModelForm):
77
class Meta:
88
fields = "__all__"
99
model = Author
10+
11+
12+
class BookForm(forms.ModelForm):
13+
class Meta:
14+
fields = "__all__"
15+
model = Book

tests/model_forms_/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,13 @@ class Author(models.Model):
1616
age = models.IntegerField()
1717
address = EmbeddedModelField(Address)
1818
billing_address = EmbeddedModelField(Address, blank=True, null=True)
19+
20+
21+
class Publisher(EmbeddedModel):
22+
name = models.CharField(max_length=50)
23+
address = EmbeddedModelField(Address)
24+
25+
26+
class Book(models.Model):
27+
title = models.CharField(max_length=50)
28+
publisher = EmbeddedModelField(Publisher)

tests/model_forms_/test_embedded_model.py

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.test import TestCase
22

3-
from .forms import AuthorForm
4-
from .models import Address, Author
3+
from .forms import AuthorForm, BookForm
4+
from .models import Address, Author, Book, Publisher
55

66

77
class ModelFormTests(TestCase):
@@ -128,3 +128,91 @@ def test_rendering(self):
128128
<input type="number" name="address-zip_code" required id="id_address-zip_code">
129129
</div>""",
130130
)
131+
132+
133+
class NestedFormTests(TestCase):
134+
def test_update(self):
135+
book = Book.objects.create(
136+
title="Learning MongoDB",
137+
publisher=Publisher(
138+
name="Random House", address=Address(city="NYC", state="NY", zip_code="10001")
139+
),
140+
)
141+
data = {
142+
"title": "Learning MongoDB!",
143+
"publisher-name": "Random House!",
144+
"publisher-address-po_box": "",
145+
"publisher-address-city": "New York City",
146+
"publisher-address-state": "NY",
147+
"publisher-address-zip_code": "10001",
148+
}
149+
form = BookForm(data, instance=book)
150+
self.assertTrue(form.is_valid())
151+
form.save()
152+
book.refresh_from_db()
153+
self.assertEqual(book.title, "Learning MongoDB!")
154+
self.assertEqual(book.publisher.name, "Random House!")
155+
self.assertEqual(book.publisher.address.city, "New York City")
156+
157+
def test_some_missing_data(self):
158+
"""A required field (zip_code) is missing."""
159+
book = Book.objects.create(
160+
title="Learning MongoDB",
161+
publisher=Publisher(
162+
name="Random House", address=Address(city="NYC", state="NY", zip_code="10001")
163+
),
164+
)
165+
data = {
166+
"title": "Learning MongoDB!",
167+
"publisher-name": "Random House!",
168+
"publisher-address-po_box": "",
169+
"publisher-address-city": "New York City",
170+
"publisher-address-state": "NY",
171+
"publisher-address-zip_code": "",
172+
}
173+
form = BookForm(data, instance=book)
174+
self.assertFalse(form.is_valid())
175+
self.assertEqual(form.errors["publisher"], ["Enter all required values."])
176+
177+
def test_invalid_field_data(self):
178+
"""A field's data (state) is too long."""
179+
book = Book.objects.create(
180+
title="Learning MongoDB",
181+
publisher=Publisher(
182+
name="Random House", address=Address(city="NYC", state="NY", zip_code="10001")
183+
),
184+
)
185+
data = {
186+
"title": "Learning MongoDB!",
187+
"publisher-name": "Random House!",
188+
"publisher-address-po_box": "",
189+
"publisher-address-city": "New York City",
190+
"publisher-address-state": "TOO LONG",
191+
"publisher-address-zip_code": "10001",
192+
}
193+
form = BookForm(data, instance=book)
194+
self.assertFalse(form.is_valid())
195+
self.assertEqual(
196+
form.errors["publisher"],
197+
["Ensure this value has at most 2 characters (it has 8)."],
198+
)
199+
200+
def test_all_missing_data(self):
201+
"""An embedded model with all data missing triggers a required error."""
202+
book = Book.objects.create(
203+
title="Learning MongoDB",
204+
publisher=Publisher(
205+
name="Random House", address=Address(city="NYC", state="NY", zip_code="10001")
206+
),
207+
)
208+
data = {
209+
"title": "Learning MongoDB!",
210+
"publisher-name": "Random House!",
211+
"publisher-address-po_box": "",
212+
"publisher-address-city": "",
213+
"publisher-address-state": "",
214+
"publisher-address-zip_code": "",
215+
}
216+
form = BookForm(data, instance=book)
217+
self.assertFalse(form.is_valid())
218+
self.assertEqual(form.errors["publisher"], ["This field is required."])

0 commit comments

Comments
 (0)