PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/blogmaker/blog/tests.py

http://blogmaker.googlecode.com/
Python | 92 lines | 85 code | 5 blank | 2 comment | 0 complexity | 4fe5370f16b38dfc7658c19e56500e8a MD5 | raw file
 1''' @copyright:Copyright (c) 2006-2007, PreFab Software Inc. '''
 2
 3
 4import datetime, unittest
 5import blogcosm.django_tst_setup
 6from django.contrib.auth.models import User
 7from blogmaker.blog.models import Entry, TrackbackStatus
 8
 9class EntryTest(unittest.TestCase):
10    def test_entry(self):
11        # Test link finding
12        text = '''<a href="http://www.example.com">One link</a>
13more &quot;text&quot;<a href="/profile/foo">Internal link</a>
14<a 
15href='http://www.example.com/foo'>This one has 
16<b>mixed</b>content</a>
17<a href="#foo">anchor</a>
18'''
19        entry = Entry(body=text)
20        
21        self.assertEquals(set(["http://www.example.com", 'http://www.example.com/foo']), entry.links)
22        
23        # Test excerpt
24        # Where does the whitespace come from? markdown, apparently...
25        self.assertEquals('''One link\n   more "text"Internal link\n   This one has \n   mixedcontent\n   anchor\n''', entry.excerpt)
26
27        # Test markdown links
28        text = '''[One link](http://www.example.com)
29        more text [Internal link](/profile/foo)
30        [This one has 
31        *mixed* content](http://www.example.com/foo "Title") and a title.
32        <a href="#foo">anchor</a>
33'''
34        entry = Entry(body=text)
35        
36        self.assertEquals(set(["http://www.example.com", 'http://www.example.com/foo']), entry.links)
37
38        # Test duplicate links
39        text = '''<a href="http://www.example.com">One link</a>
40        [duplicate link](http://www.example.com)
41        '''
42        entry = Entry(body=text)
43        self.assertEquals(set(["http://www.example.com"]), entry.links)
44
45        # Test excerpt with UTF-8 text
46        entry = Entry(body='Amazon&#8217;s Kindle press release: The Kindle e-reader ISN\xe2\x80\x99T ugly?')
47        self.assertEquals(u'Amazon\u2019s Kindle press release: The Kindle e-reader ISN\u2019T ugly?\n', entry.excerpt)
48        
49    def test_trackbacks(self):
50        Entry.objects.all().delete()
51        TrackbackStatus.objects.all().delete()
52        
53        text = '''<a href='http://www.example.com/foo'>link</a>
54        <a href='http://www.example.com/foo2'>link2</a>
55        <a href='http://www.example.com/?foo2'>link2</a>
56        <a href='http://www.example.com'>no path</a>
57        <a href='http://www.example.com/'>no path</a>
58        <a href='http://www.example.com/blog/'>blog path</a>
59        <a href='http://www.quantcast.com/blogcosm.com'>excluded site</a>
60'''
61        entry = Entry(body=text, pub_date=datetime.date.today(), user_id=self.user.id)
62        entry.save()
63        
64        # Saving the entry harvests trackback links for URLs with a path or query component
65        self.assertEquals(3, entry.trackbacks.all().count())
66        self.assertEquals(3, TrackbackStatus.objects.all().count())
67        tbLinks = [ item['link'] for item in entry.trackbacks.all().values('link')]
68        self.assert_('http://www.example.com/foo' in tbLinks)
69        self.assert_('http://www.example.com/foo2' in tbLinks)
70        self.assert_('http://www.example.com/?foo2' in tbLinks)
71        
72        # Unused trackbacks are deleted if the links are deleted from the entry
73        tb = entry.trackbacks.get(link='http://www.example.com/foo')
74        tb.status = 'Success'
75        tb.save()
76        
77        entry.body = ''
78        entry.save()
79 
80        self.assertEquals(1, entry.trackbacks.all().count())
81        self.assertEquals(1, TrackbackStatus.objects.all().count())
82        self.assertEquals('http://www.example.com/foo', entry.trackbacks.all()[0].link)
83        
84    def setUp(self):
85        self.user, _ = User.objects.get_or_create(username='test', 
86            defaults=dict(password='sha1$21f77$5035b86adb98c9e9e9ab8736ba6415e2ad322e4b', is_staff=True))
87
88    def tearDown(self):
89        self.user.delete()
90    
91if __name__ == '__main__':
92    unittest.main()