test_goto.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from misago.acl import add_acl
  2. from misago.forums.models import Forum
  3. from misago.readtracker import threadstracker
  4. from misago.users.testutils import AuthenticatedUserTestCase
  5. from misago.threads import goto
  6. from misago.threads.testutils import post_thread, reply_thread
  7. class MockThreadsCounter(object):
  8. def set(self, *args, **kwargs):
  9. pass
  10. def decrease(self, *args, **kwargs):
  11. pass
  12. class GotoTests(AuthenticatedUserTestCase):
  13. def setUp(self):
  14. super(GotoTests, self).setUp()
  15. self.forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  16. self.forum.labels = []
  17. self.thread = post_thread(self.forum)
  18. add_acl(self.user, self.forum)
  19. add_acl(self.user, self.thread)
  20. def test_get_thread_pages(self):
  21. """get_thread_pages returns valid count of pages for given positions"""
  22. self.assertEqual(goto.get_thread_pages(1), 1)
  23. self.assertEqual(goto.get_thread_pages(10), 1)
  24. self.assertEqual(goto.get_thread_pages(13), 1)
  25. self.assertEqual(goto.get_thread_pages(14), 2)
  26. self.assertEqual(goto.get_thread_pages(19), 2)
  27. self.assertEqual(goto.get_thread_pages(20), 2)
  28. self.assertEqual(goto.get_thread_pages(23), 2)
  29. self.assertEqual(goto.get_thread_pages(24), 3)
  30. self.assertEqual(goto.get_thread_pages(27), 3)
  31. self.assertEqual(goto.get_thread_pages(36), 4)
  32. self.assertEqual(goto.get_thread_pages(373), 37)
  33. def test_get_post_page(self):
  34. """get_post_page returns valid page number for given queryset"""
  35. self.assertEqual(goto.get_post_page(1, self.thread.post_set), 1)
  36. # add 12 posts, bumping no of posts on page to to 13
  37. [reply_thread(self.thread) for p in xrange(12)]
  38. self.assertEqual(goto.get_post_page(13, self.thread.post_set), 1)
  39. # add 2 posts
  40. [reply_thread(self.thread) for p in xrange(2)]
  41. self.assertEqual(goto.get_post_page(15, self.thread.post_set), 2)
  42. def test_hashed_reverse(self):
  43. """hashed_reverse returns complete url for given post"""
  44. url = goto.hashed_reverse(self.thread, self.thread.first_post)
  45. url_formats = self.thread.get_absolute_url(), self.thread.first_post_id
  46. self.assertEqual(url, '%s#post-%s' % url_formats)
  47. url = goto.hashed_reverse(self.thread, self.thread.first_post, 4)
  48. url_formats = self.thread.get_absolute_url(), self.thread.first_post_id
  49. self.assertEqual(url, '%s4/#post-%s' % url_formats)
  50. def test_last(self):
  51. """last returns link to last post in thread"""
  52. url_last = goto.last(self.user, self.thread)
  53. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  54. self.assertEqual(url_last, '%s#post-%s' % url_formats)
  55. # add 12 posts to reach page limit
  56. [reply_thread(self.thread) for p in xrange(12)]
  57. url_last = goto.last(self.user, self.thread)
  58. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  59. self.assertEqual(url_last, '%s#post-%s' % url_formats)
  60. # add 2 posts to add second page to thread
  61. [reply_thread(self.thread) for p in xrange(2)]
  62. url_last = goto.last(self.user, self.thread)
  63. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  64. self.assertEqual(url_last, '%s2/#post-%s' % url_formats)
  65. def test_get_post_link(self):
  66. """get_post_link returns link to specified post"""
  67. post_link = goto.get_post_link(
  68. 1, self.thread.post_set, self.thread, self.thread.last_post)
  69. self.assertEqual(post_link, goto.last(self.user, self.thread))
  70. # add 16 posts to add extra page to thread
  71. [reply_thread(self.thread) for p in xrange(16)]
  72. post_link = goto.get_post_link(
  73. 17, self.thread.post_set, self.thread, self.thread.last_post)
  74. self.assertEqual(post_link, goto.last(self.user, self.thread))
  75. def test_new(self):
  76. """new returns link to first unread post"""
  77. self.user.new_threads = MockThreadsCounter()
  78. self.user.unread_threads = MockThreadsCounter()
  79. post_link = goto.new(self.user, self.thread)
  80. self.assertEqual(post_link, goto.last(self.user, self.thread))
  81. # add 18 posts to add extra page to thread, then read them
  82. [reply_thread(self.thread) for p in xrange(18)]
  83. threadstracker.read_thread(
  84. self.user, self.thread, self.thread.last_post)
  85. # add extra unread posts
  86. first_unread = reply_thread(self.thread)
  87. [reply_thread(self.thread) for p in xrange(30)]
  88. new_link = goto.new(self.user, self.thread)
  89. post_link = goto.get_post_link(
  90. 50, self.thread.post_set, self.thread, first_unread)
  91. self.assertEqual(new_link, post_link)
  92. # read thread
  93. threadstracker.read_thread(
  94. self.user, self.thread, self.thread.last_post)
  95. # assert new() points to last reply
  96. post_link = goto.new(self.user, self.thread)
  97. self.assertEqual(post_link, goto.last(self.user, self.thread))
  98. def test_post(self):
  99. """post returns link to post given"""
  100. self.assertEqual(
  101. goto.last(self.user, self.thread),
  102. goto.post(self.user, self.thread, self.thread.last_post))
  103. # add 24 posts
  104. [reply_thread(self.thread) for p in xrange(24)]
  105. self.assertEqual(
  106. goto.last(self.user, self.thread),
  107. goto.post(self.user, self.thread, self.thread.last_post))