Fakultas Ilmu Komputer UI

Commit 908534e9 authored by Syahrul Findi's avatar Syahrul Findi
Browse files

Merge branch 'latihan2' into 'master'

Latihan2

See merge request !2
parents f27cb1fb 7b7d2bfc
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
import time
class NewVisitorTest(unittest.TestCase):
......@@ -9,6 +11,11 @@ class NewVisitorTest(unittest.TestCase):
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):
# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
......@@ -16,6 +23,46 @@ class NewVisitorTest(unittest.TestCase):
# She notices the page title and header mention to-do lists
self.assertIn('To-Do', self.browser.title)
header3_text = self.browser.find_element_by_tag_name('h3').text
self.assertIn('To-Do', header3_text)
# She notices the page header mention your name
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('Syahrul Findi Ardiansyah', 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'
)
# 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 table
inputbox.send_keys(Keys.ENTER)
time.sleep(1)
self.check_for_row_in_list_table('1: Buy peacock feathers')
# 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)
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)
# The page updates again, and now shows both items on her list
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')
# 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.
self.fail('Finish the test!')
......
......@@ -38,6 +38,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'latihan2',
]
MIDDLEWARE = [
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-09-24 03:17
from __future__ import unicode_literals
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')),
],
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-09-24 03:18
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('latihan2', '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='')
<html>
<head>
<title>To-Do lists</title>
</head>
<body>
<h1>Syahrul Findi Ardiansyah</h1>
<h2>1506757352</h2>
<h3>My To-Do Lists</h3>
<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>
from django.urls import resolve
from django.test import TestCase
from django.http import HttpRequest
from latihan2.views import home_page
from latihan2.models import Item
class HomePageTest(TestCase):
......@@ -10,11 +12,56 @@ class HomePageTest(TestCase):
self.assertEqual(found.func, home_page)
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
response = self.client.get('/')
html = response.content.decode('utf8')
self.assertTrue(html.startswith('<html>'))
self.assertIn('<title>To-Do lists</title>', html)
self.assertIn('<h1>Syahrul Findi Ardiansyah</h1>', html)
self.assertIn('<h2>1506757352</h2>', html)
self.assertTrue(html.endswith('</html>'))
self.assertIn('<h3>My To-Do Lists</h3>', html)
self.assertTrue(html.strip().endswith('</html>'))
self.assertTemplateUsed(response, 'home.html')
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.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
def test_redirect_after_POST(self):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
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('/')
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')
from django.shortcuts import render
from django.shortcuts import redirect, render
from django.http import HttpResponse
from latihan2.models import Item
def home_page(request):
return HttpResponse('<html><title>To-Do lists</title><h1>Syahrul Findi Ardiansyah</h1><h2>1506757352</h2></html>')
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/')
items = Item.objects.all()
return render(request, 'home.html', {'items': items})
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