From d5facdfa2cba8090c0d970163c8182ba184a1ef1 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Sun, 8 Nov 2020 21:48:15 +1100 Subject: [PATCH] 5.5 The Django ORM and Our First Model, created ItemModelTest --- src/lists/models.py | 3 ++- src/lists/tests.py | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/lists/models.py b/src/lists/models.py index 71a8362..2075c54 100644 --- a/src/lists/models.py +++ b/src/lists/models.py @@ -1,3 +1,4 @@ from django.db import models -# Create your models here. +class Item(models.Model): + pass \ No newline at end of file diff --git a/src/lists/tests.py b/src/lists/tests.py index c285297..fa5dbd6 100644 --- a/src/lists/tests.py +++ b/src/lists/tests.py @@ -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): @@ -18,4 +19,23 @@ class HomePageTest(TestCase): def test_can_save_a_POST_request(self): 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') \ No newline at end of file + 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') \ No newline at end of file