Source

wishlist_project / wishlist_projetc / wishlist / tests / test_views.py

Full commit
from datetime import datetime

from django.test import TestCase
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

from wishlist.models import Event
from utils import LoginClient

class WishlistViewsIndexTest(TestCase):
    def test_index(self):
        resp = self.client.get(reverse('wishlist.index'))
        self.assertEqual(resp.status_code, 200)
        
class WishlistViewsEventTest(TestCase):
    fixtures = ['wishlist_test_models.json']
    
    def setUp(self):
        self.client_logged = LoginClient()
        self.user = User.objects.get(username="RafaelBuy")
        self.client_logged.login_user(self.user)
    
    def test_events(self):
        resp = self.client_logged.get(reverse('wishlist.events'))
        self.assertEqual(resp.status_code, 200)
        self.assertTrue('event_list' in resp.context)
        
    def test_if_the_events_displays_only_the_events_of_the_user_logged(self):
        user_test = User()
        user_test.username = 'test'
        user_test.email = 'test@test.com'
        user_test.save()
        
        new_event = Event()
        new_event.title = 'Barbecue'
        new_event.address = 'My house'
        new_event.date = datetime.now()
        new_event.creator = user_test
        new_event.save()
        
        # all events
        self.assertEqual(Event.objects.count(), 2)
        # only events of the user logged
        self.assertEqual(Event.objects.filter(creator=self.user).count(), 1)
        
    def test_event_detail(self):
        resp = self.client_logged.get(reverse('wishlist.event_detail', kwargs={'event_pk': 1}))
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.context['event'].pk, 1)
        self.assertEqual(resp.context['event'].title, "Elena's bridal shower")
        
    def test_event_detail_throw_404(self):
        resp = self.client_logged.get(reverse('wishlist.event_detail', kwargs={'event_pk': 999}))
        self.assertEqual(resp.status_code, 404)
        
    def test_new_event(self):
        resp = self.client_logged.get(reverse('wishlist.new_event'))
        self.assertEqual(resp.status_code, 200)

    def test_edit_event(self):
        resp = self.client_logged.get(reverse('wishlist.edit_event', kwargs={'event_pk': 1}))
        self.assertEqual(resp.status_code, 200)
        
    def test_delete_event(self):
        self.assertEqual(Event.objects.filter(creator=self.user).count(), 1)
        resp = self.client_logged.get(reverse('wishlist.delete_event', kwargs={'event_pk': 1}))
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(Event.objects.filter(creator=self.user).count(), 0)