Fakultas Ilmu Komputer UI

test_views.py 2.23 KB
Newer Older
1
from django.urls import resolve
2
from django.test import TestCase
Rani Lasma Uli's avatar
Rani Lasma Uli committed
3
from django.http import HttpRequest
4
5
from django.template.loader import render_to_string
from lists.models import Item
Rani Lasma Uli's avatar
Rani Lasma Uli committed
6

7
from lists.views import index
8

RANI LASMA ULI's avatar
RANI LASMA ULI committed
9
TO_DO_LIST_URL = '/'
RANI LASMA ULI's avatar
RANI LASMA ULI committed
10
class ListViewTest(TestCase):
RANI LASMA ULI's avatar
RANI LASMA ULI committed
11
		
12
	def test_root_url_resolves_to_home_page_view(self):
RANI LASMA ULI's avatar
RANI LASMA ULI committed
13
		found = resolve(TO_DO_LIST_URL)
14
		self.assertEqual(found.func, index)
Rani Lasma Uli's avatar
Rani Lasma Uli committed
15
16

	def test_home_page_returns_correct_html(self):
RANI LASMA ULI's avatar
RANI LASMA ULI committed
17
		response = self.client.get(TO_DO_LIST_URL)
Rani Lasma Uli's avatar
Rani Lasma Uli committed
18
		html = response.content.decode('utf8')
19

Rani Lasma Uli's avatar
Rani Lasma Uli committed
20
21
22
23
		# assertion
		self.assertTrue(html.startswith('<html>'))
		self.assertIn('<title>To-Do lists</title>', html)
		self.assertTrue(html.endswith('</html>'))
24
25
26
		self.assertTemplateUsed(response, 'to-do-list.html')

	def test_can_save_a_POST_request(self):
RANI LASMA ULI's avatar
RANI LASMA ULI committed
27
		response = self.client.post(TO_DO_LIST_URL, data={'item_text': 'A new list item'})
28
29
30
31
32
33
34
35

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

		self.assertIn(new_item.text, 'A new list item')

	def test_redirects_after_POST(self):
RANI LASMA ULI's avatar
RANI LASMA ULI committed
36
		response = self.client.post(TO_DO_LIST_URL, data={'item_text': 'A new list item'})
37
		self.assertEqual(response.status_code, 302)
RANI LASMA ULI's avatar
RANI LASMA ULI committed
38
		self.assertEqual(response['location'], TO_DO_LIST_URL)
39
40

	def test_only_saves_items_when_necessary(self):
RANI LASMA ULI's avatar
RANI LASMA ULI committed
41
		self.client.get(TO_DO_LIST_URL)
42
43
44
45
46
47
48
		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')
		
RANI LASMA ULI's avatar
RANI LASMA ULI committed
49
		response = self.client.get(TO_DO_LIST_URL)
50
51
52
53
54
55
		
		self.assertIn('itemey 1', response.content.decode())
		self.assertIn('itemey 2', response.content.decode())

	def test_display_correct_comment_message(self):
		Item.objects.all().delete()
RANI LASMA ULI's avatar
RANI LASMA ULI committed
56
		response = self.client.get(TO_DO_LIST_URL)
57
58
59
60
61
62
		html = response.content.decode('utf8')
		self.assertIn("yey, waktunya berlibur", html)

		for x in range(0,4):
			Item.objects.create(text='itemey '+str(x))

RANI LASMA ULI's avatar
RANI LASMA ULI committed
63
		response = self.client.get(TO_DO_LIST_URL)
64
65
66
67
		html = response.content.decode('utf8')
		self.assertIn("sibuk tapi santai", html)

		Item.objects.create(text='itemey 5')
RANI LASMA ULI's avatar
RANI LASMA ULI committed
68
		response = self.client.get(TO_DO_LIST_URL)
69
70
71
72
73
74
75
76
		html = response.content.decode('utf8')
		self.assertIn("oh tidak", html)