Fakultas Ilmu Komputer UI

Commit 414ebf39 authored by Rayza Arasj Mahardhika's avatar Rayza Arasj Mahardhika
Browse files

Merge branch 'exercise-2' into 'master'

Exercise 2

See merge request !1
parents ee53307a 1475948e
Pipeline #21005 passed with stage
in 3 minutes and 56 seconds
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest
class NewVisitorTest(unittest.TestCase):
......@@ -10,15 +12,57 @@ class NewVisitorTest(unittest.TestCase):
def tearDown(self):
self.browser.quit()
def test_can_access_homepage(self):
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
self.browser.get('http://localhost:8000')
self.assertIn('Rayza\'s Homepage', self.browser.title)
# She notices the page title and header meantion to-do lists
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'
)
# She types "Buy peacock feathers" into a text box (Edhits' 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)
def test_can_access_about_page(self):
self.browser.get('http://localhost:8000/about')
# 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')
self.assertIn('Rayza\'s About Page', self.browser.title)
# 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__':
unittest.main(warnings='ignore')
# Generated by Django 2.2.5 on 2019-09-25 15:01
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.2.5 on 2019-09-25 15:02
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='')
<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>
......@@ -3,6 +3,7 @@ from django.test import TestCase
from django.http import HttpRequest
from lists.views import home_page, about_page
from lists.models import Item
class HomePageTest(TestCase):
......@@ -10,14 +11,35 @@ class HomePageTest(TestCase):
found = resolve('/')
self.assertEqual(found.func, home_page)
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>Rayza\'s Homepage</title>', html)
self.assertIn('<h1>Hello, my name is Rayza Arasj Mahardhika</h1>', html)
self.assertTrue(html.endswith('</html>'))
def test_uses_home_template(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'home.html')
def test_can_save_a_POST_request(self):
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_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'], '/')
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 AboutPageTest(TestCase):
......@@ -36,3 +58,22 @@ class AboutPageTest(TestCase):
self.assertIn('<p>I\'m from Bogor</p>', html)
self.assertIn('<p>I\'m currently studying Computer Science at Faculty of Computer Science, Universitas Indonesia</p>', html)
self.assertTrue(html.endswith('</html>'))
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.http import HttpResponse
from django.shortcuts import redirect, render
from lists.models import Item
def home_page(request):
return HttpResponse(
'<html>' +
'<title>Rayza\'s Homepage</title>' +
'<h1>Hello, my name is Rayza Arasj Mahardhika</h1>' +
'</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})
def about_page(request):
return HttpResponse(
......
......@@ -26,7 +26,8 @@ SECRET_KEY = '-c!mdiysslpd4fvwjvju&y3s93r_xiu2!&w(moz0byj3n)nx)7'
DEBUG = True
ALLOWED_HOSTS = [
'pmpl-rayza.herokuapp.com'
'pmpl-rayza.herokuapp.com',
'*'
]
......@@ -39,6 +40,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'lists',
]
MIDDLEWARE = [
......
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