/blogmaker/blog/tests.py

http://blogmaker.googlecode.com/ · Python · 92 lines · 65 code · 18 blank · 9 comment · 2 complexity · 4fe5370f16b38dfc7658c19e56500e8a MD5 · raw file

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