annotate bandmap/tests/test_views.py @ 917:0365fdbb4d78

Fix app conflict with messages. Django's messages app label conflicts with our messages app. We can't easily rename our label as that will make us rename database tables. Since our app came first we'll just customize Django messages label. For Django 1.7.7 upgrade.
author Brian Neal <bgneal@gmail.com>
date Mon, 06 Apr 2015 20:02:25 -0500
parents 5103edd3acc4
children
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@827 6 import json
bgneal@825 7
bgneal@824 8 from django.test import TestCase
bgneal@824 9 from django.core.urlresolvers import reverse
bgneal@824 10 from django.contrib.auth.models import User
bgneal@824 11
bgneal@824 12 from bandmap.models import BandEntry
bgneal@824 13
bgneal@824 14
bgneal@824 15 class NotLoggedInTestCase(TestCase):
bgneal@824 16 """Tests for a non-authenticated user.
bgneal@824 17
bgneal@824 18 """
bgneal@824 19 LOGIN_URL = reverse('accounts-login')
bgneal@824 20
bgneal@824 21 def test_map(self):
bgneal@824 22 url = reverse('bandmap-map')
bgneal@824 23 response = self.client.get(url)
bgneal@824 24 self.assertEqual(response.status_code, 200)
bgneal@824 25
bgneal@824 26 def test_get_add(self):
bgneal@824 27 url = reverse('bandmap-add')
bgneal@824 28 response = self.client.get(url, follow=True)
bgneal@824 29 self.assertRedirects(response, self.LOGIN_URL + '?next=' + url)
bgneal@824 30
bgneal@824 31 def test_post_add(self):
bgneal@824 32 url = reverse('bandmap-add')
bgneal@824 33 response = self.client.post(url, follow=True)
bgneal@824 34 self.assertRedirects(response, self.LOGIN_URL + '?next=' + url)
bgneal@824 35
bgneal@824 36
bgneal@824 37 class BasicTestCase(TestCase):
bgneal@824 38 """Tests for an authenticated user."""
bgneal@824 39
bgneal@824 40 def setUp(self):
bgneal@824 41 self.user = User.objects.create_user(
bgneal@824 42 username='pj', email='pj@example.com', password='top_secret')
bgneal@824 43 self.client.login(username='pj', password='top_secret')
bgneal@824 44
bgneal@824 45 def test_map(self):
bgneal@824 46 url = reverse('bandmap-map')
bgneal@824 47 response = self.client.get(url)
bgneal@824 48 self.assertEqual(response.status_code, 200)
bgneal@824 49
bgneal@824 50 def test_get_add(self):
bgneal@824 51 url = reverse('bandmap-add')
bgneal@824 52 response = self.client.get(url, follow=True)
bgneal@824 53 self.assertEqual(response.status_code, 200)
bgneal@824 54
bgneal@824 55 def test_post_add(self):
bgneal@825 56 url = reverse('bandmap-add')
bgneal@825 57 post_data = {
bgneal@825 58 'name': 'Test Band',
bgneal@825 59 'url': 'http://example.com/',
bgneal@825 60 'location': 'Here & There',
bgneal@825 61 'note': 'They suck',
bgneal@825 62 'is_active': 'on',
bgneal@825 63 'lat': '42.0',
bgneal@825 64 'lon': '-23.5',
bgneal@825 65 }
bgneal@825 66 now = datetime.datetime.now()
bgneal@825 67 delta_t = datetime.timedelta(seconds=1)
bgneal@825 68 response = self.client.post(url, data=post_data, follow=True)
bgneal@825 69 self.assertRedirects(response, url)
bgneal@825 70
bgneal@825 71 self.assertEqual(BandEntry.objects.all().count(), 1)
bgneal@825 72 entry = BandEntry.objects.get(pk=1)
bgneal@825 73 self.assertEqual(entry.name, post_data['name'])
bgneal@825 74 self.assertEqual(entry.user, self.user)
bgneal@825 75 self.assertEqual(entry.user, self.user)
bgneal@825 76 self.assertTrue(now - entry.date_submitted < delta_t)
bgneal@825 77 self.assertIsNone(entry.date_approved)
bgneal@825 78 self.assertEqual(entry.url, post_data['url'])
bgneal@825 79 self.assertEqual(entry.location, post_data['location'])
bgneal@825 80 self.assertAlmostEqual(entry.lat, float(post_data['lat']))
bgneal@825 81 self.assertAlmostEqual(entry.lon, float(post_data['lon']))
bgneal@825 82 self.assertEqual(entry.note, post_data['note'])
bgneal@825 83 self.assertTrue(entry.is_active)
bgneal@825 84 self.assertFalse(entry.is_approved)
bgneal@827 85
bgneal@827 86 class QueryTestCase(TestCase):
bgneal@827 87 """Testing the query view function which returns JSON."""
bgneal@827 88
bgneal@827 89 def setUp(self):
bgneal@827 90 self.user = User.objects.create_user(
bgneal@827 91 username='pj', email='pj@example.com', password='top_secret')
bgneal@827 92
bgneal@827 93 now = datetime.datetime.now()
bgneal@827 94
bgneal@827 95 kwargs = {
bgneal@827 96 'name': 'Test Band 1',
bgneal@827 97 'user': self.user,
bgneal@827 98 'date_submitted': now,
bgneal@827 99 'date_approved': now,
bgneal@827 100 'url': 'http://example.com',
bgneal@827 101 'location': 'Somewhere',
bgneal@827 102 'lat': 42.0,
bgneal@827 103 'lon': -30.0,
bgneal@827 104 'note': 'Some text here',
bgneal@827 105 'is_active': True,
bgneal@827 106 'is_approved': True,
bgneal@827 107 }
bgneal@827 108 BandEntry.objects.create(**kwargs)
bgneal@827 109
bgneal@827 110 kwargs['name'] = 'Test Band 2'
bgneal@827 111 kwargs['is_active'] = False
bgneal@827 112 BandEntry.objects.create(**kwargs)
bgneal@827 113
bgneal@827 114 kwargs['name'] = 'Test Band 3'
bgneal@827 115 kwargs['is_active'] = True
bgneal@827 116 kwargs['is_approved'] = False
bgneal@827 117 BandEntry.objects.create(**kwargs)
bgneal@827 118
bgneal@827 119 def test_query(self):
bgneal@827 120 url = reverse('bandmap-query')
bgneal@827 121 response = self.client.get(url)
bgneal@827 122 self.assertEqual(response.status_code, 200)
bgneal@827 123
bgneal@827 124 result = json.loads(response.content)
bgneal@827 125 self.assertEqual(len(result), 2)
bgneal@827 126 self.assertEqual(result[0]['name'], 'Test Band 1')
bgneal@827 127 self.assertEqual(result[1]['name'], 'Test Band 2')
bgneal@827 128
bgneal@827 129 def test_query_all(self):
bgneal@827 130 url = reverse('bandmap-query')
bgneal@827 131 response = self.client.get(url, {'show': 'all'})
bgneal@827 132 self.assertEqual(response.status_code, 200)
bgneal@827 133
bgneal@827 134 result = json.loads(response.content)
bgneal@827 135 self.assertEqual(len(result), 2)
bgneal@827 136 self.assertEqual(result[0]['name'], 'Test Band 1')
bgneal@827 137 self.assertEqual(result[1]['name'], 'Test Band 2')
bgneal@827 138
bgneal@827 139 def test_query_active(self):
bgneal@827 140 url = reverse('bandmap-query')
bgneal@827 141 response = self.client.get(url, {'show': 'active'})
bgneal@827 142 self.assertEqual(response.status_code, 200)
bgneal@827 143
bgneal@827 144 result = json.loads(response.content)
bgneal@827 145 self.assertEqual(len(result), 1)
bgneal@827 146 self.assertEqual(result[0]['name'], 'Test Band 1')
bgneal@827 147
bgneal@827 148 def test_query_inactive(self):
bgneal@827 149 url = reverse('bandmap-query')
bgneal@827 150 response = self.client.get(url, {'show': 'inactive'})
bgneal@827 151 self.assertEqual(response.status_code, 200)
bgneal@827 152
bgneal@827 153 result = json.loads(response.content)
bgneal@827 154 self.assertEqual(len(result), 1)
bgneal@827 155 self.assertEqual(result[0]['name'], 'Test Band 2')