split apps.dashboard unittests into test_views & test_models
This commit is contained in:
83
src/apps/dashboard/tests/test_views.py
Normal file
83
src/apps/dashboard/tests/test_views.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from django.test import TestCase
|
||||
from ..models import Item, List
|
||||
import lxml.html
|
||||
|
||||
class HomePageTest(TestCase):
|
||||
def test_uses_home_template(self):
|
||||
response = self.client.get('/')
|
||||
self.assertTemplateUsed(response, 'apps/dashboard/home.html')
|
||||
|
||||
def test_renders_input_form(self):
|
||||
response = self.client.get('/')
|
||||
parsed = lxml.html.fromstring(response.content)
|
||||
[form] = parsed.cssselect('form[method=POST]')
|
||||
self.assertEqual(form.get('action'), '/apps/dashboard/newlist')
|
||||
inputs = form.cssselect('input')
|
||||
self.assertIn('item_text', [input.get('name') for input in inputs])
|
||||
|
||||
class DashViewTest(TestCase):
|
||||
def test_uses_list_template(self):
|
||||
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):
|
||||
mylist = List.objects.create()
|
||||
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")
|
||||
inputs = form.cssselect('input')
|
||||
self.assertIn('item_text', [input.get('name') for input in inputs])
|
||||
|
||||
def test_displays_only_items_for_that_list(self):
|
||||
# Given/Arrange
|
||||
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(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):
|
||||
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}/')
|
||||
|
||||
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}/')
|
||||
Reference in New Issue
Block a user