Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
PMPL
Collection of Practice
2019
1506722720-practice
Commits
92d7487f
Commit
92d7487f
authored
Sep 25, 2019
by
Dwi Nanda Susanto
Browse files
Testinggoat/ch4 5
parent
23277d35
Changes
8
Hide whitespace changes
Inline
Side-by-side
functional_tests.py
View file @
92d7487f
from
selenium
import
webdriver
from
selenium.webdriver.common.keys
import
Keys
import
unittest
import
time
import
unittest
class
NewVisitorTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
browser
=
webdriver
.
Chrome
(
'
E
:/webdriver/chromedriver.exe'
)
self
.
browser
=
webdriver
.
Chrome
(
'
C
:/webdriver/chromedriver.exe'
)
def
tearDown
(
self
):
self
.
browser
.
quit
()
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
# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
self
.
browser
.
get
(
'http://localhost:8000'
)
# She notices the page title and header mention to-do lists
self
.
assertIn
(
'Simple Homepage'
,
self
.
browser
.
title
)
# She is invited to enter a to-do item straight away
# [...rest of comments as before]
self
.
assertIn
(
'To-Do'
,
self
.
browser
.
title
)
header_text
=
self
.
browser
.
find_element_by_tag_name
(
'h1'
).
text
self
.
assertIn
(
'To-Do'
,
header_text
)
inputbox
=
self
.
browser
.
find_element_by_id
(
'id_new_item'
)
self
.
assertEqual
(
inputbox
.
get_attribute
(
'placeholder'
),
'Enter a to-do item'
)
inputbox
.
send_keys
(
'Buy peacock feathers'
)
inputbox
.
send_keys
(
Keys
.
ENTER
)
time
.
sleep
(
1
)
table
=
self
.
browser
.
find_element_by_id
(
'id_list_table'
)
rows
=
table
.
find_elements_by_tag_name
(
'tr'
)
self
.
assertIn
(
'1: Buy peacock feathers'
,
[
row
.
text
for
row
in
rows
])
self
.
check_for_row_in_list_table
(
'2: Use peacock feathers to make a fly'
)
self
.
fail
(
"finish the test"
)
if
__name__
==
'__main__'
:
unittest
.
main
(
warnings
=
'ignore'
)
# pass
\ No newline at end of file
lists/migrations/0001_initial.py
0 → 100644
View file @
92d7487f
# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-09-25 12:42
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'
)),
],
),
]
lists/migrations/0002_item_text.py
0 → 100644
View file @
92d7487f
# -*- coding: utf-8 -*-
# Generated by Django 1.11.24 on 2019-09-25 12:43
from
__future__
import
unicode_literals
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
=
''
),
),
]
lists/models.py
View file @
92d7487f
from
django.db
import
models
# Create your models here.
class
Item
(
models
.
Model
):
text
=
models
.
TextField
(
default
=
''
)
lists/templates/home.html
0 → 100644
View file @
92d7487f
<html>
<head>
<title>
To-Do
</title>
</head>
<body>
<h1>
Dwi Nanda Susanto - 1506722720 - 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>
{% if items|length
<
1
%}
<
h3
style=
"color:green;"
>
yey, waktunya berlibur
</h3>
{% elif items|length > 0 and items|length
<
5
%}
<
h3
style=
"color:blue;"
>
sibuk tapi santai
</h3>
{% else %}
<h3
style=
"color:red;"
>
oh tidak
</h3>
{% endif %}
</body>
</html>
lists/tests.py
View file @
92d7487f
...
...
@@ -2,16 +2,84 @@ from django.urls import resolve
from
django.test
import
TestCase
from
django.http
import
HttpRequest
from
lists.views
import
home_page
from
django.template.loader
import
render_to_string
from
django.test
import
TestCase
from
lists.models
import
Item
class
HomePageTest
(
TestCase
):
def
test_root_url_resolves_to_home_page_view
(
self
):
found
=
resolve
(
'/'
)
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>Simple Homepage</title>'
,
html
)
self
.
assertIn
(
'<h1>Dwi Nanda Susanto</h1>'
,
html
)
self
.
assertIn
(
'<h2>1506722720</h2>'
,
html
)
self
.
assertTrue
(
html
.
endswith
(
'</html>'
))
\ No newline at end of file
self
.
assertIn
(
'<title>To-Do</title>'
,
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
):
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_only_saves_items_when_necessary
(
self
):
self
.
client
.
get
(
'/'
)
self
.
assertEqual
(
Item
.
objects
.
count
(),
0
)
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_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
())
def
test_comment_on_zero_items
(
self
):
response
=
self
.
client
.
get
(
'/'
)
self
.
assertIn
(
'yey, waktunya berlibur'
,
response
.
content
.
decode
())
def
test_comment_on_few_items
(
self
):
for
i
in
range
(
2
):
Item
.
objects
.
create
(
text
=
'itemey %d'
%
i
)
response
=
self
.
client
.
get
(
'/'
)
self
.
assertIn
(
'sibuk tapi santai'
,
response
.
content
.
decode
())
def
test_comment_on_many_items
(
self
):
for
i
in
range
(
6
):
Item
.
objects
.
create
(
text
=
'itemey %d'
%
i
)
response
=
self
.
client
.
get
(
'/'
)
self
.
assertIn
(
'oh tidak'
,
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'
)
\ No newline at end of file
lists/views.py
View file @
92d7487f
from
django.shortcuts
import
render
from
django.http
import
HttpResponse
from
django.shortcuts
import
redirect
,
render
from
lists.models
import
Item
# Create your views here.
def
home_page
(
req
):
return
HttpResponse
(
'<html><title>Simple Homepage</title><h1>Dwi Nanda Susanto</h1><h2>1506722720</h2></html>'
)
\ No newline at end of file
if
req
.
method
==
'POST'
:
Item
.
objects
.
create
(
text
=
req
.
POST
[
'item_text'
])
return
redirect
(
'/'
)
items
=
Item
.
objects
.
all
()
return
render
(
req
,
'home.html'
,
{
'items'
:
items
})
\ No newline at end of file
superlists/settings.py
View file @
92d7487f
...
...
@@ -25,7 +25,7 @@ SECRET_KEY = 'hy8i*t3!(z=6ycjz@n80d*rd!*@i^4j!p9wasjdqd)1@+_gqv2'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG
=
True
ALLOWED_HOSTS
=
[
'dwi-simplehomepage.herokuapp.com'
,
'127.0.0.1'
]
ALLOWED_HOSTS
=
[
'dwi-simplehomepage.herokuapp.com'
,
'127.0.0.1'
,
'localhost'
]
# Application definition
...
...
@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions'
,
'django.contrib.messages'
,
'django.contrib.staticfiles'
,
'lists'
,
]
MIDDLEWARE
=
[
...
...
@@ -122,3 +123,6 @@ STATICFILES_DIRS = [
os
.
path
.
join
(
BASE_DIR
,
"static"
),
]
STATIC_ROOT
=
os
.
path
.
join
(
BASE_DIR
,
"staticfiles"
)
from
django.conf
import
settings
settings
.
configure
()
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment