Fakultas Ilmu Komputer UI

Commit e7085434 authored by emil farisan's avatar emil farisan
Browse files

fix a few errors and apply linter

Functional test now checks we can input a to-do item

Refactor home page view to use a template

Front page HTML now generated from a template

parent 11e596ad
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
import time
class NewVisitorTest(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
......@@ -15,25 +16,43 @@ class NewVisitorTest(unittest.TestCase):
# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
# She notices the page title and header mention to-do lists
self.assertIn('To-Do', self.browser.title)
self.fail('Finish the test!')
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-Do', header_text)
# She is invited to enter a to-do item straight away
inputbox = self.browser.find_element_by_id('id_new_item')
# She types "Buy peacock feathers" into a text box (Edith's hobby
# is tying fly-fishing lures)
inputbox.send_keys('Buy peacock feathers')
# When she hits enter, the page updates, and now the page lists
# "1: Buy peacock feathers" as an item in a to-do list
# "1: Buy peacock feathers" as an item in a to-do list table
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
'2: Use peacock feathers to make a fly',
[row.text for row in rows]
# There is still a text box inviting her to add another item. She
# enters "Use peacock feathers to make a fly"
# (Edith is very methodical)
# enters "Use peacock feathers to make a fly" (Edith is very
# methodical)
self.fail('Finish the test!')
# The page updates again, and now shows both items on her list
# Edith wonders whether the site will remember her list. Then she sees
# that the site has generated a unique URL for her -- there is some
# explanatory text to that effect.
# She visits that URL - her to-do list is still there.
# Satisfied, she goes back to sleep
if __name__ == '__main__':
if __name__ == '__main__':
<title>To-Do lists</title>
<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 %}
<table id="id_list_table">
<tr><td>1: {{ new_item_text }}</td></tr>
\ No newline at end of file
from django.urls import resolve
from django.test import TestCase
from lists.views import home_page
from django.http import HttpRequest
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = resolve('/')
self.assertEqual(found.func, home_page)
def test_uses_home_template(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'home.html')
def test_can_save_a_POST_request(self):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
self.assertIn(b'<title>To-Do lists</title>', response.content)
response = self.client.get('/')
html = response.content.decode('utf8')
self.assertIn('<title>To-Do lists</title>', html)
self.assertTemplateUsed(response, 'home.html')
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_page(request):
return HttpResponse('<html><title>To-Do lists</title></html>')
return render(request, 'home.html', {
'new_item_text': request.POST.get('item_text', ''),
......@@ -37,6 +37,7 @@ INSTALLED_APPS = [
......@@ -54,7 +55,7 @@ ROOT_URLCONF = 'superlists.urls'
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'context_processors': [
......@@ -13,10 +13,9 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from lists import views
urlpatterns = [
path('$', 'lists.views.home')
# path('admin/', admin.site.urls),
url(r'^$', views.home_page, name='home'),
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