Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 109 lines (79 sloc) 4.364 kb
8fa47b7 Charles Leifer Initial commit of djangoembed, rich media consuming and providing in
coleifer authored
1 import datetime
2
3 import oembed
4 from oembed.exceptions import OEmbedMissingEndpoint
5 from oembed.models import StoredOEmbed, StoredProvider, AggregateMedia
6 from oembed.providers import DjangoProvider
7 from oembed.tests.tests.base import BaseOEmbedTestCase
8
9 from oembed.tests.models import Blog, Category, Rich
10
11
12 class ModelTestCase(BaseOEmbedTestCase):
13 def test_stored_oembeds(self):
14 video = oembed.site.embed(self.youtube_url)
15 stored = StoredOEmbed.objects.get(match=self.youtube_url)
16 self.assertEqual(stored.response, video.get_data())
17
18 photo = oembed.site.embed(self.flickr_url)
19 stored = StoredOEmbed.objects.get(match=self.flickr_url)
20 self.assertEqual(stored.response, photo.get_data())
21
22 # now create some on-the-fly
23 link = oembed.site.embed(self.blog_url)
24 stored = StoredOEmbed.objects.get(match=self.blog_url)
25 self.assertEqual(stored.response, link.get_data())
26
27 photo = oembed.site.embed(self.category_url)
28 stored = StoredOEmbed.objects.get(match=self.category_url)
29 self.assertEqual(stored.response, photo.get_data())
30
31 rich = oembed.site.embed(self.rich_url)
32 stored = StoredOEmbed.objects.get(match=self.rich_url)
33 self.assertEqual(stored.response, rich.get_data())
34
35 def test_stored_providers(self):
36 active = StoredProvider.objects.get(pk=100)
37 inactive = StoredProvider.objects.get(pk=101)
38
39 active_qs = StoredProvider.objects.active()
40 self.assertTrue(active in active_qs)
41 self.assertFalse(inactive in active_qs)
42
43 provider_list = oembed.site.get_providers()
44 self.assertTrue(active in provider_list)
45 self.assertFalse(inactive in provider_list)
46
47 active.active = False
48 active.save()
49 provider_list = oembed.site.get_providers()
50 self.assertFalse(active in provider_list)
51
52 def test_media_aggregation(self):
53 r = Rich(name='Test', slug='test', content='Hey check this out: %s' % self.youtube_url)
54 r.save()
55
56 am_queryset = AggregateMedia.objects.all()
57 self.assertEqual(am_queryset.count(), 1)
58
59 aggregated_object = am_queryset[0]
60 self.assertEqual(aggregated_object.url, self.youtube_url)
61 self.assertEqual(aggregated_object.media, StoredOEmbed.objects.get(match=self.youtube_url))
62
63 self.assertQuerysetEqual(r.media.all(), am_queryset)
64 self.assertQuerysetEqual(r.videos.all(), am_queryset)
65 self.assertQuerysetEqual(r.photos.all(), [])
66
67 r.content = 'Whoa i changed my mind, you should check this out: %s' % self.flickr_url
68 r.save()
69
70 am_queryset = AggregateMedia.objects.all()
71
72 # the youtube one sticks around, but records from the rel table are killed
73 self.assertEqual(am_queryset.count(), 2)
74
75 # the flickr embed is there
76 aggregated_object = am_queryset.get(url=self.flickr_url)
77
78 # check that the flickr aggregated object GFKs to the StoredOEmbed
79 self.assertEqual(aggregated_object.media, StoredOEmbed.objects.get(match=self.flickr_url))
80
81 # the m2m fields should all be cleared out now
82 self.assertQuerysetEqual(r.media.all(), am_queryset.filter(url=self.flickr_url))
83 self.assertQuerysetEqual(r.videos.all(), [])
84 self.assertQuerysetEqual(r.photos.all(), am_queryset.filter(url=self.flickr_url))
85
a90ed21 Charles Leifer Fixing issue 9
coleifer authored
86 r.content = 'Just text please'
87 r.save()
88
89 self.assertQuerysetEqual(r.media.all(), [])
90 self.assertQuerysetEqual(r.videos.all(), [])
91 self.assertQuerysetEqual(r.photos.all(), [])
92
8fa47b7 Charles Leifer Initial commit of djangoembed, rich media consuming and providing in
coleifer authored
93 def test_internal_media_aggregation(self):
94 category1 = Category.objects.get(pk=1)
95
96 r = Rich(name='Container', slug='container', content='Check this out: %s' % self.category_url)
97 r.save()
98
99 am_queryset = AggregateMedia.objects.all()
100 self.assertEqual(am_queryset.count(), 1)
101
102 aggregated_object = am_queryset[0]
103 self.assertEqual(aggregated_object.url, self.category_url)
104 self.assertEqual(aggregated_object.media, category1) # gfk to actual category
105
106 self.assertQuerysetEqual(r.media.all(), am_queryset)
107 self.assertQuerysetEqual(r.videos.all(), [])
108 self.assertQuerysetEqual(r.photos.all(), am_queryset)
Something went wrong with that request. Please try again.