Fakultas Ilmu Komputer UI

Commit 9e8404e4 authored by Rayza Arasj Mahardhika's avatar Rayza Arasj Mahardhika
Browse files

Final exam

parent b0c1210a
...@@ -37,3 +37,7 @@ Deployment: ...@@ -37,3 +37,7 @@ Deployment:
environment: environment:
name: production name: production
url: $HEROKU_APP_HOST url: $HEROKU_APP_HOST
only:
refs:
- master
from django.contrib import admin
# Register your models here.
from django.apps import apps
from django.test import TestCase
from accounts.apps import AccountsConfig
class AccountsConfigTest(TestCase):
def test_apps(self):
self.assertEqual(AccountsConfig.name, 'accounts')
self.assertEqual(apps.get_app_config('accounts').name, 'accounts')
\ No newline at end of file
import time
from .base import FunctionalTest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
class ItemCategoryTest(FunctionalTest):
def add_work(self):
self.browser.find_element_by_id('id_new_item').send_keys('Todo Work')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Work')
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
time.sleep(0.5)
def add_study(self):
self.browser.find_element_by_id('id_new_item').send_keys('Todo Study')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Study')
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
time.sleep(0.5)
def add_life(self):
self.browser.find_element_by_id('id_new_item').send_keys('Todo Life')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Life')
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
time.sleep(0.5)
def test_category(self):
self.browser.get(self.live_server_url)
self.browser.set_window_size(1024, 768)
self.add_work()
self.add_study()
self.add_life()
self.browser.find_element_by_id('id_all_category_button').click()
self.wait_for_row_in_list_table('1: Todo Work - Work')
self.wait_for_row_in_list_table('2: Todo Study - Study')
self.wait_for_row_in_list_table('3: Todo Life - Life')
self.browser.find_element_by_id('id_work_category_button').click()
self.wait_for_row_in_list_table('1: Todo Work - Work')
self.browser.find_element_by_id('id_study_category_button').click()
self.wait_for_row_in_list_table('1: Todo Study - Study')
self.browser.find_element_by_id('id_life_category_button').click()
self.wait_for_row_in_list_table('1: Todo Life - Life')
\ No newline at end of file
...@@ -13,7 +13,7 @@ class LayoutAndStylingTest(FunctionalTest): ...@@ -13,7 +13,7 @@ class LayoutAndStylingTest(FunctionalTest):
inputbox = self.browser.find_element_by_id('id_new_item') inputbox = self.browser.find_element_by_id('id_new_item')
self.assertAlmostEqual( self.assertAlmostEqual(
inputbox.location['x'] + inputbox.size['width'] / 2, inputbox.location['x'] + inputbox.size['width'] / 2,
512, 481,
delta=10 delta=10
) )
...@@ -21,10 +21,20 @@ class LayoutAndStylingTest(FunctionalTest): ...@@ -21,10 +21,20 @@ class LayoutAndStylingTest(FunctionalTest):
# centered there too # centered there too
inputbox.send_keys('testing') inputbox.send_keys('testing')
inputbox.send_keys(Keys.ENTER) inputbox.send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: testing') self.wait_for_row_in_list_table('1: testing - Work')
inputbox = self.browser.find_element_by_id('id_new_item') inputbox = self.browser.find_element_by_id('id_new_item')
self.assertAlmostEqual( self.assertAlmostEqual(
inputbox.location['x'] + inputbox.size['width'] / 2, inputbox.location['x'] + inputbox.size['width'] / 2,
512, 481,
delta=10 delta=10
) )
def test_name_in_header_and_footer(self):
self.browser.get(self.live_server_url)
header = self.browser.find_element_by_id('header').text
footer = self.browser.find_element_by_id('footer').text
name = 'Rayza Arasj Mahardhika'
self.assertIn(name, header)
self.assertIn(name, footer)
from .base import FunctionalTest from .base import FunctionalTest
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from unittest import skip from unittest import skip
...@@ -20,8 +21,10 @@ class ItemValidationTest(FunctionalTest): ...@@ -20,8 +21,10 @@ class ItemValidationTest(FunctionalTest):
# She tries again with some text for the item, which now works # She tries again with some text for the item, which now works
self.browser.find_element_by_id('id_new_item').send_keys('Buy milk') self.browser.find_element_by_id('id_new_item').send_keys('Buy milk')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Life')
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER) self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: Buy milk') self.wait_for_row_in_list_table('1: Buy milk - Life')
# Perversely, she now decides to submit a scond blank list item # Perversely, she now decides to submit a scond blank list item
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER) self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
...@@ -34,6 +37,8 @@ class ItemValidationTest(FunctionalTest): ...@@ -34,6 +37,8 @@ class ItemValidationTest(FunctionalTest):
# And she can correct it by filling some text in # And she can correct it by filling some text in
self.browser.find_element_by_id('id_new_item').send_keys('Make tea') self.browser.find_element_by_id('id_new_item').send_keys('Make tea')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Life')
self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER) self.browser.find_element_by_id('id_new_item').send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: Buy milk') self.wait_for_row_in_list_table('1: Buy milk - Life')
self.wait_for_row_in_list_table('2: Make tea') self.wait_for_row_in_list_table('2: Make tea - Life')
from .base import FunctionalTest from .base import FunctionalTest
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time import time
...@@ -26,24 +27,28 @@ class NewVisitorTest(FunctionalTest): ...@@ -26,24 +27,28 @@ class NewVisitorTest(FunctionalTest):
# She types "Buy peacock feathers" into a text box (Edhits' hobby # She types "Buy peacock feathers" into a text box (Edhits' hobby
# is tying fly-fishing lures) # is tying fly-fishing lures)
inputbox.send_keys('Buy peacock feathers') inputbox.send_keys('Buy peacock feathers')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Life')
# When she hits enter, the page updates, and now the page lists # 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 # "1: Buy peacock feathers" as an item in a to-do list table
inputbox.send_keys(Keys.ENTER) inputbox.send_keys(Keys.ENTER)
time.sleep(1) time.sleep(1)
self.wait_for_row_in_list_table('1: Buy peacock feathers') self.wait_for_row_in_list_table('1: Buy peacock feathers - Life')
# There is still a text box inviting her to add another item. She # There is still a text box inviting her to add another item. She
# enters "Use peacock feathers to make a fly" (Edith is very # enters "Use peacock feathers to make a fly" (Edith is very
# methodical) # methodical)
inputbox = self.browser.find_element_by_id('id_new_item') inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Use peacock feathers to make a fly') inputbox.send_keys('Use peacock feathers to make a fly')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Life')
inputbox.send_keys(Keys.ENTER) inputbox.send_keys(Keys.ENTER)
time.sleep(1) time.sleep(1)
# The page updates again, and now shows both items on her list # The page updates again, and now shows both items on her list
self.wait_for_row_in_list_table('1: Buy peacock feathers') self.wait_for_row_in_list_table('1: Buy peacock feathers - Life')
self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly') self.wait_for_row_in_list_table('2: Use peacock feathers to make a fly - Life')
# Edith wonders whether the site will remember her list. Then she sees # 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 # that the site has generated a unique URL for her -- there is some
...@@ -58,8 +63,10 @@ class NewVisitorTest(FunctionalTest): ...@@ -58,8 +63,10 @@ class NewVisitorTest(FunctionalTest):
self.browser.get(self.live_server_url) self.browser.get(self.live_server_url)
inputbox = self.browser.find_element_by_id('id_new_item') inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Buy peacock feathers') inputbox.send_keys('Buy peacock feathers')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Life')
inputbox.send_keys(Keys.ENTER) inputbox.send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: Buy peacock feathers') self.wait_for_row_in_list_table('1: Buy peacock feathers - Life')
# She notices that her list has a unique URL # She notices that her list has a unique URL
edith_list_url = self.browser.current_url edith_list_url = self.browser.current_url
...@@ -83,8 +90,10 @@ class NewVisitorTest(FunctionalTest): ...@@ -83,8 +90,10 @@ class NewVisitorTest(FunctionalTest):
# is less interesting than Edith... # is less interesting than Edith...
inputbox = self.browser.find_element_by_id('id_new_item') inputbox = self.browser.find_element_by_id('id_new_item')
inputbox.send_keys('Buy milk') inputbox.send_keys('Buy milk')
select_element = Select(self.browser.find_element_by_id('id_item_category'))
select_element.select_by_value('Life')
inputbox.send_keys(Keys.ENTER) inputbox.send_keys(Keys.ENTER)
self.wait_for_row_in_list_table('1: Buy milk') self.wait_for_row_in_list_table('1: Buy milk - Life')
# Francis gets his own unique URL # Francis gets his own unique URL
francis_list_url = self.browser.current_url francis_list_url = self.browser.current_url
......
from django.contrib import admin
# Register your models here.
# Generated by Django 2.2.5 on 2019-12-23 12:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('lists', '0004_item_list'),
]
operations = [
migrations.AddField(
model_name='item',
name='category',
field=models.TextField(default=''),
),
]
...@@ -9,5 +9,6 @@ class List(models.Model): ...@@ -9,5 +9,6 @@ class List(models.Model):
class Item(models.Model): class Item(models.Model):
text = models.TextField(default='') text = models.TextField(default='')
category = models.TextField(default='')
list = models.ForeignKey(List, default=None, on_delete=models.CASCADE) list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
</head> </head>
<body> <body>
<header>
<h5 id="header" class="text-center">Made with Love by Rayza Arasj Mahardhika</h5>
</header>
<div class="container"> <div class="container">
<nav class="navbar navbar-default" role="navigation"> <nav class="navbar navbar-default" role="navigation">
...@@ -55,6 +58,11 @@ ...@@ -55,6 +58,11 @@
<input name="item_text" id="id_new_item" <input name="item_text" id="id_new_item"
class="form-control input-lg" class="form-control input-lg"
placeholder="Enter a to-do item" /> placeholder="Enter a to-do item" />
<select name="item_category" id="id_item_category">
<option value="Work">Work</option>
<option value="Study">Study</option>
<option value="Life">Life</option>
</select>
​{% csrf_token %} ​{% csrf_token %}
{% if error %} {% if error %}
<div class="form-group has-error"> <div class="form-group has-error">
...@@ -74,5 +82,8 @@ ...@@ -74,5 +82,8 @@
</div> </div>
</div> </div>
<footer>
<h5 id="footer" class="text-center">Made with Love by Rayza Arasj Mahardhika</h5>
</footer>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -2,12 +2,19 @@ ...@@ -2,12 +2,19 @@
{% block header_text %}Your To-Do list{% endblock %} {% block header_text %}Your To-Do list{% endblock %}
{% block form_action %}{% url 'view_list' list.id %}{% endblock %} {% block form_action %}{% url 'view_list' list.id 'all' %}{% endblock %}
{% block table %} {% block table %}
<div>
<p>Categorize by : </p>
<a href="{% url 'view_list' list.id 'all' %}" id='id_all_category_button'>All</a>
<a href="{% url 'view_list' list.id 'work' %}" id='id_work_category_button'>Work</a>
<a href="{% url 'view_list' list.id 'study' %}" id='id_study_category_button'>Study</a>
<a href="{% url 'view_list' list.id 'life' %}" id='id_life_category_button'>Life</a>
</div>
<table id="id_list_table" class="table"> <table id="id_list_table" class="table">
{% for item in list.item_set.all %} {% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr> <tr><td>{{ forloop.counter }}: {{ item.text }} - {{ item.category }}</td></tr>
{% endfor %} {% endfor %}
</table> </table>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -83,7 +83,10 @@ class ListViewTest(TestCase): ...@@ -83,7 +83,10 @@ class ListViewTest(TestCase):
self.client.post( self.client.post(
f'/lists/{correct_list.id}/', f'/lists/{correct_list.id}/',
data={'item_text': 'A new item for an existing list'} data={
'item_text': 'A new item for an existing list',
'item_category': 'A category'
}
) )
self.assertEqual(Item.objects.count(), 1) self.assertEqual(Item.objects.count(), 1)
...@@ -97,7 +100,10 @@ class ListViewTest(TestCase): ...@@ -97,7 +100,10 @@ class ListViewTest(TestCase):
response = self.client.post( response = self.client.post(
f'/lists/{correct_list.id}/', f'/lists/{correct_list.id}/',
data={'item_text': 'A new item for an existing list'} data={
'item_text': 'A new item for an existing list',
'item_category': 'A category'
}
) )
self.assertRedirects(response, f'/lists/{correct_list.id}/') self.assertRedirects(response, f'/lists/{correct_list.id}/')
...@@ -106,7 +112,7 @@ class ListViewTest(TestCase): ...@@ -106,7 +112,7 @@ class ListViewTest(TestCase):
list_ = List.objects.create() list_ = List.objects.create()
response = self.client.post( response = self.client.post(
f'/lists/{list_.id}/', f'/lists/{list_.id}/',
data={'item_text': ''} data={'item_text': '', 'item_category': ''}
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'list.html') self.assertTemplateUsed(response, 'list.html')
...@@ -116,23 +122,34 @@ class ListViewTest(TestCase): ...@@ -116,23 +122,34 @@ class ListViewTest(TestCase):
class NewListTest(TestCase): class NewListTest(TestCase):
def test_validation_errors_are_sent_back_to_home_page_template(self): def test_validation_errors_are_sent_back_to_home_page_template(self):
response = self.client.post('/lists/new', data={'item_text': ''}) response = self.client.post('/lists/new', data={'item_text': '', 'item_category':''})
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'home.html') self.assertTemplateUsed(response, 'home.html')
expected_error = escape("You can't have an empty list item") expected_error = escape("You can't have an empty list item")
self.assertContains(response, expected_error) self.assertContains(response, expected_error)
def test_invalid_list_items_arent_saved(self): def test_invalid_list_items_arent_saved(self):
self.client.post('/lists/new', data={'item_text': ''}) self.client.post('/lists/new', data={'item_text': '', 'item_category': ''})
self.assertEqual(List.objects.count(), 0) self.assertEqual(List.objects.count(), 0)
self.assertEqual(Item.objects.count(), 0) self.assertEqual(Item.objects.count(), 0)
class CustomTest(TestCase): class CustomTest(TestCase):
def test_commentary_correct_count_0_correct(self):
result = get_commentary(0)
self.assertEqual(result, COMMENTARY_ZERO)
def test_commentary_correct_count_5_correct(self): def test_commentary_correct_count_5_correct(self):
result = get_commentary(5) result = get_commentary(5)
self.assertEqual(result, COMMENTARY_MORE_THAN_EQUAL_FIVE) self.assertEqual(result, COMMENTARY_MORE_THAN_EQUAL_FIVE)
def test_commentary_correct_count_less_than_5_correct(self): def test_commentary_correct_count_less_than_5_correct(self):
result = get_commentary(4) result = get_commentary(4)
self.assertEqual(result, COMMENTARY_LESS_THAN_FIVE) self.assertEqual(result, COMMENTARY_LESS_THAN_FIVE)
\ No newline at end of file
class CategoryTest(TestCase):
def test_can_save_with_category(self):
list_ = List.objects.create()
Item.objects.create(text='item 1', category='category 1', list=list_)
self.assertEqual(list_.item_set.all().filter(category='category 1').count(), 1)
...@@ -21,4 +21,5 @@ urlpatterns = [ ...@@ -21,4 +21,5 @@ urlpatterns = [
# path('admin/', admin.site.urls), # path('admin/', admin.site.urls),
path('new', views.new_list, name='new_list'), path('new', views.new_list, name='new_list'),
path('<int:list_id>/', views.view_list, name='view_list'), path('<int:list_id>/', views.view_list, name='view_list'),
path('<int:list_id>/<str:category>/', views.view_list, name='view_list'),
] ]
...@@ -30,24 +30,28 @@ def about_page(request): ...@@ -30,24 +30,28 @@ def about_page(request):
'</html>' '</html>'
) )
def view_list(request, list_id): def view_list(request, list_id, category=None):
list_ = List.objects.get(id=list_id) list_ = List.objects.get(id=list_id)
items = Item.objects.filter(list=list_)
error = None error = None
if category in ['work', 'study', 'life']:
items = Item.objects.filter(list=list_, category=category.capitalize())
if request.method == 'POST': if request.method == 'POST':
try: try:
item = Item(text=request.POST['item_text'], list=list_) item = Item(text=request.POST['item_text'], category=request.POST['item_category'], list=list_)
item.full_clean() item.full_clean()
item.save() item.save()
return redirect(list_) return redirect(list_)
except ValidationError: except ValidationError:
error = "You can't have an empty list item" error = "You can't have an empty list item"
return render(request, 'list.html', {'list': list_, 'error': error}) return render(request, 'list.html', {'list': list_, 'items': items, 'error': error})
def new_list(request): def new_list(request):
list_ = List.objects.create() list_ = List.objects.create()
item = Item(text=request.POST['item_text'], list=list_) item = Item(text=request.POST['item_text'], category=request.POST['item_category'], list=list_)
try: try:
item.full_clean() item.full_clean()
item.save() item.save()
......
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