Fakultas Ilmu Komputer UI

Commit 7154a25d authored by Yumna Pratista's avatar Yumna Pratista
Browse files

new url, beberapa modifikasi biar sesuai buku

parent 22194bf0
<!DOCTYPE html>
<html>
<head>
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
<table id="id_list_table">
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
<h2>{{ state }}</h2>
</body>
</html>
\ No newline at end of file
<body>
<h1>Start a new To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
</body>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST" action="/">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
<table id="id_list_table">
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
<h2>{{ state }}</h2>
</body>
</html>
\ No newline at end of file
......@@ -21,40 +21,31 @@ class HomePageTest(TestCase):
def test_redirects_after_POST(self):
response = self.client.post('/', data={'item_text':'A new list item'})
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
self.assertEqual(response['location'], '/lists/the-only-list-in-the-world/')
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('/')
# def test_if_item_smaller_than_5(self):
# response = self.client.get('/')
# self.assertIn('yey, waktunya libur', response.content.decode())
self.assertIn('itemey 1', response.content.decode())
self.assertIn('itemey 2', response.content.decode())
# def test_if_item_smaller_than_5(self):
# Item.objects.create(text='itemey 1')
# Item.objects.create(text='itemey 2')
# response = self.client.get('/')
# self.assertIn('sibuk tapi santai', response.content.decode())
def test_if_item_smaller_than_5(self):
response = self.client.get('/')
self.assertIn('yey, waktunya libur', response.content.decode())
def test_if_item_smaller_than_5(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
response = self.client.get('/')
self.assertIn('sibuk tapi santai', response.content.decode())
def test_if_item_more_than_5(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
Item.objects.create(text='itemey 3')
Item.objects.create(text='itemey 4')
Item.objects.create(text='itemey 5')
Item.objects.create(text='itemey 6')
response = self.client.get('/')
self.assertIn('oh tidak', response.content.decode())
# def test_if_item_more_than_5(self):
# Item.objects.create(text='itemey 1')
# Item.objects.create(text='itemey 2')
# Item.objects.create(text='itemey 3')
# Item.objects.create(text='itemey 4')
# Item.objects.create(text='itemey 5')
# Item.objects.create(text='itemey 6')
# response = self.client.get('/')
# self.assertIn('oh tidak', response.content.decode())
class ItemModelTest(TestCase):
......@@ -75,3 +66,19 @@ class ItemModelTest(TestCase):
second_saved_item = saved_items[1]
self.assertEqual(first_saved_item.text, 'The first (ever) list item')
self.assertEqual(second_saved_item.text, 'Item the second')
class ListViewTest(TestCase):
def test_uses_list_template(self):
response = self.client.get('/lists/the-only-list-in-the-world/')
self.assertTemplateUsed(response, 'app/list.html')
def test_displays_all_items(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
response = self.client.get('/lists/the-only-list-in-the-world/')
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
......@@ -4,17 +4,19 @@ from .models import Item
# Create your views here.
def home_page(request):
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/')
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/lists/the-only-list-in-the-world/')
return render(request, 'app/home.html')
# state = ""
# if(Item.objects.count() == 0):
# state = "yey, waktunya libur"
# elif(Item.objects.count() < 5):
# state = "sibuk tapi santai"
# else:
# state = "oh tidak"
# return render(request, 'app/home.html', {'items':items, 'state':state})
items = Item.objects.all()
state = ""
if(Item.objects.count() == 0):
state = "yey, waktunya libur"
elif(Item.objects.count() < 5):
state = "sibuk tapi santai"
else:
state = "oh tidak"
return render(request, 'app/home.html', {'items':items, 'state':state})
def view_list(request):
items = Item.objects.all()
return render(request, 'app/list.html', {'items': items})
\ No newline at end of file
from selenium import webdriver
import unittest
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
from django.test import LiveServerTestCase
from selenium.webdriver.chrome.options import Options
from app_homepage.models import Item
import time
MAX_WAIT = 10
......@@ -19,13 +19,13 @@ class NewVisitorTest(LiveServerTestCase):
# self.browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
self.browser = webdriver.Firefox()
# self.browser.implicitly_wait(20)
def tearDown(self):
self.browser.quit()
def check_for_row_in_list_table(self, row_text):
table = self.browser.find_element_by_id('id_list_table')
time_sleep(5)
rows = table.find_elements_by_tag_name('tr')
self.assertIn(row_text, [row_text for row in rows])
......@@ -81,8 +81,9 @@ class NewVisitorTest(LiveServerTestCase):
# Edith has heard about a cool new online to-do app. She goes
# The page updates again, and now shows both items on her list
self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly')
self.wait_for_row_in_list_table('1: Buy peacock feathers')
self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly')
# Satisfied, she goes back to sleep
......@@ -93,6 +94,7 @@ class NewVisitorTest(LiveServerTestCase):
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Buy peacock feathers')
inputbox.send_keys(Keys.ENTER)
time.sleep(5)
self.wait_for_row_in_list_table('1: Buy peacock feathers')
# She notices that her list has a unique URL
......@@ -118,6 +120,7 @@ class NewVisitorTest(LiveServerTestCase):
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Buy milk')
inputbox.send_keys(Keys.ENTER)
time.sleep(5)
self.wait_for_row_in_list_table('1: Buy milk')
# Francis gets his own unique URL
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -15,9 +15,10 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from app_homepage import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home_page)
]
url(r'^$', views.home_page, name='home'),
url(r'^lists/the-only-list-in-the-world/$', views.view_list, name='view_list'),
]
\ No newline at end of file
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