5.5 The Django ORM and Our First Model, created ItemModelTest
parent
87834f5a0b
commit
d5facdfa2c
|
@ -1,3 +1,4 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
# Create your models here.
|
class Item(models.Model):
|
||||||
|
pass
|
|
@ -4,6 +4,7 @@ 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):
|
||||||
|
|
||||||
|
@ -19,3 +20,22 @@ class HomePageTest(TestCase):
|
||||||
response = self.client.post('/', data={'item_text': 'A new list item'})
|
response = self.client.post('/', data={'item_text': 'A new list item'})
|
||||||
self.assertIn('A new list item', response.content.decode())
|
self.assertIn('A new list item', response.content.decode())
|
||||||
self.assertTemplateUsed(response, 'home.html')
|
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')
|
Loading…
Reference in New Issue