7.11 One More View to Handle Adding Items to an Existing List

chap7-new
Jason Zhu 2020-11-11 15:15:51 +11:00
parent 29124d8fcd
commit 3b999bda98
1 changed files with 29 additions and 0 deletions

View File

@ -21,6 +21,35 @@ class NewListTest(TestCase):
new_list = List.objects.first() new_list = List.objects.first()
self.assertRedirects(response=response, expected_url=f'/lists/{new_list.id}/') self.assertRedirects(response=response, expected_url=f'/lists/{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'/lists/{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.first()
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'/lists/{correct_list.id}/add_item',
data={'item_text': 'A new item for an existing list'}
)
self.assertRedirects(
response=response,
expected_url=f'/lists/{correct_list.id}/')
class ListViewTest(TestCase): class ListViewTest(TestCase):
def test_uses_list_template(self): def test_uses_list_template(self):