Fakultas Ilmu Komputer UI

Commit c179133a authored by Kevin Albert Simanjuntak's avatar Kevin Albert Simanjuntak
Browse files

Merge branch 'excercise-2' into 'master'

Excercise 2

See merge request !2
parents 46bb5598 849a62fe
Pipeline #20776 passed with stage
in 2 minutes and 50 seconds
No preview for this file type
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest
class NewVisitorTest(unittest.TestCase):
......@@ -9,18 +11,35 @@ class NewVisitorTest(unittest.TestCase):
def tearDown(self):
self.browser.quit()
def test_can_start_a_list_and_retrieve_it_later(self):
# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
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])
# She is invited to enter a to-do item straight away
def test_can_start_a_list_and_retrieve_it_later(self):
self.browser.get('http://localhost:8000/homepage')
# She notices the page title and header mention to-do lists
body_text = self.browser.find_element_by_tag_name('body').text
self.assertIn('Tutorial PMPL', self.browser.title)
self.assertIn('Kevin Albert Simanjuntak', body_text)
self.assertIn('NPM', body_text)
self.assertIn('To-Do', self.browser.title)
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')
self.assertEqual(
inputbox.get_attribute('placeholder'),
'Enter a to-do item'
)
inputbox.send_keys('Buy peacock feathers')
inputbox.send_keys(Keys.ENTER)
time.sleep(5)
inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Use peacock feathers to make a fly')
inputbox.send_keys(Keys.ENTER)
time.sleep(5)
self.check_for_row_in_list_table('1: Buy peacock feathers')
self.check_for_row_in_list_table('2: Use peacock feathers to make a fly')
if __name__ == '__main__':
unittest.main(warnings='ignore')
\ No newline at end of file
# Generated by Django 2.0 on 2019-09-24 15:43
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Item',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
# Generated by Django 2.0 on 2019-09-24 15:46
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('lists', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='item',
name='text',
field=models.TextField(default=''),
),
]
from django.db import models
# Create your models here.
class Item(models.Model):
text = models.TextField(default='')
\ No newline at end of file
<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>
</body>
</html>
\ No newline at end of file
<html>
<head>
<meta charset="UTF-8">
<title>Tutorial PMPL</title>
</head>
<body>
<h1>Name: {{ name }}</h1>
<h2> NPM: {{npm}} </h2>
</body>
</html>
\ No newline at end of file
......@@ -3,21 +3,78 @@ from django.test import TestCase
from django.http import HttpRequest
from django.test import Client
from lists.views import home_page
from django.template.loader import render_to_string
from lists.models import Item
class HomePageTest(TestCase):
def test_home_page_returns_correct_html(self):
response = self.client.get('/homepage/')
html = response.content.decode('utf8')
self.assertTrue(html.startswith('<html>'))
self.assertIn('<title>To-Do lists</title>', html)
self.assertTrue(html.strip().endswith('</html>'))
self.assertTemplateUsed(response, 'home.html')
def test_home_page_is_exist(self):
response = Client().get('/homepage/')
self.assertEqual(response.status_code,200)
def test_root_url_resolves_to_home_page_view(self):
found = resolve('/homepage/')
self.assertEqual(found.func, home_page)
def test_uses_home_template(self):
response = self.client.get('/homepage/')
self.assertTemplateUsed(response, 'home.html')
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
html = response.content.decode('utf8')
self.assertTrue(html.startswith('<html>'))
self.assertIn('<title>Tutorial PMPL</title>', html)
self.assertIn('Kevin Albert Simanjuntak', html)
self.assertTrue(html.endswith('</html>'))
\ No newline at end of file
def test_can_save_a_POST_request(self):
response = self.client.post('/homepage/', data={'item_text': 'A new list item'})
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
def test_only_saves_items_when_necessary(self):
self.client.get('/homepage/')
self.assertEqual(Item.objects.count(), 0)
def test_can_save_a_POST_request(self):
response = self.client.post('/homepage/', 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')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/homepage/')
def test_displays_all_list_items(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
response = self.client.get('/homepage/')
self.assertIn('itemey 1', response.content.decode())
self.assertIn('itemey 2', response.content.decode())
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.save()
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(second_saved_item.text, 'Item the second')
def test_can_save_a_POST_request(self):
self.client.post('/homepage/', 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('/homepage/', data={'item_text': 'A new list item'})
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/homepage/')
\ No newline at end of file
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect, render
from lists.models import Item
# Create your views here.
name = "Kevin Albert Simanjuntak"
npm = "1606835595"
def home_page(request):
response = {"name" : name, "npm" : npm}
return render(request, 'homepage.html', response)
\ No newline at end of file
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/homepage/')
items = Item.objects.all()
return render(request, 'home.html', {'items': items})
\ No newline at end of file
diff --git a/functional_tests.py b/functional_tests.py
index c9ac63e..a438209 100644
--- a/functional_tests.py
+++ b/functional_tests.py
@@ -35,18 +35,21 @@ class NewVisitorTest(unittest.TestCase):

# When she hits enter, the page updates, and now the page lists
# "1: Buy peacock feathers" as an item in a to-do list table
- inputbox.send_keys(Keys.ENTER) 
- time.sleep(1) 

+ inputbox.send_keys(Keys.ENTER)
+ time.sleep(1)
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr') 
- self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),"New to-do item did not appear in table")
-
- # 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)
- self.fail('Finish the test!')
-
- # The page updates again, and now shows both items on her list 
+ #self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),f"New to-do item did not appear in table")
+ self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
+ inputbox = self.browser.find_element_by_id('id_new_item')
+ inputbox.send_keys('Use peacock feathers to make a fly')
+ inputbox.send_keys(Keys.ENTER)
+ time.sleep(1)
+ 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])
+ self.assertIn('2: Use peacock feathers to make a fly',[row.text for row in rows])
+ 
if __name__ == '__main__': 
unittest.main(warnings='ignore')
\ No newline at end of file
diff --git a/lists/__pycache__/tests.cpython-37.pyc b/lists/__pycache__/tests.cpython-37.pyc
index f371935..4b6e9b9 100644
Binary files a/lists/__pycache__/tests.cpython-37.pyc and b/lists/__pycache__/tests.cpython-37.pyc differ
diff --git a/lists/__pycache__/views.cpython-37.pyc b/lists/__pycache__/views.cpython-37.pyc
index 8a440d0..2bd5393 100644
Binary files a/lists/__pycache__/views.cpython-37.pyc and b/lists/__pycache__/views.cpython-37.pyc differ
diff --git a/lists/templates/home.html b/lists/templates/home.html
index 9a2a093..dd544fc 100644
--- a/lists/templates/home.html
+++ b/lists/templates/home.html
@@ -4,8 +4,12 @@
</head>
<body>
​<h1>Your To-Do list</h1>
- ​<input id="id_new_item" placeholder="Enter a to-do item" />
- ​<table id="id_list_table">
- ​</table>
- ​</body>
+ <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">
+  <tr><td>1: {{ new_item_text }}</td></tr>
+  </table>
+ </body>
</html>
\ No newline at end of file
diff --git a/lists/tests.py b/lists/tests.py
index 4e973ff..44473b0 100644
--- a/lists/tests.py
+++ b/lists/tests.py
@@ -20,4 +20,9 @@ class HomePageTest(TestCase):

def test_uses_home_template(self):
response = self.client.get('/homepage/')
+ self.assertTemplateUsed(response, 'home.html')
+
+ def test_can_save_a_POST_request(self):
+ response = self.client.post('/homepage/', data={'item_t
\ 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