tests.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from django.urls import resolve
  2. from django.test import TestCase
  3. from django.http import HttpRequest
  4. from django.template.loader import render_to_string
  5. from lists.views import home_page
  6. from lists.models import Item, List
  7. class HomePageTest(TestCase):
  8. def test_root_url_resolves_to_home_page_view(self):
  9. found = resolve('/')
  10. self.assertEqual(found.func, home_page)
  11. def test_uses_home_template(self):
  12. response = self.client.get('/')
  13. # The Manual Way of doing it:
  14. html = response.content.decode('utf8')
  15. self.assertTrue(html.startswith('<html>'))
  16. self.assertIn('<title>To-Do lists</title>', html)
  17. self.assertTrue(html.strip().endswith('</html>'))
  18. # Using Django's Builtin Testing Client:
  19. self.assertTemplateUsed(response, 'home.html')
  20. def test_can_save_POST_request(self):
  21. response = self.client.post('/', data={'item_text': 'A new list item'})
  22. self.assertEqual(Item.objects.count(), 1)
  23. new_item = Item.objects.first()
  24. self.assertEqual(new_item.text, 'A new list item')
  25. def test_redirects_after_POST(self):
  26. response = self.client.post('/', data={'item_text': 'A new list item'})
  27. self.assertEqual(response.status_code, 302)
  28. self.assertEqual(response['location'], '/lists/the-only-list/')
  29. class ListViewTest(TestCase):
  30. def test_uses_list_template(self):
  31. response = self.client.get('/lists/the-only-list/')
  32. self.assertTemplateUsed(response, 'list.html')
  33. def test_displays_all_items(self):
  34. list_ = List.objects.create()
  35. Item.objects.create(text='itemey 1', listi=list_)
  36. Item.objects.create(text='itemey 2', listi=list_)
  37. response = self.client.get('/lists/the-only-list/')
  38. self.assertContains(response, 'itemey 1')
  39. self.assertContains(response, 'itemey 2')
  40. class ListAndItemModelsTest(TestCase):
  41. def test_save_and_retrieve_items(self):
  42. list_ = List()
  43. list_.save()
  44. item1 = Item()
  45. item1.text = 'The first (ever) list item'
  46. item1.listi = list_
  47. item1.save()
  48. item2 = Item()
  49. item2.text = 'Item the 2nd'
  50. item2.listi = list_
  51. item2.save()
  52. savedL = List.objects.first()
  53. self.assertEqual(savedL, list_)
  54. savedI = Item.objects.all()
  55. self.assertEqual(savedI.count(), 2)
  56. first_item = savedI[0]
  57. second_item = savedI[1]
  58. self.assertEqual(first_item.text, item1.text)
  59. self.assertEqual(first_item.listi, list_)
  60. self.assertEqual(second_item.text, item2.text)
  61. self.assertEqual(second_item.listi, list_)
  62. class NewListTest(TestCase):
  63. def test_can_save_a_POST_request(self):
  64. self.client.post('/lists/new', data={'item_text': 'A new list item'})
  65. self.assertEqual(Item.objects.count(), 1)
  66. new_item = Item.objects.first()
  67. self.assertEqual(new_item.text, 'A new list item')
  68. def test_redirects_after_POST(self):
  69. response = self.client.post('/lists/new', data={'item_text': 'A new list item'})
  70. self.assertRedirects(response, '/lists/the-only-list/')