Fakultas Ilmu Komputer UI

Commit 87d1b323 authored by Muhammad Ilham Peruzzi's avatar Muhammad Ilham Peruzzi
Browse files

new URL, view and template to display lists

parent 17203989
......@@ -33,6 +33,11 @@ class NewVisitorTest(LiveServerTestCase):
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')
rows = table.find_elements_by_tag_name('tr')
self.assertIn(row_text, [row.text for row in rows])
def test_can_start_a_list_and_retrieve_it_later(self):
self.browser.get(self.live_server_url)
......@@ -60,4 +65,38 @@ class NewVisitorTest(LiveServerTestCase):
edith_list_url = self.browser.current_url
self.assertRegex(edith_list_url, '/lists/.+')
self.check_for_row_in_list_table('1: Buy peacock feathers')
\ No newline at end of file
self.check_for_row_in_list_table('1: Buy peacock feathers')
# Now a new user, Francis, comes along to the site.
## We use a new browser session to make sure that no information
## of Edith's is coming through from cookies etc #
self.browser.quit()
chrome_options = Options()
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('disable-gpu')
try:
self.browser = webdriver.Chrome('./chromedriver.exe', chrome_options=chrome_options)
except:
self.browser = webdriver.Chrome('./chromedriver', chrome_options=chrome_options)
# Francis visits the home page. There is no sign of Edith's
# list
self.browser.get(self.live_server_url)
page_text = self.browser.find_element_by_tag_name('body').text
self.assertNotIn('Buy peacock feathers', page_text)
self.assertNotIn('make a fly', page_text)
# Francis starts a new list by entering a new item. He
# is less interesting than Edith...
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Buy milk')
inputbox.send_keys(Keys.ENTER)
# Francis gets his own unique URL
francis_list_url = self.browser.current_url
self.assertRegex(francis_list_url, '/lists/.+')
self.assertNotEqual(francis_list_url, edith_list_url)
# Again, there is no trace of Edith's list
page_text = self.browser.find_element_by_tag_name('body').text
self.assertNotIn('Buy peacock feathers', page_text)
self.assertIn('Buy milk', page_text)
# Satisfied, they both go back to sleep
\ No newline at end of file
......@@ -11,24 +11,10 @@
<p>{{bio}}</p>
</div>
<hr>
<h1>Your To-Do list</h1>
<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>
{% if items|length == 0 %}
<p>yey, waktunya berlibur</p>
{% elif items|length < 5 %}
<p>sibuk tapi santai</p>
{% else %}
<p>oh tidak</p>
{% endif %}
<table id="id_list_table">
{% for item in items %}
<tr>
<td>{{ forloop.counter }}: {{ item.text }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title>To-Do lists</title>
</head>
<body>
<h2>Homepage</h2>
<hr>
<div id="nama_lengkap">Muhammad Ilham Peruzzi</div>
<h5>Bio</h5>
<div>
<p>{{bio}}</p>
</div>
<hr>
<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>
{% if items|length == 0 %}
<p>yey, waktunya berlibur</p>
{% elif items|length < 5 %}
<p>sibuk tapi santai</p>
{% else %}
<p>oh tidak</p>
{% endif %}
<table id="id_list_table">
{% for item in items %}
<tr>
<td>{{ forloop.counter }}: {{ item.text }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
\ No newline at end of file
......@@ -4,7 +4,7 @@ from django.http import HttpRequest
from django.template.loader import render_to_string
from lists.models import Item
from lists.views import home_page, about_me
from lists.views import home_page, about_me, view_list
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
......@@ -66,7 +66,7 @@ class HomePageTest(TestCase):
self.assertEqual(new_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_home_page_only_saves_items_when_necessary(self):
request = HttpRequest()
......@@ -79,20 +79,12 @@ class HomePageTest(TestCase):
request.POST['item_text'] = 'A new list item'
response = home_page(request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
def test_home_page_displays_all_list_items(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
request = HttpRequest()
response = home_page(request)
self.assertIn('itemey 1', response.content.decode())
self.assertIn('itemey 2', response.content.decode())
self.assertEqual(response['location'], '/lists/the-only-list-in-the-world/')
def test_automatic_comment_when_no_items(self):
self.assertEqual(Item.objects.count(), 0)
request = HttpRequest()
response = home_page(request)
response = view_list(request)
self.assertIn('yey, waktunya berlibur', response.content.decode())
def test_automatic_comment_when_to_do_have_less_five_items(self):
......@@ -100,7 +92,7 @@ class HomePageTest(TestCase):
Item.objects.create(text='Activity 2')
self.assertEqual(Item.objects.count(), 2)
request = HttpRequest()
response = home_page(request)
response = view_list(request)
self.assertIn('Activity 1', response.content.decode())
self.assertIn('sibuk tapi santai', response.content.decode())
......@@ -113,6 +105,19 @@ class HomePageTest(TestCase):
Item.objects.create(text='Activity 6')
self.assertEqual(Item.objects.count(), 6)
request = HttpRequest()
response = home_page(request)
response = view_list(request)
self.assertIn('Activity 6', response.content.decode())
self.assertIn('oh tidak', response.content.decode())
\ No newline at end of file
self.assertIn('oh tidak', response.content.decode())
class ListViewTest(TestCase):
def test_uses_list_template(self):
response = self.client.get('/lists/the-only-list-in-the-world/')
self.assertTemplateUsed(response, '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')
\ No newline at end of file
......@@ -10,13 +10,16 @@ def home_page(request):
response["bio"] = about_me()
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/')
items = Item.objects.all()
return redirect('/lists/the-only-list-in-the-world/')
return render(request, html, {'bio':about_me(),'items': items})
return render(request, html, {'bio':about_me()})
def about_me():
return "Hello. My name is Muhammad Ilham Peruzzi. \
I am now study at Faculty of Computer Science, University of Indonesia, since 2016.\
I like to read books and listening the music. Now I am learning about how to make a \
test in Software Quality Assurance class and this is my first practice."
\ No newline at end of file
test in Software Quality Assurance class and this is my first practice."
def view_list(request):
items = Item.objects.all()
return render(request, 'list.html', {'items': items})
\ No newline at end of file
......@@ -19,5 +19,6 @@ from lists import views
urlpatterns = [
url(r'^$', views.home_page , name='home'),
url(r'^lists/the-only-list-in-the-world/$', views.view_list,name='view_list'),
#url(r'^admin/', admin.site.urls),
]
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