|
1 |
| -from django.test import TestCase |
2 | 1 | from django.utils.html import escape
|
3 | 2 | from lists.forms import ItemForm, EMPTY_ITEM_ERROR, DUPLICATE_ITEM_ERROR, ExistingListItemForm
|
4 | 3 | from lists.models import Item, List
|
5 |
| -from unittest import skip |
6 | 4 |
|
| 5 | +import pytest |
7 | 6 |
|
8 |
| -class HomePageTest(TestCase): |
9 |
| - def test_home_page_renders_home_template(self): |
10 |
| - response = self.client.get('/') |
11 |
| - self.assertTemplateUsed(response, 'home.html') |
| 7 | +pytestmark = pytest.mark.django_db |
12 | 8 |
|
13 |
| - def test_home_page_uses_item_form(self): |
14 |
| - response = self.client.get('/') |
15 |
| - self.assertIsInstance(response.context['form'], ItemForm) |
16 | 9 |
|
| 10 | +class TestHomePage: |
| 11 | + def test_home_page_renders_home_template(self, client): |
| 12 | + response = client.get('/') |
| 13 | + pytest.assertTemplateUsed(response, 'home.html') |
17 | 14 |
|
18 |
| -class ListViewTest(TestCase): |
| 15 | + def test_home_page_uses_item_form(self, client): |
| 16 | + response = client.get('/') |
| 17 | + assert isinstance(response.context['form'], ItemForm) |
19 | 18 |
|
20 |
| - def post_invalid_input(self): |
| 19 | + |
| 20 | +class TestListView: |
| 21 | + |
| 22 | + def post_invalid_input(self, client): |
21 | 23 | list_ = List.objects.create()
|
22 |
| - return self.client.post( |
| 24 | + return client.post( |
23 | 25 | '/lists/{}/'.format(list_.id),
|
24 | 26 | data={'text': ''}
|
25 | 27 | )
|
26 | 28 |
|
27 |
| - def test_uses_list_templates(self): |
| 29 | + def test_uses_list_templates(self, client): |
28 | 30 | list_ = List.objects.create()
|
29 |
| - response = self.client.get('/lists/{}/'.format(list_.id)) |
30 |
| - self.assertTemplateUsed(response, 'list.html') |
| 31 | + response = client.get('/lists/{}/'.format(list_.id)) |
| 32 | + pytest.assertTemplateUsed(response, 'list.html') |
31 | 33 |
|
32 |
| - def test_displays_only_items_for_that_list(self): |
| 34 | + def test_displays_only_items_for_that_list(self, client): |
33 | 35 | correct_list = List.objects.create()
|
34 | 36 | Item.objects.create(text='itemey 1', list=correct_list)
|
35 | 37 | Item.objects.create(text='itemey 2', list=correct_list)
|
36 | 38 | other_list = List.objects.create()
|
37 | 39 | Item.objects.create(text='other list item 1', list=other_list)
|
38 | 40 | Item.objects.create(text='other list item 2', list=other_list)
|
39 | 41 |
|
40 |
| - response = self.client.get('/lists/{}/'.format(correct_list.id)) |
| 42 | + response = client.get('/lists/{}/'.format(correct_list.id)) |
41 | 43 |
|
42 |
| - self.assertContains(response, 'itemey 1') |
43 |
| - self.assertContains(response, 'itemey 2') |
44 |
| - self.assertNotContains(response, 'other list item 1') |
45 |
| - self.assertNotContains(response, 'other list item 2') |
| 44 | + pytest.assertContains(response, 'itemey 1') |
| 45 | + pytest.assertContains(response, 'itemey 2') |
| 46 | + pytest.assertNotContains(response, 'other list item 1') |
| 47 | + pytest.assertNotContains(response, 'other list item 2') |
46 | 48 |
|
47 |
| - def test_passes_correct_list_to_template(self): |
| 49 | + def test_passes_correct_list_to_template(self, client): |
48 | 50 | other_list = List.objects.create()
|
49 | 51 | correct_list = List.objects.create()
|
50 | 52 |
|
51 |
| - response = self.client.get('/lists/{}/'.format(correct_list.id)) |
| 53 | + response = client.get('/lists/{}/'.format(correct_list.id)) |
52 | 54 |
|
53 |
| - self.assertEqual(response.context['list'], correct_list) |
| 55 | + assert correct_list == response.context['list'] |
54 | 56 |
|
55 |
| - def test_displays_item_form(self): |
| 57 | + def test_displays_item_form(self, client): |
56 | 58 | list_ = List.objects.create()
|
57 |
| - response = self.client.get('/lists/{}/'.format(list_.id)) |
58 |
| - self.assertIsInstance(response.context['form'], ExistingListItemForm) |
59 |
| - self.assertContains(response, 'name="text"') |
| 59 | + response = client.get('/lists/{}/'.format(list_.id)) |
| 60 | + assert isinstance(response.context['form'], ExistingListItemForm) |
| 61 | + pytest.assertContains(response, 'name="text"') |
60 | 62 |
|
61 |
| - def test_can_save_a_POST_request_to_an_existing_list(self): |
| 63 | + def test_can_save_a_POST_request_to_an_existing_list(self, client): |
62 | 64 | other_list = List.objects.create()
|
63 | 65 | correct_list = List.objects.create()
|
64 | 66 |
|
65 |
| - self.client.post( |
| 67 | + client.post( |
66 | 68 | '/lists/{}/'.format(correct_list.id),
|
67 | 69 | data={'text': 'A new item for an existing list'}
|
68 | 70 | )
|
69 | 71 |
|
70 |
| - self.assertEqual(Item.objects.count(), 1) |
| 72 | + assert 1 == Item.objects.count() |
71 | 73 | new_item = Item.objects.first()
|
72 |
| - self.assertEqual(new_item.text, 'A new item for an existing list') |
73 |
| - self.assertEqual(new_item.list, correct_list) |
| 74 | + assert 'A new item for an existing list' == new_item.text |
| 75 | + assert correct_list == new_item.list |
74 | 76 |
|
75 |
| - def test_POST_redirects_to_list_view(self): |
| 77 | + def test_POST_redirects_to_list_view(self, client): |
76 | 78 | other_list = List.objects.create()
|
77 | 79 | correct_list = List.objects.create()
|
78 |
| - response = self.client.post( |
| 80 | + response = client.post( |
79 | 81 | '/lists/{}/'.format(correct_list.id),
|
80 | 82 | data={'text': 'A new item for an existing list'}
|
81 | 83 | )
|
82 |
| - self.assertRedirects(response, '/lists/{}/'.format(correct_list.id)) |
83 |
| - |
84 |
| - def test_for_invalid_input_nothing_saved_to_db(self): |
85 |
| - self.post_invalid_input() |
86 |
| - self.assertEqual(Item.objects.count(), 0) |
87 |
| - |
88 |
| - def test_for_invalid_input_renders_list_template(self): |
89 |
| - response = self.post_invalid_input() |
90 |
| - self.assertEqual(response.status_code, 200) |
91 |
| - self.assertTemplateUsed(response, 'list.html') |
| 84 | + pytest.assertRedirects(response, '/lists/{}/'.format(correct_list.id)) |
92 | 85 |
|
93 |
| - def test_for_invalid_input_passes_form_to_template(self): |
94 |
| - response = self.post_invalid_input() |
95 |
| - self.assertIsInstance(response.context['form'], ExistingListItemForm) |
| 86 | + def test_for_invalid_input_nothing_saved_to_db(self, client): |
| 87 | + self.post_invalid_input(client) |
| 88 | + assert 0 == Item.objects.count() |
96 | 89 |
|
97 |
| - def test_for_invalid_input_shows_error_on_page(self): |
98 |
| - response = self.post_invalid_input() |
99 |
| - self.assertContains(response, escape(EMPTY_ITEM_ERROR)) |
| 90 | + def test_for_invalid_input_renders_list_template(self, client): |
| 91 | + response = self.post_invalid_input(client) |
| 92 | + assert 200 == response.status_code |
| 93 | + pytest.assertTemplateUsed(response, 'list.html') |
100 | 94 |
|
| 95 | + def test_for_invalid_input_passes_form_to_template(self, client): |
| 96 | + response = self.post_invalid_input(client) |
| 97 | + assert isinstance(response.context['form'], ExistingListItemForm) |
101 | 98 |
|
| 99 | + def test_for_invalid_input_shows_error_on_page(self, client): |
| 100 | + response = self.post_invalid_input(client) |
| 101 | + pytest.assertContains(response, escape(EMPTY_ITEM_ERROR)) |
102 | 102 |
|
103 |
| - def test_duplicate_item_validation_errors_end_up_on_lists_page(self): |
| 103 | + def test_duplicate_item_validation_errors_end_up_on_lists_page(self, client): |
104 | 104 | list1 = List.objects.create()
|
105 | 105 | item1 = Item.objects.create(list=list1, text='textey')
|
106 |
| - response = self.client.post( |
| 106 | + response = client.post( |
107 | 107 | '/lists/%d/' % (list1.id,),
|
108 | 108 | data={'text': 'textey'}
|
109 | 109 | )
|
110 | 110 |
|
111 | 111 | expected_error = escape(DUPLICATE_ITEM_ERROR)
|
112 |
| - self.assertContains(response, expected_error) |
113 |
| - self.assertTemplateUsed(response, 'list.html') |
114 |
| - self.assertEqual(Item.objects.all().count(), 1) |
| 112 | + pytest.assertContains(response, expected_error) |
| 113 | + pytest.assertTemplateUsed(response, 'list.html') |
| 114 | + assert 1 == Item.objects.all().count() |
115 | 115 |
|
116 | 116 |
|
117 |
| -class NewListTest(TestCase): |
118 |
| - def test_saving_a_POST_request(self): |
119 |
| - self.client.post( |
| 117 | +class TestNewList: |
| 118 | + def test_saving_a_POST_request(self, client): |
| 119 | + client.post( |
120 | 120 | '/lists/new',
|
121 | 121 | data={'text': 'A new list item'}
|
122 | 122 | )
|
123 | 123 |
|
124 |
| - self.assertEqual(Item.objects.count(), 1) |
| 124 | + assert 1 == Item.objects.count(), 1 |
125 | 125 | new_item = Item.objects.first()
|
126 |
| - self.assertEqual(new_item.text, 'A new list item') |
| 126 | + assert 'A new list item' == new_item.text |
127 | 127 |
|
128 |
| - def test_redirects_after_POST(self): |
129 |
| - response = self.client.post( |
| 128 | + def test_redirects_after_POST(self, client): |
| 129 | + response = client.post( |
130 | 130 | '/lists/new',
|
131 | 131 | data={'text': 'A new list item'}
|
132 | 132 | )
|
133 | 133 | new_list = List.objects.first()
|
134 |
| - self.assertRedirects(response, '/lists/{}/'.format(new_list.id)) |
| 134 | + pytest.assertRedirects(response, '/lists/{}/'.format(new_list.id)) |
135 | 135 |
|
136 |
| - def test_for_invalid_input_renders_home_template(self): |
137 |
| - response = self.client.post('/lists/new', data={'text': ''}) |
138 |
| - self.assertEqual(response.status_code, 200) |
139 |
| - self.assertTemplateUsed(response, 'home.html') |
| 136 | + def test_for_invalid_input_renders_home_template(self, client): |
| 137 | + response = client.post('/lists/new', data={'text': ''}) |
| 138 | + assert 200 == response.status_code |
| 139 | + pytest.assertTemplateUsed(response, 'home.html') |
140 | 140 |
|
141 |
| - def test_validation_errors_are_shown_on_home_page(self): |
142 |
| - response = self.client.post('/lists/new', data={'text': ''}) |
143 |
| - self.assertContains(response, escape(EMPTY_ITEM_ERROR)) |
| 141 | + def test_validation_errors_are_shown_on_home_page(self, client): |
| 142 | + response = client.post('/lists/new', data={'text': ''}) |
| 143 | + pytest.assertContains(response, escape(EMPTY_ITEM_ERROR)) |
144 | 144 |
|
145 |
| - def test_for_invalid_input_passes_form_to_template(self): |
146 |
| - response = self.client.post('/lists/new', data={'text': ''}) |
147 |
| - self.assertIsInstance(response.context['form'], ItemForm) |
| 145 | + def test_for_invalid_input_passes_form_to_template(self, client): |
| 146 | + response = client.post('/lists/new', data={'text': ''}) |
| 147 | + assert isinstance(response.context['form'], ItemForm) |
148 | 148 |
|
149 |
| - def test_invalid_list_items_arent_saved(self): |
150 |
| - self.client.post('/lists/new', data={'text': ''}) |
151 |
| - self.assertEqual(List.objects.count(), 0) |
152 |
| - self.assertEqual(Item.objects.count(), 0) |
| 149 | + def test_invalid_list_items_arent_saved(self, client): |
| 150 | + client.post('/lists/new', data={'text': ''}) |
| 151 | + assert 0 == List.objects.count() |
| 152 | + assert 0 == Item.objects.count() |
0 commit comments