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}/')

View File

@@ -4,11 +4,18 @@ from .models import Item, List
def home_page(request):
return render(request, 'apps/dashboard/home.html')
def view_list(request):
items = Item.objects.all()
return render(request, 'apps/dashboard/list.html', {'items': items})
def view_list(request, list_id):
our_list = List.objects.get(id=list_id)
items = Item.objects.filter(list=our_list)
return render(request, 'apps/dashboard/list.html', {'items': items, 'list': our_list})
def new_list(request):
nulist = List.objects.create()
Item.objects.create(text=request.POST['item_text'], list=nulist)
return redirect('/apps/dashboard/the-only-list-in-the-world/')
return redirect(f'/apps/dashboard/{nulist.id}/')
def add_item(request, list_id):
our_list = List.objects.get(id=list_id)
Item.objects.create(text=request.POST['item_text'], list=our_list)
return redirect(f'/apps/dashboard/{our_list.id}/')

View File

@@ -6,5 +6,6 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home_page, name='home'),
path('apps/dashboard/newlist', views.new_list, name='new_list'),
path('apps/dashboard/the-only-list-in-the-world/', views.view_list, name='view_list'),
path('apps/dashboard/<int:list_id>/', views.view_list, name='view_list'),
path('apps/dashboard/<int:list_id>/add-item', views.add_item, name='add_item'),
]

View File

@@ -4,7 +4,7 @@
</head>
<body>
<h1>Your To-Do List</h1>
<form method="POST" action="/apps/dashboard/newlist">
<form method="POST" action="/apps/dashboard/{{ list.id }}/add-item">
<input name="item_text" id="id-new-item" placeholder="Enter a to-do item">
{% csrf_token %}
</form>