|
| 1 | +from django.test import TestCase |
| 2 | + |
| 3 | +from .forms import AuthorForm |
| 4 | +from .models import Address, Author |
| 5 | + |
| 6 | + |
| 7 | +class ModelFormTests(TestCase): |
| 8 | + def test_update(self): |
| 9 | + author = Author.objects.create( |
| 10 | + name="Bob", age=50, address=Address(city="NYC", state="NY", zip_code="10001") |
| 11 | + ) |
| 12 | + data = { |
| 13 | + "name": "Bob", |
| 14 | + "age": 51, |
| 15 | + "address-po_box": "", |
| 16 | + "address-city": "New York City", |
| 17 | + "address-state": "NY", |
| 18 | + "address-zip_code": "10001", |
| 19 | + } |
| 20 | + form = AuthorForm(data, instance=author) |
| 21 | + self.assertTrue(form.is_valid()) |
| 22 | + form.save() |
| 23 | + author.refresh_from_db() |
| 24 | + self.assertEqual(author.age, 51) |
| 25 | + self.assertEqual(author.address.city, "New York City") |
| 26 | + |
| 27 | + def test_some_missing_data(self): |
| 28 | + author = Author.objects.create( |
| 29 | + name="Bob", age=50, address=Address(city="NYC", state="NY", zip_code="10001") |
| 30 | + ) |
| 31 | + data = { |
| 32 | + "name": "Bob", |
| 33 | + "age": 51, |
| 34 | + "address-po_box": "", |
| 35 | + "address-city": "New York City", |
| 36 | + "address-state": "NY", |
| 37 | + "address-zip_code": "", |
| 38 | + } |
| 39 | + form = AuthorForm(data, instance=author) |
| 40 | + self.assertFalse(form.is_valid()) |
| 41 | + self.assertEqual(form.errors["address"], ["Enter all required values."]) |
| 42 | + |
| 43 | + def test_invalid_field_data(self): |
| 44 | + """A field's data (state) is too long.""" |
| 45 | + author = Author.objects.create( |
| 46 | + name="Bob", age=50, address=Address(city="NYC", state="NY", zip_code="10001") |
| 47 | + ) |
| 48 | + data = { |
| 49 | + "name": "Bob", |
| 50 | + "age": 51, |
| 51 | + "address-po_box": "", |
| 52 | + "address-city": "New York City", |
| 53 | + "address-state": "TOO LONG", |
| 54 | + "address-zip_code": "", |
| 55 | + } |
| 56 | + form = AuthorForm(data, instance=author) |
| 57 | + self.assertFalse(form.is_valid()) |
| 58 | + self.assertEqual( |
| 59 | + form.errors["address"], |
| 60 | + [ |
| 61 | + "Ensure this value has at most 2 characters (it has 8).", |
| 62 | + "Enter all required values.", |
| 63 | + ], |
| 64 | + ) |
| 65 | + |
| 66 | + def test_all_missing_data(self): |
| 67 | + author = Author.objects.create( |
| 68 | + name="Bob", age=50, address=Address(city="NYC", state="NY", zip_code="10001") |
| 69 | + ) |
| 70 | + data = { |
| 71 | + "name": "Bob", |
| 72 | + "age": 51, |
| 73 | + "address-po_box": "", |
| 74 | + "address-city": "", |
| 75 | + "address-state": "", |
| 76 | + "address-zip_code": "", |
| 77 | + } |
| 78 | + form = AuthorForm(data, instance=author) |
| 79 | + self.assertFalse(form.is_valid()) |
| 80 | + self.assertEqual(form.errors["address"], ["This field is required."]) |
| 81 | + |
| 82 | + def test_nullable_field(self): |
| 83 | + """A nullable EmbeddedModelField is removed if all fields are empty.""" |
| 84 | + author = Author.objects.create( |
| 85 | + name="Bob", |
| 86 | + age=50, |
| 87 | + address=Address(city="NYC", state="NY", zip_code="10001"), |
| 88 | + billing_address=Address(city="NYC", state="NY", zip_code="10001"), |
| 89 | + ) |
| 90 | + data = { |
| 91 | + "name": "Bob", |
| 92 | + "age": 51, |
| 93 | + "address-po_box": "", |
| 94 | + "address-city": "New York City", |
| 95 | + "address-state": "NY", |
| 96 | + "address-zip_code": "10001", |
| 97 | + "billing_address-po_box": "", |
| 98 | + "billing_address-city": "", |
| 99 | + "billing_address-state": "", |
| 100 | + "billing_address-zip_code": "", |
| 101 | + } |
| 102 | + form = AuthorForm(data, instance=author) |
| 103 | + self.assertTrue(form.is_valid()) |
| 104 | + form.save() |
| 105 | + author.refresh_from_db() |
| 106 | + self.assertIsNone(author.billing_address) |
| 107 | + |
| 108 | + def test_rendering(self): |
| 109 | + form = AuthorForm() |
| 110 | + self.assertHTMLEqual( |
| 111 | + str(form.fields["address"].get_bound_field(form, "address")), |
| 112 | + """ |
| 113 | + <div> |
| 114 | + <label for="id_address-po_box">PO Box:</label> |
| 115 | + <input id="id_address-po_box" maxlength="50" name="address-po_box" type="text"> |
| 116 | + </div> |
| 117 | + <div> |
| 118 | + <label for="id_address-city">City:</label> |
| 119 | + <input type="text" name="address-city" maxlength="20" required id="id_address-city"> |
| 120 | + </div> |
| 121 | + <div> |
| 122 | + <label for="id_address-state">State:</label> |
| 123 | + <input type="text" name="address-state" maxlength="2" required |
| 124 | + id="id_address-state"> |
| 125 | + </div> |
| 126 | + <div> |
| 127 | + <label for="id_address-zip_code">Zip code:</label> |
| 128 | + <input type="number" name="address-zip_code" required id="id_address-zip_code"> |
| 129 | + </div>""", |
| 130 | + ) |
0 commit comments