|
1 | 1 | from django.test import TestCase
|
2 | 2 |
|
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 |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class ModelFormTests(TestCase):
|
@@ -128,3 +128,91 @@ def test_rendering(self):
|
128 | 128 | <input type="number" name="address-zip_code" required id="id_address-zip_code">
|
129 | 129 | </div>""",
|
130 | 130 | )
|
| 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