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
,
30 last_update
+ timedelta(hours
=update_interval
))
33 def test_get_or_create_for_url(self
):
34 """ Test that get_or_create_for_url returns existing Podcast """
35 URL
= 'http://example.com/get_or_create.rss'
36 p1
= Podcast
.objects
.get_or_create_for_url(URL
)
37 p2
= Podcast
.objects
.get_or_create_for_url(URL
)
38 self
.assertEqual(p1
.pk
, p2
.pk
)
40 def test_episode_count(self
):
41 """ Test if Podcast.episode_count is updated correctly """
42 PODCAST_URL
= 'http://example.com/podcast.rss'
43 EPISODE_URL
= 'http://example.com/episode%d.mp3'
46 p
= Podcast
.objects
.get_or_create_for_url(PODCAST_URL
)
47 for n
in range(NUM_EPISODES
):
48 Episode
.objects
.get_or_create_for_url(p
, EPISODE_URL
% (n
, ))
50 p
= Podcast
.objects
.get(pk
=p
.pk
)
51 self
.assertEqual(p
.episode_count
, NUM_EPISODES
)
53 # the episodes already exist this time -- no episode is created
54 for n
in range(NUM_EPISODES
):
55 Episode
.objects
.get_or_create_for_url(p
, EPISODE_URL
% (n
, ))
57 p
= Podcast
.objects
.get(pk
=p
.pk
)
58 self
.assertEqual(p
.episode_count
, NUM_EPISODES
)
60 real_count
= Episode
.objects
.filter(podcast
=p
).count()
61 self
.assertEqual(real_count
, NUM_EPISODES
)
64 class PodcastGroupTests(unittest
.TestCase
):
65 """ Test grouping of podcasts """
68 self
.podcast1
= create_podcast()
69 self
.podcast2
= create_podcast()
71 group
= self
.podcast1
.group_with(self
.podcast2
, 'My Group', 'p1', 'p2')
73 self
.assertIn(self
.podcast1
, group
.podcast_set
.all())
74 self
.assertIn(self
.podcast2
, group
.podcast_set
.all())
75 self
.assertEqual(len(group
.podcast_set
.all()), 2)
76 self
.assertEqual(group
.title
, 'My Group')
77 self
.assertEqual(self
.podcast1
.group_member_name
, 'p1')
78 self
.assertEqual(self
.podcast2
.group_member_name
, 'p2')
81 self
.podcast3
= create_podcast()
83 group
= self
.podcast1
.group_with(self
.podcast3
, 'My Group', 'p1', 'p3')
85 self
.assertIn(self
.podcast3
, group
.podcast_set
.all())
86 self
.assertEqual(self
.podcast3
.group_member_name
, 'p3')
88 # add group to podcast
89 self
.podcast4
= create_podcast()
91 group
= self
.podcast4
.group_with(self
.podcast1
, 'My Group', 'p4', 'p1')
93 self
.assertIn(self
.podcast4
, group
.podcast_set
.all())
94 self
.assertEqual(self
.podcast4
.group_member_name
, 'p4')
97 class SlugTests(TestCase
):
98 """ Test various slug functionality """
100 def test_update_slugs(self
):
102 # this is the current number of queries when writing the test; this has
103 # not been optimized in any particular way, it should just be used to
104 # alert when something changes
105 podcast
= create_podcast()
107 with self
.assertNumQueries(8):
108 # set the canonical slug
109 podcast
.set_slug('podcast-1')
110 self
.assertEqual(podcast
.slug
, 'podcast-1')
112 with self
.assertNumQueries(9):
113 # set a new list of slugs
114 podcast
.set_slugs(['podcast-2', 'podcast-1'])
115 self
.assertEqual(podcast
.slug
, 'podcast-2')
117 with self
.assertNumQueries(2):
118 # remove the canonical slug
119 podcast
.remove_slug('podcast-2')
120 self
.assertEqual(podcast
.slug
, 'podcast-1')
122 with self
.assertNumQueries(3):
123 # add a non-canonical slug
124 podcast
.add_slug('podcast-3')
125 self
.assertEqual(podcast
.slug
, 'podcast-1')