now enforcing model validation in list FBV; all UTs & FTs passing

This commit is contained in:
Disco DeDisco
2026-01-19 19:09:11 -05:00
parent af3e20faef
commit 91ede73e89
4 changed files with 44 additions and 25 deletions

View File

@@ -16,6 +16,30 @@ class HomePageTest(TestCase):
inputs = form.cssselect('input')
self.assertIn('item_text', [input.get('name') for input in inputs])
class NewListTest(TestCase):
def test_can_save_a_POST_request(self):
self. client.post('/apps/dashboard/newlist', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.get()
self.assertEqual(new_item.text, 'A new list item')
def test_redirects_after_POST(self):
response = self.client.post('/apps/dashboard/newlist', data={'item_text': 'A new list item'})
new_list = List.objects.get()
self.assertRedirects(response, f'/apps/dashboard/{new_list.id}/')
def test_validation_errors_are_sent_back_to_home_page_template(self):
response = self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "apps/dashboard/home.html")
expected_error = html.escape("You can't have an empty list item")
self.assertContains(response, expected_error)
def test_invalid_list_items_never_save(self):
self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
self.assertEqual(List.objects.count(), 0)
self.assertEqual(Item.objects.count(), 0)
class DashViewTest(TestCase):
def test_uses_list_template(self):
mylist = List.objects.create()
@@ -70,26 +94,13 @@ class DashViewTest(TestCase):
self.assertRedirects(response, f'/apps/dashboard/{correct_list.id}/')
class NewListTest(TestCase):
def test_can_save_a_POST_request(self):
self. client.post('/apps/dashboard/newlist', data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.get()
self.assertEqual(new_item.text, 'A new list item')
def test_redirects_after_POST(self):
response = self.client.post('/apps/dashboard/newlist', data={'item_text': 'A new list item'})
new_list = List.objects.get()
self.assertRedirects(response, f'/apps/dashboard/{new_list.id}/')
def test_validation_errors_are_sent_back_to_home_page_template(self):
response = self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
def test_validation_errors_end_up_on_lists_page(self):
list_ = List.objects.create()
response = self.client.post(
f"/apps/dashboard/{list_.id}/",
data={"item_text": ""},
)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "apps/dashboard/home.html")
self.assertTemplateUsed(response, "apps/dashboard/list.html")
expected_error = html.escape("You can't have an empty list item")
self.assertContains(response, expected_error)
def test_invalid_list_items_never_save(self):
self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
self.assertEqual(List.objects.count(), 0)
self.assertEqual(Item.objects.count(), 0)