Fakultas Ilmu Komputer UI

test_views.py 2.54 KB
Newer Older
Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
1
from django.urls import resolve
2
from django.test import TestCase
3

Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
4
from latihan2.views import home_page
5
from latihan2.models import Item
6
7


Syahrul Ardiansyah's avatar
Syahrul Ardiansyah committed
8
9
10
11
class HomePageTest(TestCase):
    def test_root_url_resolves_to_home_page_view(self):
        found = resolve('/')
        self.assertEqual(found.func, home_page)
12
13

    def test_home_page_returns_correct_html(self):
14
        response = self.client.get('/')
15
        html = response.content.decode('utf8')
Syahrul Findi's avatar
Syahrul Findi committed
16
        self.assertTrue(html.startswith('<!DOCTYPE html>'))
17
18
19
        self.assertIn('<title>To-Do lists</title>', html)
        self.assertIn('<h1>Syahrul Findi Ardiansyah</h1>', html)
        self.assertIn('<h2>1506757352</h2>', html)
20
        self.assertIn('<h3>My To-Do Lists</h3>', html)
21
22
23
24
25
26
27
        self.assertTrue(html.strip().endswith('</html>'))

        self.assertTemplateUsed(response, 'home.html')

    def test_uses_home_template(self):
        response = self.client.get('/')
        self.assertTemplateUsed(response, 'home.html')
28
29
30

    def test_can_save_a_POST_request(self):
        response = self.client.post('/', data={'item_text': 'A new list item'})
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

        self.assertEqual(Item.objects.count(), 1)
        new_item = Item.objects.first()
        self.assertEqual(new_item.text, 'A new list item')

    def test_redirect_after_POST(self):
        response = self.client.post('/', data={'item_text': 'A new list item'})
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], '/')

    def test_only_saves_items_when_necessary(self):
        self.client.get('/')
        self.assertEqual(Item.objects.count(), 0)

    def test_displays_all_list_items(self):
        Item.objects.create(text='itemey 1')
        Item.objects.create(text='itemey 2')
        response = self.client.get('/')
        self.assertIn('itemey 1', response.content.decode())
        self.assertIn('itemey 2', response.content.decode())
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    def test_home_page_returns_correct_comment_on_empty_items(self):
        response = self.client.get('/')
        self.assertIn('yey, waktunya berlibur', response.content.decode())

    def test_home_page_returns_correct_comment_on_few_items(self):
        for i in range(2):
            Item.objects.create(text='itemey %d' % i)

        response = self.client.get('/')
        self.assertIn('sibuk tapi santai', response.content.decode())

    def test_home_page_returns_correct_comment_on_many_items(self):
        for i in range(6):
            Item.objects.create(text='itemey %d' % i)

        response = self.client.get('/')
        self.assertIn('oh tidak', response.content.decode())