annotate bandmap/tests/test_views.py @ 825:d91356818cef

Bandmap WIP: added test to add band.
author Brian Neal <bgneal@gmail.com>
date Thu, 25 Sep 2014 21:15:22 -0500
parents 704b47356a49
children 5103edd3acc4
rev   line source
bgneal@824 1 """
bgneal@824 2 Unit tests for the bandmap application views.
bgneal@824 3
bgneal@824 4 """
bgneal@825 5 import datetime
bgneal@825 6
bgneal@824 7 from django.test import TestCase
bgneal@824 8 from django.core.urlresolvers import reverse
bgneal@824 9 from django.contrib.auth.models import User
bgneal@824 10
bgneal@824 11 from bandmap.models import BandEntry
bgneal@824 12
bgneal@824 13
bgneal@824 14 class NotLoggedInTestCase(TestCase):
bgneal@824 15 """Tests for a non-authenticated user.
bgneal@824 16
bgneal@824 17 """
bgneal@824 18 LOGIN_URL = reverse('accounts-login')
bgneal@824 19
bgneal@824 20 def test_map(self):
bgneal@824 21 url = reverse('bandmap-map')
bgneal@824 22 response = self.client.get(url)
bgneal@824 23 self.assertEqual(response.status_code, 200)
bgneal@824 24
bgneal@824 25 def test_get_add(self):
bgneal@824 26 url = reverse('bandmap-add')
bgneal@824 27 response = self.client.get(url, follow=True)
bgneal@824 28 self.assertRedirects(response, self.LOGIN_URL + '?next=' + url)
bgneal@824 29
bgneal@824 30 def test_post_add(self):
bgneal@824 31 url = reverse('bandmap-add')
bgneal@824 32 response = self.client.post(url, follow=True)
bgneal@824 33 self.assertRedirects(response, self.LOGIN_URL + '?next=' + url)
bgneal@824 34
bgneal@824 35
bgneal@824 36 class BasicTestCase(TestCase):
bgneal@824 37 """Tests for an authenticated user."""
bgneal@824 38
bgneal@824 39 def setUp(self):
bgneal@824 40 self.user = User.objects.create_user(
bgneal@824 41 username='pj', email='pj@example.com', password='top_secret')
bgneal@824 42 self.client.login(username='pj', password='top_secret')
bgneal@824 43
bgneal@824 44 def test_map(self):
bgneal@824 45 url = reverse('bandmap-map')
bgneal@824 46 response = self.client.get(url)
bgneal@824 47 self.assertEqual(response.status_code, 200)
bgneal@824 48
bgneal@824 49 def test_get_add(self):
bgneal@824 50 url = reverse('bandmap-add')
bgneal@824 51 response = self.client.get(url, follow=True)
bgneal@824 52 self.assertEqual(response.status_code, 200)
bgneal@824 53
bgneal@824 54 def test_post_add(self):
bgneal@825 55 url = reverse('bandmap-add')
bgneal@825 56 post_data = {
bgneal@825 57 'name': 'Test Band',
bgneal@825 58 'url': 'http://example.com/',
bgneal@825 59 'location': 'Here & There',
bgneal@825 60 'note': 'They suck',
bgneal@825 61 'is_active': 'on',
bgneal@825 62 'lat': '42.0',
bgneal@825 63 'lon': '-23.5',
bgneal@825 64 }
bgneal@825 65 now = datetime.datetime.now()
bgneal@825 66 delta_t = datetime.timedelta(seconds=1)
bgneal@825 67 response = self.client.post(url, data=post_data, follow=True)
bgneal@825 68 self.assertRedirects(response, url)
bgneal@825 69
bgneal@825 70 self.assertEqual(BandEntry.objects.all().count(), 1)
bgneal@825 71 entry = BandEntry.objects.get(pk=1)
bgneal@825 72 self.assertEqual(entry.name, post_data['name'])
bgneal@825 73 self.assertEqual(entry.user, self.user)
bgneal@825 74 self.assertEqual(entry.user, self.user)
bgneal@825 75 self.assertTrue(now - entry.date_submitted < delta_t)
bgneal@825 76 self.assertIsNone(entry.date_approved)
bgneal@825 77 self.assertEqual(entry.url, post_data['url'])
bgneal@825 78 self.assertEqual(entry.location, post_data['location'])
bgneal@825 79 self.assertAlmostEqual(entry.lat, float(post_data['lat']))
bgneal@825 80 self.assertAlmostEqual(entry.lon, float(post_data['lon']))
bgneal@825 81 self.assertEqual(entry.note, post_data['note'])
bgneal@825 82 self.assertTrue(entry.is_active)
bgneal@825 83 self.assertFalse(entry.is_approved)