from django.test import TestCase
from lists.models import Item,List

class HomePageTest(TestCase):

    def test_uses_home_template(self):
        response = self.client.get('/')
        self.assertTemplateUsed(response, 'home.html')
		# refactoring of test_uses_home_template
		# from django.core.urlresolvers import resolve
		# from django.test import TestCase
		# from lists.views import home_page 
		# from django.http import HttpRequest 

		# class HomePageTest(TestCase):

		# def test_root_url_resolves_to_home_page_view(self):
		#      found = resolve('/')  
		#      self.assertEqual(found.func, home_page)  

		# def test_home_page_returns_correct_html(self):
		#      request = HttpRequest()  
		#      response = self.client.get('/')  
		#      html = response.content.decode('utf8')  
		#      self.assertIn('<title>To-Do lists</title>', html)  
		#      self.assertTrue(html.endswith('</html>'))  
		#      self.assertTemplateUsed(response ,'home.html')


class ListViewTest(TestCase):

	def test_displays_all_items(self):
		list_ = List.objects.create()
		Item.objects.create(text='itemey 1', list=list_)
		Item.objects.create(text='itemey 2', list=list_)

	def test_uses_list_template(self):
		response = self.client.get('/lists/the-only-list-in-the-world/')
		self.assertTemplateUsed(response,'list.html')



class ListandItemModelTest(TestCase):
	def test_saving_and_retriving_items(self):
		list_ = List()
		list_.save()

		first_item =Item()
		first_item.text = "The first ITEM"
		first_item.list =list_	
		first_item.save()

		second_item =Item()
		second_item.text = "The 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.list,list_)
		self.assertEqual(second_saved_item.list,list_)
		self.assertEqual(first_saved_item.text,'The first ITEM')
		self.assertEqual(second_saved_item.text,'The second ITEM')



class NewListTest(TestCase):

    def test_can_save_a_POST_request(self):
        self.client.post('/lists/new', data={'item_text': 'A new list item'})
        self.assertEqual(Item.objects.count(), 1)
        new_item = Item.objects.first()
        self.assertEqual(new_item.text, 'A new list item')


    def test_redirects_after_POST(self):
        response = self.client.post('/lists/new', data={'item_text': 'A new list item'})
        self.assertRedirects(response, '/lists/the-only-list-in-the-world/')





