from django.test import TestCase
from django.core.exceptions import ValidationError
from django.core.urlresolvers import resolve
from lists.views import home_page
from django.http import HttpRequest
from lists.models import Item,List
from django.template.loader import render_to_string 
# Create your tests here.
class ItemModelTest(TestCase):
    def test_saving_and_retrieving_item(self):
        list_ = List()
        list_.save()

        first_item = Item()
        first_item.text = "First(ever) list item"
        first_item.list = list_
        first_item.save()

        second_item = Item()
        second_item.text="second Item"
        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,"First(ever) list item")
        self.assertEqual(first_saved_item.list,list_)
        self.assertEqual(second_saved_item.text,"second Item")
        self.assertEqual(second_saved_item.list,list_)

    def test_validation_user_input(self):
        list_=List.objects.create()
        item = Item(list = list_,text="")
        with self.assertRaises(ValidationError):
            item.save()
            item.full_clean()
    def test_get_absolute_url(self):
        list_=List.objects.create()
        self.assertEqual(list_.get_absolute_url(),'/lists/%d/'%(list_.id,))
