Mercurial > public > sg101
comparison bandmap/tests/test_views.py @ 827:5103edd3acc4
Bandmap: initial version complete.
Javascript queries server for band data.
View created to handle query.
Test created for view.
Link added to map in sidebar.
Template tweaks.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 27 Sep 2014 19:41:41 -0500 |
parents | d91356818cef |
children |
comparison
equal
deleted
inserted
replaced
826:d7e4c08b2e8b | 827:5103edd3acc4 |
---|---|
1 """ | 1 """ |
2 Unit tests for the bandmap application views. | 2 Unit tests for the bandmap application views. |
3 | 3 |
4 """ | 4 """ |
5 import datetime | 5 import datetime |
6 import json | |
6 | 7 |
7 from django.test import TestCase | 8 from django.test import TestCase |
8 from django.core.urlresolvers import reverse | 9 from django.core.urlresolvers import reverse |
9 from django.contrib.auth.models import User | 10 from django.contrib.auth.models import User |
10 | 11 |
79 self.assertAlmostEqual(entry.lat, float(post_data['lat'])) | 80 self.assertAlmostEqual(entry.lat, float(post_data['lat'])) |
80 self.assertAlmostEqual(entry.lon, float(post_data['lon'])) | 81 self.assertAlmostEqual(entry.lon, float(post_data['lon'])) |
81 self.assertEqual(entry.note, post_data['note']) | 82 self.assertEqual(entry.note, post_data['note']) |
82 self.assertTrue(entry.is_active) | 83 self.assertTrue(entry.is_active) |
83 self.assertFalse(entry.is_approved) | 84 self.assertFalse(entry.is_approved) |
85 | |
86 class QueryTestCase(TestCase): | |
87 """Testing the query view function which returns JSON.""" | |
88 | |
89 def setUp(self): | |
90 self.user = User.objects.create_user( | |
91 username='pj', email='pj@example.com', password='top_secret') | |
92 | |
93 now = datetime.datetime.now() | |
94 | |
95 kwargs = { | |
96 'name': 'Test Band 1', | |
97 'user': self.user, | |
98 'date_submitted': now, | |
99 'date_approved': now, | |
100 'url': 'http://example.com', | |
101 'location': 'Somewhere', | |
102 'lat': 42.0, | |
103 'lon': -30.0, | |
104 'note': 'Some text here', | |
105 'is_active': True, | |
106 'is_approved': True, | |
107 } | |
108 BandEntry.objects.create(**kwargs) | |
109 | |
110 kwargs['name'] = 'Test Band 2' | |
111 kwargs['is_active'] = False | |
112 BandEntry.objects.create(**kwargs) | |
113 | |
114 kwargs['name'] = 'Test Band 3' | |
115 kwargs['is_active'] = True | |
116 kwargs['is_approved'] = False | |
117 BandEntry.objects.create(**kwargs) | |
118 | |
119 def test_query(self): | |
120 url = reverse('bandmap-query') | |
121 response = self.client.get(url) | |
122 self.assertEqual(response.status_code, 200) | |
123 | |
124 result = json.loads(response.content) | |
125 self.assertEqual(len(result), 2) | |
126 self.assertEqual(result[0]['name'], 'Test Band 1') | |
127 self.assertEqual(result[1]['name'], 'Test Band 2') | |
128 | |
129 def test_query_all(self): | |
130 url = reverse('bandmap-query') | |
131 response = self.client.get(url, {'show': 'all'}) | |
132 self.assertEqual(response.status_code, 200) | |
133 | |
134 result = json.loads(response.content) | |
135 self.assertEqual(len(result), 2) | |
136 self.assertEqual(result[0]['name'], 'Test Band 1') | |
137 self.assertEqual(result[1]['name'], 'Test Band 2') | |
138 | |
139 def test_query_active(self): | |
140 url = reverse('bandmap-query') | |
141 response = self.client.get(url, {'show': 'active'}) | |
142 self.assertEqual(response.status_code, 200) | |
143 | |
144 result = json.loads(response.content) | |
145 self.assertEqual(len(result), 1) | |
146 self.assertEqual(result[0]['name'], 'Test Band 1') | |
147 | |
148 def test_query_inactive(self): | |
149 url = reverse('bandmap-query') | |
150 response = self.client.get(url, {'show': 'inactive'}) | |
151 self.assertEqual(response.status_code, 200) | |
152 | |
153 result = json.loads(response.content) | |
154 self.assertEqual(len(result), 1) | |
155 self.assertEqual(result[0]['name'], 'Test Band 2') |