Compare commits

..

No commits in common. "05b8f2194354f889dd45acf76f250d0b8cb0ae86" and "bfaffaa9972a37e387342cf4ce68f90f03e277c7" have entirely different histories.

7 changed files with 20 additions and 109 deletions

1
.gitignore vendored
View File

@ -1,2 +1 @@
.vscode .vscode
**/migrations/**

View File

@ -1,6 +1,4 @@
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest import unittest
class NewVisitorTest(unittest.TestCase): class NewVisitorTest(unittest.TestCase):
@ -11,11 +9,6 @@ class NewVisitorTest(unittest.TestCase):
def tearDown(self): def tearDown(self):
self.browser.quit() 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): def test_can_start_a_list_and_retrieve_it_later(self):
# Edith has heard about a cool new online to-do app. She goes # Edith has heard about a cool new online to-do app. She goes
# to check out its homepage # to check out its homepage
@ -23,36 +16,20 @@ class NewVisitorTest(unittest.TestCase):
# She notices the page title and header mention to-do lists # She notices the page title and header mention to-do lists
self.assertIn('To-Do', self.browser.title) self.assertIn('To-Do', self.browser.title)
header_text = self.browser.find_element_by_tag_name('h1').text self.fail('Finish the test!')
self.assertIn('To-Do', header_text)
# She is invited to enter a to-do item straight away # 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 # She types "Buy peacock feathers" into a text box (Edith's hobby
# is tying fly-fishing lures) # is tying fly-fishing lures)
inputbox.send_keys('Buy peacock feathers')
# 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 # "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 # 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) # 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 # 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 # 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

View File

@ -1,4 +1,3 @@
from django.db import models from django.db import models
class Item(models.Model): # Create your models here.
text = models.TextField(default='')

View File

@ -1,18 +0,0 @@
<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>

View File

@ -1,11 +1,8 @@
from typing import Text
from django.http import response
from django.test import TestCase from django.test import TestCase
from django.urls import resolve from django.urls import resolve
from django.http import HttpRequest from django.http import HttpRequest
from lists.views import home_page from lists.views import home_page
from lists.models import Item
class HomePageTest(TestCase): class HomePageTest(TestCase):
@ -13,52 +10,18 @@ class HomePageTest(TestCase):
found = resolve('/') found = resolve('/')
self.assertEqual(found.func, home_page) self.assertEqual(found.func, home_page)
def test_uses_home_template(self): def test_home_page_returns_correct_html(self):
response = self.client.get('/') """
self.assertTemplateUsed(response, 'home.html') 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_can_save_a_POST_request(self): request = HttpRequest() #1
self.client.post('/', data={'item_text': 'A new list item'}) response = home_page(request) #2
html = response.content.decode('utf8') #3
self.assertEqual(Item.objects.count(), 1) self.assertTrue(html.startswith('<html>')) #4
new_item = Item.objects.first() self.assertIn('<title>To-Do list</title>', html) #5
self.assertEqual(new_item.text, 'A new list item') self.assertTrue(html.endswith('</html>')) #4
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')

View File

@ -1,16 +1,8 @@
from django.http.request import HttpRequest from django.http.request import HttpRequest
from django.http.response import HttpResponse from django.http.response import HttpResponse
from django.shortcuts import redirect, render from django.shortcuts import render
from lists.models import Item
# Create your views here # Create your views here
def home_page(request): def home_page(request):
if request.method == 'POST': # return HttpResponse('<html><title>To-Do lists</title></html>')
Item.objects.create(text=request.POST['item_text']) return HttpResponse('<html><title>To-Do list</title></html>')
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

View File

@ -37,7 +37,6 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'lists'
] ]
MIDDLEWARE = [ MIDDLEWARE = [