7.9 Biting the Bullet: Adjustinig Our Models

chap7-new
Jason Zhu 2020-11-11 14:39:11 +11:00
parent 5dc4127eaa
commit 56e0feac1e
1 changed files with 14 additions and 8 deletions

View File

@ -1,11 +1,6 @@
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
from lists.models import Item, List
class HomePageTest(TestCase):
@ -44,21 +39,32 @@ class ListViewTest(TestCase):
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
class ItemModelTest(TestCase):
class ListAndItemModelsTest(TestCase):
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
first_item = Item() # Create an object
first_item.text = 'The first (ever) list item' # Assign attributes
first_item.list = list_
first_item.save() # Calling .save() function
second_item = Item()
second_item.text = 'Item the second'
second_item.list = list_
second_item.save()
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
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(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, 'Item the second')
self.assertEqual(second_saved_item.list, list_)