not getting the same precise error message, but perhaps the intent is the same?; anyway, updated views & some FTs, base.html for more dynamic error handling (tho incomplete)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.utils import IntegrityError
|
||||
from django.test import TestCase
|
||||
from ..models import Item, List
|
||||
|
||||
@@ -28,4 +30,15 @@ class ListAndItemModelsTest(TestCase):
|
||||
self.assertEqual(first_saved_item.list, mylist)
|
||||
self.assertEqual(second_saved_item.text, "A sequel somehow better than the first")
|
||||
self.assertEqual(second_saved_item.list, mylist)
|
||||
|
||||
|
||||
def test_cannot_save_null_list_items(self):
|
||||
mylist = List.objects.create()
|
||||
item = Item(list=mylist, text=None)
|
||||
with self.assertRaises(IntegrityError):
|
||||
item.save()
|
||||
|
||||
def test_cannot_save_empty_list_items(self):
|
||||
mylist = List.objects.create()
|
||||
item = Item(list=mylist, text="")
|
||||
with self.assertRaises(ValidationError):
|
||||
item.full_clean()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from django.test import TestCase
|
||||
from django.utils import html
|
||||
from ..models import Item, List
|
||||
import lxml.html
|
||||
|
||||
@@ -56,6 +57,18 @@ class NewListTest(TestCase):
|
||||
new_list = List.objects.get()
|
||||
self.assertRedirects(response, f'/apps/dashboard/{new_list.id}/')
|
||||
|
||||
def test_validation_errors_are_sent_back_to_home_page_template(self):
|
||||
response = self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, "apps/dashboard/home.html")
|
||||
expected_error = html.escape("You can't have an empty list item")
|
||||
self.assertContains(response, expected_error)
|
||||
|
||||
def test_invalid_list_items_never_save(self):
|
||||
self.client.post("/apps/dashboard/newlist", data={"item_text": ""})
|
||||
self.assertEqual(List.objects.count(), 0)
|
||||
self.assertEqual(Item.objects.count(), 0)
|
||||
|
||||
class NewItemTest(TestCase):
|
||||
def test_can_save_a_POST_request_to_an_existing_list(self):
|
||||
other_list = List.objects.create()
|
||||
|
||||
@@ -1,18 +1,26 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.shortcuts import redirect, render
|
||||
from .models import Item, List
|
||||
|
||||
def home_page(request):
|
||||
return render(request, 'apps/dashboard/home.html')
|
||||
|
||||
def new_list(request):
|
||||
nulist = List.objects.create()
|
||||
item = Item(text=request.POST['item_text'], list=nulist)
|
||||
try:
|
||||
item.full_clean()
|
||||
item.save()
|
||||
except ValidationError:
|
||||
nulist.delete()
|
||||
error = "You can't have an empty list item"
|
||||
return render(request, "apps/dashboard/home.html", {"error": error})
|
||||
return redirect(f'/apps/dashboard/{nulist.id}/')
|
||||
|
||||
def view_list(request, list_id):
|
||||
our_list = List.objects.get(id=list_id)
|
||||
return render(request, 'apps/dashboard/list.html', {'list': our_list})
|
||||
|
||||
def new_list(request):
|
||||
nulist = List.objects.create()
|
||||
Item.objects.create(text=request.POST['item_text'], list=nulist)
|
||||
return redirect(f'/apps/dashboard/{nulist.id}/')
|
||||
|
||||
def add_item(request, list_id):
|
||||
our_list = List.objects.get(id=list_id)
|
||||
Item.objects.create(text=request.POST['item_text'], list=our_list)
|
||||
|
||||
Reference in New Issue
Block a user