def test_saving_and_retrieving_items(self): list_ = List() list_.save() first_item = Item() first_item.text = "The first (ever) list item" first_item.list = list_ first_item.save() 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_)
def view_list(request, list_id): list_ = List.objects.get(id=list_id) error = None result = items_to_comment(list_.item_set.all()) boxtype = result[1] comment = result[0] if request.method == 'POST': try: item = Item(text=request.POST['item_text'], list=list_) item.full_clean() item.save() return redirect(list_) except ValidationError: error = "You can't have an empty list item" return render(request, 'list.html', {'list': list_ , 'comment': comment, 'boxtype' : boxtype, 'error' : error})
def test_cannot_save_empty_list_items(self): list_ = List.objects.create() item = Item(list=list_, text="") with self.assertRaises(ValidationError): item.save() item.full_clean()