bgneal@824: """ bgneal@824: Unit tests for the bandmap application views. bgneal@824: bgneal@824: """ bgneal@825: import datetime bgneal@827: import json bgneal@825: bgneal@824: from django.test import TestCase bgneal@824: from django.core.urlresolvers import reverse bgneal@824: from django.contrib.auth.models import User bgneal@824: bgneal@824: from bandmap.models import BandEntry bgneal@824: bgneal@824: bgneal@824: class NotLoggedInTestCase(TestCase): bgneal@824: """Tests for a non-authenticated user. bgneal@824: bgneal@824: """ bgneal@824: LOGIN_URL = reverse('accounts-login') bgneal@824: bgneal@824: def test_map(self): bgneal@824: url = reverse('bandmap-map') bgneal@824: response = self.client.get(url) bgneal@824: self.assertEqual(response.status_code, 200) bgneal@824: bgneal@824: def test_get_add(self): bgneal@824: url = reverse('bandmap-add') bgneal@824: response = self.client.get(url, follow=True) bgneal@824: self.assertRedirects(response, self.LOGIN_URL + '?next=' + url) bgneal@824: bgneal@824: def test_post_add(self): bgneal@824: url = reverse('bandmap-add') bgneal@824: response = self.client.post(url, follow=True) bgneal@824: self.assertRedirects(response, self.LOGIN_URL + '?next=' + url) bgneal@824: bgneal@824: bgneal@824: class BasicTestCase(TestCase): bgneal@824: """Tests for an authenticated user.""" bgneal@824: bgneal@824: def setUp(self): bgneal@824: self.user = User.objects.create_user( bgneal@824: username='pj', email='pj@example.com', password='top_secret') bgneal@824: self.client.login(username='pj', password='top_secret') bgneal@824: bgneal@824: def test_map(self): bgneal@824: url = reverse('bandmap-map') bgneal@824: response = self.client.get(url) bgneal@824: self.assertEqual(response.status_code, 200) bgneal@824: bgneal@824: def test_get_add(self): bgneal@824: url = reverse('bandmap-add') bgneal@824: response = self.client.get(url, follow=True) bgneal@824: self.assertEqual(response.status_code, 200) bgneal@824: bgneal@824: def test_post_add(self): bgneal@825: url = reverse('bandmap-add') bgneal@825: post_data = { bgneal@825: 'name': 'Test Band', bgneal@825: 'url': 'http://example.com/', bgneal@825: 'location': 'Here & There', bgneal@825: 'note': 'They suck', bgneal@825: 'is_active': 'on', bgneal@825: 'lat': '42.0', bgneal@825: 'lon': '-23.5', bgneal@825: } bgneal@825: now = datetime.datetime.now() bgneal@825: delta_t = datetime.timedelta(seconds=1) bgneal@825: response = self.client.post(url, data=post_data, follow=True) bgneal@825: self.assertRedirects(response, url) bgneal@825: bgneal@825: self.assertEqual(BandEntry.objects.all().count(), 1) bgneal@825: entry = BandEntry.objects.get(pk=1) bgneal@825: self.assertEqual(entry.name, post_data['name']) bgneal@825: self.assertEqual(entry.user, self.user) bgneal@825: self.assertEqual(entry.user, self.user) bgneal@825: self.assertTrue(now - entry.date_submitted < delta_t) bgneal@825: self.assertIsNone(entry.date_approved) bgneal@825: self.assertEqual(entry.url, post_data['url']) bgneal@825: self.assertEqual(entry.location, post_data['location']) bgneal@825: self.assertAlmostEqual(entry.lat, float(post_data['lat'])) bgneal@825: self.assertAlmostEqual(entry.lon, float(post_data['lon'])) bgneal@825: self.assertEqual(entry.note, post_data['note']) bgneal@825: self.assertTrue(entry.is_active) bgneal@825: self.assertFalse(entry.is_approved) bgneal@827: bgneal@827: class QueryTestCase(TestCase): bgneal@827: """Testing the query view function which returns JSON.""" bgneal@827: bgneal@827: def setUp(self): bgneal@827: self.user = User.objects.create_user( bgneal@827: username='pj', email='pj@example.com', password='top_secret') bgneal@827: bgneal@827: now = datetime.datetime.now() bgneal@827: bgneal@827: kwargs = { bgneal@827: 'name': 'Test Band 1', bgneal@827: 'user': self.user, bgneal@827: 'date_submitted': now, bgneal@827: 'date_approved': now, bgneal@827: 'url': 'http://example.com', bgneal@827: 'location': 'Somewhere', bgneal@827: 'lat': 42.0, bgneal@827: 'lon': -30.0, bgneal@827: 'note': 'Some text here', bgneal@827: 'is_active': True, bgneal@827: 'is_approved': True, bgneal@827: } bgneal@827: BandEntry.objects.create(**kwargs) bgneal@827: bgneal@827: kwargs['name'] = 'Test Band 2' bgneal@827: kwargs['is_active'] = False bgneal@827: BandEntry.objects.create(**kwargs) bgneal@827: bgneal@827: kwargs['name'] = 'Test Band 3' bgneal@827: kwargs['is_active'] = True bgneal@827: kwargs['is_approved'] = False bgneal@827: BandEntry.objects.create(**kwargs) bgneal@827: bgneal@827: def test_query(self): bgneal@827: url = reverse('bandmap-query') bgneal@827: response = self.client.get(url) bgneal@827: self.assertEqual(response.status_code, 200) bgneal@827: bgneal@827: result = json.loads(response.content) bgneal@827: self.assertEqual(len(result), 2) bgneal@827: self.assertEqual(result[0]['name'], 'Test Band 1') bgneal@827: self.assertEqual(result[1]['name'], 'Test Band 2') bgneal@827: bgneal@827: def test_query_all(self): bgneal@827: url = reverse('bandmap-query') bgneal@827: response = self.client.get(url, {'show': 'all'}) bgneal@827: self.assertEqual(response.status_code, 200) bgneal@827: bgneal@827: result = json.loads(response.content) bgneal@827: self.assertEqual(len(result), 2) bgneal@827: self.assertEqual(result[0]['name'], 'Test Band 1') bgneal@827: self.assertEqual(result[1]['name'], 'Test Band 2') bgneal@827: bgneal@827: def test_query_active(self): bgneal@827: url = reverse('bandmap-query') bgneal@827: response = self.client.get(url, {'show': 'active'}) bgneal@827: self.assertEqual(response.status_code, 200) bgneal@827: bgneal@827: result = json.loads(response.content) bgneal@827: self.assertEqual(len(result), 1) bgneal@827: self.assertEqual(result[0]['name'], 'Test Band 1') bgneal@827: bgneal@827: def test_query_inactive(self): bgneal@827: url = reverse('bandmap-query') bgneal@827: response = self.client.get(url, {'show': 'inactive'}) bgneal@827: self.assertEqual(response.status_code, 200) bgneal@827: bgneal@827: result = json.loads(response.content) bgneal@827: self.assertEqual(len(result), 1) bgneal@827: self.assertEqual(result[0]['name'], 'Test Band 2')