Fakultas Ilmu Komputer UI

Commit 1c599051 authored by Rahmania Astrid Mochtar's avatar Rahmania Astrid Mochtar
Browse files

Merge branch 'testinggoat/ch7' into 'master'

Testinggoat/ch7

See merge request !4
parents ee553e18 60be2e12
// This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment.
require('../../js/transition.js')
require('../../js/alert.js')
require('../../js/button.js')
require('../../js/carousel.js')
require('../../js/collapse.js')
require('../../js/dropdown.js')
require('../../js/modal.js')
require('../../js/tooltip.js')
require('../../js/popover.js')
require('../../js/scrollspy.js')
require('../../js/tab.js')
require('../../js/affix.js')
\ No newline at end of file
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>To-Do lists</title>
<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet/less" type="text/css" href="/static/base.less" />
<script src="//cdnjs.cloudflare.com/ajax/libs/less.js/3.9.0/less.min.js" ></script>
</head>
<body>
<h2>Rahmania Astrid Mochtar</h2>
<h4>1606828702</h4>
<h4>PMPL - B</h4>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 jumbotron">
<div class="text-center">
<h1>{% block header_text %}{% endblock %}</h1>
<form method="POST" action="{% block form_action %}{% endblock %}">
<input name="item_text" id="id_new_item"
class="form-control input-lg"
placeholder="Enter a to-do item" />
​{% csrf_token %}
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
​{% block table %}
​{% endblock %}
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
<html>
<title>To-Do lists</title>
<body>
<h2>Rahmania Astrid Mochtar</h2>
<h4>1606828702</h4>
<h4>PMPL - B</h4>
{% extends 'base.html' %}
<br/>
{% block header_text %}Start a new To-Do list{% endblock %}
<h1>To-Do List</h1>
<form method="POST">
<input id="id_new_item" placeholder="Enter a to-do item" name="item_text"/>
{% csrf_token %}
</form>
<table id="id_list_table">
{% if items %}
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
{% endif %}
</table>
{% if items|length == 0 %}
<h5>yey, waktunya berlibur</h5>
{% elif items|length < 5 %}
<h5>sibuk tapi santai</h5>
{% else %}
<h5>oh tidak</h5>
{% endif %}
</body>
</html>
\ No newline at end of file
{% block form_action %}/lists/new{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block header_text %}Your To-Do list{% endblock %}
{% block form_action %}/lists/{{ list.id }}/add_item{% endblock %}
{% block table %}
<table id="id_list_table" class="table">
{% for item in list.item_set.all %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
{% if list.item_set.all|length == 0 %}
<h5>yey, waktunya berlibur</h5>
{% elif list.item_set.all|length < 5 %}
<h5>sibuk tapi santai</h5>
{% else %}
<h5>oh tidak</h5>
{% endif %}
{% endblock %}
......@@ -4,11 +4,17 @@ from django.http import HttpRequest
from django.template.loader import render_to_string
from lists.views import home_page
from lists.models import Item
from lists.models import Item, List
import re
def create_items(num, list_=None):
for i in range(1, num+1):
text = 'itemey ' + str(i)
Item.objects.create(text=text, list=list_)
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
found = resolve('/')
......@@ -19,12 +25,6 @@ class HomePageTest(TestCase):
csrf_regex = r'<input[^>]+csrfmiddlewaretoken[^>]+>'
return re.sub(csrf_regex, '', html_code)
@staticmethod
def create_items(num):
for i in range(1, num+1):
text = 'itemey ' + str(i)
Item.objects.create(text=text)
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
......@@ -34,104 +34,120 @@ class HomePageTest(TestCase):
expected_html
)
def test_home_page_can_save_a_POST_request(self):
request = HttpRequest()
request.method = 'POST'
request.POST['item_text'] = 'A new list item'
# def test_home_page_can_save_a_POST_request(self):
# response = self.client.post('/', data={'item_text': 'A new list item'})
# self.assertRedirects(response, '/lists/the-only-list-in-the-world/')
response = home_page(request)
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
class ListAndItemModelsTest(TestCase):
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
def test_home_page_redirects_after_POST(self):
request = HttpRequest()
request.method = 'POST'
request.POST['item_text'] = 'A new list item'
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.list = list_
first_item.save()
response = home_page(request)
second_item = Item()
second_item.text = 'Item the second'
second_item.list = list_
second_item.save()
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
def test_home_page_only_saves_items_when_necessary(self):
request = HttpRequest()
home_page(request)
self.assertEqual(Item.objects.count(), 0)
saved_items = Item.objects.all()
self.assertEqual(saved_items.count(), 2)
def test_home_page_displays_all_list_items(self):
self.create_items(2)
# Item.objects.create(text='itemey 1')
# Item.objects.create(text='itemey 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(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, 'Item the second')
self.assertEqual(second_saved_item.list, list_)
request = HttpRequest()
response = home_page(request)
self.assertIn(
'itemey 1',
response.content.decode()
)
self.assertIn(
'itemey 2',
response.content.decode()
)
class ListViewTest(TestCase):
def test_uses_list_template(self):
list_ = List.objects.create()
response = self.client.get(f'/lists/{list_.id}/')
self.assertTemplateUsed(response, 'list.html')
def test_automated_comment_if_list_empty(self):
request = HttpRequest()
response = home_page(request)
def test_displays_only_items_for_that_list(self):
correct_list = List.objects.create()
create_items(2, correct_list)
self.assertIn(
'yey, waktunya berlibur',
response.content.decode()
)
other_list = List.objects.create()
Item.objects.create(text='other list item 1', list=other_list)
Item.objects.create(text='other list item 2', list=other_list)
def test_automated_comment_if_list_less_than_5(self):
self.create_items(4)
# Item.objects.create(text='itemey 1')
# Item.objects.create(text='itemey 2')
# Item.objects.create(text='itemey 3')
# Item.objects.create(text='itemey 4')
response = self.client.get(f'/lists/{correct_list.id}/')
request = HttpRequest()
response = home_page(request)
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
self.assertNotContains(response, 'other list item 1')
self.assertNotContains(response, 'other list item 2')
self.assertIn(
'sibuk tapi santai',
response.content.decode()
)
def test_passes_correct_list_to_template(self):
other_list = List.objects.create()
correct_list = List.objects.create()
response = self.client.get(f'/lists/{correct_list.id}/')
self.assertEqual(response.context['list'], correct_list)
def test_automated_comment_if_list_5_or_more(self):
self.create_items(5)
# Item.objects.create(text='itemey 1')
# Item.objects.create(text='itemey 2')
# Item.objects.create(text='itemey 3')
# Item.objects.create(text='itemey 4')
# Item.objects.create(text='itemey 5')
# def test_automated_comment_if_list_empty(self):
# response = self.client.get('/lists/the-only-list-in-the-world/')
request = HttpRequest()
response = home_page(request)
# self.assertIn(
# 'yey, waktunya berlibur',
# response.content.decode()
# )
self.assertIn(
'oh tidak',
response.content.decode()
)
# def test_automated_comment_if_list_less_than_5(self):
# list_ = List.objects.create()
# create_items(2, list_)
# response = self.client.get('/lists/the-only-list-in-the-world/')
class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.save()
# self.assertIn(
# 'sibuk tapi santai',
# response.content.decode()
# )
second_item = Item()
second_item.text = 'Item the second'
second_item.save()
# def test_automated_comment_if_list_5_or_more(self):
# list_ = List.objects.create()
# create_items(5, list_)
saved_items = Item.objects.all()
self.assertEqual(saved_items.count(), 2)
# response = self.client.get('/lists/the-only-list-in-the-world/')
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')
# self.assertIn(
# 'oh tidak',
# response.content.decode()
# )
class NewListTest(TestCase):
def test_can_save_a_POST_request_to_an_existing_list(self):
other_list = List.objects.create()
correct_list = List.objects.create()
self.client.post(
f'/lists/{correct_list.id}/add_item',
data={'item_text': 'A new item for an existing list'}
)
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new item for an existing list')
self.assertEqual(new_item.list, correct_list)
def test_redirects_to_list_view(self):
other_list = List.objects.create()
correct_list = List.objects.create()
response = self.client.post(
f'/lists/{correct_list.id}/add_item',
data={'item_text': 'A new item for an existing list'}
)
self.assertRedirects(response, f'/lists/{correct_list.id}/')
"""superlists URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
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.conf.urls import url
from lists import views
urlpatterns = [
url(r'^new$', views.new_list, name='new_list'),
url(r'^(\d+)/$', views.view_list, name='view_list'),
url(r'^(\d+)/add_item$', views.add_item, name='add_item'),
]
from django.http import HttpResponse
from django.shortcuts import render, redirect
from lists.models import Item
from lists.models import Item, List
def home_page(request):
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/')
return render(request, 'home.html')
items = Item.objects.all()
return render(request, 'home.html', {'items': items})
def view_list(request, list_id):
list_ = List.objects.get(id=list_id)
return render(request, 'list.html', {'list': list_})
def new_list(request):
list_ = List.objects.create()
Item.objects.create(text=request.POST['item_text'], list=list_)
return redirect(f'/lists/{list_.id}/')
def add_item(request, list_id):
list_ = List.objects.get(id=list_id)
Item.objects.create(text=request.POST['item_text'], list=list_)
return redirect(f'/lists/{list_.id}/')
......@@ -34,7 +34,7 @@ ALLOWED_HOSTS = [
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
# 'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
......@@ -122,3 +122,4 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
......@@ -13,11 +13,11 @@ 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.conf.urls import url
from lists import views
from django.conf.urls import url, include
from lists import views, urls as list_urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home_page, name='home')
# url(r'^admin/', admin.site.urls),
url(r'^$', views.home_page, name='home'),
url(r'^lists/', include(list_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