Fakultas Ilmu Komputer UI

Commit b462d752 authored by Yumna Pratista's avatar Yumna Pratista
Browse files

Merge branch 'testinggoat/ch12' into HEAD

parents 81a8d5e2 9d351e3f
Pipeline #23307 passed with stages
in 7 minutes and 13 seconds
# Generated by Django 2.2.1 on 2019-10-15 07:40
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('app_homepage', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='item',
old_name='list',
new_name='list1',
),
]
# Generated by Django 2.2.1 on 2019-10-15 07:41
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('app_homepage', '0002_auto_20191015_1440'),
]
operations = [
migrations.RenameField(
model_name='item',
old_name='list1',
new_name='list',
),
]
......@@ -5,9 +5,13 @@
{% 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>
{% endblock %}
\ No newline at end of file
{% if list %}
<table id="id_list_table" class="table">
{% for item in list.item_set.all %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
{% else %}
<h3 id="id_list_table">You can't have an empty list item</h3>
{% endif %}
{% endblock %}
......@@ -8,6 +8,7 @@
<title>To-Do lists</title>
<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/static/base.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
......@@ -15,11 +16,12 @@
<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 %}">
<h1 class="header_text">{% block header_text %}{% endblock %}</h1>
<form id="new_item_form" class="form_baru" 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" />
<div class="has-error" id="has-error"></div>
​{% csrf_token %}
</form>
</div>
......@@ -27,11 +29,31 @@
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-md-6 col-md-offset-3 has-error">
​{% block table %}
​{% endblock %}
</div>
</div>
</div>
</body>
<script>
$(document).ready(function() {
var input = document.getElementById("id_new_item");
input.addEventListener("keypress", function (e) {
if (e.keyCode === 13) { // 13 is enter
// code for enter
var isi_input = input.value;
e.preventDefault();
if(isi_input === ""){
document.getElementsByClassName("has-error")[0].innerHTML = "You can't have an empty list item";
document.getElementsByClassName("has-error")[0].style.display = "block";
console.log(document.getElementsByClassName("has-error")[0].value);
} else{
document.getElementById("new_item_form").submit();
}
};
});
});
</script>
</html>
\ No newline at end of file
from django.test import TestCase
from django.urls import resolve
from django.http import HttpRequest
from app_homepage.views import home_page
from app_homepage.models import Item, List
from django.template.loader import render_to_string
# Create your tests here.
class HomePageTest(TestCase):
def test_uses_home_template(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'app/home.html')
def test_can_save_a_POST_request(self):
response = self.client.post('/lists/new', 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('/lists/new', data={'item_text': 'A new list item'})
# self.assertRedirects(response, '/lists/the-only-list-in-the-world/')
# def test_if_item_smaller_than_5(self):
# response = self.client.get('/')
# self.assertIn('yey, waktunya libur', response.content.decode())
# def test_if_item_smaller_than_5(self):
# Item.objects.create(text='itemey 1')
# Item.objects.create(text='itemey 2')
# response = self.client.get('/')
# self.assertIn('sibuk tapi santai', response.content.decode())
# def test_if_item_more_than_5(self):
# 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')
# Item.objects.create(text='itemey 6')
# response = self.client.get('/')
# self.assertIn('oh tidak', response.content.decode())
class ListAndItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.list = list_
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.list = list_
second_item.save()
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
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(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, 'Item the second')
self.assertEqual(second_saved_item.list, list_)
class ListViewTest(TestCase):
def test_uses_list_template(self):
list_ = List.objects.create()
response = self.client.get(f'/lists/{list_.id}/')
self.assertTemplateUsed(response, 'app/list.html')
def test_displays_only_items_for_that_list(self):
correct_list = List.objects.create()
Item.objects.create(text='itemey 1', list=correct_list)
Item.objects.create(text='itemey 2', list=correct_list)
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)
response = self.client.get(f'/lists/{correct_list.id}/')
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
self.assertNotContains(response, 'other list item 1')
self.assertNotContains(response, 'other list item 2')
class NewListTest(TestCase):
def test_can_save_a_POST_request(self):
self.client.post('/lists/new', 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('/lists/new', data={'item_text': 'A new list item'})
new_list = List.objects.first()
self.assertRedirects(response, f'/lists/{new_list.id}/')
class NewItemTest(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}/')
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)
\ No newline at end of file
from django.test import TestCase
from app_homepage.models import Item, List
# Create your tests here.
class ListAndItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.list = list_
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.list = list_
second_item.save()
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
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(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, 'Item the second')
self.assertEqual(second_saved_item.list, list_)
from django.test import TestCase
from django.urls import resolve
from django.http import HttpRequest
from app_homepage.views import home_page
from app_homepage.models import Item, List
from django.template.loader import render_to_string
# Create your tests here.
class HomePageTest(TestCase):
def test_uses_home_template(self):
response = self.client.get('/')
self.assertTemplateUsed(response, 'app/home.html')
def test_can_save_a_POST_request(self):
response = self.client.post('/lists/new', 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('/lists/new', data={'item_text': 'A new list item'})
# self.assertRedirects(response, '/lists/the-only-list-in-the-world/')
# def test_if_item_smaller_than_5(self):
# response = self.client.get('/')
# self.assertIn('yey, waktunya libur', response.content.decode())
# def test_if_item_smaller_than_5(self):
# Item.objects.create(text='itemey 1')
# Item.objects.create(text='itemey 2')
# response = self.client.get('/')
# self.assertIn('sibuk tapi santai', response.content.decode())
# def test_if_item_more_than_5(self):
# 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')
# Item.objects.create(text='itemey 6')
# response = self.client.get('/')
# self.assertIn('oh tidak', 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, 'app/list.html')
def test_displays_only_items_for_that_list(self):
correct_list = List.objects.create()
Item.objects.create(text='itemey 1', list=correct_list)
Item.objects.create(text='itemey 2', list=correct_list)
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)
response = self.client.get(f'/lists/{correct_list.id}/')
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
self.assertNotContains(response, 'other list item 1')
self.assertNotContains(response, 'other list item 2')
class NewListTest(TestCase):
def test_can_save_a_POST_request(self):
self.client.post('/lists/new', 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('/lists/new', data={'item_text': 'A new list item'})
new_list = List.objects.first()
self.assertRedirects(response, f'/lists/{new_list.id}/')
class NewItemTest(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}/')
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)
\ No newline at end of file
from django.test import TestCase
from django.urls import resolve
from django.http import HttpRequest
from .views import home_page
from .models import Item, List
from app_homepage.views import home_page
from app_homepage.models import Item, List
from django.template.loader import render_to_string
# Create your tests here.
......
File deleted
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