redefined & added arguments to existing FBVs in apps.dashboard.views; added new FBV to add to-do items to existing lists; added capture groups to some paths in .urls (tho these are now due for DRY refactor); django templating added to form action in templates/apps/dashboard/list.html; DashViewTest(), NewListTest() & NewItemTest() refactored for dynamic ids in .tests

This commit is contained in:
Disco DeDisco
2026-01-03 01:30:51 -05:00
parent b09bec02dc
commit ada383c81a
4 changed files with 58 additions and 15 deletions

View File

@@ -41,24 +41,32 @@ class ListAndItemModelsTest(TestCase):
class DashViewTest(TestCase):
def test_uses_list_template(self):
response = self.client.get('/apps/dashboard/the-only-list-in-the-world/')
mylist = List.objects.create()
response = self.client.get(f'/apps/dashboard/{mylist.id}/')
self.assertTemplateUsed(response, 'apps/dashboard/list.html')
def test_renders_input_form(self):
response = self.client.get('/apps/dashboard/the-only-list-in-the-world/')
self.assertContains(response, '<form method="POST" action="/apps/dashboard/newlist">')
mylist = List.objects.create()
response = self.client.get(f'/apps/dashboard/{mylist.id}/')
self.assertContains(
response,
f'<form method="POST" action="/apps/dashboard/{mylist.id}/add-item">',
)
self.assertContains(response, '<input name="item_text"')
def test_displays_all_list_items(self):
def test_displays_only_items_for_that_list(self):
# Given/Arrange
mylist = List.objects.create()
Item.objects.create(text='itemey 1', list=mylist)
Item.objects.create(text='itemey 2', list=mylist)
correct_list = List.objects.create()
Item.objects.create(text='itemey 1', list=correct_list)
Item.objects.create(text='itemey 2', list=correct_list)
other_list = List.objects.create()
Item.objects.create(text='other list item', list=other_list)
# When/Act
response = self.client.get('/apps/dashboard/the-only-list-in-the-world/')
response = self.client.get(f'/apps/dashboard/{correct_list.id}/')
# Then/Assert
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
self.assertNotContains(response, 'other list item')
class NewListTest(TestCase):
def test_can_save_a_POST_request(self):
@@ -69,4 +77,31 @@ class NewListTest(TestCase):
def test_redirects_after_POST(self):
response = self.client.post('/apps/dashboard/newlist', data={'item_text': 'A new list item'})
self.assertRedirects(response, '/apps/dashboard/the-only-list-in-the-world/')
new_list = List.objects.get()
self.assertRedirects(response, f'/apps/dashboard/{new_list.id}/')
class NewItemTest(TestCase):
def test_can_save_a_POST_request_to_an_existing_list(self):
other_list = List.objects.create()
correct_list = List.objects.create()
self.client.post(
f'/apps/dashboard/{correct_list.id}/add-item',
data={'item_text': 'A new item for an existing list'},
)
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.get()
self.assertEqual(new_item.text, 'A new item for an existing list')
self.assertEqual(new_item.list, correct_list)
def test_redirects_to_list_view(self):
other_list = List.objects.create()
correct_list = List.objects.create()
response = self.client.post(
f'/apps/dashboard/{correct_list.id}/add-item',
data={'item_text': 'A new item for an existing list'},
)
self.assertRedirects(response, f'/apps/dashboard/{correct_list.id}/')