Fakultas Ilmu Komputer UI

test_views.py 5.32 KB
Newer Older
1
from django.urls import resolve, reverse
2
from django.test import TestCase
3
from django.http import HttpRequest
4
from django.utils.html import escape
5
from lists.views import home_page, about_page, \
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
6
    COMMENTARY_ZERO, COMMENTARY_LESS_THAN_FIVE, COMMENTARY_MORE_THAN_EQUAL_FIVE, get_commentary
7
from lists.models import Item, List
8

9

10
class HomePageTest(TestCase):
11

12
13
14
    def test_root_url_resolves_to_home_page_view(self):
        found = resolve('/')
        self.assertEqual(found.func, home_page)
15

Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
16
17
18
19
    def test_uses_home_template(self):
        response = self.client.get('/')
        self.assertTemplateUsed(response, 'home.html')

20
21
22
23
24
25
    # def test_display_correct_personal_commentary(self):
    #     cases = [
    #         (0, COMMENTARY_ZERO),
    #         (4, COMMENTARY_LESS_THAN_FIVE),
    #         (5, COMMENTARY_MORE_THAN_EQUAL_FIVE)
    #     ]
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
26

27
28
29
30
31
    #     for item_count, commentary in cases:
    #         for i in range(item_count):
    #             Item.objects.create(text='itemey {}'.format(i))
    #         response = self.client.get('/')
    #         self.assertIn(commentary, response.content.decode())
32

33
34
class AboutPageTest(TestCase):

35
    def test_about_url_resolves_to_about_page_view(self):
36
        found = resolve(reverse('about'))
37
38
39
40
        self.assertEqual(found.func, about_page)

    def test_about_page_returns_correct_html(self):
        request = HttpRequest()  
41
        response = about_page(request)  
42
43
44
        html = response.content.decode('utf8')  
        self.assertTrue(html.startswith('<html>'))  
        self.assertIn('<title>Rayza\'s About Page</title>', html)
45
        self.assertIn('<h1>Here\'s a few facts about me</h1>', html)
46
        self.assertIn('<p>I\'m 21 years old</p>', html)
47
48
        self.assertIn('<p>I\'m from Bogor</p>', html)
        self.assertIn('<p>I\'m currently studying Computer Science at Faculty of Computer Science, Universitas Indonesia</p>', html)
49
        self.assertTrue(html.endswith('</html>'))
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
50

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class ListViewTest(TestCase):

    def test_uses_list_template(self):
        list_ = List.objects.create()
        response = self.client.get(f'/lists/{list_.id}/')
        self.assertTemplateUsed(response, 'list.html')


    def test_displays_only_items_for_that_list(self):
        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 1', list=other_list)
        Item.objects.create(text='other list item 2', list=other_list)

        response = self.client.get(f'/lists/{correct_list.id}/')

        self.assertContains(response, 'itemey 1')
        self.assertContains(response, 'itemey 2')
        self.assertNotContains(response, 'other list item 1')
        self.assertNotContains(response, 'other list item 2')

    def test_passes_correct_list_to_template(self):
        other_list = List.objects.create()
        correct_list = List.objects.create()
        response = self.client.get(f'/lists/{correct_list.id}/')
        self.assertEqual(response.context['list'], correct_list)

    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(
85
            f'/lists/{correct_list.id}/',
86
87
88
89
90
91
92
93
94
95
96
97
98
            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(
99
            f'/lists/{correct_list.id}/',
100
101
102
            data={'item_text': 'A new item for an existing list'}
        )

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
        self.assertRedirects(response, f'/lists/{correct_list.id}/')

    def test_validation_errors_end_up_on_lists_page(self):
        list_ = List.objects.create()
        response = self.client.post(
            f'/lists/{list_.id}/',
            data={'item_text': ''}
        )
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'list.html')
        expected_error = escape("You can't have an empty list item")
        self.assertContains(response, expected_error)

class NewListTest(TestCase):

    def test_validation_errors_are_sent_back_to_home_page_template(self):
        response = self.client.post('/lists/new', data={'item_text': ''})
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'home.html')
        expected_error = escape("You can't have an empty list item")
        self.assertContains(response, expected_error)

    def test_invalid_list_items_arent_saved(self):
        self.client.post('/lists/new', data={'item_text': ''})
        self.assertEqual(List.objects.count(), 0)
Rayza Arasj Mahardhika's avatar
Rayza Arasj Mahardhika committed
128
129
130
131
132
133
134
135
136
137
138
        self.assertEqual(Item.objects.count(), 0)

class CustomTest(TestCase):

    def test_commentary_correct_count_5_correct(self):
        result = get_commentary(5)
        self.assertEqual(result, COMMENTARY_MORE_THAN_EQUAL_FIVE)

    def test_commentary_correct_count_less_than_5_correct(self):
        result = get_commentary(4)
        self.assertEqual(result, COMMENTARY_LESS_THAN_FIVE)