apps.dashboard.views refactored to handle new item POST requests; add_item() FBV eliminated for newfound redundancy

This commit is contained in:
Disco DeDisco
2026-01-19 18:48:21 -05:00
parent d406b38207
commit af3e20faef
5 changed files with 33 additions and 38 deletions

View File

@@ -27,7 +27,7 @@ class DashViewTest(TestCase):
response = self.client.get(f'/apps/dashboard/{mylist.id}/')
parsed = lxml.html.fromstring(response.content)
[form] = parsed.cssselect('form[method=POST]')
self.assertEqual(form.get('action'), f"/apps/dashboard/{mylist.id}/add-item")
self.assertEqual(form.get('action'), f"/apps/dashboard/{mylist.id}/")
inputs = form.cssselect('input')
self.assertIn('item_text', [input.get('name') for input in inputs])
@@ -45,6 +45,31 @@ class DashViewTest(TestCase):
self.assertContains(response, 'itemey 2')
self.assertNotContains(response, 'other list item')
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}/',
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_POST_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}/',
data={'item_text': 'A new item for an existing list'},
)
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'})
@@ -68,29 +93,3 @@ class NewListTest(TestCase):
self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
self.assertEqual(List.objects.count(), 0)
self.assertEqual(Item.objects.count(), 0)
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}/')