Compare commits
16 Commits
bfaffaa997
...
05b8f21943
Author | SHA1 | Date |
---|---|---|
Jason Zhu | 05b8f21943 | |
Jason Zhu | ff01b880eb | |
Jason Zhu | deceab97c3 | |
Jason Zhu | 8bfd695ece | |
Jason Zhu | ac7a1a70e1 | |
Jason Zhu | d5facdfa2c | |
Jason Zhu | 87834f5a0b | |
Jason Zhu | 9bacbfef4c | |
Jason Zhu | 892cdbf541 | |
Jason Zhu | c0661056cf | |
Jason Zhu | 81b586ac07 | |
Jason Zhu | 9c102d7add | |
Jason Zhu | 1fb94c9d74 | |
Jason Zhu | 747e0c7009 | |
Jason Zhu | 89224c9928 | |
Jason Zhu | 4f7cb1bb5a |
|
@ -1 +1,2 @@
|
|||
.vscode
|
||||
**/migrations/**
|
|
@ -1,4 +1,6 @@
|
|||
from selenium import webdriver
|
||||
from selenium.webdriver.common.keys import Keys
|
||||
import time
|
||||
import unittest
|
||||
|
||||
class NewVisitorTest(unittest.TestCase):
|
||||
|
@ -9,6 +11,11 @@ class NewVisitorTest(unittest.TestCase):
|
|||
def tearDown(self):
|
||||
self.browser.quit()
|
||||
|
||||
def check_for_row_in_list_table(self, row_text):
|
||||
table = self.browser.find_element_by_id('id_list_table')
|
||||
rows = table.find_elements_by_tag_name('tr')
|
||||
self.assertIn(row_text, [row.text for row in rows])
|
||||
|
||||
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
|
||||
|
@ -16,20 +23,36 @@ class NewVisitorTest(unittest.TestCase):
|
|||
|
||||
# She notices the page title and header mention to-do lists
|
||||
self.assertIn('To-Do', self.browser.title)
|
||||
self.fail('Finish the test!')
|
||||
header_text = self.browser.find_element_by_tag_name('h1').text
|
||||
self.assertIn('To-Do', header_text)
|
||||
|
||||
# She is invited to enter a to-do item straight away
|
||||
inputbox = self.browser.find_element_by_id('id_new_item')
|
||||
self.assertEqual(
|
||||
inputbox.get_attribute('placeholder'),
|
||||
'Enter a to-do item'
|
||||
)
|
||||
|
||||
# She types "Buy peacock feathers" into a text box (Edith's hobby
|
||||
# is tying fly-fishing lures)
|
||||
inputbox.send_keys('Buy peacock feathers')
|
||||
|
||||
# When she hits enter, the page updates, and now the page lists
|
||||
# "1: Buy peacock feathers" as an item in a to-do list
|
||||
inputbox.send_keys(Keys.ENTER)
|
||||
time.sleep(1)
|
||||
self.check_for_row_in_list_table('1: Buy peacock feathers')
|
||||
|
||||
# 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)
|
||||
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)
|
||||
|
||||
# The page updates again, and now shows both items on her list
|
||||
self.check_for_row_in_list_table('1: Buy peacock feathers')
|
||||
self.check_for_row_in_list_table('2: Use peacock feathers to make a fly')
|
||||
|
||||
# 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
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
class Item(models.Model):
|
||||
text = models.TextField(default='')
|
|
@ -0,0 +1,18 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>To-Do lists</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Your 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>
|
||||
</body>
|
||||
</html>
|
|
@ -1,8 +1,11 @@
|
|||
from typing import Text
|
||||
from django.http import response
|
||||
from django.test import TestCase
|
||||
from django.urls import resolve
|
||||
from django.http import HttpRequest
|
||||
|
||||
from lists.views import home_page
|
||||
from lists.models import Item
|
||||
|
||||
class HomePageTest(TestCase):
|
||||
|
||||
|
@ -10,18 +13,52 @@ class HomePageTest(TestCase):
|
|||
found = resolve('/')
|
||||
self.assertEqual(found.func, home_page)
|
||||
|
||||
def test_home_page_returns_correct_html(self):
|
||||
"""
|
||||
1. Create an HttpRequest object, which is what Django will see when a user's browser asks for a page
|
||||
2. Pass it to `home_page` view, which gives us a response.
|
||||
3. Extract `.content` of the response, which are byte value, and should be decoded into string (HTML format)
|
||||
4. Check HTML starts and end with <html> tag
|
||||
5. Want to find <title> tag in the middle
|
||||
"""
|
||||
def test_uses_home_template(self):
|
||||
response = self.client.get('/')
|
||||
self.assertTemplateUsed(response, 'home.html')
|
||||
|
||||
request = HttpRequest() #1
|
||||
response = home_page(request) #2
|
||||
html = response.content.decode('utf8') #3
|
||||
self.assertTrue(html.startswith('<html>')) #4
|
||||
self.assertIn('<title>To-Do list</title>', html) #5
|
||||
self.assertTrue(html.endswith('</html>')) #4
|
||||
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_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_only_saves_items_when_necessary(self):
|
||||
self.client.get('/')
|
||||
self.assertEqual(Item.objects.count(), 0)
|
||||
|
||||
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())
|
||||
|
||||
|
||||
class ItemModelTest(TestCase):
|
||||
|
||||
def test_saving_and_retrieving_items(self):
|
||||
first_item = Item() # Create an object
|
||||
first_item.text = 'The first (ever) list item' # Assign attributes
|
||||
first_item.save() # Calling .save() function
|
||||
|
||||
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')
|
|
@ -1,8 +1,16 @@
|
|||
from django.http.request import HttpRequest
|
||||
from django.http.response import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import redirect, render
|
||||
|
||||
from lists.models import Item
|
||||
|
||||
# Create your views here
|
||||
def home_page(request):
|
||||
# return HttpResponse('<html><title>To-Do lists</title></html>')
|
||||
return HttpResponse('<html><title>To-Do list</title></html>')
|
||||
if request.method == 'POST':
|
||||
Item.objects.create(text=request.POST['item_text'])
|
||||
return redirect('/')
|
||||
|
||||
items = Item.objects.all() # get objects from database (model)
|
||||
return render(request=request,
|
||||
template_name='home.html',
|
||||
context={'items': items}) # pass items into template using render
|
|
@ -37,6 +37,7 @@ INSTALLED_APPS = [
|
|||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'lists'
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
|
Loading…
Reference in New Issue