from datetime import datetime

from django.core.urlresolvers import reverse
from django.test import TestCase

from animals.models import Animal


class AnimalsViewsTestCase(TestCase):
    fixtures = ['animals_testdata.json']

    def test_index(self):
        """
        Checks for 200 status and that expected variables in context
        and that some animals are displayed by default.
        """
        resp = self.client.get(reverse('animals_index'))
        self.assertTrue('form' in resp.context)
        self.assertTrue('alist' in resp.context)
        self.assertTrue('results_count' in resp.context)
        self.assertNotContains(resp, '<div>0 animals displayed</div>',
                               status_code=200)

    def test_type_search(self):
        resp = self.client.post(reverse('animals_index'),
            {'animal_type': 'CAT'})
        self.assertContains(resp, "5/31/2012")
        self.assertContains(resp, "brn tabby domestic sh", status_code=200)

    def test_paginated_type_search(self):
        self.client.post(reverse('animals_index'), {'animal_type': 'CAT'})
        resp = self.client.get('%s?page=2' % reverse('animals_index'))
        self.assertContains(resp, "6/2/2012", count=5)
        self.assertContains(resp, "calico domestic sh", status_code=200)

    def test_sex_search(self):
        resp = self.client.post(reverse('animals_index'), {'sex': 'M'})
        self.assertContains(resp, "5/30/2012")
        self.assertContains(resp, "beagle")
        self.assertContains(resp, "tricolor beagle", status_code=200)

    def test_date_search(self):
        resp = self.client.post(reverse('animals_index'),
            {'intake_date_start': '2012-06-04',
             'intake_date_end': '2012-06-04'})
        self.assertContains(resp, "6/4/2012")
        self.assertContains(resp, "chalupa")
        self.assertContains(resp, "white rat terrier", status_code=200)

    def test_condition_search(self):
        resp = self.client.post(reverse('animals_index'),
            {'intake_condition': 'INJURED'})
        self.assertContains(resp, "5/30/2012")
        self.assertContains(resp, "bo jangles")
        self.assertContains(resp, "white catahoula", status_code=200)

    def test_markers_displayed(self):
        resp = self.client.post(reverse('animals_index'),
            {'intake_date_start': '2012-06-04',
             'intake_date_end': '2012-06-04'})
        self.assertContains(resp, "map.addMarker", count=43, status_code=200)

@@ -992,21 +992,11 @@
 est_
-paginated_typ
+dat
 e_se
@@ -1010,24 +1010,31 @@
 lf):%0A       
+ resp =
  self.client
@@ -1068,102 +1068,102 @@
 x'),
- %7B'animal_type': 'CAT'%7D)%0A        resp = self.client.get('%25s?page=2' %25 reverse('animals_index')
+%0A            %7B'intake_date_start': '2012-06-04',%0A             'intake_date_end': '2012-06-04'%7D
 )%0A  
@@ -1201,24 +1201,15 @@
  %226/
-2
+4
 /2012%22
-, count=5
 )%0A  
@@ -1246,25 +1246,69 @@
 , %22c
+h
 al
-ico domestic sh
+upa%22)%0A        self.assertContains(resp, %22white rat terrier
 %22, s
@@ -1337,19 +1337,30 @@
 ef test_
-sex
+paginated_type
 _search(
@@ -1365,39 +1365,32 @@
 h(self):%0A       
- resp =
  self.client.pos
@@ -1423,65 +1423,146 @@
 , %7B'
-sex': 'M'%7D)%0A        self.assertContains(resp, %225/30/2012%22
+animal_type': 'CAT',%0A            'intake_date_start': '2012-05-01'%7D)%0A        resp = self.client.get('%25s?page=2' %25 reverse('animals_index')
 )%0A  
@@ -1594,23 +1594,34 @@
 (resp, %22
-beagle%22
+6/2/2012%22, count=5
 )%0A      
@@ -1653,23 +1653,26 @@
 p, %22
-tricolor beagle
+calico domestic sh
 %22, s
@@ -1693,36 +1693,35 @@
 )%0A%0A    def test_
-date
+sex
 _search(self):%0A 
@@ -1780,100 +1780,19 @@
 x'),
-%0A            %7B'intake_date_start': '2012-06-04',%0A             'intake_date_end': '2012-06-04
+ %7B'sex': 'M
 '%7D)%0A
@@ -1818,35 +1818,36 @@
 Contains(resp, %22
-6/4
+5/30
 /2012%22)%0A        
@@ -1865,39 +1865,38 @@
 Contains(resp, %22
-chalupa
+beagle
 %22)%0A        self.
@@ -1917,33 +1917,31 @@
 (resp, %22
-white rat terrier
+tricolor beagle
 %22, statu
@@ -2039,32 +2039,33 @@
 animals_index'),
+%7B
 %0A            %7B'i
@@ -2053,33 +2053,32 @@
 ),%7B%0A            
-%7B
 'intake_conditio
@@ -2090,16 +2090,63 @@
 INJURED'
+,%0A            'intake_date_start': '2012-05-01'
 %7D)%0A     

fix paginated type & condition search tests