[Search] get search results from Elasticsearch
[mygpo.git] / mygpo / podcasts / tests.py
blob0416c492a18aab5f26605c565774d783ef29f9db
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 uuid
20 from datetime import datetime, timedelta
22 from django.test import TestCase
24 from mygpo.podcasts.models import Podcast
27 def create_podcast(**kwargs):
28 return Podcast.objects.create(id=uuid.uuid1().hex, **kwargs)
31 class PodcastTests(unittest.TestCase):
32 """ Test podcasts and their properties """
34 def test_next_update(self):
35 """ Test calculation of Podcast.next_update """
36 last_update = datetime(2014, 03, 31, 11, 00)
37 update_interval = 123 # hours
39 # create an "old" podcast with update-information
40 create_podcast(last_update=last_update, update_interval=update_interval)
42 # the podcast should be the next to be updated
43 p = Podcast.objects.all().order_by_next_update().first()
45 # assert that the next_update property is calculated correctly
46 self.assertEqual(p.next_update,
47 last_update + timedelta(hours=update_interval))
50 def test_get_or_create_for_url(self):
51 """ Test that get_or_create_for_url returns existing Podcast """
52 URL = 'http://example.com/get_or_create.rss'
53 p1 = Podcast.objects.get_or_create_for_url(URL)
54 p2 = Podcast.objects.get_or_create_for_url(URL)
55 self.assertEqual(p1.pk, p2.pk)
58 class PodcastGroupTests(unittest.TestCase):
59 """ Test grouping of podcasts """
61 def test_group(self):
62 self.podcast1 = create_podcast()
63 self.podcast2 = create_podcast()
65 group = self.podcast1.group_with(self.podcast2, 'My Group', 'p1', 'p2')
67 self.assertIn(self.podcast1, group.podcast_set.all())
68 self.assertIn(self.podcast2, group.podcast_set.all())
69 self.assertEquals(len(group.podcast_set.all()), 2)
70 self.assertEquals(group.title, 'My Group')
71 self.assertEquals(self.podcast1.group_member_name, 'p1')
72 self.assertEquals(self.podcast2.group_member_name, 'p2')
74 # add to group
75 self.podcast3 = create_podcast()
77 group = self.podcast1.group_with(self.podcast3, 'My Group', 'p1', 'p3')
79 self.assertIn(self.podcast3, group.podcast_set.all())
80 self.assertEquals(self.podcast3.group_member_name, 'p3')
82 # add group to podcast
83 self.podcast4 = create_podcast()
85 group = self.podcast4.group_with(self.podcast1, 'My Group', 'p4', 'p1')
87 self.assertIn(self.podcast4, group.podcast_set.all())
88 self.assertEquals(self.podcast4.group_member_name, 'p4')
91 class SlugTests(TestCase):
92 """ Test various slug functionality """
94 def test_update_slugs(self):
96 # this is the current number of queries when writing the test; this has
97 # not been optimized in any particular way, it should just be used to
98 # alert when something changes
99 with self.assertNumQueries(25):
100 podcast = create_podcast()
102 # set the canonical slug
103 podcast.set_slug('podcast-1')
104 self.assertEquals(podcast.slug, 'podcast-1')
106 # set a new list of slugs
107 podcast.set_slugs(['podcast-2', 'podcast-1'])
108 self.assertEquals(podcast.slug, 'podcast-2')
110 # remove the canonical slug
111 podcast.remove_slug('podcast-2')
112 self.assertEquals(podcast.slug, 'podcast-1')
114 # add a non-canonical slug
115 podcast.add_slug('podcast-3')
116 self.assertEquals(podcast.slug, 'podcast-1')
119 def load_tests(loader, tests, ignore):
120 tests.addTest(unittest.TestLoader().loadTestsFromTestCase(PodcastTests))
121 tests.addTest(unittest.TestLoader().loadTestsFromTestCase(PodcastGroupTests))
122 tests.addTest(unittest.TestLoader().loadTestsFromTestCase(SlugTests))
123 return tests