[Tests] Check number of queries on Podcast / Episode page
[mygpo.git] / mygpo / web / tests.py
blob3af0ca27fb8d9b487eb0b516a5a3427c2040e5ce
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 import unittest
19 import doctest
20 import uuid
22 from django.test import TestCase
23 from django.core.urlresolvers import reverse, resolve
24 from django.test.client import RequestFactory
25 from django.contrib.auth.models import AnonymousUser
27 from mygpo.podcasts.models import Podcast, Episode, Slug
28 import mygpo.web.utils
29 from mygpo.users.models import User
30 from mygpo.test import create_auth_string
33 class SimpleWebTests(TestCase):
35 @classmethod
36 def setUpClass(self):
37 self.user = User(username='web-test', email='web-test@example.com')
38 self.user.set_password('pwd')
39 self.user.save()
41 self.auth_string = create_auth_string('test', 'pwd')
43 @classmethod
44 def tearDownClass(self):
45 self.user.delete()
47 def test_access_parameterless_pages(self):
48 pages = [
49 'history',
50 'suggestions',
51 'tags',
52 'subscriptions',
53 'subscriptions-opml',
54 'subscriptions-download',
55 'favorites',
56 'account',
57 'privacy',
58 'delete-account',
59 'share',
60 'toplist',
61 'episode-toplist',
62 'devices',
63 'device-create',
64 'login',
65 'logout',
66 'home']
68 self.access_pages(pages, [], True)
70 def test_access_podcast_pages(self):
71 pages = ['podcast', ]
73 def access_pages(self, pages, args, login):
74 if login:
75 self.client.post('/login/', dict(
76 login_username=self.user.username, pwd='pwd'))
78 for page in pages:
79 response = self.client.get(reverse(page, args=args), follow=True)
80 self.assertEquals(response.status_code, 200)
83 class PodcastPageTests(TestCase):
84 """ Test the podcast page """
86 def setUp(self):
87 self.factory = RequestFactory()
89 # create a podcast and some episodes
90 podcast = Podcast.objects.create(id=uuid.uuid1().hex)
91 for n in range(20):
92 episode = Episode.objects.create(id=uuid.uuid1().hex,
93 podcast=podcast,
95 # we only need (the last) one
96 self.episode_slug = Slug.objects.create(content_object=episode,
97 order=0,
98 scope=podcast.scope,
99 slug=str(n),
102 self.podcast_slug = Slug.objects.create(content_object=podcast,
103 order=n, scope=podcast.scope,
104 slug='podcast')
106 def test_podcast_queries(self):
107 """ Test that the expected number of queries is executed """
108 url = reverse('podcast-slug', args=(self.podcast_slug.slug, ))
109 # the number of queries must be independent of the number of episodes
110 self._check_queries_anon_page(url, 5)
113 def test_episode_queries(self):
114 """ Test that the expected number of queries is executed """
115 url = reverse('episode-slug', args=(self.podcast_slug.slug,
116 self.episode_slug.slug))
117 self._check_queries_anon_page(url, 5)
119 def _check_queries_anon_page(self, url, num_queries):
120 request = self.factory.get(url)
121 request.user = AnonymousUser()
122 view = resolve(url)
124 with self.assertNumQueries(num_queries):
125 response = view.func(request, *view.args, **view.kwargs)
128 def suite():
129 suite = unittest.TestSuite()
130 suite.addTest(doctest.DocTestSuite(mygpo.web.utils))
131 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SimpleWebTests))
132 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(PodcastPageTests))
133 return suite