Compare commits

..

3 Commits

3 changed files with 31 additions and 13 deletions

View File

@ -11,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
@ -36,9 +41,7 @@ class NewVisitorTest(unittest.TestCase):
# "1: Buy peacock feathers" as an item in a to-do list
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('1: Buy peacock feathers', [row.text for row in rows])
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)
@ -48,14 +51,8 @@ class NewVisitorTest(unittest.TestCase):
time.sleep(1)
# The page updates again, and now shows both items on her list
table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
print(rows)
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]
)
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

View File

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

View File

@ -4,6 +4,7 @@ from django.urls import resolve
from django.http import HttpRequest
from lists.views import home_page
from lists.models import Item
class HomePageTest(TestCase):
@ -19,3 +20,22 @@ class HomePageTest(TestCase):
response = self.client.post('/', data={'item_text': 'A new list item'})
self.assertIn('A new list item', response.content.decode())
self.assertTemplateUsed(response, 'home.html')
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')