Fakultas Ilmu Komputer UI

Commit 7518a18e authored by Muhammad Ilham Peruzzi's avatar Muhammad Ilham Peruzzi
Browse files

deleting test.py in root app folder

parent 47bc8563
Pipeline #23220 passed with stages
in 20 minutes and 26 seconds
from django.urls import resolve
from django.test import TestCase
from django.http import HttpRequest
from django.template.loader import render_to_string
from lists.models import Item, List
from lists.views import home_page, about_me, view_list
class ListAndItemModelsTest(TestCase):
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.list = list_
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.list = list_
second_item.save()
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
saved_items = Item.objects.all()
self.assertEqual(saved_items.count(), 2)
first_saved_item = saved_items[0]
second_saved_item = saved_items[1]
self.assertEqual(first_saved_item.text, 'The first (ever) list item')
self.assertEqual(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, 'Item the second')
self.assertEqual(second_saved_item.list, list_)
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = resolve('/')
self.assertEqual(found.func, home_page)
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
self.assertIn(b'<title>To-Do lists</title>', response.content)
self.assertTrue(response.content.strip().endswith(b'</html>'))
def test_my_name_exist_in_homepage(self):
new_response = self.client.get('/')
html_response = new_response.content.decode('utf8')
self.assertIn('Muhammad Ilham Peruzzi', html_response)
def test_homepage_use_html_file_correctly(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'homepage.html')
def test_homepage_has_title_homepage(self):
new_response = self.client.get('/')
html_response = new_response.content.decode('utf8')
self.assertIn('Homepage', html_response)
def test_homepage_have_my_biography_min_100_char(self):
new_response = self.client.get('/')
html_response = new_response.content.decode('utf8')
self.assertIn('Bio', html_response)
self.assertTrue(len(about_me()) >= 100)
def test_automatic_comment_when_no_items(self):
list_ = List.objects.create()
self.assertEqual(Item.objects.count(), 0)
request = HttpRequest()
response = view_list(request,list_.id)
self.assertIn('yey, waktunya berlibur', response.content.decode())
def test_automatic_comment_when_to_do_have_less_five_items(self):
list_ = List.objects.create()
Item.objects.create(text='Activity 1', list=list_)
Item.objects.create(text='Activity 2', list=list_)
self.assertEqual(Item.objects.count(), 2)
request = HttpRequest()
response = view_list(request,list_.id)
self.assertIn('Activity 1', response.content.decode())
self.assertIn('sibuk tapi santai', response.content.decode())
def test_automatic_comment_when_to_do_have_more_than_or_equal_five_items(self):
list_ = List.objects.create()
Item.objects.create(text='Activity 1', list=list_)
Item.objects.create(text='Activity 2', list=list_)
Item.objects.create(text='Activity 3', list=list_)
Item.objects.create(text='Activity 4', list=list_)
Item.objects.create(text='Activity 5', list=list_)
Item.objects.create(text='Activity 6', list=list_)
self.assertEqual(Item.objects.count(), 6)
request = HttpRequest()
response = view_list(request,list_.id)
self.assertIn('Activity 6', response.content.decode())
self.assertIn('oh tidak', response.content.decode())
class ListViewTest(TestCase):
def test_uses_list_template(self):
list_ = List.objects.create()
response = self.client.get('/lists/%d/' % (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('/lists/%d/' % (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_displays_all_items(self):
list_ = List.objects.create()
Item.objects.create(text='itemey 1', list=list_)
Item.objects.create(text='itemey 2', list=list_)
def test_saving_a_POST_request(self):
self.client.post('/lists/new',data={'item_text': 'A new list item'})
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
def test_redirects_after_POST(self):
response = self.client.post('/lists/new',data={'item_text': 'A new list item'})
new_list = List.objects.first()
self.assertRedirects(response, '/lists/%d/' % (new_list.id,))
def test_passes_correct_list_to_template(self):
other_list = List.objects.create()
correct_list = List.objects.create()
response = self.client.get('/lists/%d/' % (correct_list.id,))
self.assertEqual(response.context['list'], correct_list)
class NewItemTest(TestCase):
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(
'/lists/%d/add_item' % (correct_list.id,),
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('/lists/%d/add_item' % (correct_list.id,),data={'item_text': 'A new item for an existing list'})
self.assertRedirects(response, '/lists/%d/' % (correct_list.id,))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment