Fakultas Ilmu Komputer UI

Commit e98e66bd authored by Kevin Albert Simanjuntak's avatar Kevin Albert Simanjuntak
Browse files

refactor

parent 429fb6fb
Pipeline #20538 passed with stage
in 1 minute and 2 seconds
......@@ -35,18 +35,21 @@ class NewVisitorTest(unittest.TestCase):
# 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)
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.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),"New to-do item did not appear in table")
# 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)
self.fail('Finish the test!')
# The page updates again, and now shows both items on her list
#self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),f"New to-do item did not appear in table")
self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
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)
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.assertIn('2: Use peacock feathers to make a fly',[row.text for row in rows])
if __name__ == '__main__':
unittest.main(warnings='ignore')
\ No newline at end of file
......@@ -4,8 +4,12 @@
</head>
<body>
<h1>Your To-Do list</h1>
<input id="id_new_item" placeholder="Enter a to-do item" />
<table id="id_list_table">
</table>
</body>
<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">
<tr><td>1: {{ new_item_text }}</td></tr>
</table>
</body>
</html>
\ No newline at end of file
......@@ -20,4 +20,9 @@ class HomePageTest(TestCase):
def test_uses_home_template(self):
response = self.client.get('/homepage/')
self.assertTemplateUsed(response, 'home.html')
def test_can_save_a_POST_request(self):
response = self.client.post('/homepage/', data={'item_text': 'A new list item'})
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
\ No newline at end of file
from django.http import HttpResponse
from django.shortcuts import render
def home_page(request):
return render(request, 'home.html')
\ No newline at end of file
return render(request, 'home.html', {
'new_item_text': request.POST.get('item_text', ''),
})
\ No newline at end of file
diff --git a/functional_tests.py b/functional_tests.py
index c9ac63e..a438209 100644
--- a/functional_tests.py
+++ b/functional_tests.py
@@ -35,18 +35,21 @@ class NewVisitorTest(unittest.TestCase):

# 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) 

+ 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.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),"New to-do item did not appear in table")
-
- # 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)
- self.fail('Finish the test!')
-
- # The page updates again, and now shows both items on her list 
+ #self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows),f"New to-do item did not appear in table")
+ self.assertIn('1: Buy peacock feathers', [row.text for row in rows])
+ 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)
+ 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.assertIn('2: Use peacock feathers to make a fly',[row.text for row in rows])
+ 
if __name__ == '__main__': 
unittest.main(warnings='ignore')
\ No newline at end of file
diff --git a/lists/__pycache__/tests.cpython-37.pyc b/lists/__pycache__/tests.cpython-37.pyc
index f371935..4b6e9b9 100644
Binary files a/lists/__pycache__/tests.cpython-37.pyc and b/lists/__pycache__/tests.cpython-37.pyc differ
diff --git a/lists/__pycache__/views.cpython-37.pyc b/lists/__pycache__/views.cpython-37.pyc
index 8a440d0..2bd5393 100644
Binary files a/lists/__pycache__/views.cpython-37.pyc and b/lists/__pycache__/views.cpython-37.pyc differ
diff --git a/lists/templates/home.html b/lists/templates/home.html
index 9a2a093..dd544fc 100644
--- a/lists/templates/home.html
+++ b/lists/templates/home.html
@@ -4,8 +4,12 @@
</head>
<body>
​<h1>Your To-Do list</h1>
- ​<input id="id_new_item" placeholder="Enter a to-do item" />
- ​<table id="id_list_table">
- ​</table>
- ​</body>
+ <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">
+  <tr><td>1: {{ new_item_text }}</td></tr>
+  </table>
+ </body>
</html>
\ No newline at end of file
diff --git a/lists/tests.py b/lists/tests.py
index 4e973ff..44473b0 100644
--- a/lists/tests.py
+++ b/lists/tests.py
@@ -20,4 +20,9 @@ class HomePageTest(TestCase):

def test_uses_home_template(self):
response = self.client.get('/homepage/')
+ self.assertTemplateUsed(response, 'home.html')
+
+ def test_can_save_a_POST_request(self):
+ response = self.client.post('/homepage/', data={'item_t
\ No newline at end of file
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