Merge pull request #793 from gpodder/remove-advertise
[mygpo.git] / mygpo / podcasts / tests.py
blob497736aa156a18f057ca931fa297f4451597d848
1 import unittest
2 import uuid
3 from datetime import datetime, timedelta
5 from django.test import TestCase
7 from mygpo.podcasts.models import Podcast, Episode
10 def create_podcast(**kwargs):
11 return Podcast.objects.create(id=uuid.uuid1(), **kwargs)
14 class PodcastTests(unittest.TestCase):
15 """Test podcasts and their properties"""
17 def test_next_update(self):
18 """Test calculation of Podcast.next_update"""
19 last_update = datetime(2014, 3, 31, 11, 00)
20 update_interval = 123 # hours
22 # create an "old" podcast with update-information
23 create_podcast(last_update=last_update, update_interval=update_interval)
25 # the podcast should be the next to be updated
26 p = Podcast.objects.all().order_by_next_update().first()
28 # assert that the next_update property is calculated correctly
29 self.assertEqual(p.next_update, last_update + timedelta(hours=update_interval))
31 def test_get_or_create_for_url(self):
32 """Test that get_or_create_for_url returns existing Podcast"""
33 URL = "http://example.com/get_or_create.rss"
34 p1 = Podcast.objects.get_or_create_for_url(URL).object
35 p2 = Podcast.objects.get_or_create_for_url(URL).object
36 self.assertEqual(p1.pk, p2.pk)
38 def test_episode_count(self):
39 """Test if Podcast.episode_count is updated correctly"""
40 PODCAST_URL = "http://example.com/podcast.rss"
41 EPISODE_URL = "http://example.com/episode%d.mp3"
42 NUM_EPISODES = 3
44 p = Podcast.objects.get_or_create_for_url(PODCAST_URL).object
45 for n in range(NUM_EPISODES):
46 Episode.objects.get_or_create_for_url(p, EPISODE_URL % (n,))
48 p = Podcast.objects.get(pk=p.pk)
49 self.assertEqual(p.episode_count, NUM_EPISODES)
51 # the episodes already exist this time -- no episode is created
52 for n in range(NUM_EPISODES):
53 Episode.objects.get_or_create_for_url(p, EPISODE_URL % (n,))
55 p = Podcast.objects.get(pk=p.pk)
56 self.assertEqual(p.episode_count, NUM_EPISODES)
58 real_count = Episode.objects.filter(podcast=p).count()
59 self.assertEqual(real_count, NUM_EPISODES)
62 class PodcastGroupTests(unittest.TestCase):
63 """Test grouping of podcasts"""
65 def test_group(self):
66 self.podcast1 = create_podcast()
67 self.podcast2 = create_podcast()
69 group = self.podcast1.group_with(self.podcast2, "My Group", "p1", "p2")
71 self.assertIn(self.podcast1, group.podcast_set.all())
72 self.assertIn(self.podcast2, group.podcast_set.all())
73 self.assertEqual(len(group.podcast_set.all()), 2)
74 self.assertEqual(group.title, "My Group")
75 self.assertEqual(self.podcast1.group_member_name, "p1")
76 self.assertEqual(self.podcast2.group_member_name, "p2")
78 # add to group
79 self.podcast3 = create_podcast()
81 group = self.podcast1.group_with(self.podcast3, "My Group", "p1", "p3")
83 self.assertIn(self.podcast3, group.podcast_set.all())
84 self.assertEqual(self.podcast3.group_member_name, "p3")
86 # add group to podcast
87 self.podcast4 = create_podcast()
89 group = self.podcast4.group_with(self.podcast1, "My Group", "p4", "p1")
91 self.assertIn(self.podcast4, group.podcast_set.all())
92 self.assertEqual(self.podcast4.group_member_name, "p4")
95 class SlugTests(TestCase):
96 """Test various slug functionality"""
98 def test_update_slugs(self):
100 # this is the current number of queries when writing the test; this has
101 # not been optimized in any particular way, it should just be used to
102 # alert when something changes
103 podcast = create_podcast()
105 with self.assertNumQueries(8):
106 # set the canonical slug
107 podcast.set_slug("podcast-1")
108 self.assertEqual(podcast.slug, "podcast-1")
110 with self.assertNumQueries(9):
111 # set a new list of slugs
112 podcast.set_slugs(["podcast-2", "podcast-1"])
113 self.assertEqual(podcast.slug, "podcast-2")
115 with self.assertNumQueries(2):
116 # remove the canonical slug
117 podcast.remove_slug("podcast-2")
118 self.assertEqual(podcast.slug, "podcast-1")
120 with self.assertNumQueries(3):
121 # add a non-canonical slug
122 podcast.add_slug("podcast-3")
123 self.assertEqual(podcast.slug, "podcast-1")